// DriveConstants is a public static final class used to reference unchanging information from anywhere
// In this case, we reference LEFT_LEADER_ID or LEFT_FOLLOWER_ID to determine motor IDs
// While useful, this is not required, and these can be replaced by integer values
// For reference, here are the default values mentioned in this project:
// LEFT_LEADER_ID = 1
// LEFT_FOLLOWER_ID = 2
// RIGHT_LEADER_ID = 3
// RIGHT_FOLLOWER_ID = 4
// DRIVE_MOTOR_CURRENT_LIMIT = 60
// ASK A MENTOR/TEAM MEMBER WHAT THESE VALUES ARE, AS THEY MAY LIKELY BE DIFFERENT ON YOUR ROBOT
// Define the motor config
SparkMaxConfig config = new SparkMaxConfig();
config.voltageCompensation(12);
config.smartCurrentLimit(DriveConstants.DRIVE_MOTOR_CURRENT_LIMIT);
// Define the motor
leftLeader = new SparkMax(DriveConstants.LEFT_LEADER_ID, MotorType.kBrushed);
- Checkpoint 1
All of the motors on the drivetrain have been established according to existing ID, motor type, motor specifications, etc. (Reference other members/teams for this info!)
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
// Tell the motor to move
double x = 0; // Where (-1 < x < 1)
leftLeader.set(x); // Values outside of this range will effectively be moved inside the range
// Tell the motor to stop
leftLeader.stopMotor();
// Have the motor follow another motor at the same speed
leftFollower = new SparkMax(DriveConstants.LEFT_FOLLOWER_ID, MotorType.kBrushed);
config.follow(DriveConstants.LEFT_LEADER_ID);
leftFollower.configure(config, ResetMode.kResetSafeParameters, PersistMode.kPersistParameters);
// Now, the follower motor will automatically copy the speed of the leader
// For example…
leftLeader.set(0.5);
// … now sets both leader and follower to 50% speed
- Checkpoint 2
Have all motors moving upon request, making sure each ID is matched to the correct motor.
// DifferentialDrive is just tank/arcade drive made for you automatically!
drive = new DifferentialDrive(leftLeader, rightLeader);
// Use driveArcade() or driveTank() to move. (Pick 1)
boolean squareInputs = false; // Reduces the sensitivity of controls at low input values
// Tank Drive
double leftSpeed = 0;
double rightSpeed = 0;
drive.tankDrive(leftSpeed, rightSpeed, squareInputs);
// Arcade Drive
double driveSpeed = 0;
double turnSpeed = 0;
drive.arcadeDrive(driveSpeed, turnSpeed, squareInputs);
// Note that there may be a few other drive styles, such as curvatureDrive()
- Checkpoint 3
Have the drivetrain moving upon request in the correct directions, and get ready to replace some code in step 4!
////// IF USING PERIODIC FUNCTION... (Not using Commands)
/* Robot.java */
// A function called every 20 ms during Autonomous
@Override
public void autonomousPeriodic() {
// Run code that runs the motors
leftLeader.set(0.5).withTimeout(5);
rightLeader.set(0.5).withTimeout(5);
// [If Step 3] Replace with your drivetrain run function
}
@Override
public void teleopInit() {
// Run code that stops the motors to prevent overlap
leftLeader.stop();
rightLeader.stop();
// [If Step 3] Replace with your drivetrain stop function
}
////// IF USING COMMANDS...
/* Robot.java */
@Override
public void autonomousInit() {
m_autonomousCommand = Commands.runEnd(
() -> { // Runs continuously while the command is active
leftLeader.set(0.5).withTimeout(5);
rightLeader.set(0.5).withTimeout(5);
// [If Step 3] Replace with your drivetrain run function
},
() -> { // Runs once when the command is ended
leftLeader.stop();
rightLeader.stop();
// [If Step 3] Replace with your drivetrain stop function
}
);
// Schedule the autonomous command just created
if (m_autonomousCommand != null) {
m_autonomousCommand.schedule();
}
}
@Override
public void teleopInit() {
// Interrupt and end the Autonomous command if it is still running to prevent accidental overlap
if (m_autonomousCommand != null) {
m_autonomousCommand.cancel();
}
}
- Checkpoint 4
Test the auto, and be sure to test both alliance permutations. If we move in the right direction, success!