Skip to content

Commit eb7c83d

Browse files
committed
cleaner implementation of DriveCommand and PathDriveCommand
1 parent 536a6b5 commit eb7c83d

File tree

3 files changed

+77
-38
lines changed

3 files changed

+77
-38
lines changed

src/main/java/frc/robot/CommandComposer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public static Command moveOnEllipse(double radius, double initialAngularIncremen
161161
var l = new LinkedList<Pose2d>();
162162
for (double i = 0; i < poseCount; i++) {
163163
var t = translation(radius, 0).rotateBy(angle);
164-
l.add(new Pose2d(translation(t.getX(), t.getY() / 2), angle));
164+
l.add(new Pose2d(translation(t.getX() + radius, t.getY() / 2), angle));
165165
double progress = i / poseCount;
166166
double angularVelocity = progress * finalAngularIncrement + (1 - progress) * initialAngularIncrement;
167167
angle = angle.plus(rotation(angularVelocity));

src/main/java/frc/robot/commands/DriveCommand.java

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import static frc.robot.Constants.DriveConstants.*;
44

5-
import java.util.List;
65
import java.util.function.Supplier;
76

87
import edu.wpi.first.math.controller.ProfiledPIDController;
@@ -58,10 +57,10 @@ public class DriveCommand extends Command {
5857
protected Pose2d m_targetPose;
5958

6059
/**
61-
* The {@code Supplier<Pose2d>}s that provide the {@code Pose2d}s to which the
60+
* The {@code Supplier<Pose2d>} that provides the {@code Pose2d} to which the
6261
* robot should move.
6362
*/
64-
protected List<Supplier<Pose2d>> m_targetPoseSuppliers;
63+
protected Supplier<Pose2d> m_targetPoseSupplier;
6564

6665
/**
6766
* Constructs a new {@code DriveCommand} whose purpose is to move
@@ -91,21 +90,6 @@ public DriveCommand(DriveSubsystem driveSubsystem, double distanceTolerance, dou
9190
*/
9291
public DriveCommand(DriveSubsystem driveSubsystem, double distanceTolerance, double angleToleranceInDegrees,
9392
Supplier<Pose2d> targetPoseSupplier) {
94-
this(driveSubsystem, distanceTolerance, angleToleranceInDegrees, List.of(targetPoseSupplier));
95-
}
96-
97-
/**
98-
* Constructs a new {@code DriveCommand} whose purpose is to move
99-
* the robot to a certain {@code Pose2d}.
100-
*
101-
* @param driveSubsystem the {@code DriveSubsystem} to use
102-
* @param distanceTolerance the distance error in meters which is tolerable
103-
* @param angleToleranceInDegrees the angle error in degrees which is tolerable
104-
* @param targetPoseSuppliers {@code Supplier<Pose2d>}s that provide the
105-
* {@code Pose2d}s to which the robot should move.
106-
*/
107-
public DriveCommand(DriveSubsystem driveSubsystem, double distanceTolerance, double angleToleranceInDegrees,
108-
List<Supplier<Pose2d>> targetPoseSuppliers) {
10993
m_driveSubsystem = driveSubsystem;
11094
m_distanceTolerance = distanceTolerance;
11195
m_angleTolerance = Math.toRadians(angleToleranceInDegrees);
@@ -115,7 +99,7 @@ public DriveCommand(DriveSubsystem driveSubsystem, double distanceTolerance, dou
11599
new TrapezoidProfile.Constraints(kTurnMaxAngularSpeed,
116100
kTurnMaxAcceleration));
117101
m_controllerYaw.enableContinuousInput(0, 2 * Math.PI);
118-
m_targetPoseSuppliers = targetPoseSuppliers;
102+
m_targetPoseSupplier = targetPoseSupplier;
119103
addRequirements(m_driveSubsystem);
120104
}
121105

@@ -129,7 +113,7 @@ public void initialize() {
129113
Pose2d pose = m_driveSubsystem.getPose();
130114
m_targetPose = pose;
131115
try {
132-
m_targetPose = m_targetPoseSuppliers.get(0).get();
116+
m_targetPose = m_targetPoseSupplier.get();
133117
} catch (Exception e) {
134118
e.printStackTrace();
135119
m_targetPose = pose;

src/main/java/frc/robot/commands/PathDriveCommand.java

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
*/
1818
public 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

Comments
 (0)