ThunderLib
Loading...
Searching...
No Matches
ThunderAutoTrajectoryCommand.hpp
1#pragma once
2
3#include <ThunderLib/Auto/ThunderAutoTrajectory.hpp>
4#include <ThunderLib/Auto/ThunderAutoProject.hpp>
5#include <ThunderLib/Trajectory/TrajectoryRunnerProperties.hpp>
6#include <ThunderLib/Types/CanonicalAngle.hpp>
7#include <frc2/command/Command.h>
8#include <frc2/command/Commands.h>
9#include <frc2/command/CommandHelper.h>
10#include <frc/DriverStation.h>
11#include <frc/Timer.h>
12#include <memory>
13#include <optional>
14#include <list>
15
16namespace thunder {
17
21class ThunderAutoTrajectoryCommand : public frc2::CommandHelper<frc2::Command, ThunderAutoTrajectoryCommand> {
22 public:
31 ThunderAutoTrajectoryCommand(const std::string& trajectoryName,
32 std::shared_ptr<ThunderAutoProject> project,
33 const TrajectoryRunnerProperties& properties,
34 bool shouldResetPose = true);
35
36 bool isValid() const;
37
38 explicit operator bool() const { return isValid(); }
39
40 void Initialize() override;
41 void Execute() override;
42 void End(bool interrupted) override;
43 bool IsFinished() override;
44
45 private:
46 std::unique_ptr<ThunderAutoTrajectory> m_trajectory;
47 std::shared_ptr<ThunderAutoProject> m_project;
48 TrajectoryRunnerProperties m_runnerProperties;
49 bool m_shouldResetPose;
50
51 frc::Timer m_timer;
52
53 std::optional<frc::DriverStation::Alliance>
54 m_alliance; // Checked during initialization, not updated afterward
55
56 enum class ExecutionState {
57 START_ACTIONS,
58 FOLLOW_TRAJECTORY,
59 STOPPED,
60 END_ACTIONS,
61 FINISHED,
62 };
63
64 ExecutionState m_executionState = ExecutionState::START_ACTIONS;
65
66 frc2::CommandPtr m_startActionCommand = frc2::cmd::None();
67 frc2::CommandPtr m_endActionCommand = frc2::cmd::None();
68
69 struct StopActionCommand {
70 units::second_t stopTime;
71 frc2::CommandPtr command;
72 };
73
74 std::list<StopActionCommand> m_stopActionCommands;
75 using StopIterator = decltype(m_stopActionCommands)::const_iterator;
76 StopIterator m_nextStop = m_stopActionCommands.end();
77
78 struct PositionedActionCommand {
79 units::second_t actionTime;
80 frc2::CommandPtr command;
81 };
82
83 std::list<PositionedActionCommand> m_positionedActionCommands;
84 using PositionedActionIterator = decltype(m_positionedActionCommands)::const_iterator;
85 PositionedActionIterator m_nextAction = m_positionedActionCommands.end();
86
87 std::list<PositionedActionIterator> m_runningActions;
88
89 private:
90 void beginStartAction();
91 void executeStartAction();
92 void endStartAction(bool interrupted);
93
94 void beginFollowTrajectory();
95 void resumeFollowTrajectory();
96 void executeFollowTrajectory();
97 void endFollowTrajectory(bool interrupted);
98
99 void beginStopped();
100 void executeStopped();
101 void endStopped(bool interrupted);
102
103 void beginEndAction();
104 void executeEndAction();
105 void endEndAction(bool interrupted);
106
107 void stopRobot();
108
109 static CanonicalAngle flipAngleForAlliance(CanonicalAngle originalAngle,
110 std::optional<frc::DriverStation::Alliance> alliance,
111 FieldSymmetry fieldSymmetry);
112
113 static frc::Pose2d flipPoseForAlliance(const frc::Pose2d& originalPose,
114 std::optional<frc::DriverStation::Alliance> alliance,
115 FieldSymmetry fieldSymmetry,
116 FieldDimensions fieldDimensions);
117};
118
119} // namespace thunder
Definition ThunderAutoTrajectoryCommand.hpp:21
ThunderAutoTrajectoryCommand(const std::string &trajectoryName, std::shared_ptr< ThunderAutoProject > project, const TrajectoryRunnerProperties &properties, bool shouldResetPose=true)
Definition ThunderAutoTrajectoryCommand.cpp:5
Definition CanonicalAngle.hpp:16
Definition Trajectory.hpp:25
Definition TrajectoryRunnerProperties.hpp:15