Welcome to MicroROS V2 Robot Car repository
1. Color Follow
Color Follow (No-PTZ)
1. Experiment Objective
The camera detects a target of a specified color, and the car body uses PD control for rotation tracking + distance following to keep the target centered in the frame and maintain a constant distance.
2. Experiment Principle
1. Key Terms
| Term | Description |
|---|---|
| Visual Follow | Adds distance control (linear.x) on top of tracking, making the car rotate and move forward/backward simultaneously |
| Distance Follow | Estimates distance from the target's pixel size (color block radius), with PD control keeping the car at the desired distance |
| Body PD Control | Uses independent PD controllers for angle (angular.z) and distance (linear.x) |
| Lost-target Tolerance | When the target is temporarily lost, motion is allowed to continue for lost_frame_threshold (5) frames to avoid frequent stops |
| HSV Color Space | Describes colors in three dimensions — Hue (H), Saturation (S), Value (V) — more robust to lighting changes than RGB |
| Deadband Control | A horizontal non-responsive region at the center of the frame (deadband_x_pixel = 60 px) that prevents jitter when the target is near the center |
| QoS | best_effort (image input + cmd_vel downlink), depth=1 |
Difference Between Tracking and Following: Tracking only controls the body rotation to keep the target at the center of the frame (angular.z); following adds distance control (linear.x) on top of that, keeping the car at the desired distance from the target.
2. Technical Architecture
Shares the same node color_tracker_node as Color Tracking; distance following is enabled with enable_distance_follow:=true:
/camera/image_raw (BEST_EFFORT)→ color_tracker_node (enable_distance_follow:=true)├── Image processing: HSV binarization → largest contour → centroid + color block radius└── Body control timer (10 Hz):├── Pixel error → PD angular velocity → angular.z└── Color block radius error → PD linear velocity → linear.x → /cmd_vel (BEST_EFFORT)
3. Core Topics Quick Reference
Firmware Downlink
| Topic | Message Type | QoS | Field | Range | Direction |
|---|---|---|---|---|---|
/cmd_vel | geometry_msgs/Twist | best_effort + keep_last(1) | linear.x | -0.12~0.12 m/s | += forward (approach), −= backward (move away) |
angular.z | -0.60~0.60 rad/s | += left turn, −= right turn |
3. Experiment Steps
1. Hardware Preparation
| Hardware | Requirement |
|---|---|
| PTZ Version | ❌ |
| No-PTZ Version | ✅ |
| Required Peripherals | ESP32 camera (mounted on bracket) |
2. Start Agent + Camera + Joint Model (Terminal 1)
xxxxxxxxxxros2 launch microros_v2_bringup car_base.launch.py
3. Start Color Follow (Terminal 2)
xxxxxxxxxxros2 launch microros_v2_no_ptz_visual_control_pkg color_follow.launch.pyFollow mode is equivalent to
color_tracker.launch.py enable_distance_follow:=true.

5. Operation Instructions
| Key | Function |
|---|---|
| Space | Toggle follow/standby |
| q / ESC | Exit |
| Operation | Function |
|---|---|
| Left mouse button drag | Box-select the target color region and automatically compute the HSV thresholds |
Distance Follow Behavior: The target approaches (color block radius increases) → move backward; the target moves away (radius decreases) → move forward; radius within tolerance → stay still.
4. Experiment Result
- Target approaches → the car automatically moves backward to keep the distance
- Target moves away → the car automatically moves forward to follow
- Distance within the desired range → the car stays still
- Target lost → the car automatically stops once the lost-target count exceeds the threshold
5. Code Explanation
Source code paths:
- Launch:
~/microros_ws/src/microros_v2_no_ptz_visual_control_pkg/launch/color_follow.launch.py- Node source:
~/microros_ws/src/microros_v2_no_ptz_visual_control_pkg/microros_v2_no_ptz_visual_control_pkg/color_tracker_node.py
1. Core Node Explanation
color_follow.launch.py launches color_tracker_node (the same node shared with Color Tracking); the only difference is that enable_distance_follow defaults to true.
Distance Follow PD Logic (in the body control timer):
x# Source file: ~/microros_ws/src/microros_v2_no_ptz_visual_control_pkg/microros_v2_no_ptz_visual_control_pkg/color_tracker_node.py# Distance follow: color block radius error → PD linear velocityif self._dist_follow and tr is not None: err_r = self._target_r_px - tr # desired radius - actual radius if abs(err_r) > self._dist_tol_r: # deadband check now = time.perf_counter() dt = now - self._prev_dt if self._prev_dt > 0 else 1.0 / self._body_rate dt = max(dt, 1e-3) d_err_r = (err_r - self._prev_derr) / dt self._prev_derr = err_r; self._prev_dt = now lx = self._dist_kp * err_r + self._dist_kd * d_err_r lx = clamp(lx, -self._max_lx, self._max_lx) cmd.linear.x = lxKey Logic:
err_r > 0(actual radius < desired = target far) → positive linear.x → move forward to approacherr_r < 0(actual radius > desired = target close) → negative linear.x → move backward to retreat- Radius error within
distance_tolerance_r(12 px) → no adjustment (deadband)
2. Key Parameters
Parameter definition files:
- Launch:
~/microros_ws/src/microros_v2_no_ptz_visual_control_pkg/launch/color_follow.launch.py- Node:
~/microros_ws/src/microros_v2_no_ptz_visual_control_pkg/microros_v2_no_ptz_visual_control_pkg/color_tracker_node.py
| Parameter | Type | Default | Description |
|---|---|---|---|
deadband_x_pixel | float | 60.0 px | Horizontal deadband |
angular_kp | float | 1.5 | Angle PD proportional gain |
angular_kd | float | 0.45 | Angle PD derivative gain |
max_angular_z | float | 0.6 rad/s | Maximum turning speed |
enable_distance_follow | bool | true | Enable distance following |
target_radius | float | 60.0 px | Desired target color block radius |
distance_kp | float | 0.008 | Distance PD proportional gain |
distance_kd | float | 0.003 | Distance PD derivative gain |
distance_tolerance_r | float | 12.0 px | Distance tolerance deadband |
max_linear_x | float | 0.12 m/s | Maximum forward/backward speed |
lost_frame_threshold | int | 5 | Lost-target tolerance frame count |
body_control_rate | float | 10.0 Hz | Body control rate |
3. Node Description
| Node | Package | Description |
|---|---|---|
color_tracker_node | microros_v2_no_ptz_visual_control_pkg | Color tracking + following, dual timers + dual PD (angle + distance) |
6. Common Issues
| Issue | Cause | Solution |
|---|---|---|
| Sudden stop | Target loss triggers freezing | Keep the target in the frame and increase lost_frame_threshold |
| Poor distance following | Unreasonable distance PD parameters | Adjust distance_kp / distance_kd / target_radius |
| Car moves forward/backward frequently | Distance tolerance distance_tolerance_r too small | Increase the tolerance value |