Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

themrleon/ultrasonic-tracker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

What is this?

A dual-sensor ultrasonic tracking system running on an embedded Linux SoC, featuring real-time object tracking with pattern suppression and direct framebuffer rendering. The system uses two ultrasonic sensors to locate and track an object while rendering graphics directly to the Linux framebuffer with remote access capability via VNC.

Watch the video

Hardware

  • Luckfox pico mini b (Rockchip RV1103 SoC)
  • 2x HC-SR04 Ultrasound modules (that support UART mode)
  • 9g servo motor
  • Wires, capacitors and USB cable

How It Works

The system uses two ultrasonic sensors mounted on a servo motor to perform stereo object tracking. The Python script:

  • Controls the servo motor via the SoC's PWM pin to pan the sensor array
  • Receives ultrasonic measurements from both sensors simultaneously through separate UART interfaces
  • Performs background calibration by sweeping the entire range and building a reference model
  • Tracks objects in real-time by comparing current readings against the background model
  • Implements pattern suppression to filter out repetitive noise and false positives
  • Renders the tracking display directly to the Linux framebuffer with visual feedback

Additionally, it can run an X11 VNC server for remote viewing from any device with a VNC client.

Sensor Requirements

image

Warning

Enable UART mode by following the instructions table on the PCB and solder the shown pad (typically M1). If your module doesn't have these configuration options, it may only support the traditional GPIO pulse method, which is not compatible with this implementation.

The system uses UART mode because Linux cannot guarantee the high-precision timing required for the traditional GPIO pulse method due to its preemptive kernel scheduler. In UART mode:

  1. Send the trigger command 0xA0 via UART
  2. The module handles ultrasound emission and echo processing internally
  3. Wait for response with timeout
  4. Successful measurements return exactly 3 bytes that combine to give distance in micrometers (µm)

Configuration and Wiring

Both UART interfaces should be configured for 9600 bps 8N1 communication. The ultrasonic sensors can use 3.3V, while the servo motor requires 5V from the USB VBUS line. Decoupling capacitors are crucial to prevent system instability from servo motor electrical noise.

image
SoC        SR04-Right   SR04-Left   Servo
─────────────────────────────────────────
3V3  ─────→ VCC    ───→ VCC
VBUS ──────────────────────────────→ VCC
GND  ─────→ GND    ───→ GND    ────→ GND  
P53  ─────→ RX
P52  ─────→ TX
P56  ────────────────→ RX
P57  ────────────────→ TX
P54  ──────────────────────────────→ PWM

Power:
SoC 3V3  → Both SR04 VCC
SoC VBUS → Servo VCC  
SoC GND  → Both SR04 GND → Servo GND

Signals:
SoC Pin 53 → Right SR04 RX
SoC Pin 52 → Right SR04 TX
SoC Pin 56 → Left SR04 RX  
SoC Pin 57 → Left SR04 TX
SoC Pin 54 → Servo PWM

Key Features

  • Dual-sensor stereo tracking for accurate object positioning
  • Background calibration sweeps to establish environmental baseline
  • Pattern suppression using cosine similarity to filter repetitive noise
  • State machine with tracking/searching modes for robust operation
  • Adaptive control with dynamic KP based on object motion
  • Real-time radar display with dual colored beams
  • Object trail showing recent positions with fading intensity
  • Visual status indicators for tracking state and suppression
  • Automatic scaling to fit display resolution
  • Configurable colors and display parameters

Tracking Parameters

DISTANCE_TOLERANCE_CM = 5.0           # Tolerance for stereo disparity
MOTION_THRESHOLD_CM = 1.5             # Minimum movement to consider
OBJECT_THRESHOLD_CM = 10.0            # Difference from background to detect object
TRACK_KP = 2.5                        # Tracking responsiveness
MAX_CORRECTION_PER_STEP = 15.0        # Limit servo movement per frame

# Pattern Suppression  
PATTERN_WINDOW_SIZE = 50              # Readings to analyze for patterns
PATTERN_SIMILARITY_THRESHOLD = 0.97   # Similarity threshold for suppression
MIN_PATTERN_LENGTH = 5                # Minimum pattern length to detect

# Servo Configuration
START_ANGLE = 45                      # Minimum servo angle
END_ANGLE = 135                       # Maximum servo angle
INVERT_SERVO = True                   # Reverse servo direction if needed

Installation & Usage

Pre-requisites

  • Python 3
  • Python libraries: periphery and pyserial
  • Framebuffer device at /dev/fb0
  • Framebuffer configured: fbset -g 320 240 320 240 16
  • X11 VNC server: x11vnc

Execute the tracker with python3 tracker.py, the system will perform automatic background calibration, once calibrated, it will begin tracking. For remote viewing:

  • Run VNC server: x11vnc -rawfb console -auth /dev/null -noxdamage -forever -shared -repeat -defer 0 -wait 0 -noxinerama -nowf -nowcr -speeds modem -tightfilexfer
  • Connect to the server IP with any VNC client

Screencast from 10-12-2025 10_58_56 AM

Portability

The code is designed for easy adaptation:

  • Uses standard Linux device nodes (/dev/ttyS*, /dev/pwm*, /dev/fb*)
  • Leverages python-periphery library for hardware access
  • All hardware parameters configurable at script top
  • Automatic display detection and scaling
  • Modular design for sensor/display replacements

Performance Tips

  • Use lower resolutions (320x240) for better performance over WiFi streaming
  • Keep MAX_DISTANCE_CM reasonable (100-200cm) for better zoom and accuracy
  • Ensure servo supports your chosen START_ANGLE and END_ANGLE range
  • Supply sensors with 5V for better range (use level shifters for 3.3V SoC compatibility)
  • Allow sufficient time between ultrasonic measurements for echo processing
  • Consider material properties - some surfaces absorb or deflect ultrasound unpredictably