1717 */
1818public class PathDriveCommand extends DriveCommand {
1919
20- /**
21- * The index for indicating the current {@code Pose2d} to which the robot
22- * should move.
23- */
24- protected int m_targetPoseIndex = 0 ;
25-
2620 /**
2721 * The ratio to apply to the distance and angle tolerances for intermeidate
2822 * target {@code Pose2d}s
@@ -64,7 +58,8 @@ public PathDriveCommand(DriveSubsystem driveSubsystem,
6458 double distanceTolerance ,
6559 double angleToleranceInDegrees , double intermediateToleranceRatio ,
6660 List <Supplier <Pose2d >> targetPoseSuppliers ) {
67- super (driveSubsystem , distanceTolerance , angleToleranceInDegrees , targetPoseSuppliers );
61+ super (driveSubsystem , distanceTolerance , angleToleranceInDegrees ,
62+ new IterativeTargetPoseSupplier (targetPoseSuppliers ));
6863 m_intermediateToleranceRatio = intermediateToleranceRatio ;
6964 }
7065
@@ -75,28 +70,25 @@ public PathDriveCommand(DriveSubsystem driveSubsystem,
7570 */
7671 @ Override
7772 public void initialize () {
78- setTargetPose ( 0 );
73+ moveToNextTargetPose ( );
7974 Pose2d pose = m_driveSubsystem .getPose ();
8075 m_controllerXY .reset (m_targetPose .minus (pose ).getTranslation ().getNorm ());
8176 m_controllerYaw .reset (pose .getRotation ().getRadians ());
8277 }
8378
8479 /**
85- * Sets the current target {@code Pose2d}
86- *
87- * @param targetPoseIndex the index of the target {@code Pose2d}
80+ * Moves to the next target {@code Pose2d}.
8881 */
89- void setTargetPose (int targetPoseIndex ) {
90- m_targetPoseIndex = targetPoseIndex ;
82+ protected void moveToNextTargetPose () {
9183 Pose2d pose = m_driveSubsystem .getPose ();
9284 m_targetPose = pose ;
9385 try {
94- m_targetPose = m_targetPoseSuppliers . get ( m_targetPoseIndex ) .get ();
86+ m_targetPose = m_targetPoseSupplier .get ();
9587 } catch (Exception e ) {
9688 e .printStackTrace ();
9789 m_targetPose = pose ;
9890 }
99- if (m_targetPoseIndex < m_targetPoseSuppliers . size () - 1 ) {
91+ if (targetPoseSupplier (). hasNext () ) {
10092 m_controllerXY .setTolerance (m_intermediateToleranceRatio * m_distanceTolerance );
10193 m_controllerYaw .setTolerance (m_intermediateToleranceRatio * m_angleTolerance );
10294 } else {
@@ -114,9 +106,10 @@ void setTargetPose(int targetPoseIndex) {
114106 @ Override
115107 public boolean isFinished () {
116108 if (alignedTo (m_targetPose )) {
117- if (m_targetPoseIndex == m_targetPoseSuppliers .size () - 1 )
109+ if (targetPoseSupplier ().hasNext ())
110+ moveToNextTargetPose ();
111+ else
118112 return true ;
119- setTargetPose (m_targetPoseIndex + 1 );
120113 }
121114 return false ;
122115 }
@@ -135,4 +128,66 @@ boolean alignedTo(Pose2d targetPose) {
135128 && Math .abs (diff .getRotation ().getRadians ()) < m_controllerYaw .getPositionTolerance ();
136129 }
137130
131+ /**
132+ * Returns the {@code IterativeTargetPoseSupplier} used by this
133+ * {@code PathDriveCommand}.
134+ *
135+ * @return the {@code IterativeTargetPoseSupplier} used by this
136+ * {@code PathDriveCommand}
137+ */
138+ protected IterativeTargetPoseSupplier targetPoseSupplier () {
139+ return (IterativeTargetPoseSupplier ) m_targetPoseSupplier ;
140+ }
141+
142+ /**
143+ * An {@code IterativeTargetPoseSupplier} is a {@code Supplier<Pose2d>} that
144+ * iterates over a number of {@code Supplier<Pose2d>}.
145+ * The {@link get()} method of an {@code IterativeTargetPoseSupplier} returns
146+ * the {@code Pose2d} from the current {@code Supplier<Pose2d>} in iteration.
147+ */
148+ static class IterativeTargetPoseSupplier implements Supplier <Pose2d > {
149+
150+ /**
151+ * The {@code Supplier<Pose2d>}s that provide the {@code Pose2d}s to which the
152+ * robot should move.
153+ */
154+ List <Supplier <Pose2d >> m_targetPoseSuppliers ;
155+
156+ /**
157+ * The index indicating the current {@code Pose2d} to which the robot
158+ * should move.
159+ */
160+ protected int m_targetPoseIndex = 0 ;
161+
162+ /**
163+ * Constructs an {@code IterativeTargetPoseSupplier}.
164+ *
165+ * @param targetPoseSuppliers {@code Supplier<Pose2d>}s that provide the
166+ * {@code Pose2d}s to which the robot should move
167+ */
168+ public IterativeTargetPoseSupplier (List <Supplier <Pose2d >> targetPoseSuppliers ) {
169+ m_targetPoseSuppliers = targetPoseSuppliers ;
170+ }
171+
172+ /**
173+ * Returns the curent target {@code Pose2d}.
174+ *
175+ * @return the curent target {@code Pose2d}
176+ */
177+ @ Override
178+ public Pose2d get () {
179+ return m_targetPoseSuppliers .get (m_targetPoseIndex ++).get ();
180+ }
181+
182+ /**
183+ * Determines whether or not there are more {@code Supplier<Pose2d>}s to iterate
184+ * over.
185+ *
186+ * @return {@code true} if there are more {@code Supplier<Pose2d>}s to iterate
187+ * over; {@code false} otherwise
188+ */
189+ public boolean hasNext () {
190+ return m_targetPoseIndex < m_targetPoseSuppliers .size ();
191+ }
192+ }
138193}
0 commit comments