Skip to content

Commit

Permalink
[choreolib] Add utility methods to convert choreo trajectories to wpi…
Browse files Browse the repository at this point in the history
…lib trajectories
  • Loading branch information
spacey-sooty committed Jul 29, 2024
1 parent e53d988 commit 62bc704
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
22 changes: 22 additions & 0 deletions choreolib/src/main/java/com/choreo/lib/ChoreoTrajectory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package com.choreo.lib;

import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.trajectory.Trajectory;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -174,4 +175,25 @@ public ChoreoTrajectory flipped() {
}
return new ChoreoTrajectory(flippedStates);
}

/**
* Converts the trajectory to a WPILib {@link Trajectory}.
*
* @return The converted Trajectory.
*/
public Trajectory toTrajectory() {
var trajectory = new Trajectory();
var lastVel = 0.0;
var lastTime = 0.0;
for (var state : samples) {
var vel = Math.hypot(state.velocityX, state.velocityY);
trajectory
.getStates()
.add(state.toTrajectoryState((vel - lastVel) / (state.timestamp - lastTime)));
lastVel = vel;
lastTime = state.timestamp;
}

return trajectory;
}
}
12 changes: 12 additions & 0 deletions choreolib/src/main/java/com/choreo/lib/ChoreoTrajectoryState.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.interpolation.Interpolatable;
import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.math.trajectory.Trajectory;
import java.util.Arrays;

/** A single robot state in a ChoreoTrajectory. */
Expand Down Expand Up @@ -147,4 +148,15 @@ public ChoreoTrajectoryState flipped() {
Arrays.stream(this.moduleForcesX).map(x -> -x).toArray(),
this.moduleForcesY);
}

/**
* Converts the state to a WPILib {@link Trajectory.State}.
*
* @return The WPILib {@link Trajectory.State}.
*/
public Trajectory.State toTrajectoryState(double accelerationMetersPerSecondSq) {
var vel = Math.hypot(velocityX, velocityY);
return new Trajectory.State(
timestamp, vel, accelerationMetersPerSecondSq, getPose(), angularVelocity / vel);
}
}

0 comments on commit 62bc704

Please sign in to comment.