Welcome to MicroROS V2 Robot Car repository
8. Object Follow
Object Follow (No-PTZ)
Function package: microros_v2_kcf_follow_pkg
1. Function Description
microros_v2_kcf_follow_pkg is a following controller based on the target bounding box. It only subscribes to the target box published by the tracking node, and then controls the PTZ and/or body motion.
Therefore, the complete functionality consists of two parts:
- The tracker is responsible for obtaining the target bounding box from the image, for example
smart_tracker/super_trackeror the KCF tracking node. tracking_bbox_follow_nodecomputes the PTZ angle, body angular velocity, and body linear velocity from the target box center and target box size.
The default control rate is 10 Hz. When the target box is invalid, the confidence is too low, or it has not been updated for more than target_stale_timeout_sec, the node immediately publishes zero velocity; the source code has no "allow continued motion for 5 frames" lost-target tolerance logic.
2. Control Chain
Camera/visual tracker └── /tracking_bbox └── tracking_bbox_follow_node (10 Hz) ├── PTZ following: target center -> /cmd_ptz_angle ├── Body turning: target horizontal error or PTZ deflection -> /cmd_vel.angular.z └── Distance following: target box size error -> /cmd_vel.linear.xPTZ Version
By default enable_ptz_follow:=true:
- Uses independent X/Y PD control to update the PTZ angle from the pixel error of the target center relative to the image center.
- After the PTZ horizontal angle deviates from the initial angle by
servo1_engage_deg, body PD control is used forangular.z, letting the body gradually follow the PTZ back to center. - At the same time,
linear.xcan be controlled based on the target box size.
No-PTZ Version
A fixed camera cannot adjust its line of sight through the PTZ; the following must be set at startup:
xxxxxxxxxxenable_ptz_follow:=false publish_initial_ptz_command:=falseIn this case, the node uses the horizontal pixel error of the target center to control the body angular.z, and controls linear.x based on the target box size. Setting only publish_initial_ptz_command:=false does not disable the PTZ following logic, so it cannot be the only modification for a No-PTZ configuration.
3. Topics and Messages
| Topic | Direction | Message Type | QoS | Description |
|---|---|---|---|---|
/tracking_bbox | Subscribe | Auto-detected by default | BEST_EFFORT, depth=1 | Target bounding box input |
/cmd_vel | Publish | geometry_msgs/msg/Twist | BEST_EFFORT, depth=1 | linear.x forward/backward, angular.z left/right turn |
/cmd_ptz_angle | Publish | geometry_msgs/msg/Vector3 | depth=10 | Published in PTZ mode; x is the horizontal angle and y is the pitch angle, in degrees |
/JoyState | Subscribe | std_msgs/msg/Bool | depth=10 | When true, pauses automatic following and publishes zero velocity |
The node is also compatible with:
smart_tracker/msg/TrackBox, auto-detected viatracking_bbox_msg_type:=auto;std_msgs/msg/Int32MultiArray: when the length is at least 11, it is parsed as[valid, x1, y1, x2, y2, cx, cy, width, height, image_width, image_height]; when the length is 4 to 10, the first four elements are taken as the bounding box;- messages with
x1,y1,x2,y2fields.
The bounding box format is uniformly the top-left corner (x1, y1) and the bottom-right corner (x2, y2). The node automatically sorts the coordinates and checks the width, height, and confidence.
4. Startup Steps
The following commands assume the workspace is ~/microros_car_v2_ws and the ROS 2 environment has been configured.
4.1 Start the Chassis, Agent, Camera, and Model
Terminal 1:
xxxxxxxxxxros2 launch microros_v2_bringup car_base.launch.py
4.2 Start the Target Tracker
Run the program smart_tracker/super_tracker, Terminal 2:
xxxxxxxxxxros2 launch microros_v2_bringup super_tracker.launch.py display_windows_only_track:=false display_windows:=true
Press c to enter box-selection mode, then use the mouse to draw a box

Then press Enter to start following the coordinate data.

4.4 Start the Follow Controller
Terminal 3:
xxxxxxxxxxros2 launch microros_v2_kcf_follow_pkg tracking_bbox_follow.launch.py \ enable_ptz_follow:=false \ publish_initial_ptz_command:=falseAfter a successful startup, slowly move the object to test left/right tracking and forward/backward following.

