1- // Copyright (c) 2021-2025 Littleton Robotics
2- // http://github.com/Mechanical-Advantage
3- //
4- // Use of this source code is governed by a BSD
5- // license that can be found in the LICENSE file
6- // at the root directory of this project.
7-
81package frc .robot .Subsystems .Drive ;
92
103import static frc .robot .Subsystems .Drive .DriveConstants .*;
@@ -34,50 +27,53 @@ public class Drive extends Subsystem<DriveStates> {
3427 private final GyroIO gyroIO ;
3528 private final GyroIOInputs gyroInputs = new GyroIOInputs ();
3629 private final DifferentialDriveKinematics kinematics =
37- new DifferentialDriveKinematics (trackWidth );
38- private final double kS = GlobalConstants .ROBOT_MODE == RobotMode .SIM ? simKs : realKs ;
39- private final double kV = GlobalConstants .ROBOT_MODE == RobotMode .SIM ? simKv : realKv ;
30+ new DifferentialDriveKinematics (TRACK_WIDTH );
31+ private final double kS = GlobalConstants .ROBOT_MODE == RobotMode .SIM ? SIM_KS : REAL_KS ;
32+ private final double kV = GlobalConstants .ROBOT_MODE == RobotMode .SIM ? SIM_KV : REAL_KV ;
4033 private final DifferentialDrivePoseEstimator poseEstimator =
4134 new DifferentialDrivePoseEstimator (kinematics , Rotation2d .kZero , 0.0 , 0.0 , Pose2d .kZero );
4235 private Rotation2d rawGyroRotation = Rotation2d .kZero ;
4336 private double lastLeftPositionMeters = 0.0 ;
4437 private double lastRightPositionMeters = 0.0 ;
4538 private XboxController controller = new XboxController (0 );
4639
40+ private boolean slowMode = false ; // Slow mode variable
41+
4742 private Drive (DriveIO io , GyroIO gyroIO ) {
48- super ("Drive" , DriveStates .TANK_DRIVE );
43+ super ("Drive" , DriveStates .TANK_DRIVE );
4944 this .io = io ;
5045 this .gyroIO = gyroIO ;
5146 // Configure SysId
5247 }
48+
5349 public XboxController getController () {
54- return controller ;
50+ return controller ;
5551 }
5652
5753 public static Drive getInstance () {
58- if (instance == null ) {
59- switch (GlobalConstants .ROBOT_MODE ) {
60- case REAL :
61- instance = new Drive (new DriveIOReal (), new GyroIOReal ());
62- break ;
63- case SIM :
64- instance = new Drive (new DriveIOSim (), new GyroIO () {});
65- break ;
66- case TESTING :
67- instance = new Drive (new DriveIOReal (), new GyroIOReal ());
68- break ;
69- default :
70- throw new IllegalStateException ("Unexpected value: " + GlobalConstants .ROBOT_MODE );
71- }
72- }
73- return instance ;
54+ if (instance == null ) {
55+ switch (GlobalConstants .ROBOT_MODE ) {
56+ case REAL :
57+ instance = new Drive (new DriveIOReal (), new GyroIOReal ());
58+ break ;
59+ case SIM :
60+ instance = new Drive (new DriveIOSim (), new GyroIO () {});
61+ break ;
62+ case TESTING :
63+ instance = new Drive (new DriveIOReal (), new GyroIOReal ());
64+ break ;
65+ default :
66+ throw new IllegalStateException ("Unexpected value: " + GlobalConstants .ROBOT_MODE );
67+ }
68+ }
69+ return instance ;
7470 }
7571
7672 @ Override
7773 public void runState () {
7874 io .updateInputs (inputs );
7975 gyroIO .updateInputs (gyroInputs );
80- getState ().driveRobot ();
76+ getState ().driveRobot ();
8177
8278 // Update gyro angle
8379 if (gyroInputs .connected ) {
@@ -106,8 +102,8 @@ public void runClosedLoop(ChassisSpeeds speeds) {
106102
107103 /** Runs the drive at the desired left and right velocities. */
108104 public void runClosedLoop (double leftMetersPerSec , double rightMetersPerSec ) {
109- double leftRadPerSec = leftMetersPerSec / wheelRadiusMeters ;
110- double rightRadPerSec = rightMetersPerSec / wheelRadiusMeters ;
105+ double leftRadPerSec = leftMetersPerSec / WHEEL_RADIUS_METERS ;
106+ double rightRadPerSec = rightMetersPerSec / WHEEL_RADIUS_METERS ;
111107 SmartDashboard .putNumber ("Drive/LeftSetpointRadPerSec" , leftRadPerSec );
112108 SmartDashboard .putNumber ("Drive/RightSetpointRadPerSec" , rightRadPerSec );
113109
@@ -125,28 +121,27 @@ public void runOpenLoop(double leftVolts, double rightVolts) {
125121 public void stop () {
126122 runOpenLoop (0.0 , 0.0 );
127123 }
128-
129- public void tankDrive (double leftSpeed , double rightSpeed ) {
130- double left = MathUtil .applyDeadband (leftSpeed , 0.02 );
131- double right = MathUtil .applyDeadband (rightSpeed , 0.02 );
132124
125+ public void tankDrive (double leftSpeed , double rightSpeed ) {
126+ double left = MathUtil .applyDeadband (leftSpeed , 0.02 );
127+ double right = MathUtil .applyDeadband (rightSpeed , 0.02 );
133128
134- runClosedLoop (left * maxSpeedMetersPerSec , right * maxSpeedMetersPerSec );
129+ runClosedLoop (left * MAX_SPEED_METERS_PER_SEC , right * MAX_SPEED_METERS_PER_SEC );
135130 }
136131
137132 public void arcadeDrive (double forward , double rotation ) {
138- double x = MathUtil .applyDeadband (forward , 0.02 );
139- double z = MathUtil .applyDeadband (rotation , 0.02 );
133+ double x = MathUtil .applyDeadband (forward , 0.02 );
134+ double z = MathUtil .applyDeadband (rotation , 0.02 );
140135
141- // Calculate speeds
142- var speeds = DifferentialDrive .arcadeDriveIK (x , z , true );
136+ // Calculate speeds
137+ var speeds = DifferentialDrive .arcadeDriveIK (x , z , true );
143138
144- // Apply output
145- runClosedLoop (speeds .left * maxSpeedMetersPerSec , speeds .right * maxSpeedMetersPerSec );
139+ // Apply output
140+ runClosedLoop (speeds .left * MAX_SPEED_METERS_PER_SEC , speeds .right * MAX_SPEED_METERS_PER_SEC );
146141 }
147142
148143 /** Returns the current odometry pose. */
149-
144+
150145 public Pose2d getPose () {
151146 return poseEstimator .getEstimatedPosition ();
152147 }
@@ -173,31 +168,45 @@ public void addVisionMeasurement(Pose2d visionPose, double timestamp, Matrix<N3,
173168 }
174169
175170 /** Returns the position of the left wheels in meters. */
176-
171+
177172 public double getLeftPositionMeters () {
178- return inputs .leftPositionRad * wheelRadiusMeters ;
173+ return inputs .leftPositionRad * WHEEL_RADIUS_METERS ;
179174 }
180175
181176 /** Returns the position of the right wheels in meters. */
182-
177+
183178 public double getRightPositionMeters () {
184- return inputs .rightPositionRad * wheelRadiusMeters ;
179+ return inputs .rightPositionRad * WHEEL_RADIUS_METERS ;
185180 }
186181
187182 /** Returns the velocity of the left wheels in meters/second. */
188-
183+
189184 public double getLeftVelocityMetersPerSec () {
190- return inputs .leftVelocityRadPerSec * wheelRadiusMeters ;
185+ return inputs .leftVelocityRadPerSec * WHEEL_RADIUS_METERS ;
186+ }
187+
188+ public double getAngularVelocityRadPerSec () {
189+ return gyroInputs .yawVelocityRadPerSec ;
191190 }
192191
193192 /** Returns the velocity of the right wheels in meters/second. */
194-
193+
195194 public double getRightVelocityMetersPerSec () {
196- return inputs .rightVelocityRadPerSec * wheelRadiusMeters ;
195+ return inputs .rightVelocityRadPerSec * WHEEL_RADIUS_METERS ;
197196 }
198197
199198 /** Returns the average velocity in radians/second. */
200199 public double getCharacterizationVelocity () {
201200 return (inputs .leftVelocityRadPerSec + inputs .rightVelocityRadPerSec ) / 2.0 ;
202201 }
202+
203+ /** Returns true if slow mode is enabled. */
204+ public boolean isSlowMode () {
205+ return slowMode ;
206+ }
207+
208+ // Optionally, you may want to add a setter for slow mode:
209+ public void setSlowMode (boolean enabled ) {
210+ slowMode = enabled ;
211+ }
203212}
0 commit comments