@@ -79,14 +79,14 @@ public class PoseEstimationSubsystem extends SubsystemBase {
7979 private final StructPublisher <Pose2d > m_estimatedPosePublisher ;
8080
8181 /**
82- * The {@code ProfiledPIDController } for controlling the robot in the x and y
82+ * The {@code PIDController } for controlling the robot in the x and y
8383 * dimensions in meters (input: error in meters, output: velocity in meters per
8484 * second).
8585 */
8686 private PIDController m_controllerXY ;
8787
8888 /**
89- * The {@code ProfiledPIDController } for controlling the robot in the yaw
89+ * The {@code PIDController } for controlling the robot in the yaw
9090 * dimension in degrees (input: error in degrees, output: velocity in radians
9191 * per second).
9292 */
@@ -172,28 +172,72 @@ public Pose2d getEstimatedPose() {
172172 }
173173
174174 /**
175- * Calculates the {@code ChassisSpeeds} for moving to the closest
175+ * Calculates the {@code ChassisSpeeds} to move the robot towards the closest
176176 * {@code AprilTag}.
177177 *
178- * @param robotToTarget the {@code Tranform2d} representing the pose of the
179- * target relative to the robot
178+ * @param robotToTag the {@code Tranform2d} representing the pose of the
179+ * {@code AprilTag} relative to the robot when the robot is aligned
180180 * @param distanceThresholdInMeters the maximum distance (in meters) within
181181 * which {@code AprilTag}s are considered
182- * @return the calculated {@code ChassisSpeeds} for moving to the closest
183- * {@code AprilTag}
182+ * @return the calculated {@code ChassisSpeeds} to move the robot towards the
183+ * closest {@code AprilTag}
184184 */
185- public ChassisSpeeds chassisSpeedsToClosestTag (Transform2d robotToTarget , double distanceThresholdInMeters ) {
186- var pose = getEstimatedPose ();
187- var targetPose = closestTagPose (180 , distanceThresholdInMeters );
188- if (targetPose != null ) {
189- targetPose = targetPose .plus (robotToTarget );
190- Translation2d t = targetPose .getTranslation ().minus (pose .getTranslation ());
191- double speed = m_controllerXY .calculate (t .getNorm ());
192- t = t .getNorm () > 0 ? t .times (-speed / t .getNorm ()) : t ;
193- return new ChassisSpeeds (t .getX (), t .getY (), -m_controllerYaw
194- .calculate (targetPose .getRotation ().minus (pose .getRotation ()).getDegrees ()));
185+ public ChassisSpeeds chassisSpeedsTowardClosestTag (Transform2d robotToTag , double distanceThresholdInMeters ) {
186+ var currentRobotPose = getEstimatedPose ();
187+ var closestTagPose = closestTagPose (180 , distanceThresholdInMeters );
188+ if (closestTagPose == null )
189+ return new ChassisSpeeds ();
190+ var targetRobotPose = closestTagPose .plus (robotToTag );
191+ return chassisSpeeds (currentRobotPose , targetRobotPose , m_controllerXY , m_controllerYaw );
192+ }
193+
194+ /**
195+ * Calculates the {@code ChassisSpeeds} to move from the current {@code Pose2d}
196+ * towards the target {@code Pose2d}.
197+ *
198+ * @param currentPose the current {@code Pose2d}
199+ * @param targetPose the target {@code Pose2d}
200+ * @param contollerXY the {@code PIDController} for controlling the robot in the
201+ * x and y dimensions in meters (input: error in meters, output: velocity
202+ * in meters per second)
203+ * @param controllerYaw the {@code PIDController} for controlling the robot in
204+ * the yaw dimension in degrees (input: error in degrees, output:
205+ * velocity in radians per second)
206+ * @return the calculated {@code ChassisSpeeds} to move from the current
207+ * {@code Pose2d} towards the target {@code Pose2d}
208+ */
209+ public static ChassisSpeeds chassisSpeeds (Pose2d currentPose , Pose2d targetPose , PIDController contollerXY ,
210+ PIDController controllerYaw ) {
211+ Translation2d translationalDisplacement = targetPose .getTranslation ()
212+ .minus (currentPose .getTranslation ());
213+ double velocityX = 0 , velocityY = 0 ;
214+ double distance = translationalDisplacement .getNorm ();
215+ if (distance > 0 ) {
216+ // calculate(double) returns a non-positive value (setpoint: 0)
217+ double speed = -contollerXY .calculate (distance );
218+ velocityX = speed * translationalDisplacement .getAngle ().getCos ();
219+ velocityY = speed * translationalDisplacement .getAngle ().getSin ();
195220 }
196- return new ChassisSpeeds ();
221+ Rotation2d angularDisplacement = targetPose .getRotation ().minus (currentPose .getRotation ());
222+ // calculate(double) returns a non-positive value (setpoint: 0)
223+ double angularVelocityRadiansPerSecond = -controllerYaw .calculate (angularDisplacement .getDegrees ());
224+ return new ChassisSpeeds (velocityX , velocityY , angularVelocityRadiansPerSecond );
225+ }
226+
227+ /**
228+ * Finds the {@code Pose2d} of the {@code AprilTag} that is closest to the robot
229+ * ({@code null} if no such {@code AprilTag}).
230+ *
231+ * @param angleOfCoverageInDegrees the angular coverage (in degrees) within
232+ * which {@code AprilTag}s are considered (maximum: 180)
233+ * @param distanceThresholdInMeters the maximum distance (in meters) within
234+ * which {@code AprilTag}s are considered
235+ * @return the {@code Pose2d} of the {@code AprilTag} that is closest to the
236+ * robot ({@code null} if no such {@code AprilTag})
237+ */
238+ public Pose2d closestTagPose (double angleOfCoverageInDegrees , double distanceThresholdInMeters ) {
239+ var i = closestTagID (getEstimatedPose (), angleOfCoverageInDegrees , distanceThresholdInMeters );
240+ return i == null ? null : kFieldLayout .getTagPose (i ).get ().toPose2d ();
197241 }
198242
199243 /**
@@ -245,22 +289,6 @@ public static Integer closestTagID(Pose2d pose, double distanceThresholdInMeters
245289 return null ;
246290 }
247291
248- /**
249- * Finds the {@code Pose2d} of the {@code AprilTag} that is closest to the robot
250- * ({@code null} if no such {@code AprilTag}).
251- *
252- * @param angleOfCoverageInDegrees the angular coverage (in degrees) within
253- * which {@code AprilTag}s are considered (maximum: 180)
254- * @param distanceThresholdInMeters the maximum distance (in meters) within
255- * which {@code AprilTag}s are considered
256- * @return the {@code Pose2d} of the {@code AprilTag} that is closest to the
257- * robot ({@code null} if no such {@code AprilTag})
258- */
259- public Pose2d closestTagPose (double angleOfCoverageInDegrees , double distanceThresholdInMeters ) {
260- var i = closestTagID (getEstimatedPose (), angleOfCoverageInDegrees , distanceThresholdInMeters );
261- return i == null ? null : kFieldLayout .getTagPose (i ).get ().toPose2d ();
262- }
263-
264292 /**
265293 * Determines the ID of the {@code AprilTag} that is closest to the robot
266294 * ({@code null} if no such {@code AprilTag}).
0 commit comments