Basic Swerve Code for a Swerve Module using TalonFX Motors, a CTRE CANCoder, and a CTRE Pigeon Gyro
This code was designed with Swerve Drive Specialties MK3, MK4, and MK4i style modules in mind, but should be easily adaptable to other styles of modules.
As this is a fully featured iteration of the BaseFalconSwerve made for the purpose of practical application, we have implemented tools made by other members of the community including Advantagekit and Pathplanner.
The following things must be adjusted to your robot and module's specific constants in the Constants.java file (all distance units must be in meters, and rotation units in radians):
These instructions are mostly followable from Step
-
Gyro Settings:
pigeonID
andinvertGyro
(ensure that the gyro rotation is CCW+ (Counter Clockwise Positive) -
chosenModule
:
If you are using a COTS SDS Module (more modules will be added in the future), set the module and drive ratio you are using here.
This will automatically set certain constants for the specific module required to function properly.
If you are not using a COTS supported module, you should delete this variable, and fix all the errors that pop up with correct values for the module you are using
Here is a list of the constants that will automatically be set if you are using a supported module:- Wheel Circumference
- Angle Motor Invert
- Drive Motor Invert
- CANCoder Sensor Invert
- Angle Motor Gear Ratio
- Drive Motor Gear Ratio
- Angle Falcon Motor PID Values
-
trackWidth
: Center to Center distance of left and right modules in meters. -
wheelBase
: Center to Center distance of front and rear module wheels in meters. -
wheelCircumference
: Cirumference of the wheel (including tread) in meters.
If you are using a supported module, this value will be automatically set. -
driveGearRatio
: Total gear ratio for the drive motor.
If you are using a supported module, this value will be automatically set. -
angleGearRatio
: Total gear ratio for the angle motor.
If you are using a supported module, this value will be automatically set. -
canCoderInvert
andangleMotorInvert
: Both must be set such that they are CCW+.
If you are using a supported module, this value will be automatically set. -
driveMotorInvert
: If you are using a supported module, this value will be automatically set.
This can always remain false, since you set your offsets in step 11 such that a positive input to the drive motor will cause the robot to drive forwards.
However this can be set to true if for some reason you prefer the bevel gears on the wheel to face one direction or another when setting offsets. See Step 11 for more information. -
Module Specific Constants
: set the Can Id's of the motors and CANCoders for the respective modules, see the next step for setting offsets. -
Setting Offsets
- For finding the offsets, use a piece of 1x1 metal that is straight against the forks of the front and back modules (on the left and right side) to ensure that the modules are straight.
- Point the bevel gears of all the wheels in the same direction (either facing left or right), where a postive input to the drive motor drives the robot forward (you can use phoenix tuner to test this). If for some reason you set the offsets with the wheels backwards, you can change the
driveMotorInvert
value to fix. - Open smartdashboard (or shuffleboard and go to the smartdashboard tab), you will see 4 printouts called "Mod 0 Cancoder", "Mod 1 Cancoder", etc.
If you have already straightened the modules, copy those 4 numbers exactly (to 2 decimal places) to their respectiveangleOffset
variable in constants.
Note: The CANcoder values printed to smartdashboard are in degrees, when copying the values toangleOffset
you must useRotation2d.fromDegrees("copied value")
.
-
Angle Motor PID Values:
If you are using a supported module, this value will be automatically set. If you are not, or prefer a more or less aggressive response, you can use the below instructions to tune.- To tune start with a low P value (0.01).
- Multiply by 10 until the module starts oscilating around the set point
- Scale back by searching for the value (for example, if it starts oscillating at a P of 10, then try (10 -> 5 -> 7.5 -> etc)) until the module doesn't oscillate around the setpoint.
- If there is any overshoot you can add in some D by repeating the same process, leave at 0 if not. Always leave I at 0.
-
maxSpeed
: In Meters Per Second.maxAngularVelocity
: In Radians Per Second. For these you can use the theoretical values, but it is better to physically drive the robot and find the actual max values. -
Get the drive characterization values (KS, KV, KA) by using the WPILib characterization tool, found here. You will need to lock your modules straight forward, and complete the characterization as if it was a standard tank drive.
-
driveKP
:
After completeing characterization and inserting the KS, KV, and KA values into the code, tune the drive motor kP until it doesn't overshoot and doesnt oscilate around a target velocity.
LeavedriveKI
,driveKD
, anddriveKF
at 0.0.