This branch demonstrates how to control two motors working together using the leader-follower pattern.
- Increased Torque - Intake rollers, elevator, climber
- Redundancy - If one motor fails, can still limp back to field
- Even Force Distribution - Prevents tilting or binding
- HigherSpeed - Two small motors = one fast system
- Dual Intake Rollers - Both spin same direction to grab game piece
- Elevator - Two motors lift together, share the load
- Climber - Multiple motors pull up together
- Conveyor - Several rollers move item forward
One motor controls, other copies:
Leader Motor:
- Has encoder
- Runs velocity control PID
- Calculates needed power
Follower Motor:
- No encoder
- Copies leader's power output
- Simple setup
Pros:
- Simple to implement
- Works great if motors are identical
- One encoder = less cost
Cons:
- Can't detect if follower fails
- If motors mismatch, they drift apart
Each motor has its own encoder and PID:
Motor 1: Velocity control on left side
Motor 2: Velocity control on right side
Both reach same target speed independently
Pros:
- Detects motor failure
- Works even if motors mismatch
- More robust
Cons:
- More complex
- Two encoders = more cost
- Two PID controllers = tuning both
Key Line:
followerConfig.follow(leaderMotor, false);This magic line makes the follower copy the leader!
false= same direction as leadertrue= opposite direction as leader
For example, dual intake rollers usually use false:
/----\
| ⟲ ⟳ | Both spin inward to grab game piece
\----/
For opposite directions (like conveyor belts):
Motor 1 spins up ⟵----⟶ Motor 2 spins down
(leader) roller (follower inverted)
The command logs both motor outputs:
Leader Output: 0.875
Follower Output: 0.875 ← Should be identical if working correctly
If they differ significantly, check:
- Are motors the same model?
- Is gear ratio the same?
- Is one motor stalling (high current)?
- Deploy to robot or simulation
- Press A button
- Both motors should spin up together
- Watch SmartDashboard:
- Dual Motor Velocity (RPM) - Leader motor speed (both should reach same RPM)
- Leader Output - Power applied to leader
- Follower Output - Should match leader output (displays in console)
- Try blocking one motor - both should maintain speed via PID
- Release button - both motors stop
Solution: Change false to true in subsystem:
followerConfig.follow(leaderMotor, true); // InvertedPossible causes:
- Motors are different models (different KV)
- Different gear ratios
- One motor is loaded more than the other (stalling)
Solutions:
- Use two identical motors
- Use independent velocity control (not leader-follower)
- Add load-sensing logic to balance power
- Check CAN IDs are correct (MOTOR_ID vs MOTOR_FOLLOWER_ID)
- Check motor is powered
- Check motor connector
- Check motor is not in a stalled state
- Usually means PID tuning is off
- Try decreasing kP value
- Try increasing kD value for damping
Create multiple commands:
// Half speed
motorSubsystem.bothMotorsAtVelocity(2500.0);
// Full speed
motorSubsystem.bothMotorsAtVelocity(5000.0);
// Reverse
motorSubsystem.bothMotorsAtVelocity(-5000.0);Use joystick for smooth speed:
motorSubsystem.bothMotorsAtVelocity(driverXbox.getLeftY() * 5000.0);Add method to detect motor failure:
public boolean isFollowerActive() {
double diff = Math.abs(getLeaderOutput() - getFollowerOutput());
return diff < 0.1; // If difference > 10%, something wrong
}Move at velocity for specific time:
bothMotorsAtVelocity(5000.0);
Timer.delay(2.0); // Run for 2 seconds
stop();- Motor Coordination: Multiple motors working as one unit
- Follow Mode: Motor controller feature to copy another motor
- Load Sharing: Two motors spread the work
- Synchronization: Keeping motors at same speed
- Leader-Follower Pattern: Common architecture pattern in robotics
❑ Both motors connecting to CAN bus?
❑ Motor IDs in Constants are correct?
❑ Motor power connectors tight?
❑ Are motors the same model?
❑ Same gear ratio on both?
❑ PID values similar to velocity control example?
❑ Velocity target is reasonable (not 0)?
❑ Follower inverted setting matches your needs?
- Add Load Detection - Check if motors are imbalanced
- Try Independent Control - Add encoder to follower motor
- Add Current Monitoring - Detect if motor is stalling
- More Complex Mechanisms - Coordinate velocity AND position
- Multi-Motor Systems - 3+ motors on one subsystem
See other teaching examples:
- Duty Cycle - Simple speed control
- Position Control - Move to exact location
- Velocity Control - Maintain constant speed (single motor)
- Button Press - Respond to controller inputs