Skip to content

Commit 2f805fa

Browse files
committed
Merge branch 'auto-align' into unit_tests
2 parents adaa7be + c6aeb22 commit 2f805fa

File tree

3 files changed

+28
-65
lines changed

3 files changed

+28
-65
lines changed

src/main/java/frc/robot/commands/drive/ChainableDriveCommand2Controllers.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ public void initialize() {
126126
m_controllerXY.setTolerance(m_distanceTolerance);
127127
m_controllerYaw.setTolerance(m_angleTolerance);
128128
if (m_previous == null) {
129-
m_controllerXY.reset(m_targetPose.getTranslation().minus(pose.getTranslation()).getNorm());
130-
m_controllerYaw.reset(m_targetPose.getRotation().minus(pose.getRotation()).getDegrees());
129+
m_controllerXY.reset(m_targetPose.minus(pose).getTranslation().getNorm());
130+
m_controllerYaw.reset(pose.getRotation().getDegrees());
131131
}
132132
}
133133

src/main/java/frc/robot/commands/drive/DriveCommand2Controllers.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import edu.wpi.first.math.geometry.Pose2d;
99
import edu.wpi.first.math.geometry.Rotation2d;
1010
import edu.wpi.first.math.geometry.Transform2d;
11+
import edu.wpi.first.math.geometry.Translation2d;
1112
import edu.wpi.first.math.trajectory.TrapezoidProfile;
1213
import edu.wpi.first.wpilibj2.command.Command;
1314
import frc.robot.subsystems.DriveSubsystem;
14-
import frc.robot.subsystems.PoseEstimationSubsystem;
1515

1616
/**
1717
* This {@code DriveCommand} aims to maneuver the robot to a certain
@@ -133,8 +133,6 @@ public DriveCommand2Controllers(DriveSubsystem driveSubsystem, Supplier<Pose2d>
133133
new TrapezoidProfile.Constraints(Math.toDegrees(kTurnMaxAngularSpeed),
134134
Math.toDegrees(kTurnMaxAcceleration))));
135135
m_controllerYaw.enableContinuousInput(-180, 180);
136-
m_controllerXY.setGoal(0);
137-
m_controllerYaw.setGoal(0);
138136
}
139137

140138
/**
@@ -188,8 +186,8 @@ public void initialize() {
188186
}
189187
m_controllerXY.setTolerance(m_distanceTolerance);
190188
m_controllerYaw.setTolerance(m_angleTolerance);
191-
m_controllerXY.reset(m_targetPose.getTranslation().minus(pose.getTranslation()).getNorm());
192-
m_controllerYaw.reset(m_targetPose.getRotation().minus(pose.getRotation()).getDegrees());
189+
m_controllerXY.reset(m_targetPose.minus(pose).getTranslation().getNorm());
190+
m_controllerYaw.reset(pose.getRotation().getDegrees());
193191
}
194192

195193
/**
@@ -198,9 +196,21 @@ public void initialize() {
198196
*/
199197
@Override
200198
public void execute() {
201-
var speeds = PoseEstimationSubsystem
202-
.chassisSpeeds(m_poseSupplier.get(), m_targetPose, m_controllerXY, m_controllerYaw);
203-
m_driveSubsystem.drive(speeds, true);
199+
var currentPose = m_poseSupplier.get();
200+
Translation2d translationalDisplacement = m_targetPose.getTranslation()
201+
.minus(currentPose.getTranslation());
202+
double velocityX = 0, velocityY = 0;
203+
double distance = translationalDisplacement.getNorm();
204+
if (distance > 0) {
205+
double speed = Math.abs(m_controllerXY.calculate(distance, 0));
206+
speed = applyThreshold(speed, kDriveMinSpeed);
207+
velocityX = speed * translationalDisplacement.getAngle().getCos();
208+
velocityY = speed * translationalDisplacement.getAngle().getSin();
209+
}
210+
double angularVelocityRadiansPerSecond = m_controllerYaw
211+
.calculate(currentPose.getRotation().getDegrees(), m_targetPose.getRotation().getDegrees());
212+
angularVelocityRadiansPerSecond = applyThreshold(angularVelocityRadiansPerSecond, kTurnMinAngularSpeed);
213+
m_driveSubsystem.drive(velocityX, velocityY, angularVelocityRadiansPerSecond, true);
204214
}
205215

