Skip to content

Commit

Permalink
Stubbed out auto path logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mpulte committed Feb 4, 2024
1 parent a8882e4 commit 05b57c4
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 11 deletions.
11 changes: 10 additions & 1 deletion src/main/java/com/team1701/lib/util/GeometryUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ public final class GeometryUtil {
public static final Rotation3d kRotation3dIdentity = new Rotation3d();

public static Rotation2d flipX(Rotation2d rotation) {
return rotation.getRadians() < 0 ? kRotationMinusPi.minus(rotation) : kRotationPi.minus(rotation);
return kRotationPi.minus(rotation);
}

public static Pose2d flipX(Pose2d pose, double fieldLength) {
return new Pose2d(fieldLength - pose.getX(), pose.getY(), flipX(pose.getRotation()));
}

public static Pose2d[] flipX(Pose2d[] poses, double fieldLength) {
// TODO: Flip poses
return poses;
}

public static Rotation2d angleModulus(Rotation2d rotation) {
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/com/team1701/lib/util/PathBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.team1701.lib.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.pathplanner.lib.path.PathPlannerPath;
import edu.wpi.first.math.geometry.Pose2d;

public class PathBuilder {
private final List<Pose2d> mPath = new ArrayList<>();

public Pose2d[] build() {
return mPath.toArray(new Pose2d[mPath.size()]);
}

public Pose2d[] buildAndClear() {
var path = build();
clear();
return path;
}

public void clear() {
mPath.clear();
}

public PathBuilder addPose(Pose2d pose) {
// TODO: Add to mPath
return this;
}

public PathBuilder addPath(Pose2d[] path) {
Collections.addAll(mPath, path);
return this;
}

public PathBuilder addPath(PathPlannerPath path) {
// TODO: Add to mPath
return this;
}
}
31 changes: 29 additions & 2 deletions src/main/java/com/team1701/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,37 @@ private void setupAutonomous() {
PathPlannerLogging.setLogActivePathCallback(
poses -> Logger.recordOutput("PathPlanner/Path", poses.toArray(Pose2d[]::new)));

// TODO: Create wrapper class for autonomous chooser
// TODO: Also update on change to team color
var commands = new AutonomousCommands(mRobotState, mDrive);
autonomousModeChooser.addDefaultOption("Demo", commands.demo());
autonomousModeChooser.addOption("Four Piece", commands.fourPiece());
var demoCommand = commands.demo();
var fourPieceCommand = commands.fourPiece();

autonomousModeChooser.addDefaultOption("Demo", demoCommand.command());
autonomousModeChooser.addOption("Four Piece", fourPieceCommand.command());
autonomousModeChooser.addOption("Four Piece Planner", new PathPlannerAuto("FourPiece"));

autonomousModeChooser.getSendableChooser().onChange(name -> {
Pose2d[] path;
switch (name) {
case "Demo":
path = demoCommand.path();
break;
case "Four Piece":
path = fourPieceCommand.path();
break;
default:
path = new Pose2d[] {};
break;
}

if (Configuration.isRedAlliance()) {
path = GeometryUtil.flipX(path, FieldConstants.kFieldLongLengthMeters);
}

// TODO: Also export as Translation2d[]
Logger.recordOutput("Autonomous/PathPose2d", path);
});
}

private void setupStateTriggers() {
Expand Down
27 changes: 19 additions & 8 deletions src/main/java/com/team1701/robot/commands/AutonomousCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.pathplanner.lib.path.PathPlannerPath;
import com.team1701.lib.swerve.SwerveSetpointGenerator.KinematicLimits;
import com.team1701.lib.util.GeometryUtil;
import com.team1701.lib.util.PathBuilder;
import com.team1701.robot.Configuration;
import com.team1701.robot.Constants;
import com.team1701.robot.FieldConstants;
Expand All @@ -23,6 +24,13 @@
public class AutonomousCommands {
private final RobotState mRobotState;
private final Drive mDrive;
private PathBuilder mPathBuilder = new PathBuilder();

public static record AutonomousCommand(Command command, Pose2d[] path) {
public AutonomousCommand(Command command) {
this(command, new Pose2d[] {});
}
}

public AutonomousCommands(RobotState robotState, Drive drive) {
mRobotState = robotState;
Expand All @@ -32,10 +40,7 @@ public AutonomousCommands(RobotState robotState, Drive drive) {
}

private Pose2d autoFlipPose(Pose2d pose) {
var flippedPose = new Pose2d(
FieldConstants.kFieldLongLengthMeters - pose.getX(),
pose.getY(),
GeometryUtil.kRotationPi.minus(pose.getRotation()));
var flippedPose = GeometryUtil.flipX(pose, FieldConstants.kFieldLongLengthMeters);
return Configuration.isRedAlliance() ? flippedPose : pose;
}

Expand Down Expand Up @@ -93,11 +98,13 @@ private Command followChoreoPath(String pathName, boolean resetPose) {
return idle(); // Prevent auto mode from continuing if trajectory failed to load
}

mPathBuilder.addPath(trajectory.getPoses());

return new DriveChoreoTrajectory(mDrive, trajectory, mRobotState, resetPose);
}

public Command demo() {
return loggedSequence(
public AutonomousCommand demo() {
var command = loggedSequence(
print("Starting demo"),
followPath("demo1", true),
driveToPose(new Pose2d(2.0, 1.0, Rotation2d.fromRadians(-Math.PI * 2.0 / 3.0))),
Expand All @@ -108,13 +115,17 @@ public Command demo() {
followPath("demo2"),
driveToPose(new Pose2d(10.0, 5.0, GeometryUtil.kRotationMinusHalfPi), false))
.withName("AutonomousDemo");

return new AutonomousCommand(command, new Pose2d[] {}); // TODO: Add path
}

public Command fourPiece() {
return loggedSequence(
public AutonomousCommand fourPiece() {
var command = loggedSequence(
print("Started four piece auto"),
followChoreoPath("FourPiece.1", true),
followChoreoPath("FourPiece.2"),
followChoreoPath("FourPiece.3"));

return new AutonomousCommand(command, mPathBuilder.buildAndClear());
}
}

0 comments on commit 05b57c4

Please sign in to comment.