Skip to content

Commit

Permalink
chore: constants fix (#16)
Browse files Browse the repository at this point in the history
* chore: constants fix

* Update src/main/java/frc/robot/Constants.java

* Update src/main/java/frc/robot/Constants.java

* Update src/main/java/frc/robot/Constants.java
  • Loading branch information
JetBluefan1 authored Nov 18, 2024
1 parent 39bfca1 commit 7f150da
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 31 deletions.
20 changes: 14 additions & 6 deletions src/main/java/frc/robot/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@

package frc.robot;

import static edu.wpi.first.units.Units.*;

import com.pathplanner.lib.config.PIDConstants;
import edu.wpi.first.math.geometry.Translation3d;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.units.Distance;
import edu.wpi.first.units.Mass;
import edu.wpi.first.units.Measure;
import edu.wpi.first.units.Time;
import edu.wpi.first.units.Velocity;
import swervelib.math.Matter;

/**
Expand All @@ -18,11 +25,12 @@
* constants are needed, to reduce verbosity.
*/
public final class Constants {
public static final double ROBOT_MASS = (148 - 20.3) * 0.453592; // 32lbs * kg per pound
public static final Measure<Mass> ROBOT_MASS = Pounds.of(125);
public static final Matter CHASSIS =
new Matter(new Translation3d(0, 0, Units.inchesToMeters(8)), ROBOT_MASS);
public static final double LOOP_TIME = 0.13; // s, 20ms + 110ms sprk max velocity lag
public static final double MAX_SPEED = Units.feetToMeters(14.5);
new Matter(new Translation3d(0, 0, Units.inchesToMeters(8)), ROBOT_MASS.in(Pounds));
public static final Measure<Time> LOOP_TIME =
Seconds.of(0.13); // s, 20ms + 110ms sprk max velocity lag
public static final Measure<Velocity<Distance>> MAX_SPEED = FeetPerSecond.of(14.5);

public static final class AutoConstants {
public static final PIDConstants TRANSLATION_PID = new PIDConstants(0.7, 0, 0);
Expand All @@ -31,11 +39,11 @@ public static final class AutoConstants {

public static final class DrivebaseConstants {
// Hold time on motor brakes when disabled.
public static final double WHEEL_LOCK_TIME = 10; // seconds
public static final Measure<Time> WHEEL_LOCK_TIME = Seconds.of(10);
}

public static class OperatorConstants {
public static final int kDriverControllerPort = 0;
public static final int DRIVER_CONTROLLER_PORT = 0;

// Joystick deadband.
public static final double LEFT_X_DEADBAND = 0.1;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package frc.robot;

import static edu.wpi.first.units.Units.Seconds;

import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj2.command.Command;
Expand Down Expand Up @@ -74,7 +76,7 @@ public void disabledInit() {

@Override
public void disabledPeriodic() {
if (disabledTimer.hasElapsed(Constants.DrivebaseConstants.WHEEL_LOCK_TIME)) {
if (disabledTimer.hasElapsed(Constants.DrivebaseConstants.WHEEL_LOCK_TIME.in(Seconds))) {
m_robotContainer.setMotorBrake(false);
disabledTimer.stop();
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public class RobotContainer {

private final SendableChooser<Command> autoChooser;
// Replace with CommandPS4Controller or CommandJoystick if needed
final CommandXboxController driverXbox = new CommandXboxController(0);
final CommandXboxController driverXbox =
new CommandXboxController(OperatorConstants.DRIVER_CONTROLLER_PORT);
// The robot's subsystems and commands are defined here...
private final SwerveSubsystem drivebase =
new SwerveSubsystem(new File(Filesystem.getDeployDirectory(), "swerve"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

package frc.robot.commands.drivebase;

import static edu.wpi.first.units.Units.Kilograms;
import static edu.wpi.first.units.Units.Seconds;

import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.math.kinematics.ChassisSpeeds;
Expand Down Expand Up @@ -64,8 +67,8 @@ public void execute() {
translation,
swerve.getFieldVelocity(),
swerve.getPose(),
Constants.LOOP_TIME,
Constants.ROBOT_MASS,
Constants.LOOP_TIME.in(Seconds),
Constants.ROBOT_MASS.in(Kilograms),
List.of(Constants.CHASSIS),
swerve.getSwerveDriveConfiguration());
SmartDashboard.putNumber("LimitedTranslation", translation.getX());
Expand Down
28 changes: 7 additions & 21 deletions src/main/java/frc/robot/subsystems/SwerveSubsystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package frc.robot.subsystems;

import static edu.wpi.first.units.Units.*;

import com.pathplanner.lib.auto.AutoBuilder;
import com.pathplanner.lib.commands.PathPlannerAuto;
import com.pathplanner.lib.config.PIDConstants;
Expand Down Expand Up @@ -48,28 +50,12 @@ public class SwerveSubsystem extends SubsystemBase {
* @param directory Directory of swerve drive config files.
*/
public SwerveSubsystem(File directory) {
// Angle conversion factor is 360 / (GEAR RATIO * ENCODER RESOLUTION)
// In this case the gear ratio is 12.8 motor revolutions per wheel rotation.
// The encoder resolution per motor revolution is 1 per motor revolution.
double angleConversionFactor = SwerveMath.calculateDegreesPerSteeringRotation(12.8);
// Motor conversion factor is (PI * WHEEL DIAMETER IN METERS) / (GEAR RATIO * ENCODER
// RESOLUTION).
// In this case the wheel diameter is 4 inches, which must be converted to meters to get
// meters/second.
// The gear ratio is 6.75 motor revolutions per wheel rotation.
// The encoder resolution per motor revolution is 1 per motor revolution.
double driveConversionFactor =
SwerveMath.calculateMetersPerRotation(Units.inchesToMeters(4), 6.75);
System.out.println("\"conversionFactors\": {");
System.out.println("\t\"angle\": {\"factor\": " + angleConversionFactor + " },");
System.out.println("\t\"drive\": {\"factor\": " + driveConversionFactor + " }");
System.out.println("}");

// Configure the Telemetry before creating the SwerveDrive to avoid unnecessary objects being
// created.
SwerveDriveTelemetry.verbosity = TelemetryVerbosity.HIGH;
try {
swerveDrive = new SwerveParser(directory).createSwerveDrive(Constants.MAX_SPEED);
swerveDrive =
new SwerveParser(directory).createSwerveDrive(Constants.MAX_SPEED.in(MetersPerSecond));
// Alternative method if you don't want to supply the conversion factor via JSON files.
// swerveDrive = new SwerveParser(directory).createSwerveDrive(maximumSpeed,
// angleConversionFactor, driveConversionFactor);
Expand Down Expand Up @@ -107,7 +93,7 @@ public SwerveSubsystem(File directory) {
*/
public SwerveSubsystem(
SwerveDriveConfiguration driveCfg, SwerveControllerConfiguration controllerCfg) {
swerveDrive = new SwerveDrive(driveCfg, controllerCfg, Constants.MAX_SPEED);
swerveDrive = new SwerveDrive(driveCfg, controllerCfg, Constants.MAX_SPEED.in(MetersPerSecond));
}

@Override
Expand Down Expand Up @@ -503,7 +489,7 @@ public ChassisSpeeds getTargetSpeeds(
headingX,
headingY,
getHeading().getRadians(),
Constants.MAX_SPEED);
Constants.MAX_SPEED.in(MetersPerSecond));
}

/**
Expand All @@ -523,7 +509,7 @@ public ChassisSpeeds getTargetSpeeds(double xInput, double yInput, Rotation2d an
scaledInputs.getY(),
angle.getRadians(),
getHeading().getRadians(),
Constants.MAX_SPEED);
Constants.MAX_SPEED.in(MetersPerSecond));
}

/**
Expand Down

0 comments on commit 7f150da

Please sign in to comment.