206216
/**

src/main/java/frc/robot/subsystems/PoseEstimationSubsystem.java

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.util.Map;
1010
import java.util.Map.Entry;
1111
import java.util.Optional;
12-
import java.util.function.Function;
1312

1413
import org.photonvision.EstimatedRobotPose;
1514
import org.photonvision.PhotonCamera;
@@ -19,7 +18,6 @@
1918
import edu.wpi.first.math.VecBuilder;
2019
import edu.wpi.first.math.Vector;
2120
import edu.wpi.first.math.controller.PIDController;
22-
import edu.wpi.first.math.controller.ProfiledPIDController;
2321
import edu.wpi.first.math.estimator.SwerveDrivePoseEstimator;
2422
import edu.wpi.first.math.geometry.Pose2d;
2523
import edu.wpi.first.math.geometry.Rotation2d;
@@ -118,11 +116,8 @@ public PoseEstimationSubsystem(DriveSubsystem driveSubsystem) {
118116
.getStructTopic("/SmartDashboard/Pose@PoseEstimationSubsystem", Pose2d.struct)
119117
.publish();
120118
m_controllerXY = new PIDController(kDriveP, kDriveI, kDriveD);
121-
m_controllerXY.setSetpoint(0);
122-
123119
m_controllerYaw = new PIDController(kTurnP, kTurnI, kTurnD);
124120
m_controllerYaw.enableContinuousInput(-180, 180);
125-
m_controllerYaw.setSetpoint(0);
126121
}
127122

128123
/**
@@ -152,7 +147,7 @@ public void periodic() {
152147
var poseEstimator = e.getValue();
153148
for (var r : camera.getAllUnreadResults()) { // for every result r
154149
if (firstCamera || r.getTargets().size() > 1) {
155-
Optional<EstimatedRobotPose> p = poseEstimator.update(r); // assign estimated pose to e
150+
Optional<EstimatedRobotPose> p = poseEstimator.update(r);
156151
if (p.isPresent()) { // if successful
157152
EstimatedRobotPose v = p.get(); // get successfully estimated pose
158153
m_poseEstimator.addVisionMeasurement(v.estimatedPose.toPose2d(), v.timestampSeconds);
@@ -201,71 +196,29 @@ public ChassisSpeeds chassisSpeedsTowardClosestTag(Transform2d robotToTag, doubl
201196
*
202197
* @param currentPose the current {@code Pose2d}
203198
* @param targetPose the target {@code Pose2d}
204-
* @param contollerXY the {@code PIDController} for controlling the robot in the
205-
* x and y dimensions in meters (input: error in meters, output: velocity
206-
* in meters per second)
199+
* @param controllerXY the {@code PIDController} for controlling the robot in
200+
* the x and y dimensions in meters (input: error in meters, output:
201+
* velocity in meters per second)
207202
* @param controllerYaw the {@code PIDController} for controlling the robot in
208203
* the yaw dimension in degrees (input: error in degrees, output:
209204
* velocity in radians per second)
210205
* @return the calculated {@code ChassisSpeeds} to move from the current
211206
* {@code Pose2d} toward the target {@code Pose2d}
212207
*/
213-
public static ChassisSpeeds chassisSpeeds(Pose2d currentPose, Pose2d targetPose, PIDController contollerXY,
208+
public static ChassisSpeeds chassisSpeeds(Pose2d currentPose, Pose2d targetPose, PIDController controllerXY,
214209
PIDController controllerYaw) {
215-
return chassisSpeeds(currentPose, targetPose, d -> contollerXY.calculate(d), a -> controllerYaw.calculate(a));
216-
}
217-
218-
/**
219-
* Calculates the {@code ChassisSpeeds} to move from the current {@code Pose2d}
220-
* towards the target {@code Pose2d}.
221-
*
222-
* @param currentPose the current {@code Pose2d}
223-
* @param targetPose the target {@code Pose2d}
224-
* @param contollerXY the {@code ProfiledPIDController} for controlling the
225-
* robot in the x and y dimensions in meters (input: error in meters,
226-
* output: velocity in meters per second)
227-
* @param controllerYaw the {@code ProfiledPIDController} for controlling the
228-
* robot in the yaw dimension in degrees (input: error in degrees,
229-
* output: velocity in radians per second)
230-
* @return the calculated {@code ChassisSpeeds} to move from the current
231-
* {@code Pose2d} towards the target {@code Pose2d}
232-
*/
233-
public static ChassisSpeeds chassisSpeeds(Pose2d currentPose, Pose2d targetPose,
234-
ProfiledPIDController contollerXY, ProfiledPIDController controllerYaw) {
235-
return chassisSpeeds(currentPose, targetPose, d -> contollerXY.calculate(d), a -> controllerYaw.calculate(a));
236-
}
237-
238-
/**
239-
* Calculates the {@code ChassisSpeeds} to move from the current {@code Pose2d}
240-
* towards the target {@code Pose2d}.
241-
*
242-
* @param currentPose the current {@code Pose2d}
243-
* @param targetPose the target {@code Pose2d}
244-
* @param contollerXY the {@code Function<Double, Double>} for controlling the
245-
* robot in the x and y dimensions in meters (input: error in meters,
246-
* output: velocity in meters per second)
247-
* @param controllerYaw the {@code Function<Double, Double>} for controlling the
248-
* robot in the yaw dimension in degrees (input: error in degrees,
249-
* output: velocity in radians per second)
250-
* @return the calculated {@code ChassisSpeeds} to move from the current
251-
* {@code Pose2d} towards the target {@code Pose2d}
252-
*/
253-
public static ChassisSpeeds chassisSpeeds(Pose2d currentPose, Pose2d targetPose,
254-
Function<Double, Double> contollerXY, Function<Double, Double> controllerYaw) {
255210
Translation2d translationalDisplacement = targetPose.getTranslation()
256211
.minus(currentPose.getTranslation());
257212
double velocityX = 0, velocityY = 0;
258213
double distance = translationalDisplacement.getNorm();
259214
if (distance > 0) {
260-
// apply(double) returns a non-positive value (setpoint: 0)
261-
double speed = -contollerXY.apply(distance);
215+
double speed = controllerXY.calculate(0.0, distance);
262216
speed = applyThreshold(speed, kDriveMinSpeed);
263217
velocityX = speed * translationalDisplacement.getAngle().getCos();
264218
velocityY = speed * translationalDisplacement.getAngle().getSin();
265219
}
266-
Rotation2d angularDisplacement = targetPose.getRotation().minus(currentPose.getRotation());
267-
// apply(double) returns a non-positive value (setpoint: 0)
268-
double angularVelocityRadiansPerSecond = -controllerYaw.apply(angularDisplacement.getDegrees());
220+
double angularVelocityRadiansPerSecond = controllerYaw
221+
.calculate(currentPose.getRotation().getDegrees(), targetPose.getRotation().getDegrees());
269222
angularVelocityRadiansPerSecond = applyThreshold(angularVelocityRadiansPerSecond, kTurnMinAngularSpeed);
270223
return new ChassisSpeeds(velocityX, velocityY, angularVelocityRadiansPerSecond);
271224
}

0 commit comments

Comments
 (0)