Swerve Drive - frc-7603/VespaRobotics2024-25 GitHub Wiki
Swerve Drive
Swerve drive is a way of moving the robot that involves moving wheels composed of 2 parts: a motor that turns the direction of the wheel and a motor that drives the wheel. This allows the robot to have 2 dimensional movement which can make the robot execute tasks more efficiently because time does not need to be wasted turning the robot.
Swerve drive wheels have 3 parameters:
- Move, or "translate", in any direction
- Turn without moving
- Turn while simultaneously moving
Information Related to the Usage of Swerve Project Creator
"The swerve project creator and swerve API have several limitations. These limitations are in place to maximize performance and improve maintainability."
The Swerve Project Generator is a tool often used for configuring and creating swerve-drive robot projects, particularly in robotics competitions like FRC.
Swerve Requirements
Before starting your swerve project, ensure the following requirements are met:
- Programming Environment: Installed IDE (e.g.,
VS Codewith theWPILib extensionforFRC projects). - Hardware Setup:
- Swerve drive modules (e.g.,
MK4,MK4i). - Motor controllers (e.g.,
TalonFX,Spark MAX). - Gyroscope sensor (e.g.,
Pigeon,NavX).
- Swerve drive modules (e.g.,
- Libraries and Dependencies:
WPILiblibraries installed.- Vendor libraries (
CTRE Phoenix,REVLib, etc.).
Creating Your Project
- Open your IDE (e.g.,
VS Code WPILib) - Use the
WPILib Command Paletteto create a new project:- Select "Create a New Project."
- Choose "
Swerve Drive" as the project template. - Configure the project name and team number.
Configuring Modules
Once the base project is created:
-
Module Configuration:
- Identify your swerve module type (e.g.,
SDS MK4). - Input parameters like
gear ratios,wheel diameter, andmotor IDs.
- Identify your swerve module type (e.g.,
-
CAN Bus Setup:
- Assign unique IDs to
motor controllersfor drive and steering. - Configure CAN-based gyroscope.
- Assign unique IDs to
-
Coordinate Frame:
- Define the robot’s
coordinate framefor field-centric control. - Set the gyro's heading to align with the field at initialization.
- Define the robot’s
The ChassisSpeeds Class
The ChassisSpeeds object represents the speeds of a robot chassis. This struct has three components:
-
vx: The velocity of the robot in thex(forward) direction.Ve+= Forward,Ve-= Backwards. -
vy: The velocity of the robot in they(sideways) direction.Ve+= Left,Ve-= Right. -
omega: The angular velocity of the robot inradians per second.
Ex.
ChassisSpeeds VarName = new ChassisSpeeds(3.0, -2.0, Math.PI);
- Moves
3 mper second forward,2 mper second right, rotating counter clockwiseπ rad (180°)
The static ChassisSpeeds.fromFieldRelativeSpeeds takes angles relative to the starting position of the robot as opposed to the current position of the robot.
Swerve Drive Kinematics
Kinematics: Use of velocities and vectors to move the robot.
SwerveModuleState: Contains information on the velocity and angle a single module of the swerve drive. The constructor takes in two arguments, the velocity of the wheel on the module (m/s), and the angle of the module (Rad).
SwerveDriveKinematics : Class that converts between ChasisSpeeds object and SwerveModuleState. The constructor takes 2 or more arguments and takes the 2d location of the modules.
Swerve Drive Odometry
Odometry: Use of sensors to estimate the robots position.
SwerveDriveOdometry : Class that handles odometry, configuration, & control of drivetrain
** Class requires the following mandatory arguments:
SwerveDriveKinematicsRotation2d(Angle reported by gyroscope)SwerveModulePosition(Initial Position of swerve modules)
CANCoder
We use the CANCoder as the encoder, so they have to be instantiated like this:
CANCoder motor = new CANCoder(num);
The number goes from 1 to 4 on our robot. 2 and 3 are reversed. Look at the robot's numbers.
TalonFX
Meanwhile, for the TalonFX, they are the real motors. The drive motor is the cancoder id + 0 (concatenation, not addition), and the turn motor is the cancoder id + 1 (concatenation, not addition).
Swerve Module
The swerve module class is essentially a class which represents a wheel. It accepts many arguments, such as the drive motor, which makes it drive, the turn motor, which makes it turn, the encoder, which makes detect where it is, and the direction, which makes it detect where it goes. The direction is whether or not the motor is reversed.
pidconfig constants tell the pid controllers what to do. ks is how much voltage to apply to get past friction. kv is the velocity. ka is the acceleration voltage. kp is to deal with the error of how much rpm.
For the drive speed and turn speed, they are self explanatory. The speed is from -1 to 1, like most other motors. The turn speed is the same thing, though it should not be used.
The TurnTo method just turns to a specific region. The min rpm is 100 and max is 200. The rotation is -1 to 1.
rotation = 1: 90 deg right
rotation = -1: 90 deg left
Setpoint seems to be how to get from one position to another. 10 sec can be modified.
Drivetrain
A drivetrain groups swerve modules together, and it makes it so that they can all be moved at the same time.
Direction accepts an angle, and it goes from -1 to 1. The turn is added to it, and it is how much the robot needs to spin.
Setspeed is self-explanatory. It goes from -1 to 1.
Other stuff in Robot.java
The offset is 0.350. The unit is unknown (although it is probably the amount of rotations).
For joystick/controller stuff, see the dedicated joystick page (follow the Unix philosophy).
Link to CTR
Link to Swerve Drive doc
Link to [FRC doc] (https://docs.wpilib.org/en/stable/docs/software/kinematics-and-odometry/swerve-drive-kinematics.html)