44import static frc .robot .Constants .*;
55
66import java .util .Arrays ;
7- import java .util .LinkedList ;
87import java .util .List ;
98
9+ import edu .wpi .first .math .geometry .Pose2d ;
1010import edu .wpi .first .math .geometry .Transform2d ;
1111import edu .wpi .first .wpilibj2 .command .Command ;
1212import frc .robot .commands .AlignCommand ;
@@ -44,7 +44,7 @@ public static Command testSubsystemsAndCommands() {
4444 * @param distanceTolerance the distance error in meters which is tolerable
4545 * @param angleTolerance the angle error in degrees which is tolerable
4646 *
47- * @return a {@code Command} for moving 6 feet forward and then backward.
47+ * @return a {@code Command} for moving forward and then backward.
4848 */
4949 public static Command moveForwardBackward (double distanceInFeet , double distanceTolerance ,
5050 double angleTolerance ) {
@@ -89,14 +89,12 @@ public static Command moveForwardBackward3Controllers(double distanceInFeet, dou
8989 */
9090 public static Command visitTags (double distanceTolerance , double angleTolerance , Transform2d robotToTag ,
9191 int ... tagIDs ) {
92- List <DriveCommand > commands = Arrays .stream (tagIDs ).mapToObj (i -> kFieldLayout .getTagPose (i ))
93- .filter (p -> p .isPresent ())
94- .map (p -> p .get ())
95- .map (p -> p .toPose2d ().plus (robotToTag )).map (
96- p -> AlignCommand
97- .moveTo (
98- m_driveSubsystem , m_poseEstimationSubsystem , p , distanceTolerance ,
99- angleTolerance ))
92+ Pose2d [] poses = PoseEstimationSubsystem .tagPoses (tagIDs );
93+ var commands = Arrays .stream (poses ).map (
94+ p -> AlignCommand
95+ .moveTo (
96+ m_driveSubsystem , m_poseEstimationSubsystem , p .plus (robotToTag ), distanceTolerance ,
97+ angleTolerance ))
10098 .toList ();
10199 return sequence (commands .toArray (new Command [0 ]));
102100 }
@@ -106,32 +104,21 @@ public static Command visitTags(double distanceTolerance, double angleTolerance,
106104 *
107105 * @param distanceTolerance the distance error in meters which is tolerable
108106 * @param angleTolerance the angle error in degrees which is tolerable
107+ * @param intermediateToleranceRatio the ratio of distance and angle tolerances
108+ * at intermediate {@code AprilTags} for faster movements
109109 * @param robotToTag the {@code Tranform2d} representing the pose of the
110110 * {@code AprilTag} relative to the robot when the robot is aligned
111111 * @param tagIDs the IDs of the {@code AprilTag}s
112112 * @return a {@code Command} for visiting all the specified {@code AprilTag}s
113113 */
114- public static Command visitTagsOptimized (double distanceTolerance , double angleTolerance , Transform2d robotToTarget ,
114+ public static Command visitTagsOptimized (double distanceTolerance , double angleTolerance ,
115+ double intermediateToleranceRatio , Transform2d robotToTag ,
115116 int ... tagIDs ) {
116- var l = Arrays .stream (tagIDs ).mapToObj (i -> kFieldLayout .getTagPose (i ))
117- .filter (p -> p .isPresent ())
118- .map (p -> p .get ())
119- .map (p -> p .toPose2d ().plus (robotToTarget )).toList ();
120- List <Command > commands = new LinkedList <Command >();
121- DriveCommand previous = null ;
122- for (var p : l ) {
123- boolean last = p == l .get (l .size () - 1 );
124- DriveCommand c = previous == null ? AlignCommand .moveTo (
125- m_driveSubsystem , m_poseEstimationSubsystem , p , last ? distanceTolerance : 3 * distanceTolerance ,
126- last ? angleTolerance : 3 * angleTolerance )
127- : AlignCommand .moveTo (
128- m_driveSubsystem , m_poseEstimationSubsystem , p ,
129- last ? distanceTolerance : 3 * distanceTolerance ,
130- last ? angleTolerance : 3 * angleTolerance , previous );
131- commands .add (c );
132- previous = c ;
133- }
134- return sequence (commands .toArray (new Command [0 ]));
117+ var poses = PoseEstimationSubsystem .tagPoses (tagIDs );
118+ poses = Arrays .stream (poses ).map (p -> p .plus (robotToTag )).toList ().toArray (new Pose2d [0 ]);
119+ return AlignCommand .follow (
120+ m_driveSubsystem , m_poseEstimationSubsystem , distanceTolerance , angleTolerance ,
121+ intermediateToleranceRatio , poses );
135122 }
136123
137124 /**
@@ -148,16 +135,17 @@ public static Command visitTagsOptimized(double distanceTolerance, double angleT
148135 * (2nd step)
149136 * @param distanceTolerance the distance error in meters which is tolerable
150137 * @param angleTolerance the angle error in degrees which is tolerable
138+ * @param intermediateToleranceRatio the ratio of distance and angle tolerances
139+ * at intermediate {@code AprilTags} for faster movements
151140 * @return a {@code Command} for moving to the specified {@code AprilTag} in two
152141 * steps.
153142 */
154143 public static Command moveToTag (int tagID , Transform2d robotToTagReady , Transform2d robotToTag ,
155- double distanceTolerance ,
156- double angleTolerance ) {
144+ double distanceTolerance , double angleTolerance , double intermediateToleranceRatio ) {
157145 DriveCommand c1 = AlignCommand .moveTo (
158146 m_driveSubsystem , m_poseEstimationSubsystem ,
159147 kFieldLayout .getTagPose (tagID ).get ().toPose2d ().plus (robotToTagReady ),
160- distanceTolerance , angleTolerance );
148+ distanceTolerance * intermediateToleranceRatio , angleTolerance * intermediateToleranceRatio );
161149 DriveCommand c2 = AlignCommand .moveTo (
162150 m_driveSubsystem , m_poseEstimationSubsystem ,
163151 kFieldLayout .getTagPose (tagID ).get ().toPose2d ().plus (robotToTag ),
@@ -166,14 +154,17 @@ public static Command moveToTag(int tagID, Transform2d robotToTagReady, Transfor
166154 c1 , c2 , AlignCommand .moveTo (
167155 m_driveSubsystem , m_poseEstimationSubsystem ,
168156 kFieldLayout .getTagPose (tagID ).get ().toPose2d ().plus (robotToTagReady ),
169- distanceTolerance , angleTolerance , c2 ));
157+ distanceTolerance * intermediateToleranceRatio , angleTolerance * intermediateToleranceRatio ,
158+ c2 ));
170159 }
171160
172161 /**
173162 * Returns a {@code Command} for visiting all the specified {@code AprilTag}s.
174163 *
175164 * @param distanceTolerance the distance error in meters which is tolerable
176165 * @param angleTolerance the angle error in degrees which is tolerable
166+ * @param intermediateToleranceRatio the ratio of distance and angle tolerances
167+ * at intermediate {@code AprilTags} for faster movements
177168 * @param robotToTagReady the {@code Tranform2d} representing the pose of the
178169 * {@code AprilTag} relative to the robot when the robot is tentatively
179170 * aligned (1st step)
@@ -183,11 +174,15 @@ public static Command moveToTag(int tagID, Transform2d robotToTagReady, Transfor
183174 * @param tagIDs the IDs of the {@code AprilTag}s
184175 * @return a {@code Command} for visiting all the specified {@code AprilTag}s
185176 */
186- public static Command visitTags (double distanceTolerance , double angleTolerance , Transform2d robotToTagReady ,
177+ public static Command visitTags (double distanceTolerance , double angleTolerance , double intermediateToleranceRatio ,
178+ Transform2d robotToTagReady ,
187179 Transform2d robotToTag ,
188180 int ... tagIDs ) {
189181 List <Command > commands = Arrays .stream (tagIDs )
190- .mapToObj (i -> moveToTag (i , robotToTagReady , robotToTag , distanceTolerance , angleTolerance ))
182+ .mapToObj (
183+ i -> moveToTag (
184+ i , robotToTagReady , robotToTag , distanceTolerance , angleTolerance ,
185+ intermediateToleranceRatio ))
191186 .toList ();
192187 return sequence (commands .toArray (new Command [0 ]));
193188 }
0 commit comments