Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

Commit 033869f

Browse files
committed
Get rid of constraint abstraction
1 parent a0b9229 commit 033869f

File tree

9 files changed

+133
-143
lines changed

9 files changed

+133
-143
lines changed

src/main/java/frc/robot/autos/trailblazer/AutoPoint.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,52 @@
33
import edu.wpi.first.math.geometry.Pose2d;
44
import edu.wpi.first.wpilibj2.command.Command;
55
import edu.wpi.first.wpilibj2.command.Commands;
6-
import frc.robot.autos.trailblazer.constraints.AutoPointConstraint;
7-
import java.util.List;
6+
import frc.robot.autos.trailblazer.constraints.AutoConstraintOptions;
7+
import java.util.Optional;
88
import java.util.function.Supplier;
99

1010
public class AutoPoint {
1111
public final Supplier<Pose2d> poseSupplier;
12-
public final List<AutoPointConstraint> constraints;
12+
public final Optional<AutoConstraintOptions> constraints;
1313
public final Command command;
1414

1515
public AutoPoint(
16-
Supplier<Pose2d> poseSupplier, Command command, List<AutoPointConstraint> constraints) {
16+
Supplier<Pose2d> poseSupplier, Command command, Optional<AutoConstraintOptions> constraints) {
1717
this.poseSupplier = poseSupplier;
1818
this.command = command;
1919
this.constraints = constraints;
2020
}
2121

22+
public AutoPoint(
23+
Supplier<Pose2d> poseSupplier, Command command, AutoConstraintOptions constraints) {
24+
this(poseSupplier, command, Optional.of(constraints));
25+
}
26+
2227
public AutoPoint(Supplier<Pose2d> poseSupplier, Command command) {
23-
this(poseSupplier, command, List.of());
28+
this(poseSupplier, command, Optional.empty());
2429
}
2530

26-
public AutoPoint(Supplier<Pose2d> poseSupplier, List<AutoPointConstraint> constraints) {
31+
public AutoPoint(Supplier<Pose2d> poseSupplier, AutoConstraintOptions constraints) {
2732
this(poseSupplier, Commands.none(), constraints);
2833
}
2934

3035
public AutoPoint(Supplier<Pose2d> poseSupplier) {
31-
this(poseSupplier, Commands.none(), List.of());
36+
this(poseSupplier, Commands.none(), Optional.empty());
3237
}
3338

34-
public AutoPoint(Pose2d pose, Command command, List<AutoPointConstraint> constraints) {
39+
public AutoPoint(Pose2d pose, Command command, AutoConstraintOptions constraints) {
3540
this(() -> pose, command, constraints);
3641
}
3742

3843
public AutoPoint(Pose2d pose, Command command) {
39-
this(pose, command, List.of());
44+
this(() -> pose, command);
4045
}
4146

42-
public AutoPoint(Pose2d pose, List<AutoPointConstraint> constraints) {
47+
public AutoPoint(Pose2d pose, AutoConstraintOptions constraints) {
4348
this(pose, Commands.none(), constraints);
4449
}
4550

4651
public AutoPoint(Pose2d pose) {
47-
this(pose, Commands.none(), List.of());
52+
this(pose, Commands.none());
4853
}
4954
}
Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package frc.robot.autos.trailblazer;
22

3-
import frc.robot.autos.trailblazer.constraints.AutoPointConstraint;
3+
import frc.robot.autos.trailblazer.constraints.AutoConstraintOptions;
44
import java.util.List;
55

66
/**
@@ -13,22 +13,18 @@ public class AutoSegment {
1313
* Constraints to apply to any points that don't have their own constraints specified. If a point
1414
* specifies its own constraints, this field will be ignored.
1515
*/
16-
public final List<AutoPointConstraint> defaultConstraints;
16+
public final AutoConstraintOptions defaultConstraints;
1717

18-
public AutoSegment(List<AutoPointConstraint> defaultConstraints, List<AutoPoint> points) {
18+
public AutoSegment(AutoConstraintOptions defaultConstraints, List<AutoPoint> points) {
1919
this.defaultConstraints = defaultConstraints;
2020
this.points = points;
2121
}
2222

23-
public AutoSegment(List<AutoPointConstraint> defaultConstraints, AutoPoint... points) {
23+
public AutoSegment(AutoConstraintOptions defaultConstraints, AutoPoint... points) {
2424
this(defaultConstraints, List.of(points));
2525
}
2626

27-
public AutoSegment(List<AutoPoint> points) {
28-
this(List.of(), points);
29-
}
30-
3127
public AutoSegment(AutoPoint... points) {
32-
this(List.of(), points);
28+
this(new AutoConstraintOptions(), points);
3329
}
3430
}

src/main/java/frc/robot/autos/trailblazer/Trailblazer.java

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,23 @@
55
import edu.wpi.first.math.kinematics.ChassisSpeeds;
66
import edu.wpi.first.wpilibj2.command.Command;
77
import edu.wpi.first.wpilibj2.command.Commands;
8-
import frc.robot.autos.trailblazer.constraints.AutoPointConstraint;
8+
import frc.robot.autos.trailblazer.constraints.AutoConstraintCalculator;
9+
import frc.robot.autos.trailblazer.constraints.AutoConstraintOptions;
910
import frc.robot.autos.trailblazer.followers.PathFollower;
1011
import frc.robot.autos.trailblazer.followers.PidPathFollower;
1112
import frc.robot.autos.trailblazer.trackers.HeuristicPathTracker;
1213
import frc.robot.autos.trailblazer.trackers.PathTracker;
1314
import frc.robot.localization.LocalizationSubsystem;
1415
import frc.robot.swerve.SwerveSubsystem;
15-
import java.util.List;
1616

1717
public class Trailblazer {
1818
/**
19-
* Global default constraints, which will be applied to points that have no constraints specified,
20-
* and no default constraints specified for the segment.
19+
* Given a point and the constraints for its parent segment, resolve the constraint options to use
20+
* while following that point.
2121
*/
22-
private static final List<AutoPointConstraint> globalDefaultConstraints = List.of();
23-
24-
private static List<AutoPointConstraint> resolveConstraints(
25-
List<AutoPointConstraint> segmentConstraints, AutoPoint point) {
26-
if (!point.constraints.isEmpty()) {
27-
return point.constraints;
28-
}
29-
30-
if (!segmentConstraints.isEmpty()) {
31-
return segmentConstraints;
32-
}
33-
34-
return globalDefaultConstraints;
22+
private static AutoConstraintOptions resolveConstraints(
23+
AutoPoint point, AutoConstraintOptions segmentConstraints) {
24+
return point.constraints.orElse(segmentConstraints);
3525
}
3626

3727
private final SwerveSubsystem swerve;
@@ -53,6 +43,7 @@ public Command followSegment(AutoSegment segment) {
5343
Commands.run(
5444
() -> {
5545
var currentAutoPoint = pathTracker.getCurrentPoint();
46+
5647
var constrainedVelocityGoal =
5748
getSwerveSetpoint(currentAutoPoint, segment.defaultConstraints);
5849
swerve.setFieldRelativeAutoSpeeds(constrainedVelocityGoal);
@@ -67,16 +58,16 @@ public Command followSegment(AutoSegment segment) {
6758
}
6859

6960
private ChassisSpeeds getSwerveSetpoint(
70-
AutoPoint point, List<AutoPointConstraint> segmentConstraints) {
71-
var usedConstraints = resolveConstraints(segmentConstraints, point);
61+
AutoPoint point, AutoConstraintOptions segmentConstraints) {
62+
var usedConstraints = resolveConstraints(point, segmentConstraints);
7263

7364
var currentPose = localization.getPose();
7465
var fieldRelativeRobotSpeeds = swerve.getFieldRelativeSpeeds();
7566
var originalTargetPose = pathTracker.getTargetPose(currentPose, fieldRelativeRobotSpeeds);
7667
var constrainedTargetPose =
77-
AutoPointConstraint.applyPoseConstraints(originalTargetPose, usedConstraints);
68+
AutoConstraintCalculator.constrainTargetPose(originalTargetPose, usedConstraints);
7869

7970
var originalVelocityGoal = pathFollower.calculateSpeeds(currentPose, constrainedTargetPose);
80-
return AutoPointConstraint.applyMotionConstraints(originalVelocityGoal, usedConstraints);
71+
return AutoConstraintCalculator.constrainVelocityGoal(originalVelocityGoal, usedConstraints);
8172
}
8273
}

src/main/java/frc/robot/autos/trailblazer/constraints/AutoAccelerationConstraint.java

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package frc.robot.autos.trailblazer.constraints;
2+
3+
import edu.wpi.first.math.geometry.Pose2d;
4+
import edu.wpi.first.math.kinematics.ChassisSpeeds;
5+
6+
public class AutoConstraintCalculator {
7+
public static Pose2d constrainTargetPose(Pose2d inputPose, AutoConstraintOptions options) {
8+
if (options.collisionAvoidance()) {
9+
// TODO: Implement collision avoidance
10+
}
11+
12+
return inputPose;
13+
}
14+
15+
public static ChassisSpeeds constrainVelocityGoal(
16+
ChassisSpeeds inputSpeeds, AutoConstraintOptions options) {
17+
if (options.maxLinearVelocity() != 0) {
18+
// TODO: Implement linear velocity constraint
19+
}
20+
21+
if (options.maxAngularVelocity() != 0) {
22+
// TODO: Implement angular velocity constraint
23+
}
24+
25+
if (options.maxLinearAcceleration() != 0) {
26+
// TODO: Implement linear acceleration constraint
27+
}
28+
29+
if (options.maxAngularAcceleration() != 0) {
30+
// TODO: Implement angular acceleration constraint
31+
}
32+
33+
return inputSpeeds;
34+
}
35+
36+
private AutoConstraintCalculator() {}
37+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package frc.robot.autos.trailblazer.constraints;
2+
3+
public record AutoConstraintOptions(
4+
/** Whether collision avoidance should be enabled. */
5+
boolean collisionAvoidance,
6+
/** Max linear velocity allowed in meters per second. Set to 0 to disable. */
7+
double maxLinearVelocity,
8+
/** Max angular velocity allowed in radians per second. Set to 0 to disable. */
9+
double maxAngularVelocity,
10+
/** Max linear acceleration allowed in meters per second squared. Set to 0 to disable. */
11+
double maxLinearAcceleration,
12+
/** Max angular acceleration allowed in radians per second squared. Set to 0 to disable. */
13+
double maxAngularAcceleration) {
14+
/** Default constraint options to use if no point or segment specific options are set. */
15+
public AutoConstraintOptions() {
16+
this(false, 0, 0, 0, 0);
17+
}
18+
19+
public AutoConstraintOptions withCollisionAvoidance(boolean collisionAvoidance) {
20+
return new AutoConstraintOptions(
21+
collisionAvoidance,
22+
maxLinearVelocity(),
23+
maxAngularVelocity(),
24+
maxLinearAcceleration(),
25+
maxAngularAcceleration());
26+
}
27+
28+
public AutoConstraintOptions withMaxLinearVelocity(double maxLinearVelocity) {
29+
return new AutoConstraintOptions(
30+
collisionAvoidance(),
31+
maxLinearVelocity,
32+
maxAngularVelocity(),
33+
maxLinearAcceleration(),
34+
maxAngularAcceleration());
35+
}
36+
37+
public AutoConstraintOptions withMaxAngularVelocity(double maxAngularVelocity) {
38+
return new AutoConstraintOptions(
39+
collisionAvoidance(),
40+
maxLinearVelocity(),
41+
maxAngularVelocity,
42+
maxLinearAcceleration(),
43+
maxAngularAcceleration());
44+
}
45+
46+
public AutoConstraintOptions withMaxLinearAcceleration(double maxLinearAcceleration) {
47+
return new AutoConstraintOptions(
48+
collisionAvoidance(),
49+
maxLinearVelocity(),
50+
maxAngularVelocity(),
51+
maxLinearAcceleration,
52+
maxAngularAcceleration());
53+
}
54+
55+
public AutoConstraintOptions withMaxAngularAcceleration(double maxAngularAcceleration) {
56+
return new AutoConstraintOptions(
57+
collisionAvoidance(),
58+
maxLinearVelocity(),
59+
maxAngularVelocity(),
60+
maxLinearAcceleration(),
61+
maxAngularAcceleration);
62+
}
63+
}

src/main/java/frc/robot/autos/trailblazer/constraints/AutoPointConstraint.java

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/main/java/frc/robot/autos/trailblazer/constraints/AutoVelocityConstraint.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/main/java/frc/robot/autos/trailblazer/constraints/CollisionAvoidanceConstraint.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)