5. Distance Following Algorithm
The node does not estimate distance from the color radius, but instead uses the geometric size of the bounding box:
xxxxxxxxxxcurrent_size = sqrt(width * height)size_error = (target_size - current_size) / target_sizeBy default auto_set_target_bbox_size:=true. The first valid bounding box received after startup is used as the target distance reference. You can also manually specify target_bbox_size_px, in which case the node no longer waits for the first bounding box to initialize the reference.
The distance control uses the PID form; with the default distance_ki:=0.0, it is effectively PD:
xxxxxxxxxxlinear.x = linear_dir * (distance_kp * size_error + distance_ki * integral + distance_kd * d_error)- When the target box is smaller than the reference,
size_error > 0, and it moves forward to approach by default; - When the target box is larger than the reference,
size_error < 0, and it moves backward to retreat by default; - When
abs(size_error) <= distance_tolerance_ratio, it stops moving forward/backward; linear.xis clamped to[-max_linear_x, max_linear_x], with a default maximum absolute value of0.12 m/s.
6. Key Parameters
General Parameters
| Parameter | Default | Description |
|---|---|---|
tracking_bbox_topic | /tracking_bbox | Target box input topic; the node must be restarted after changing |
tracking_bbox_msg_type | auto | Auto-detect the message type; you can also fill in the full ROS type name |
cmd_vel_topic | /cmd_vel | Body velocity output topic; the node must be restarted after changing |
ptz_command_topic | /cmd_ptz_angle | PTZ angle output topic; the node must be restarted after changing |
joy_state_topic | /JoyState | Joystick override state topic; the node must be restarted after changing |
enable_follow | true | Master follow switch; when disabled, continuously publishes zero velocity |
enable_ptz_follow | true | Enable PTZ following; must be set to false for No-PTZ |
enable_body_turn_follow | true | Enable body horizontal turning |
enable_distance_follow | true | Enable forward/backward distance following |
enable_joy_override | true | Pause automatic following when the joystick takes over |
image_width / image_height | 640 / 480 | Used when the input message does not provide the image size |
tracking_confidence_threshold | 0.1 | Minimum valid confidence |
target_stale_timeout_sec | 1.2 | Stops the car when the target box has not been updated for longer than this time |
follow_rate_hz | 10.0 | Control timer rate, minimum 1 Hz |
PTZ and Body Turning Parameters
| Parameter | Default | Description |
|---|---|---|
pixel_tolerance | 15.0 px | PTZ horizontal/pitch pixel deadband |
kp_x / kd_x | 0.6 / 0.15 | PTZ horizontal PD coefficients |
kp_y / kd_y | 0.6 / 0.15 | PTZ pitch PD coefficients |
max_step_xy | 5.0 deg | Maximum PTZ angle step per control cycle |
horizontal_min_deg / horizontal_max_deg | -90 / 90 deg | Horizontal angle limits |
pitch_min_deg / pitch_max_deg | -90 / 20 deg | Pitch angle limits |
initial_horizontal_deg | 0.0 deg | Initial horizontal angle, also the reference angle for the body to return to center |
initial_pitch_deg | -45.0 deg | Initial pitch angle |
body_kp / body_kd | 0.03 / 0.0 | Body turning PD coefficients in PTZ mode |
servo1_engage_deg | 5.0 deg | Body turning starts when the PTZ horizontal deflection reaches this value |
body_tolerance_deg | 3.0 deg | Deadband for stopping the body when it returns to center |
max_angular_z | 0.6 rad/s | Maximum absolute body angular velocity |
body_ff_gain | 1.0 | Feed-forward coefficient in PTZ mode that compensates the PTZ horizontal angle based on body angular velocity |
fixed_camera_pixel_tolerance | 15.0 px | No-PTZ body turning stop deadband |
fixed_camera_engage_px | 30.0 px | No-PTZ body turning activation threshold |
fixed_camera_body_kp / fixed_camera_body_kd | 0.6 / 0.15 | No-PTZ body turning PD coefficients |
body_dir / fixed_camera_body_dir | -1.0 / -1.0 | Body turning direction correction; change to 1.0 if necessary |
sign_x / sign_y | 1.0 / -1.0 | PTZ horizontal/pitch direction correction |
Distance Following Parameters
| Parameter | Default | Description |
|---|---|---|
target_bbox_size_px | 0.0 | Target reference size; when 0, it is automatically set from the first valid box |
auto_set_target_bbox_size | true | Whether to initialize the reference size from the first valid box |
distance_tolerance_ratio | 0.06 | Relative size deadband, default 6% |
distance_kp / distance_ki / distance_kd | 0.18 / 0.0 / 0.02 | Distance PID coefficients |
distance_integral_limit | 0.5 | Integral limit |
max_linear_x | 0.12 m/s | Maximum absolute linear velocity |
linear_dir | 1.0 | Forward/backward direction correction; change to -1.0 if necessary |