Skip to content

Commit

Permalink
Add method to get a list of poses from a path
Browse files Browse the repository at this point in the history
  • Loading branch information
mjansen4857 committed Feb 6, 2024
1 parent b704c9a commit a22e90f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 15 deletions.
3 changes: 1 addition & 2 deletions pathplannerlib-python/pathplannerlib/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ def logActivePath(path: Union[PathPlannerPath, None]) -> None:
"""
if PathPlannerLogging._logActivePath is not None:
if path is not None:
poses = [Pose2d(p.position, Rotation2d()) for p in path.getAllPathPoints()]
PathPlannerLogging._logActivePath(poses)
PathPlannerLogging._logActivePath(path.getPathPoses())
else:
PathPlannerLogging._logActivePath([])
9 changes: 9 additions & 0 deletions pathplannerlib-python/pathplannerlib/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,15 @@ def flipPath(self) -> PathPlannerPath:
return PathPlannerPath(newBezier, self._globalConstraints, newEndState, newRotTargets, self._constraintZones,
newMarkers, self._reversed, newPreviewRot)

def getPathPoses(self) -> List[Pose2d]:
"""
Get a list of poses representing every point in this path.
This can be used to display a path on a field 2d widget, for example.
:return: List of poses for each point in this path
"""
return [Pose2d(p.position, Rotation2d()) for p in self._allPoints]

@staticmethod
def _mapPct(pct: float, seg1_pct: float) -> float:
if pct <= seg1_pct:
Expand Down
3 changes: 2 additions & 1 deletion pathplannerlib/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"forward_list": "cpp",
"ranges": "cpp",
"valarray": "cpp",
"*.mac": "cpp"
"*.mac": "cpp",
"charconv": "cpp"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,18 @@ public PathPlannerPath flipPath() {
newPreviewRot);
}

/**
* Get a list of poses representing every point in this path. This can be used to display a path
* on a field 2d widget, for example.
*
* @return List of poses for each point in this path
*/
public List<Pose2d> getPathPoses() {
return allPoints.stream()
.map(p -> new Pose2d(p.position, new Rotation2d()))
.collect(Collectors.toList());
}

/**
* Map a given percentage/waypoint relative position over 2 segments
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import com.pathplanner.lib.path.PathPlannerPath;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Rotation2d;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/** Utility class for handling custom logging callbacks */
public class PathPlannerLogging {
Expand Down Expand Up @@ -73,13 +71,8 @@ public static void logTargetPose(Pose2d targetPose) {
*/
public static void logActivePath(PathPlannerPath path) {
if (logActivePath != null) {

if (path != null) {
List<Pose2d> poses =
path.getAllPathPoints().stream()
.map(p -> new Pose2d(p.position, new Rotation2d()))
.collect(Collectors.toList());
logActivePath.accept(poses);
logActivePath.accept(path.getPathPoses());
} else {
logActivePath.accept(new ArrayList<>());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,20 @@ class PathPlannerPath: public std::enable_shared_from_this<PathPlannerPath> {
*/
std::shared_ptr<PathPlannerPath> flipPath();

/**
* Get a list of poses representing every point in this path. This can be used to display a path
* on a field 2d widget, for example.
*
* @return List of poses for each point in this path
*/
inline std::vector<frc::Pose2d> getPathPoses() {
std::vector < frc::Pose2d > poses;
for (const PathPoint &point : m_allPoints) {
poses.emplace_back(point.position, frc::Rotation2d());
}
return poses;
}

private:
static std::shared_ptr<PathPlannerPath> fromJson(const wpi::json &json);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ class PathPlannerLogging {
std::vector < frc::Pose2d > poses;

if (path) {
for (const PathPoint &point : path->getAllPathPoints()) {
poses.push_back(
frc::Pose2d(point.position, frc::Rotation2d()));
}
poses = path->getPathPoses();
}

m_logActivePath(poses);
Expand Down

0 comments on commit a22e90f

Please sign in to comment.