PathPlannerLib
Loading...
Searching...
No Matches
PathfindThenFollowPath.h
1#pragma once
2
3#include <frc2/command/SequentialCommandGroup.h>
4#include <frc2/command/Commands.h>
5#include <frc2/command/DeferredCommand.h>
6#include "pathplanner/lib/commands/FollowPathCommand.h"
7#include "pathplanner/lib/commands/PathfindingCommand.h"
8#include "pathplanner/lib/util/FlippingUtil.h"
9
10namespace pathplanner {
11class PathfindThenFollowPath: public frc2::SequentialCommandGroup {
12public:
31 PathfindThenFollowPath(std::shared_ptr<PathPlannerPath> goalPath,
32 PathConstraints pathfindingConstraints,
33 std::function<frc::Pose2d()> poseSupplier,
34 std::function<frc::ChassisSpeeds()> currentRobotRelativeSpeeds,
35 std::function<
36 void(const frc::ChassisSpeeds&, const DriveFeedforwards&)> output,
37 std::shared_ptr<PathFollowingController> controller,
38 RobotConfig robotConfig, std::function<bool()> shouldFlipPath,
39 frc2::Requirements requirements) {
40 AddCommands(
41 PathfindingCommand(goalPath, pathfindingConstraints,
42 poseSupplier, currentRobotRelativeSpeeds, output,
43 controller, robotConfig, shouldFlipPath, requirements),
44 frc2::DeferredCommand(
45 [goalPath, pathfindingConstraints, poseSupplier,
46 currentRobotRelativeSpeeds, output, controller,
47 robotConfig, shouldFlipPath, requirements]() {
48 if (goalPath->numPoints() < 2) {
49 return frc2::cmd::None();
50 }
51
52 frc::Pose2d startPose = poseSupplier();
53 frc::ChassisSpeeds startSpeeds =
54 currentRobotRelativeSpeeds();
55 frc::ChassisSpeeds startFieldSpeeds =
56 frc::ChassisSpeeds::FromRobotRelativeSpeeds(
57 startSpeeds, startPose.Rotation());
58 frc::Rotation2d startHeading = frc::Rotation2d(
59 startFieldSpeeds.vx(),
60 startFieldSpeeds.vy());
61
62 frc::Pose2d endWaypoint = frc::Pose2d(
63 goalPath->getPoint(0).position,
64 goalPath->getInitialHeading());
65 bool shouldFlip = shouldFlipPath()
66 && !goalPath->preventFlipping;
67 if (shouldFlip) {
68 endWaypoint = FlippingUtil::flipFieldPose(
69 endWaypoint);
70 }
71
72 GoalEndState endState(
73 pathfindingConstraints.getMaxVelocity(),
74 startPose.Rotation());
75 if (goalPath->getIdealStartingState().has_value()) {
76 frc::Rotation2d endRot =
77 goalPath->getIdealStartingState().value().getRotation();
78 if (shouldFlip) {
80 endRot);
81 }
82 endState =
84 goalPath->getIdealStartingState().value().getVelocity(),
85 endRot);
86 }
87
88 std::shared_ptr < PathPlannerPath > joinPath =
89 std::make_shared < PathPlannerPath
91 {
92 frc::Pose2d(
93 startPose.Translation(),
94 startHeading),
95 endWaypoint }), pathfindingConstraints, IdealStartingState(
96 units::math::hypot(
97 startSpeeds.vx,
98 startSpeeds.vy),
99 startPose.Rotation()), endState);
100 joinPath->preventFlipping = true;
101
102 return FollowPathCommand(joinPath, poseSupplier,
103 currentRobotRelativeSpeeds, output,
104 controller, robotConfig, shouldFlipPath,
105 requirements).ToPtr();
106 }, requirements),
107 FollowPathCommand(goalPath, poseSupplier,
108 currentRobotRelativeSpeeds, output, controller,
109 robotConfig, shouldFlipPath, requirements));
110 }
111};
112}
static frc::Rotation2d flipFieldRotation(const frc::Rotation2d &rotation)
Definition: FlippingUtil.h:44
static frc::Pose2d flipFieldPose(const frc::Pose2d &pose)
Definition: FlippingUtil.h:61
Definition: FollowPathCommand.h:26
Definition: GoalEndState.h:9
Definition: IdealStartingState.h:9
Definition: PathConstraints.h:12
constexpr units::meters_per_second_t getMaxVelocity() const
Definition: PathConstraints.h:63
Definition: PathPlannerPath.h:26
static std::vector< Waypoint > waypointsFromPoses(std::vector< frc::Pose2d > poses)
Definition: PathPlannerPath.cpp:76
Definition: PathfindThenFollowPath.h:11
PathfindThenFollowPath(std::shared_ptr< PathPlannerPath > goalPath, PathConstraints pathfindingConstraints, std::function< frc::Pose2d()> poseSupplier, std::function< frc::ChassisSpeeds()> currentRobotRelativeSpeeds, std::function< void(const frc::ChassisSpeeds &, const DriveFeedforwards &)> output, std::shared_ptr< PathFollowingController > controller, RobotConfig robotConfig, std::function< bool()> shouldFlipPath, frc2::Requirements requirements)
Definition: PathfindThenFollowPath.h:31
Definition: PathfindingCommand.h:24
Definition: RobotConfig.h:17
Definition: DriveFeedforwards.h:12