Code Share
Please be patient with us on this section as we plan to have this fully student lead for our new programmers to learn from our vetern student programmers.
Disclaimer
- All information on this site is shared with the intention to help.
- Before any source code or program is used it is suggested you review it and fully understand what it is doing not just what it appears it is doing.
- When you get to the point of testing it on your robot we suggest you have the robot on blocks so drive wheels can freely spin but so the robot can not drive away.
- While we ourselves have used the code below, we accept no responsibility for any damage you may do with this code.
Prerequisites
- Download and install the latest version of WPI Library Suite from https://github.com/wpilibsuite/allwpilib/releases/
- Scroll down to the "Downloads" section and download the version appropriate for your computer.
- If you need help you can visit https://docs.wpilib.org/en/stable/docs/zero-to-robot/step-2/wpilib-setup.html and it will walk you through the installation including screenshots
- Make sure your RoboRio and radio are properly updated as well.
- See https://docs.wpilib.org/en/stable/docs/zero-to-robot/step-3/index.html if you need help.
- Follow the steps on https://docs.wpilib.org/en/stable/docs/zero-to-robot/step-4/creating-test-drivetrain-program-cpp-java-python.html to get started with a new code project.
- NOTE: The code provided on the above page will walk you through PWM code for your robot. If you only want PWM you do not need to continue on this page.
Vendor Libraries
Depending on your needs you may need to add vendor libraries as explained on https://docs.wpilib.org/en/stable/docs/software/vscode-overview/3rd-party-libraries.html
- Cross The Road Electronics (CTRE)
- If you use TalonSRX or VictorSPX you will need version 5, if do not you should be good with version 6. When in doubt you could load both.
- Version 6 - https://maven.ctr-electronics.com/release/com/ctre/phoenix6/latest/Phoenix6-frc2024-latest.json
- Version 5 - https://maven.ctr-electronics.com/release/com/ctre/phoenix/Phoenix5-frc2024-latest.json
- Kauai Labs (NavX) - https://dev.studica.com/releases/2024/NavX.json
- Rev Robotics - https://software-metadata.revrobotics.com/REVLib-2024.json
- Playing With Fusion (Venom Motors) - https://www.playingwithfusion.com/frc/playingwithfusion2024.json
Examples
- Tank Drive with Rev Robotics Spark Max Motor Controllers
- Mecanum Drive with Rev Robotics Spark Max Motor Controllers
-
Full Tank / Arcade Drive Code Projects
- Rev Robotics Spark Max Motor Controllers - 4 CIM Motor Configuration
- CTRE VictorSPX / TalonSRX Motor Controllers - 4 CIM Motor Configuration
Example 01 - Rev Robotics - Spark Max Motor Controllers - Tank Drive
On WPILib Docs it explains how you can start a new project. Following these steps select the Java template of Timed Robot.
Review REV Hardware Client to set the CAN IDs.
Add the imports below to get the needed libraries for the components.
Imports - Before
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
Imports - After
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import com.revrobotics.CANSparkLowLevel.MotorType;
import com.revrobotics.CANSparkMax;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
After updating the imports you should see around line 27 a line that has this text 'private final SendableChooser
DifferentialDrive m_robotDrive;
Joystick js_Driver;
Joystick js_Operator;
// Make sure you use the REV Hardware Client to set the CAN IDs
CANSparkMax m_leftMotor = new CANSparkMax(10,MotorType.kBrushed);
CANSparkMax m_rightMotor = new CANSparkMax(11,MotorType.kBrushed);
Using NEO or other Brushless
DifferentialDrive m_robotDrive;
Joystick js_Driver;
Joystick js_Operator;
// Make sure you use the REV Hardware Client to set the CAN IDs
CANSparkMax m_leftMotor = new CANSparkMax(10,MotorType.kBrushless);
CANSparkMax m_rightMotor = new CANSparkMax(11,MotorType.kBrushless);
Wait, what if I have/want four drive motors? Simple update.
Using CIM Motors (Motors that came with kit of parts)
DifferentialDrive m_robotDrive;
Joystick js_Driver;
Joystick js_Operator;
// Make sure you use the REV Hardware Client to set the CAN IDs
CANSparkMax m_FrontLeftMotor = new CANSparkMax(10, MotorType.kBrushed);
CANSparkMax m_FrontRightMotor = new CANSparkMax(11, MotorType.kBrushed);
CANSparkMax m_RearLeftMotor = new CANSparkMax(12, MotorType.kBrushed);
CANSparkMax m_RearRightMotor = new CANSparkMax(13, MotorType.kBrushed);
Using NEO or other Brushless
DifferentialDrive m_robotDrive;
Joystick js_Driver;
Joystick js_Operator;
// Make sure you use the REV Hardware Client to set the CAN IDs
CANSparkMax m_FrontLeftMotor = new CANSparkMax(10, MotorType.kBrushless);
CANSparkMax m_FrontRightMotor = new CANSparkMax(11, MotorType.kBrushless);
CANSparkMax m_RearLeftMotor = new CANSparkMax(12, MotorType.kBrushless);
CANSparkMax m_RearRightMotor = new CANSparkMax(13, MotorType.kBrushless);
Scroll down in the code until you get to 'robotInit()'
robotInit - Before
@Override
public void robotInit() {
m_chooser.setDefaultOption("Default Auto", kDefaultAuto);
m_chooser.addOption("My Auto", kCustomAuto);
SmartDashboard.putData("Auto choices", m_chooser);
}
robotInit - Two Motor Drivetrain After
@Override
public void robotInit() {
m_chooser.setDefaultOption("Default Auto", kDefaultAuto);
m_chooser.addOption("My Auto", kCustomAuto);
SmartDashboard.putData("Auto choices", m_chooser);
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
// 2 motor drive train setup
m_rightMotor.setInverted(true);
m_robotDrive = new DifferentialDrive(m_leftMotor::set, m_rightMotor::set);
js_Driver = new Joystick(0);
js_Operator = new Joystick(1);
}
robotInit - Four Motor Drivetrain After
@Override
public void robotInit() {
m_chooser.setDefaultOption("Default Auto", kDefaultAuto);
m_chooser.addOption("My Auto", kCustomAuto);
SmartDashboard.putData("Auto choices", m_chooser);
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
// 4 motor drive train setup
m_FrontRightMotor.setInverted(true);
m_RearRightMotor.setInverted(true);
m_RearLeftMotor.follow(m_FrontLeftMotor);
m_RearRightMotor.follow(m_FrontRightMotor);
m_robotDrive = new DifferentialDrive(m_FrontLeftMotor::set, m_RearLeftMotor::set);
js_Driver = new Joystick(0);
js_Operator = new Joystick(1);
}
Scroll down in the code until you get to 'teleopPeriodic()'
teleopPeriodic - Before
@Override
public void teleopPeriodic() {}
teleopPeriodic - AfterNOTE: Only choose one of the drive options based on your controller and based on your drive preference.
@Override
public void teleopPeriodic() {
// Only use one of the two and double check the axis you want to use within the Driver Station application
// Tank Drive, which controls the left and right side independently
// Tank Drive - Logitech F310 Controller in D Switch Mode
m_robotDrive.tankDrive(js_Driver.getRawAxis(1), js_Driver.getRawAxis(3));
// Tank Drive - Logitech F310 Controller in X Switch Mode
m_robotDrive.tankDrive(js_Driver.getRawAxis(1), js_Driver.getRawAxis(5));
// Tank Drive - XBox Controller
m_robotDrive.tankDrive(js_Driver.getRawAxis(1), js_Driver.getRawAxis(5));
// Arcade Drive, which controls a forward and turn speed
// Arcade Drive - Logitech F310 Controller in D Switch Mode
m_robotDrive.arcadeDrive(js_Driver.getRawAxis(1), js_Driver.getRawAxis(2));
// Arcade Drive - Logitech F310 Controller in X Switch Mode
m_robotDrive.arcadeDrive(js_Driver.getRawAxis(1), js_Driver.getRawAxis(4));
// Arcade Drive - XBox Controller
m_robotDrive.arcadeDrive(js_Driver.getRawAxis(1), js_Driver.getRawAxis(4));
}
Example 02 - Rev Robotics - Spark Max Motor Controllers - Mecanum Drive
NOTE: Mecanum Drive programming is very similar to the four motor Tank Drive programming.On WPILib Docs it explains how you can start a new project. Following these steps select the Java template of Timed Robot.
Review REV Hardware Client to set the CAN IDs.
Add the imports below to get the needed libraries for the components.
Imports - Before
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
Imports - After
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import com.revrobotics.CANSparkLowLevel.MotorType;
import com.revrobotics.CANSparkMax;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.drive.MecanumDrive;
After updating the imports you should see around line 27 a line that has this text 'private final SendableChooser
MecanumDrive m_robotDrive;
Joystick js_Driver;
Joystick js_Operator;
// Make sure you use the REV Hardware Client to set the CAN IDs
CANSparkMax m_FrontLeftMotor = new CANSparkMax(10, MotorType.kBrushed);
CANSparkMax m_FrontRightMotor = new CANSparkMax(11, MotorType.kBrushed);
CANSparkMax m_RearLeftMotor = new CANSparkMax(12, MotorType.kBrushed);
CANSparkMax m_RearRightMotor = new CANSparkMax(13, MotorType.kBrushed);
Using NEO or other Brushless
MecanumDrive m_robotDrive;
Joystick js_Driver;
Joystick js_Operator;
// Make sure you use the REV Hardware Client to set the CAN IDs
CANSparkMax m_FrontLeftMotor = new CANSparkMax(10, MotorType.kBrushless);
CANSparkMax m_FrontRightMotor = new CANSparkMax(11, MotorType.kBrushless);
CANSparkMax m_RearLeftMotor = new CANSparkMax(12, MotorType.kBrushless);
CANSparkMax m_RearRightMotor = new CANSparkMax(13, MotorType.kBrushless);
Scroll down in the code until you get to 'robotInit()'
robotInit - Before
@Override
public void robotInit() {
m_chooser.setDefaultOption("Default Auto", kDefaultAuto);
m_chooser.addOption("My Auto", kCustomAuto);
SmartDashboard.putData("Auto choices", m_chooser);
}
robotInit - Four Motor Drivetrain After
@Override
public void robotInit() {
m_chooser.setDefaultOption("Default Auto", kDefaultAuto);
m_chooser.addOption("My Auto", kCustomAuto);
SmartDashboard.putData("Auto choices", m_chooser);
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
// 4 motor drive train setup
m_FrontRightMotor.setInverted(true);
m_RearRightMotor.setInverted(true);
m_robotDrive = new MecanumDrive(frontLeft::set, rearLeft::set, frontRight::set, rearRight::set);
js_Driver = new Joystick(0);
js_Operator = new Joystick(1);
}
Scroll down in the code until you get to 'teleopPeriodic()'
teleopPeriodic - Before
@Override
public void teleopPeriodic() {}
teleopPeriodic - AfterNOTE: Only choose one of the drive options based on your controller and based on your drive preference.
@Override
public void teleopPeriodic() {
// Only use one of the two and double check the axis you want to use within the Driver Station application
// Use the joystick Y axis for forward movement, X axis for lateral movement, and Z axis for rotation.
// Logitech F310 Controller in D Switch Mode
m_robotDrive.driveCartesian(-js_Driver.getRawAxis(1), -js_Driver.getRawAxis(0), -js_Driver.getRawAxis(2));
// Logitech F310 Controller in X Switch Mode
m_robotDrive.driveCartesian(-js_Driver.getRawAxis(2), -js_Driver.getRawAxis(0), -js_Driver.getRawAxis(4));
// XBox Controller
m_robotDrive.driveCartesian(-js_Driver.getRawAxis(1), -js_Driver.getRawAxis(0), -js_Driver.getRawAxis(4));
}