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