I am trying to dynamically create multiple PathPlanner PPSwerveController commands for an alignment routine. However, I cannot figure out how to dynamically create and run a command in a sequential command group. I need to update the Path Points based on more accurate Limelight data after I move and then run a second command. Does anyone know how this can be accomplished in the command based framework or how I could restructure my code to effectively accomplish the same as dynamically generating commands. The code of interest is put in InstantCommands for now but is clearly wrong.
Here is my current code:
package frc.robot.commands.Alignment;
import com.pathplanner.lib.PathConstraints;
import com.pathplanner.lib.PathPlanner;
import com.pathplanner.lib.PathPlannerTrajectory;
import com.pathplanner.lib.PathPoint;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.wpilibj2.command.InstantCommand;
import edu.wpi.first.wpilibj2.command.PrintCommand;
import edu.wpi.first.wpilibj2.command.SequentialCommandGroup;
import frc.robot.RobotContainer;
import frc.robot.commands.ArmMotions.Place;
import frc.robot.subsystems.Arm;
import frc.robot.subsystems.Elevator;
import frc.robot.subsystems.Limelight;
import frc.robot.subsystems.Swerve;
public class LimelightPlace extends SequentialCommandGroup {
/** Creates a new Place. */
Swerve swerve;
Limelight limelight;
Arm arm;
Elevator elevator;
RobotContainer container;
PathPlannerTrajectory targetTraj;
public LimelightPlace(Swerve swerve, Limelight limelight, RobotContainer container, Arm arm, Elevator elevator) {
this.swerve = swerve;
this.limelight = limelight;
this.container = container;
this.arm = arm;
this.elevator = elevator;
// Add your commands in the addCommands() call, e.g.
// addCommands(new FooCommand(), new BarCommand());
addCommands(
new AlignToAngle(swerve, 0),
new InstantCommand(() -> targetTraj = PathPlanner.generatePath(
new PathConstraints(1, 1),
new PathPoint(
swerve.getPose().getTranslation(),
Rotation2d.fromDegrees(0),
swerve.getYaw()
), //Sets starting point of path to current position.
new PathPoint(
swerve.getPose().getTranslation().plus(new Translation2d(limelight.getTz() + 1, -limelight.getTx())),
Rotation2d.fromDegrees(0),
swerve.getYaw()
)
),
limelight
),
new InstantCommand(() -> swerve.followTrajectoryCommand(targetTraj, false), swerve),
new InstantCommand(() -> targetTraj = PathPlanner.generatePath(
new PathConstraints(1, 1),
new PathPoint(
swerve.getPose().getTranslation(),
Rotation2d.fromDegrees(0),
swerve.getYaw()
), //Sets starting point of path to current position.
new PathPoint(
swerve.getPose().getTranslation().plus(new Translation2d(limelight.getTz() + 1, -limelight.getTx())),
Rotation2d.fromDegrees(0),
swerve.getYaw()
)
),
limelight
),
new InstantCommand(() -> swerve.followTrajectoryCommand(targetTraj, false)),
new Place(container, arm, elevator).getCommand()
//Add command to drive forward to place and then release. Add code to align with tx offset when close. Add code for cones offset.
);
}
}