Why is checkpoint 3 optional?
It depends on whether or not your robot rotates your inputs already. If it does not, we need step 3!
// Vendor Library: Phoenix 6
// https://maven.ctr-electronics.com/release/com/ctre/phoenix6/latest/Phoenix6-frc2025-latest.json
#include <ctre/phoenix6/swerve/SwerveDrivetrain.hpp>
// You should have a SwerveDrivetrain already created
ctre::phoenix6::swerve::SwerveDrivetrain m_drivetrain;
- Checkpoint 1
Have a swerve drivetrain configured and drivable, and have the request prepared for the next steps. (Reference other members/teams for this info!)
#include <ctre/phoenix6/swerve/SwerveDrivetrain.hpp>
#include <ctre/phoenix6/swerve/SwerveRequest.hpp>
/* Robot.cpp */
// NOTE: The direction of travel (DoT) will be different depending on if you are on red/blue alliance!
// NOTE: Test the DoT before use, as it may be reversed. Reverse it by negating driveSpeed below
double driveSpeed = 0.1;
ctre::phoenix6::swerve::SwerveDrivetrain m_drivetrain;
ctre::phoenix6::swerve::requests::ApplyRobotSpeeds m_driveRequest =
ctre::phoenix6::swerve::requests::ApplyRobotSpeeds()
.WithDriveRequestType(ctre::phoenix6::swerve::impl::DriveRequestType::OpenLoopVoltage)
.WithSteerRequestType(ctre::phoenix6::swerve::impl::SteerRequestType::MotionMagicExpo);
frc::ChassisSpeeds drive_speeds;
void Robot::AutonomousInit() {
drive_speeds = frc::ChassisSpeeds{
(driveSpeed)*1_mps, // x velocity in m/s
0_mps, // y velocity in m/s
units::radians_per_second_t(0) // rotational velocity in rad/s
};
}
void Robot::AutonomousPeriodic() {
m_drivetrain.setControl( m_driveRequest.WithSpeeds(drive_speeds) );
}
void Robot::TeleopInit() {
// Stop
m_drivetrain.setControl(
m_driveRequest.WithSpeeds( frc::ChassisSpeeds{ 0_mps, 0_mps, units::radians_per_second_t(0) } )
);
}
- Checkpoint 2
Have a functioning auto that moves when entering auto, and ends when entering teleop. Be sure to test both alliances to see if you need the next step at all! If you go the correct direction both times, success!
#include <frc/DriverStation.h>
/* Robot.java */
// When auto starts, check the team we are on a and switch direction of travel as needed
// If your drivetrain already rotates movement by 180 deg depending on team, ignore this step
Optional<Alliance> team;
@Override
public void autonomousInit() {
team = DriverStation.getAlliance();
if (team.isPresent())
{
// Flip DoT based on alliance
if (team.get() == frc::DriverStation::Alliance::kRed) driveSpeed *= -1;
}
else
{
// Disable movement because of unassigned alliance state
driveSpeed = 0;
System.out.println("No team assigned!");
}
//
// Continue with previously mentioned code...
//
}
- Checkpoint 3
Ensure that the auto goes the correct direction based on the alliance its on. Going the right way now? Success!