Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to get a list of poses from a path #594

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading