// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package frc.robot;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.math.kinematics.SwerveDriveKinematics;
import edu.wpi.first.math.trajectory.TrapezoidProfile;
import edu.wpi.first.math.util.Units;
/**
* The Constants class provides a convenient place for teams to hold robot-wide numerical or boolean
* constants. This class should not be used for any other purpose. All constants should be declared
* globally (i.e. public static). Do not put anything functional in this class.
*
* <p>It is advised to statically import this class (or one of its inner classes) wherever the
* constants are needed, to reduce verbosity.
*/
public final class Constants {
public static class OperatorConstants {
public static final int kDriverControllerPort = 0;
}
public static final class ModuleConstants {
public static final double kWheelDiameterMeters = Units.inchesToMeters(4); //current bot
public static final double kDriveMotorGearRatio = 1 / 5.8462;
public static final double kTurningMotorGearRatio = 1 / 18.0;
public static final double kDriveEncoderRot2Meter = kDriveMotorGearRatio * Math.PI * kWheelDiameterMeters;
public static final double kTurningEncoderRot2Rad = kTurningMotorGearRatio * 2 * Math.PI;
public static final double kDriveEncoderRPM2MeterPerSec = kDriveEncoderRot2Meter / 60;
public static final double kTurningEncoderRPM2RadPerSec = kTurningEncoderRot2Rad / 60;
public static final double kPTurning = 0.5;
}
public static final class DriveConstants {
public static final double kTrackWidth = Units.inchesToMeters(24.160); //current bot
public static final double kWheelBase = Units.inchesToMeters(24.160); //current bot
// Distance between front and back wheels
public static final SwerveDriveKinematics kDriveKinematics = new SwerveDriveKinematics(
new Translation2d(kWheelBase / 2, -kTrackWidth / 2),
new Translation2d(kWheelBase / 2, kTrackWidth / 2),
new Translation2d(-kWheelBase / 2, -kTrackWidth / 2),
new Translation2d(-kWheelBase / 2, kTrackWidth / 2));
public static final int kFrontLeftDriveMotorPort = 8;
public static final int kBackLeftDriveMotorPort = 6;
public static final int kFrontRightDriveMotorPort = 2;
public static final int kBackRightDriveMotorPort = 4;
public static final int kFrontLeftTurningMotorPort = 7;
public static final int kBackLeftTurningMotorPort = 5;
public static final int kFrontRightTurningMotorPort = 1;
public static final int kBackRightTurningMotorPort = 3;
public static final boolean kFrontLeftTurningEncoderReversed = true;
public static final boolean kBackLeftTurningEncoderReversed = true;
public static final boolean kFrontRightTurningEncoderReversed = true;
public static final boolean kBackRightTurningEncoderReversed = true;
public static final boolean kFrontLeftDriveEncoderReversed = true;
public static final boolean kBackLeftDriveEncoderReversed = true;
public static final boolean kFrontRightDriveEncoderReversed = false;
public static final boolean kBackRightDriveEncoderReversed = false;
public static final int kFrontLeftDriveAbsoluteEncoderPort = 0;
public static final int kBackLeftDriveAbsoluteEncoderPort = 2;
public static final int kFrontRightDriveAbsoluteEncoderPort = 1;
public static final int kBackRightDriveAbsoluteEncoderPort = 3;
public static final boolean kFrontLeftDriveAbsoluteEncoderReversed = false;
public static final boolean kBackLeftDriveAbsoluteEncoderReversed = false;
public static final boolean kFrontRightDriveAbsoluteEncoderReversed = false;
public static final boolean kBackRightDriveAbsoluteEncoderReversed = false;
public static final double kFrontLeftDriveAbsoluteEncoderOffsetRad = -0.254;
public static final double kBackLeftDriveAbsoluteEncoderOffsetRad = -1.252;
public static final double kFrontRightDriveAbsoluteEncoderOffsetRad = -1.816;
public static final double kBackRightDriveAbsoluteEncoderOffsetRad = -4.811;
public static final double kPhysicalMaxSpeedMetersPerSecond = 5;
public static final double kPhysicalMaxAngularSpeedRadiansPerSecond = 2 * 2 * Math.PI;
public static final double kTeleDriveMaxSpeedMetersPerSecond = kPhysicalMaxSpeedMetersPerSecond / 4;
public static final double kTeleDriveMaxAngularSpeedRadiansPerSecond = //
kPhysicalMaxAngularSpeedRadiansPerSecond / 4;
public static final double kTeleDriveMaxAccelerationUnitsPerSecond = 3;
public static final double kTeleDriveMaxAngularAccelerationUnitsPerSecond = 3;
}
public static final class AutoConstants {
public static final double kMaxSpeedMetersPerSecond = DriveConstants.kPhysicalMaxSpeedMetersPerSecond / 4;
public static final double kMaxAngularSpeedRadiansPerSecond = //
DriveConstants.kPhysicalMaxAngularSpeedRadiansPerSecond / 10;
public static final double kMaxAccelerationMetersPerSecondSquared = 3;
public static final double kMaxAngularAccelerationRadiansPerSecondSquared = Math.PI / 4;
public static final double kPXController = 1.5;
public static final double kPYController = 1.5;
public static final double kPThetaController = 3;
public static final TrapezoidProfile.Constraints kThetaControllerConstraints = //
new TrapezoidProfile.Constraints(
kMaxAngularSpeedRadiansPerSecond,
kMaxAngularAccelerationRadiansPerSecondSquared);
}
public static final class OIConstants {
public static final int kDriverControllerPort = 0;
public static final int kDriverYAxis = 1;
public static final int kDriverXAxis = 0;
public static final int kDriverRotAxis = 4;
public static final int kDriverFieldOrientedButtonIdx = 1;
public static final double kDeadband = 0.05;
}
}
``` It's the first time that our team uses SwerveDrive Systemand I decided to use 0 to Autonomus's Swerve Drive Tutorial. But I don't really know how I can find the drive and turning gear ratios and also max speeds. Please tell me how I can find these values for our robot. Thank you !
Mehmet wrote this and it got included in the code block. They probably can’t edit their post to fix the formatting, so here it is again
The gear ratios for drive and turn can be done several ways, the easiest is to look at the documentation for whichever swerve module you bought. They are probably listed somewhere in there. Another way would be to count all the teeth on the gears and do the ratios yourself.
If you let us know which swerve modules you bought, people on CD will help out with telling you what the gear ratios are.
Max speeds are dependent on your robot itself along with the motors being used. You could calculate a theoretical top speed of your robot using a calculator like https://ambcalc.com/, but that theoretical top speed is going to be higher than what your robot is capable of in real life. Not to mention the dynamics of your robot itself and if it needs a lower top speed or risk tipping over or other variables that you don’t know. The swerve module documentation may also have theoretical top speeds listed in a chart that you could reference, and then it is a matter of guessing around that number and seeing what responds well on the robot for real.
Good luck!
It depends on the swerve modules they have. I know SDS has a library with these constants in there. Not sure about other modules though.
You should definitely look at documentation to find these values. However, if you want to just check your values and find them at a glance, I recommend this file from Team 364’s Swerve Base Template.. It has drive and angle (turning) gear ratios listed for the most popular swerve drive modules being used.
This is a pretty popular Swerve Drive coding template, and I found it very useful last year (when it was our first year with swerve). You can also look at it for more advanced and up-to-date updates with Swerve coding. I love 0-To-Autonomous videos, but I know they might be a little outdated to the new 2024 libraries being changed, but they have really good conceptual and baseline knowledge about how “things work behind the scenes.”
Good luck!
Hi SatchitK thank you for your answer. So I found out that our motors turning gear ratio is 150/7:1 but when I put it into my code(Java) it gives a syntax error. Do you know how can I fix it ?
Did you reference how they did it here: BaseTalonFXSwerve/src/main/java/frc/lib/util/COTSTalonFXSwerveConstants.java at 908be33e56181fecd2f9a7f7a2baa9b0655f0804 · dirtbikerxz/BaseTalonFXSwerve · GitHub
Please post the line of code you are struggling with exactly here, else we don’t know what the possible causes of the syntax error could be.
There might be loads of reasons why this might be happening. As @AndrewMcb said, link to your code or a screenshot/copy paste would be much more helpful.
I can think of a couple reasons why it might not be working, perhaps it might have to do with integer-division in Java, and how the answer gets truncated without a double-floating point number present in your calculation. Make sure you follow the definition of your gear ratio variable as per the template linked (BaseTalonFXSwerve/src/main/java/frc/lib/util/COTSTalonFXSwerveConstants.java at 908be33e56181fecd2f9a7f7a2baa9b0655f0804 · dirtbikerxz/BaseTalonFXSwerve · GitHub).
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.