|
9 | 9 | import java.util.Map; |
10 | 10 | import java.util.Map.Entry; |
11 | 11 | import java.util.Optional; |
12 | | -import java.util.function.Function; |
13 | 12 |
|
14 | 13 | import org.photonvision.EstimatedRobotPose; |
15 | 14 | import org.photonvision.PhotonCamera; |
|
19 | 18 | import edu.wpi.first.math.VecBuilder; |
20 | 19 | import edu.wpi.first.math.Vector; |
21 | 20 | import edu.wpi.first.math.controller.PIDController; |
22 | | -import edu.wpi.first.math.controller.ProfiledPIDController; |
23 | 21 | import edu.wpi.first.math.estimator.SwerveDrivePoseEstimator; |
24 | 22 | import edu.wpi.first.math.geometry.Pose2d; |
25 | 23 | import edu.wpi.first.math.geometry.Rotation2d; |
@@ -118,11 +116,8 @@ public PoseEstimationSubsystem(DriveSubsystem driveSubsystem) { |
118 | 116 | .getStructTopic("/SmartDashboard/Pose@PoseEstimationSubsystem", Pose2d.struct) |
119 | 117 | .publish(); |
120 | 118 | m_controllerXY = new PIDController(kDriveP, kDriveI, kDriveD); |
121 | | - m_controllerXY.setSetpoint(0); |
122 | | - |
123 | 119 | m_controllerYaw = new PIDController(kTurnP, kTurnI, kTurnD); |
124 | 120 | m_controllerYaw.enableContinuousInput(-180, 180); |
125 | | - m_controllerYaw.setSetpoint(0); |
126 | 121 | } |
127 | 122 |
|
128 | 123 | /** |
@@ -152,7 +147,7 @@ public void periodic() { |
152 | 147 | var poseEstimator = e.getValue(); |
153 | 148 | for (var r : camera.getAllUnreadResults()) { // for every result r |
154 | 149 | 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); |
156 | 151 | if (p.isPresent()) { // if successful |
157 | 152 | EstimatedRobotPose v = p.get(); // get successfully estimated pose |
158 | 153 | m_poseEstimator.addVisionMeasurement(v.estimatedPose.toPose2d(), v.timestampSeconds); |
@@ -201,71 +196,29 @@ public ChassisSpeeds chassisSpeedsTowardClosestTag(Transform2d robotToTag, doubl |
201 | 196 | * |
202 | 197 | * @param currentPose the current {@code Pose2d} |
203 | 198 | * @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) |
207 | 202 | * @param controllerYaw the {@code PIDController} for controlling the robot in |
208 | 203 | * the yaw dimension in degrees (input: error in degrees, output: |
209 | 204 | * velocity in radians per second) |
210 | 205 | * @return the calculated {@code ChassisSpeeds} to move from the current |
211 | 206 | * {@code Pose2d} toward the target {@code Pose2d} |
212 | 207 | */ |
213 | | - public static ChassisSpeeds chassisSpeeds(Pose2d currentPose, Pose2d targetPose, PIDController contollerXY, |
| 208 | + public static ChassisSpeeds chassisSpeeds(Pose2d currentPose, Pose2d targetPose, PIDController controllerXY, |
214 | 209 | 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) { |
255 | 210 | Translation2d translationalDisplacement = targetPose.getTranslation() |
256 | 211 | .minus(currentPose.getTranslation()); |
257 | 212 | double velocityX = 0, velocityY = 0; |
258 | 213 | double distance = translationalDisplacement.getNorm(); |
259 | 214 | 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); |
262 | 216 | speed = applyThreshold(speed, kDriveMinSpeed); |
263 | 217 | velocityX = speed * translationalDisplacement.getAngle().getCos(); |
264 | 218 | velocityY = speed * translationalDisplacement.getAngle().getSin(); |
265 | 219 | } |
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()); |
269 | 222 | angularVelocityRadiansPerSecond = applyThreshold(angularVelocityRadiansPerSecond, kTurnMinAngularSpeed); |
270 | 223 | return new ChassisSpeeds(velocityX, velocityY, angularVelocityRadiansPerSecond); |
271 | 224 | } |
|
0 commit comments