Skip to content

Commit c2583c8

Browse files
author
TKM-inShop
committed
Fixed turning in sim, attempted to fix encoder resetting (code stub)
1 parent 0d2c8d8 commit c2583c8

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

src/main/java/frc/robot/RobotContainer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ public RobotContainer() {
222222
autoChooser.addOption(
223223
"middle score encoder",
224224
new SequentialCommandGroup(
225+
// Resets drive encoders
226+
drive.resetEncoders(),
225227
// Drive 2.23 meters forward
226228
DriveCommands.arcadeDrive(drive, () -> 0.5, () -> 0.0)
227229
.until(() -> drive.getLeftPositionMeters() >= 2.23),

src/main/java/frc/robot/subsystems/drive/Drive.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323
import edu.wpi.first.math.estimator.DifferentialDrivePoseEstimator;
2424
import edu.wpi.first.math.geometry.Pose2d;
2525
import edu.wpi.first.math.geometry.Rotation2d;
26+
import edu.wpi.first.math.geometry.Twist2d;
2627
import edu.wpi.first.math.kinematics.ChassisSpeeds;
2728
import edu.wpi.first.math.kinematics.DifferentialDriveKinematics;
2829
import edu.wpi.first.math.kinematics.DifferentialDriveWheelSpeeds;
2930
import edu.wpi.first.wpilibj.DriverStation;
3031
import edu.wpi.first.wpilibj.DriverStation.Alliance;
3132
import edu.wpi.first.wpilibj2.command.Command;
33+
import edu.wpi.first.wpilibj2.command.Commands;
3234
import edu.wpi.first.wpilibj2.command.SubsystemBase;
3335
import edu.wpi.first.wpilibj2.command.sysid.SysIdRoutine;
3436
import frc.robot.Constants;
@@ -98,6 +100,14 @@ public void periodic() {
98100
Logger.processInputs("Drive", inputs);
99101

100102
// Update gyro angle
103+
// Use the angle delta from the kinematics and module deltas
104+
Twist2d twist =
105+
kinematics.toTwist2d(
106+
getLeftPositionMeters() - lastLeftPositionMeters,
107+
getRightPositionMeters() - lastRightPositionMeters);
108+
rawGyroRotation = rawGyroRotation.plus(new Rotation2d(twist.dtheta));
109+
lastLeftPositionMeters = getLeftPositionMeters();
110+
lastRightPositionMeters = getRightPositionMeters();
101111

102112
// Update odometry
103113
poseEstimator.update(rawGyroRotation, getLeftPositionMeters(), getRightPositionMeters());
@@ -171,13 +181,13 @@ public void addVisionMeasurement(Pose2d visionPose, double timestamp) {
171181
/** Returns the position of the left wheels in meters. */
172182
@AutoLogOutput
173183
public double getLeftPositionMeters() {
174-
return inputs.leftPositionRad * wheelRadiusMeters;
184+
return inputs.leftPositionRad * wheelRadiusMeters - this.offsetLeft;
175185
}
176186

177187
/** Returns the position of the right wheels in meters. */
178188
@AutoLogOutput
179189
public double getRightPositionMeters() {
180-
return inputs.rightPositionRad * wheelRadiusMeters;
190+
return inputs.rightPositionRad * wheelRadiusMeters - this.offsetRight;
181191
}
182192

183193
/** Returns the velocity of the left wheels in meters/second. */
@@ -196,4 +206,17 @@ public double getRightVelocityMetersPerSec() {
196206
public double getCharacterizationVelocity() {
197207
return (inputs.leftVelocityRadPerSec + inputs.rightVelocityRadPerSec) / 2.0;
198208
}
209+
210+
private double offsetLeft = 0.0;
211+
private double offsetRight = 0.0;
212+
213+
public Command resetEncoders() {
214+
return Commands.runOnce(
215+
() -> {
216+
io.resetEncoders();
217+
this.offsetLeft += this.getLeftPositionMeters();
218+
this.offsetRight += this.getRightPositionMeters();
219+
},
220+
this);
221+
}
199222
}

src/main/java/frc/robot/subsystems/drive/DriveIO.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ public default void setVoltage(double leftVolts, double rightVolts) {}
3838
/** Run closed loop at the specified velocity. */
3939
public default void setVelocity(
4040
double leftRadPerSec, double rightRadPerSec, double leftFFVolts, double rightFFVolts) {}
41+
42+
public default void resetEncoders() {}
4143
}

0 commit comments

Comments
 (0)