|
| 1 | +# Coffee Head Control Package |
| 2 | + |
| 3 | +A ROS2 package for controlling the physical head movements of the Coffee Buddy robot, including face tracking, motor control, and interactive GUI interface. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The `coffee_head_control` package provides comprehensive head movement control for the Coffee Buddy robot system. It includes: |
| 8 | + |
| 9 | +- **Face Tracking**: Automatically tracks detected faces using camera input |
| 10 | +- **Motor Control**: Controls pan/tilt Dynamixel motors for head movement |
| 11 | +- **PID Control**: Smooth, precise motor movements with configurable PID controllers |
| 12 | +- **Interactive GUI**: Qt-based interface for manual control and parameter tuning |
| 13 | +- **Scanning Mode**: Automatic scanning when no faces are detected |
| 14 | +- **Coordinate Transformation**: Integrates with eye tracking systems |
| 15 | + |
| 16 | +## Architecture |
| 17 | + |
| 18 | +``` |
| 19 | +┌─────────────────────────────────────────────────────────────────┐ |
| 20 | +│ Head Tracking Node │ |
| 21 | +│ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │ |
| 22 | +│ │ HeadTrackingUI │ │HeadTrackingSystem│ │ PIDController │ │ |
| 23 | +│ │ - Qt Controls │ │ - Face tracking │ │ - Smooth motion │ │ |
| 24 | +│ │ - Parameter UI │ │ - Motor commands │ │ - Anti-windup │ │ |
| 25 | +│ │ - Status │ │ - Scanning mode │ │ - Rate limiting │ │ |
| 26 | +│ └─────────────────┘ └─────────────────┘ └─────────────────┘ │ |
| 27 | +└─────────────────────────────────────────────────────────────────┘ |
| 28 | + │ |
| 29 | + ▼ |
| 30 | + Dynamixel Motors: |
| 31 | + • Pan Motor (ID: 1) |
| 32 | + • Tilt Motor (ID: 9) |
| 33 | +``` |
| 34 | + |
| 35 | +## Components |
| 36 | + |
| 37 | +### HeadTrackingNode |
| 38 | + |
| 39 | +Main ROS2 node that coordinates head tracking functionality. |
| 40 | + |
| 41 | +**Subscribers:** |
| 42 | +- `/robot/affective_state` (coffee_expressions_msgs/AffectiveState): Face detection data |
| 43 | + |
| 44 | +**Publishers:** |
| 45 | +- `set_position` (dynamixel_sdk_custom_interfaces/SetPosition): Motor position commands |
| 46 | +- `face_velocity` (geometry_msgs/Vector3): Face velocity information |
| 47 | +- `head_pan_angle` (std_msgs/Float32): Current pan angle for eye coordination |
| 48 | +- `head_tilt_angle` (std_msgs/Float32): Current tilt angle for eye coordination |
| 49 | + |
| 50 | +**Services:** |
| 51 | +- Uses `get_position` (dynamixel_sdk_custom_interfaces/GetPosition): Read motor positions |
| 52 | + |
| 53 | +### HeadTrackingSystem |
| 54 | + |
| 55 | +Core tracking logic with multiple operating modes: |
| 56 | + |
| 57 | +**States:** |
| 58 | +- `INITIALIZING`: Reading initial motor positions |
| 59 | +- `IDLE`: No tracking, motors at rest |
| 60 | +- `TRACKING`: Actively following faces |
| 61 | +- `SCANNING`: Searching for faces with sweep motion |
| 62 | +- `MOVING`: Executing smooth movements to target positions |
| 63 | + |
| 64 | +**Features:** |
| 65 | +- **Face Selection**: Automatically selects largest/closest face to track |
| 66 | +- **Velocity Calculation**: Tracks face movement speed and direction |
| 67 | +- **Deadzone Management**: Prevents jittery movements near center |
| 68 | +- **Coordinate Movement**: Synchronized pan/tilt for smooth tracking |
| 69 | + |
| 70 | +### PIDController |
| 71 | + |
| 72 | +Advanced PID controller with anti-windup and rate limiting: |
| 73 | +- **Proportional**: Immediate response to error |
| 74 | +- **Integral**: Eliminates steady-state error with anti-windup |
| 75 | +- **Derivative**: Reduces overshoot and oscillation |
| 76 | +- **Output Limiting**: Prevents excessive motor commands |
| 77 | + |
| 78 | +## Installation |
| 79 | + |
| 80 | +### Dependencies |
| 81 | + |
| 82 | +- **ROS2**: rclpy, std_msgs, geometry_msgs, sensor_msgs |
| 83 | +- **Dynamixel**: dynamixel_sdk_custom_interfaces |
| 84 | +- **Coffee Buddy**: coffee_expressions_msgs |
| 85 | +- **GUI**: python_qt_binding, PyQt5 |
| 86 | +- **Math/Vision**: numpy, opencv-python |
| 87 | + |
| 88 | +### Build Instructions |
| 89 | + |
| 90 | +```bash |
| 91 | +# Navigate to your ROS2 workspace |
| 92 | +cd coffee_ws |
| 93 | + |
| 94 | +# Build the package |
| 95 | +colcon build --packages-select coffee_head_control |
| 96 | + |
| 97 | +# Source the workspace |
| 98 | +source install/setup.bash |
| 99 | +``` |
| 100 | + |
| 101 | +## Usage |
| 102 | + |
| 103 | +### Basic Usage |
| 104 | + |
| 105 | +```bash |
| 106 | +# Launch head tracking node with GUI |
| 107 | +ros2 launch coffee_head_control head_tracking.launch.py |
| 108 | + |
| 109 | +# Launch with custom parameters |
| 110 | +ros2 launch coffee_head_control head_tracking.launch.py \ |
| 111 | + enable_tracking:=true \ |
| 112 | + update_rate:=25.0 \ |
| 113 | + baud_rate:=1000000 |
| 114 | +``` |
| 115 | + |
| 116 | +### GUI Controls |
| 117 | + |
| 118 | +The head tracking node launches with an interactive GUI providing: |
| 119 | + |
| 120 | +1. **Tracking Controls** |
| 121 | + - Enable/disable head tracking |
| 122 | + - Reset head to default position |
| 123 | + - Toggle PID smoothing |
| 124 | + |
| 125 | +2. **Communication Settings** |
| 126 | + - Motor update rate (1-100 Hz) |
| 127 | + - Dynamixel baud rate selection |
| 128 | + - Real-time parameter updates |
| 129 | + |
| 130 | +3. **Movement Thresholds** |
| 131 | + - Pan threshold (10-100 pixels) |
| 132 | + - Tilt threshold (5-80 pixels) |
| 133 | + - Adjustable deadzones |
| 134 | + |
| 135 | +4. **Speed Controls** |
| 136 | + - Min/max pan speed (deg/s) |
| 137 | + - Min/max tilt speed (deg/s) |
| 138 | + - Adaptive speed based on distance |
| 139 | + |
| 140 | +5. **PID Tuning** |
| 141 | + - Separate pan and tilt PID parameters |
| 142 | + - Real-time tuning with sliders |
| 143 | + - Smoothing factor adjustment |
| 144 | + - Scanning frequency control |
| 145 | + |
| 146 | +### Manual Control |
| 147 | + |
| 148 | +```bash |
| 149 | +# Run the node directly |
| 150 | +ros2 run coffee_head_control head_tracking |
| 151 | + |
| 152 | +# Monitor head angles |
| 153 | +ros2 topic echo /head_pan_angle |
| 154 | +ros2 topic echo /head_tilt_angle |
| 155 | + |
| 156 | +# Monitor face velocities |
| 157 | +ros2 topic echo /face_velocity |
| 158 | +``` |
| 159 | + |
| 160 | +## Configuration |
| 161 | + |
| 162 | +### Motor Settings |
| 163 | + |
| 164 | +The system is configured for specific Dynamixel motor IDs: |
| 165 | +- **Pan Motor**: ID 1 (horizontal movement) |
| 166 | +- **Tilt Motor**: ID 9 (vertical movement) |
| 167 | + |
| 168 | +**Angle Limits:** |
| 169 | +- Pan: 143° to 210° (center: 180°) |
| 170 | +- Tilt: 169° to 206° (center: 180°) |
| 171 | + |
| 172 | +### Performance Parameters |
| 173 | + |
| 174 | +**Default Tracking Thresholds:** |
| 175 | +- Pan threshold: 80 pixels |
| 176 | +- Tilt threshold: 80 pixels |
| 177 | +- Update rate: 30 Hz |
| 178 | + |
| 179 | +**PID Default Values:** |
| 180 | +- Pan PID: P=0.1, I=0.005, D=0.08 |
| 181 | +- Tilt PID: P=0.15, I=0.01, D=0.05 |
| 182 | +- Smoothing factor: 0.8 |
| 183 | + |
| 184 | +**Speed Limits:** |
| 185 | +- Pan: 1.0-80.0 deg/s |
| 186 | +- Tilt: 1.0-15.0 deg/s |
| 187 | + |
| 188 | +## Integration |
| 189 | + |
| 190 | +### Face Detection Integration |
| 191 | + |
| 192 | +Subscribes to face detection data from camera/vision systems: |
| 193 | +```json |
| 194 | +{ |
| 195 | + "timestamp": 1672531200.123, |
| 196 | + "frame_width": 640, |
| 197 | + "frame_height": 480, |
| 198 | + "faces": [ |
| 199 | + { |
| 200 | + "x1": 100, "y1": 120, "x2": 200, "y2": 220, |
| 201 | + "center_x": 150, "center_y": 170, |
| 202 | + "confidence": 0.85, |
| 203 | + "id": "person_1" |
| 204 | + } |
| 205 | + ] |
| 206 | +} |
| 207 | +``` |
| 208 | + |
| 209 | +### Eye Tracking Coordination |
| 210 | + |
| 211 | +Publishes head angles for eye tracking coordination: |
| 212 | +- Head handles large movements (outside thresholds) |
| 213 | +- Eyes handle fine movements (within thresholds) |
| 214 | +- Prevents conflicting movements |
| 215 | + |
| 216 | +### Dynamixel Motor Service |
| 217 | + |
| 218 | +Requires `dynamixel_sdk_examples` service for motor communication: |
| 219 | +```bash |
| 220 | +# Start motor service |
| 221 | +ros2 run dynamixel_sdk_examples read_write_node |
| 222 | +``` |
| 223 | + |
| 224 | +## Behavior Modes |
| 225 | + |
| 226 | +### Tracking Mode |
| 227 | + |
| 228 | +When faces are detected: |
| 229 | +1. **Face Selection**: Chooses largest face or closest to center |
| 230 | +2. **Error Calculation**: Computes pixel offset from frame center |
| 231 | +3. **Threshold Check**: Only moves if error exceeds thresholds |
| 232 | +4. **PID Control**: Calculates smooth motor adjustments |
| 233 | +5. **Coordinate Movement**: Sends synchronized pan/tilt commands |
| 234 | + |
| 235 | +### Scanning Mode |
| 236 | + |
| 237 | +When no faces detected: |
| 238 | +1. **Sine Wave Motion**: Smooth left-right scanning |
| 239 | +2. **Configurable Frequency**: Adjustable scan speed |
| 240 | +3. **Position Continuity**: Starts from current position |
| 241 | +4. **Automatic Transition**: Switches to tracking when face found |
| 242 | + |
| 243 | +### Idle Mode |
| 244 | + |
| 245 | +When tracking disabled: |
| 246 | +- Motors remain at current position |
| 247 | +- No automatic movements |
| 248 | +- Manual reset available |
| 249 | + |
| 250 | +## Troubleshooting |
| 251 | + |
| 252 | +### Motor Issues |
| 253 | + |
| 254 | +```bash |
| 255 | +# Check if motors are connected |
| 256 | +ros2 service list | grep position |
| 257 | + |
| 258 | +# Test motor service |
| 259 | +ros2 service call /get_position dynamixel_sdk_custom_interfaces/srv/GetPosition "{id: 1}" |
| 260 | + |
| 261 | +# Check motor positions |
| 262 | +ros2 topic echo /head_pan_angle |
| 263 | +``` |
| 264 | + |
| 265 | +### Face Detection Issues |
| 266 | + |
| 267 | +```bash |
| 268 | +# Check face detection data |
| 269 | +ros2 topic echo /robot/affective_state |
| 270 | + |
| 271 | +# Monitor face velocities |
| 272 | +ros2 topic echo /face_velocity |
| 273 | + |
| 274 | +# Check timing (should be recent) |
| 275 | +ros2 topic hz /robot/affective_state |
| 276 | +``` |
| 277 | + |
| 278 | +### GUI Issues |
| 279 | + |
| 280 | +```bash |
| 281 | +# Ensure Qt5 is installed |
| 282 | +sudo apt install python3-pyqt5 |
| 283 | + |
| 284 | +# Check X11 forwarding (if using SSH) |
| 285 | +echo $DISPLAY |
| 286 | +``` |
| 287 | + |
| 288 | +### Performance Issues |
| 289 | + |
| 290 | +1. **Slow Response**: Increase update rate in GUI |
| 291 | +2. **Jittery Movement**: Increase smoothing factor |
| 292 | +3. **Overshoot**: Reduce PID gains |
| 293 | +4. **Missed Faces**: Decrease thresholds |
| 294 | + |
| 295 | +## Advanced Features |
| 296 | + |
| 297 | +### Velocity Tracking |
| 298 | + |
| 299 | +Calculates and displays face movement velocities: |
| 300 | +- **Real-time Calculation**: Based on position history |
| 301 | +- **Smoothing**: Temporal filtering for stable readings |
| 302 | +- **Direction Display**: Cardinal direction indicators |
| 303 | +- **Vector Visualization**: GUI displays velocity vectors |
| 304 | + |
| 305 | +### Coordinated Movement |
| 306 | + |
| 307 | +Synchronizes pan and tilt for natural head motion: |
| 308 | +- **Simultaneous Commands**: Both motors move together |
| 309 | +- **Speed Adaptation**: Faster movement for distant targets |
| 310 | +- **Smooth Interpolation**: Sine-wave based acceleration |
| 311 | + |
| 312 | +### State Management |
| 313 | + |
| 314 | +Robust state machine prevents conflicts: |
| 315 | +- **Initialization**: Safe startup sequence |
| 316 | +- **Movement Coordination**: Prevents simultaneous operations |
| 317 | +- **Error Recovery**: Automatic recovery from failures |
| 318 | + |
| 319 | +## Development |
| 320 | + |
| 321 | +### Extending Functionality |
| 322 | + |
| 323 | +Key extension points: |
| 324 | +- **New Tracking Algorithms**: Modify `calculate_coordinated_movement()` |
| 325 | +- **Custom Behaviors**: Add new states to `HeadState` enum |
| 326 | +- **GUI Enhancements**: Extend `HeadTrackingUI` class |
| 327 | +- **Motor Integration**: Update motor IDs and limits |
| 328 | + |
| 329 | +### Parameter Tuning |
| 330 | + |
| 331 | +For optimal performance: |
| 332 | +1. **Tune PID Values**: Start with P, then add D, finally I |
| 333 | +2. **Adjust Thresholds**: Balance responsiveness vs stability |
| 334 | +3. **Set Speed Limits**: Consider mechanical constraints |
| 335 | +4. **Test Scanning**: Ensure smooth operation without faces |
| 336 | + |
| 337 | +## License |
| 338 | + |
| 339 | +TODO: License declaration |
0 commit comments