When using Swerve Drive Specialties MK2 modules this template code will provide a quick and simple way to get your robot driving.
- A navX should be plugged into the roboRIO MXP port.
- Steering encoders (analog US digital MA3) are connected to the roboRIO analog input ports.
- Spark Max motor controllers for the drive motors are:
- powered using 40 Amp PDP ports and breakers
- controlled with CAN Bus
- set to brushless brake mode (blinking cyan LED status when disabled)
- Spark Max motor controllers for the steering motors are:
- powered with either size PDP port. We recommend connecting them to the small ports with 30 Amp breakers
- controlled with CAN Bus
- set to brushless brake mode (blinking cyan LED status when disabled)
The following port mapping is recommended
- Front Left Module
- Drive Motor Controller – CAN ID 1
- Steering Motor Controller – CAN ID 2
- Steering Encoder – Analog input 0
- Front Right Module
- Drive Motor Controller – CAN ID 3
- Steering Motor Controller – CAN ID 4
- Steering Encoder – Analog input 1
- Back Left Module
- Drive Motor Controller – CAN ID 5
- Steering Motor Controller – CAN ID 6
- Steering Encoder – Analog input 2
- Back Right Module
- Drive Motor Controller – CAN ID 7
- Steering Motor Controller – CAN ID 8
- Steering Encoder – Analog input 3
By default the robot is setup to be controlled by a XBox One controller. However any XBox-style controller should work.
The left stick is setup to control the translational movement of the robot using field-oriented control.
The right stick is setup to control the rotational movement of the robot. Right on the stick should make the robot rotate clockwise while left should make the robot rotate counter-clockwise.
The back button on the controller is setup to re-zero the robot's gyroscope. By default, the direction the robot is facing when turned on is the forwards direction but this can be changed by re-zeroing the gyroscope.
- Set your team number using the WPILib extension's "Set Team Number" action.
- In the
RobotMap
class:- If needed, change the values to match the ports and CAN IDs on your robot.
- In the
DrivetrainSubsystem
class:- Set the
TRACKWIDTH
andWHEELBASE
to your robot's trackwidth and wheelbase. - Set all of the
*_ANGLE_OFFSET
constants to-Math.toRadians(0.0)
.
- Set the
- Deploy the code to your robot.
NOTE: The robot isn't drivable quite yet, we still have to setup the module offsets
- Turn the robot on its side and align all the wheels so they are facing in the forwards direction.
NOTE: The wheels will be pointed forwards (not backwards) when modules are turned so the large bevel gears are towards the right side of the robot. When aligning the wheels they must be as straight as possible. It is recommended to use a long strait edge such as a piece of 2x1 in order to make the wheels straight.
- Record the angles of each module using the angle put onto Shuffleboard. The values are named
Front Left Module Angle
,Front Right Module Angle
, etc. - Set the values of the
*_ANGLE_OFFSET
to-Math.toRadians(<the angle you recorded>)
NOTE: All angles must be in degrees.
- Re-deploy and try to drive the robot forwards. All the wheels should stay parallel to each other. If not go back to step 3.
- Make sure all the wheels are spinning in the correct direction. If not, add 180 degrees to the offset of each wheel
that is spinning in the incorrect direction. i.e
-Math.toRadians(<angle> + 180.0)
.
To invert the controller sticks or modify the control mapping modify the DriveCommand
class.
While the default hardware setup uses NEOs & Spark MAXs to control the module, teams may desire to use different motors
to control their modules. The new Mk2SwerveModuleBuilder
class supports any combination of NEOs, CIMs, or Mini CIMs
using either CAN or PWM.
Angle motor: NEO controlled by a Spark MAX over CAN Drive motor: NEO controlled by a Spark MAX over CAN
SwerveModule module = new Mk2SwerveModuleBuilder(new Vector2(5.0, 5.0))
.angleEncoder(new AnalogInput(0), -Math.toRadians(254.16))
.angleMotor(new CANSparkMax(1, CANSparkMaxLowLevel.MotorType.kBrushless),
Mk2SwerveModuleBuilder.MotorType.NEO)
.driveMotor(new CANSparkMax(2, CANSparkMaxLowLevel.MotorType.kBrushless),
Mk2SwerveModuleBuilder.MotorType.NEO)
.build();
Angle motor: NEO controlled by a Spark MAX over PWM with custom PID constants Drive motor: NEO controlled by a Spark MAX over CAN
SwerveModule module = new Mk2SwerveModuleBuilder(new Vector2(5.0, 5.0))
.angleEncoder(new AnalogInput(0), -Math.toRadians(330.148))
.angleMotor(new Spark(4), new PidConstants(1.0, 0.0, 0.001))
.driveMotor(new CANSparkMax(4, CANSparkMaxLowLevel.MotorType.kBrushless),
Mk2SwerveModuleBuilder.MotorType.NEO)
.build();
Angle motor: Mini CIM controlled by a Talon SRX over CAN Drive motor: CIM controlled by a Talon SRX over CAN
SwerveModule module = new Mk2SwerveModuleBuilder(new Vector2(5.0, 5.0))
.angleEncoder(new AnalogInput(0), -Math.toRadians(118.1114))
.angleMotor(new TalonSRX(4), Mk2SwerveModuleBuilder.MotorType.MINI_CIM)
.driveMotor(new TalonSRX(5), Mk2SwerveModuleBuilder.MotorType.CIM)
.build();