Skip to content

Add Swerve drive solution#46

Open
ArchdukeTim wants to merge 1 commit into
frcsoftware:mainfrom
ArchdukeTim:tim/swerve
Open

Add Swerve drive solution#46
ArchdukeTim wants to merge 1 commit into
frcsoftware:mainfrom
ArchdukeTim:tim/swerve

Conversation

@ArchdukeTim

Copy link
Copy Markdown
Contributor

No description provided.

@github-actions

Copy link
Copy Markdown

🌐 Preview URL: https://pr-46.frcsoftware.pages.dev

@Adrianamm Adrianamm moved this to In progress in FRC Software Jun 26, 2026
@Daniel1464

Copy link
Copy Markdown

The file directory should probably be changed to swerveDrive/ctre/solution to stay consistent with the solution naming of the other exercises (like stage1/rev/solution and stage1/ctre/solution). Alternatively, swerveDrive/solution would work if the aim is to only teach swerve with the CTRE API.

private final CANcoder cancoder;

/** Radius of module wheel in meters */
private static final double wheelRadius = Units.inchesToMeters(2.0);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static constants should probably be at the top of the class instead of nested between instance members

*
* <p>3.5 m/s divided by radius gives radians per second
*/
private static final double maxSpeedRps = Units.radiansToRotations(SwerveDrivetrain.MAX_SPEED / wheelRadius);

@Daniel1464 Daniel1464 Jun 26, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to use methods from the DCMotor class, which allows you to access the max speed of a motor in rad/s via DCMotor.getKrakenX60(1).withReduction(reduction).freeSpeed. That can then be multiplied by the wheel radius instead of divided like here.

*/
private final DCMotorSim driveSim =
new DCMotorSim(
Models.singleJointedArmFromPhysicalConstants(DCMotor.getKrakenX60(1), 0.01, 6.0),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The motor type and gear ratio arguments should probably be constants in SwerveDrivetrain

* @param vy Velocity in the field-y direction
* @param omega Angular velocity about the z-axis.
*/
public void driveFieldCentric(double vx, double vy, double omega) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think calling this xThrottle, yThrottle, and rotationalThrottle would be more applicable since these aren't speeds.

Providing another overload that takes in a straight robot-relative ChassisVelocities would be a good idea too, to prepare them for path following and the like

/** Called periodically while running in simulation */
public void simulationPeriodic() {
// Get input from talonfx sim, and apply to mechanism
driveSim.setInputVoltage(driveMotor.getSimState().getMotorVoltage());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a question - will the sim stuff be autofilled in the actual exercise, or are we expecting people to know how to work with sim by this point?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

3 participants