|
1 | 1 | package org.carlmontrobotics.lib199.path; |
2 | 2 |
|
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.HashMap; |
3 | 5 | import java.util.function.Supplier; |
4 | 6 |
|
| 7 | +import com.pathplanner.lib.PathPlannerTrajectory; |
| 8 | +import com.pathplanner.lib.commands.PPSwerveControllerCommand; |
| 9 | + |
5 | 10 | import edu.wpi.first.math.controller.PIDController; |
6 | 11 | import edu.wpi.first.math.controller.ProfiledPIDController; |
7 | 12 | import edu.wpi.first.math.geometry.Pose2d; |
|
15 | 20 | import edu.wpi.first.math.trajectory.TrapezoidProfile.Constraints; |
16 | 21 | import edu.wpi.first.math.trajectory.constraint.SwerveDriveKinematicsConstraint; |
17 | 22 | import edu.wpi.first.wpilibj2.command.Command; |
| 23 | +import edu.wpi.first.wpilibj2.command.Subsystem; |
18 | 24 | import edu.wpi.first.wpilibj2.command.SwerveControllerCommand; |
19 | 25 |
|
20 | 26 | public interface SwerveDriveInterface extends DrivetrainInterface { |
@@ -63,6 +69,7 @@ public interface SwerveDriveInterface extends DrivetrainInterface { |
63 | 69 |
|
64 | 70 | /** |
65 | 71 | * Configures the constants for generating a trajectory |
| 72 | + * WARNING: THIS METHOD IS NOT CALLED IF USING PPRobotPath! |
66 | 73 | * |
67 | 74 | * @param path The configuration for generating a trajectory |
68 | 75 | */ |
@@ -100,6 +107,27 @@ public default Command createAutoCommand(Trajectory trajectory, Supplier<Rotatio |
100 | 107 | desiredHeading, this::drive, this); |
101 | 108 | } |
102 | 109 |
|
| 110 | + /** |
| 111 | + * Constructs a new PPSwerveControllerCommand that, when executed, will follow the |
| 112 | + * provided trajectory. |
| 113 | + * |
| 114 | + * @param trajectory The trajectory to follow. |
| 115 | + * @param eventMap Map of event marker names to the commands that should run when reaching that marker. |
| 116 | + * This SHOULD NOT contain any commands requiring Drivetrain, or it will be interrupted |
| 117 | + * @return PPSwerveControllerCommand |
| 118 | + */ |
| 119 | + public default Command createPPAutoCommand(PathPlannerTrajectory trajectory, HashMap<String, Command> eventMap) { |
| 120 | + Subsystem[] requirements = eventMap.values().stream().flatMap(command -> command.getRequirements().stream()) |
| 121 | + .toArray(Subsystem[]::new); |
| 122 | + requirements = Arrays.copyOf(requirements, requirements.length + 1); |
| 123 | + requirements[requirements.length - 1] = this; |
| 124 | + requirements = Arrays.stream(requirements).distinct().toArray(Subsystem[]::new); |
| 125 | + PIDController[] pidControllers = Arrays.stream(getPIDConstants()).map(constants -> new PIDController(constants[0], constants[1], constants[2])).toArray(PIDController[]::new); |
| 126 | + pidControllers[2].enableContinuousInput(-Math.PI, Math.PI); |
| 127 | + // Call getOdometry in the supplier because the odometry object may be reset when the command is run |
| 128 | + return new PPSwerveControllerCommand(trajectory, () -> getOdometry().getPoseMeters(), getKinematics(), pidControllers[0], pidControllers[1], pidControllers[2], this::drive, requirements); |
| 129 | + } |
| 130 | + |
103 | 131 | /** |
104 | 132 | * Sets odometry to the specified pose |
105 | 133 | * |
|
0 commit comments