Skip to content

Latest commit

 

History

History
214 lines (172 loc) · 5.73 KB

DualMotor_README.md

File metadata and controls

214 lines (172 loc) · 5.73 KB

Dual Motor Velocity Control - Teaching Example

This branch demonstrates how to control two motors working together using the leader-follower pattern.

Why Two Motors?

Common Reasons for Dual Motors:

  1. Increased Torque - Intake rollers, elevator, climber
  2. Redundancy - If one motor fails, can still limp back to field
  3. Even Force Distribution - Prevents tilting or binding
  4. HigherSpeed - Two small motors = one fast system

Examples in FRC:

  • 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

Two Approaches

1. LEADER-FOLLOWER (This Example)

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

2. INDEPENDENT CONTROL (Alternative)

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

Code Structure

DualMotorVelocitySubsystem

Key Line:

followerConfig.follow(leaderMotor, false);

This magic line makes the follower copy the leader!

  • false = same direction as leader
  • true = 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)

Important Logging

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:

  1. Are motors the same model?
  2. Is gear ratio the same?
  3. Is one motor stalling (high current)?

How to Test

  1. Deploy to robot or simulation
  2. Press A button
  3. Both motors should spin up together
  4. 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)
  5. Try blocking one motor - both should maintain speed via PID
  6. Release button - both motors stop

Common Issues

Motors Spin in Opposite Directions

Solution: Change false to true in subsystem:

followerConfig.follow(leaderMotor, true); // Inverted

Motors Don't Reach Same Speed

Possible 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

One Motor Doesn't Move

  • 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

Motors Jitter/Oscillate

  • Usually means PID tuning is off
  • Try decreasing kP value
  • Try increasing kD value for damping

Hints for Extension

Different Motor Speeds

Create multiple commands:

// Half speed
motorSubsystem.bothMotorsAtVelocity(2500.0);

// Full speed
motorSubsystem.bothMotorsAtVelocity(5000.0);

// Reverse
motorSubsystem.bothMotorsAtVelocity(-5000.0);

Analog Control

Use joystick for smooth speed:

motorSubsystem.bothMotorsAtVelocity(driverXbox.getLeftY() * 5000.0);

Safety Check

Add method to detect motor failure:

public boolean isFollowerActive() {
    double diff = Math.abs(getLeaderOutput() - getFollowerOutput());
    return diff < 0.1; // If difference > 10%, something wrong
}

Timed Duration

Move at velocity for specific time:

bothMotorsAtVelocity(5000.0);
Timer.delay(2.0); // Run for 2 seconds
stop();

Key Concepts Introduced

  • 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

Troubleshooting Checklist

❑ 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?

Next Steps

  1. Add Load Detection - Check if motors are imbalanced
  2. Try Independent Control - Add encoder to follower motor
  3. Add Current Monitoring - Detect if motor is stalling
  4. More Complex Mechanisms - Coordinate velocity AND position
  5. Multi-Motor Systems - 3+ motors on one subsystem

Related Examples

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