🎯 Motor Control for Tracking
Motor Control for Tracking
Learn how to use face coordinates to control a servo motor - making the camera follow the detected face!
From Coordinates to Angles
We have face coordinates (x, y) from the camera. Now we need to convert these into servo motor angles so the camera can follow the face!
Calculating Servo Angle from Face Position
The camera image has a width (like 320 pixels). The face center_x tells us where the face is horizontally. We need to convert this to a servo angle (0-180 degrees).
Angle Calculation Formula:
Understanding the Formula:
- face_center_x / frame_width: Gives a ratio (0.0 to 1.0)
- × 180: Converts ratio to angle (0° to 180°)
- Left side of frame (x=0) = 0°
- Right side of frame (x=width) = 180°
- Center of frame = 90°
Smooth Movement
If we move the servo directly to the calculated angle, it might be jerky. We need smooth movement algorithms!
Direct Movement
Move servo directly to target angle
Simple but can be jerky
Smooth Movement
Move in small steps toward target
Smoother, more natural
Dead Zone
Only move if difference is large enough
Prevents constant small movements
Project: Camera Follows Detected Face
Let's combine face detection with servo control to make the camera follow a face!
Materials Needed:
- Raspberry Pi
- Raspberry Pi Camera Module
- Servo motor (SG90 recommended)
- Pan/tilt mechanism (or mount camera on servo)
- Jumper wires
- Optional: External 5V power supply for servo
Wiring Instructions:
- Camera: Connect to camera port on Raspberry Pi
- Servo Signal: Connect to GPIO 18
- Servo Power: Connect to 5V (or external supply)
- Servo Ground: Connect to GND
- Mount: Attach camera to servo (pan mechanism)
Complete Tracking Code:
Understanding the Code:
- calculate_servo_angle(): Converts face X position to servo angle
- Smooth movement: Moves servo in small steps (5°) instead of jumping directly
- Dead zone: Only moves if angle difference > 2° (prevents constant tiny movements)
- current_angle: Tracks current servo position
- The servo gradually moves toward where the face is, creating smooth tracking!
Improving Tracking
You can improve tracking with these techniques:
Largest Face
Track the largest face when multiple detected
Speed Control
Move faster when face is far, slower when close
Stability
Add delays and smoothing to prevent jitter
Common Mistakes to Avoid
⚠️ Watch Out For:
- Too Fast Movement: Moving servo too quickly causes jerky motion - use small steps
- No Dead Zone: Without dead zone, servo constantly moves from tiny differences
- Wrong Coordinate System: Make sure you're using face center X, not top-left corner
- Angle Out of Range: Always clamp angles to 0-180° range
- Multiple Faces: Decide which face to track (first, largest, or closest to center)
Summary
You've learned:
- ✅ Face coordinates can be converted to servo angles
- ✅ Formula: angle = (face_x / frame_width) × 180
- ✅ Smooth movement uses small steps instead of direct jumps
- ✅ Dead zone prevents constant tiny movements
- ✅ Tracking combines face detection with servo control
- ✅ The camera/fan can now follow a detected face!
🎮 Try It: Practice Tracking!
Practice writing tracking code. Try these challenges:
📝 Challenge 1: Calculate Angle
Write a function that converts face X position to servo angle:
📝 Challenge 2: Smooth Movement
Write code that moves servo smoothly toward target angle:
🎯 Activity: Camera Follows Detected Face
What You'll Build:
Create a system where a servo motor moves the camera to follow a detected face!
Step-by-Step Instructions:
- Set Up Hardware: Mount camera on servo, connect servo to GPIO 18
- Write Detection Code: Use face detection code from previous lesson
- Add Angle Calculation: Convert face X to servo angle
- Add Smooth Movement: Move servo in small steps
- Test: Stand in front of camera and move left/right
- Adjust: Tune step size and dead zone for smooth tracking
Testing Checklist:
- ✅ Face is detected when you stand in front
- ✅ Servo moves when you move left/right
- ✅ Movement is smooth (not jerky)
- ✅ Camera follows your face as you move
💪 Practice Challenges
Challenge 1: Direct Tracking
Create a version that moves servo directly to calculated angle (no smooth movement):
Challenge 2: Speed Control
Make servo move faster when face is far from center, slower when close:
Challenge 3: Largest Face Tracking
When multiple faces detected, track the largest one:
Challenge 4: Center Zone
Only move servo if face is outside a "center zone" (e.g., middle 30% of screen):