A simulation-based autonomous perception pipeline built with ROS2, featuring real-time object detection, Kalman Filter tracking, and autonomous decision-making logic.
This project demonstrates a complete perception-to-control pipeline for autonomous systems. Instead of relying on expensive hardware, it uses software-in-the-loop (SIL) simulation to validate core robotics concepts:
- Perception: Detect and track objects using computer vision
- State Estimation: Predict object motion using Kalman Filter
- Decision Making: Autonomous navigation logic based on object position
- Control: Generate velocity commands in real-time
"A working pipeline > a fancy model"
This project prioritizes understanding the full robotics stack over complex algorithms. It proves that you can build a functional autonomous system with:
- Simple color-based detection (no heavy ML models)
- Classical state estimation (Kalman Filter)
- Rule-based control logic
Perfect for learning ROS2 architecture, testing perception algorithms, and demonstrating software-in-the-loop testing capabilities.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β ROS2 Perception Stack β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ β β β ββββββββββββββββ ββββββββββββββββββ ββββββββββββββββ β β β Camera Node β β Tracker Node β β Mission Node β β β β β β β β β β β β πΉ Video βββββββΆβ π Detection βββββββΆβ π€ Decision β β β β Input β β π― Kalman β β Logic β β β β β β Filter β β β β β ββββββββββββββββ ββββββββββββββββββ ββββββββββββββββ β β β β β β β βΌ βΌ βΌ β β /camera/image_raw /object/position /cmd_vel β β (sensor_msgs/Image) (geometry_msgs/Point) (geometry_msgs/ β β Twist) β β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- Publishes:
/camera/image_raw(sensor_msgs/Image) - Function: Streams video frames from webcam or prerecorded video
- Frequency: ~30 Hz
- Subscribes:
/camera/image_raw - Publishes:
/object/position(geometry_msgs/Point) - Functions:
- Converts BGR to HSV color space
- Applies color thresholding (red object detection)
- Finds contours and calculates centroid
- Kalman Filter for smooth position tracking
- Normalizes position to [-1.0, 1.0] range
Kalman Filter State:
- State Vector:
[x, y, vx, vy](position + velocity) - Measurement:
[x, y](detected centroid) - Process Noise: 0.03
- Measurement Noise: 0.5
- Subscribes:
/object/position - Publishes:
/cmd_vel(geometry_msgs/Twist) - Control Logic:
- Object Left (x < -0.2) β Turn Left (angular.z = 0.5)
- Object Right (x > 0.2) β Turn Right (angular.z = -0.5)
- Object Centered (-0.2 β€ x β€ 0.2) β Move Forward (linear.x = 0.5)
- Object Lost (z = 0.0) β Stop (all velocities = 0)
- OS: Ubuntu 22.04 (Jammy) or 24.04 (Noble)
- ROS2: Humble or Jazzy
- Python: 3.10+
- WSL2: (if running on Windows)
# For Ubuntu 24.04 + ROS2 Jazzy sudo apt update sudo apt install ros-jazzy-desktop python3-pip python3-colcon-common-extensions -y # Install Python packages pip3 install opencv-python numpy==1.26.4
# Create workspace mkdir -p ~/ros2_perception/src cd ~/ros2_perception # Clone repository git clone https://github.com/RoKenobi/ros2-perception-stack.git src/perception_stack # Build colcon build source install/setup.bash
If not using a webcam, place a video file in your home directory:
# Example: red object moving across frame cp /path/to/your/video.mp4 ~/test_video.mp4
cd ~/ros2_perception source install/setup.bash ros2 run perception_stack camera_node
cd ~/ros2_perception source install/setup.bash ros2 run perception_stack tracker_node
cd ~/ros2_perception source install/setup.bash ros2 run perception_stack mission_node
# View tracked position ros2 topic echo /object/position # View velocity commands ros2 topic echo /cmd_vel # Visualize graph rqt_graph
Terminal logs showing autonomous decisions:
[INFO] [mission_node]: Object Left -> TURN LEFT
[INFO] [mission_node]: Object Center -> FORWARD
[INFO] [mission_node]: Object Lost -> STOP
Position data from Kalman Filter:
x: -0.96 # Object on left side y: -0.97 # Vertical position z: 1.0 # 1.0 = detected, 0.0 = lost
The Kalman Filter serves two critical purposes:
-
Noise Reduction: Raw detection from color thresholding can be jittery due to:
- Lighting changes
- Compression artifacts
- Detection noise
-
Prediction: When the object is temporarily occluded or lost, the Kalman Filter:
- Predicts position based on velocity
- Maintains tracking continuity
- Smooths the trajectory
# 1. Convert to HSV (better color segmentation than RGB) hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # 2. Define red color range lower_red = np.array([0, 70, 50]) upper_red = np.array([10, 255, 255]) # 3. Create binary mask mask = cv2.inRange(hsv, lower_red, upper_red) # 4. Find largest contour contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) c = max(contours, key=cv2.contourArea) # 5. Calculate centroid x, y, w, h = cv2.boundingRect(c) center_x, center_y = x + w//2, y + h//2
Position is normalized to [-1.0, 1.0] for platform-independent control:
norm_x = (pixel_x - image_width/2) / (image_width/2) # Result: -1.0 (far left) to 1.0 (far right)
This allows the same logic to work on:
- 640x480 resolution
- 1920x1080 resolution
- Any aspect ratio
ros2_perception/
βββ src/
β βββ perception_stack/
β βββ perception_stack/
β β βββ __init__.py
β β βββ camera_node.py # Video streaming
β β βββ tracker_node.py # Detection + Kalman
β β βββ mission_node.py # Control logic
β βββ setup.py
β βββ package.xml
βββ .gitignore
βββ README.md
Edit tracker_node.py:
# For blue objects lower_blue = np.array([100, 70, 50]) upper_blue = np.array([130, 255, 255])
Edit mission_node.py:
# Tighter centering threshold if self.target_x < -0.1: # was -0.2 twist.angular.z = 0.8 # faster turn
Edit camera_node.py:
self.cap = cv2.VideoCapture(0) # 0 = default webcam
ros2 topic list # Should show: # /camera/image_raw # /object/position # /cmd_vel
ros2 topic hz /object/position
# Expected: ~30 Hzrqt_image_view /camera/image_raw
Contributions are welcome! Areas for improvement:
- Add YOLOv5/v8 detection for multi-object tracking
- Implement data association (Hungarian algorithm)
- Add RViz visualization
- Support multiple color tracking
- Gazebo simulation integration
This project is licensed under the MIT License - see the LICENSE file for details.
Built with β€οΈ for learning robotics and autonomous systems
- ROS2 Community
- OpenCV Team
- Inspired by BeeX robotics challenges
If this helped you learn ROS2, please β star the repo!
Happy Coding & Happy Robotics! π€