Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
State.hpp
Go to the documentation of this file.
1
11#ifndef STATE_HPP
12#define STATE_HPP
13
14#include "../AutonomyLogging.h"
15
16
22namespace statemachine
23{
24
30 enum class States
31 {
32 eIdle,
33 eNavigating,
34 eSearchPattern,
35 eApproachingMarker,
36 eApproachingObject,
37 eVerifyingPosition,
38 eVerifyingMarker,
39 eVerifyingObject,
40 eAvoidance,
41 eReversing,
42 eStuck,
43
44 NUM_STATES
45 };
46
47
53 enum class Event
54 {
55 eStart,
56 eReachedGpsCoordinate,
57 eReachedMarker,
58 eReachedObject,
59 eMarkerSeen,
60 eObjectSeen,
61 eMarkerUnseen,
62 eObjectUnseen,
63 eVerifyingComplete,
64 eVerifyingFailed,
65 eAbort,
66 eRestart,
67 eObstacleAvoidance,
68 eEndObstacleAvoidance,
69 eNoWaypoint,
70 eNewWaypoint,
71 eReverse,
72 eReverseComplete,
73 eSearchFailed,
74 eStuck,
75 eUnstuck,
76
77 NUM_EVENTS
78 };
79
80
89 inline std::string StateToString(States eState)
90 {
91 switch (eState)
92 {
93 case States::eIdle: return "Idle";
94 case States::eNavigating: return "Navigating";
95 case States::eSearchPattern: return "Search Pattern";
96 case States::eApproachingMarker: return "Approaching Marker";
97 case States::eApproachingObject: return "Approaching Object";
98 case States::eVerifyingPosition: return "Verifying Position";
99 case States::eVerifyingMarker: return "Verifying Marker";
100 case States::eVerifyingObject: return "Verifying Object";
101 case States::eAvoidance: return "Avoidance";
102 case States::eReversing: return "Reversing";
103 case States::eStuck: return "Stuck";
104 default: return "Unknown";
105 }
106 }
107
108
114 class State
115 {
116 private:
117 States m_eState;
118 std::string m_szStateName;
119
120 protected:
121
129 virtual void Start() { return; }
130
131
139 virtual void Exit() { return; }
140
141 public:
142
150 State(States eState)
151 {
152 // Set State
153 m_eState = eState;
154
155 // Set State Name
156 m_szStateName = StateToString(m_eState);
157
158 // Start the State
159 Start();
160 }
161
162
168 virtual ~State() = default;
169
170
179 virtual States TriggerEvent(Event eEvent) = 0;
180
181
187 virtual void Run() = 0;
188
189
197 States GetState() const { return m_eState; }
198
199
207 virtual std::string ToString() const { return m_szStateName; }
208
209
219 virtual bool operator==(const State& other) const { return ToString() == other.ToString(); }
220
221
231 virtual bool operator!=(const State& other) const { return !operator==(other); }
232 };
233} // namespace statemachine
234
235#endif // STATE_HPP
The abstract state class. All states inherit from this class.
Definition State.hpp:115
virtual bool operator!=(const State &other) const
Checks to see if the current state is not equal to the passed state.
Definition State.hpp:231
virtual std::string ToString() const
Accessor for the State private member. Returns the state as a string.
Definition State.hpp:207
virtual States TriggerEvent(Event eEvent)=0
Trigger an event in the state machine. Returns the next state.
virtual void Start()
This method is called when the state is first started. It is used to initialize the state.
Definition State.hpp:129
State(States eState)
Construct a new State object.
Definition State.hpp:150
virtual void Run()=0
Run the state machine. Returns the next state.
virtual ~State()=default
Destroy the State object.
States GetState() const
Accessor for the State private member.
Definition State.hpp:197
virtual void Exit()
This method is called when the state is exited. It is used to clean up the state.
Definition State.hpp:139
virtual bool operator==(const State &other) const
Checks to see if the current state is equal to the passed state.
Definition State.hpp:219
Namespace containing all state machine related classes.
Definition State.hpp:23
std::string StateToString(States eState)
Converts a state object to a string.
Definition State.hpp:89
Event
The events that can be triggered in the state machine.
Definition State.hpp:54
States
The states that the state machine can be in.
Definition State.hpp:31