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


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


Examples


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 m_chooser = new SendableChooser<>();'. One of the code blocks below will go right below it.

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_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 - After
NOTE: 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 m_chooser = new SendableChooser<>();'. One of the code blocks below will go right below it.

Using CIM Motors (Motors that came with kit of parts)

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 - After
NOTE: 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));

  }
Last modified: May 16 2024 03:26:03 PM