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 eReversing,
41 eStuck,
42 NUM_STATES
43 };
44
45
51 enum class Event
52 {
53 eStart,
54 eReachedGpsCoordinate,
55 eReachedMarker,
56 eReachedObject,
57 eMarkerSeen,
58 eObjectSeen,
59 eMarkerUnseen,
60 eObjectUnseen,
61 eVerifyingComplete,
62 eVerifyingFailed,
63 eAbort,
64 eRestart,
65 eNoWaypoint,
66 eNewWaypoint,
67 eReverse,
68 eReverseComplete,
69 eSearchFailed,
70 eStuck,
71 eUnstuck,
72
73 NUM_EVENTS
74 };
75
76
85 inline std::string StateToString(States eState)
86 {
87 switch (eState)
88 {
89 case States::eIdle: return "Idle";
90 case States::eNavigating: return "Navigating";
91 case States::eSearchPattern: return "Search Pattern";
92 case States::eApproachingMarker: return "Approaching Marker";
93 case States::eApproachingObject: return "Approaching Object";
94 case States::eVerifyingPosition: return "Verifying Position";
95 case States::eVerifyingMarker: return "Verifying Marker";
96 case States::eVerifyingObject: return "Verifying Object";
97 case States::eReversing: return "Reversing";
98 case States::eStuck: return "Stuck";
99 default: return "Unknown";
100 }
101 }
102
103
109 class State
110 {
111 private:
112 States m_eState;
113 std::string m_szStateName;
114
115 protected:
116
124 virtual void Start() { return; }
125
126
134 virtual void Exit() { return; }
135
136 public:
137
145 State(States eState)
146 {
147 // Set State
148 m_eState = eState;
149
150 // Set State Name
151 m_szStateName = StateToString(m_eState);
152
153 // Start the State
154 Start();
155 }
156
157
163 virtual ~State() = default;
164
165
174 virtual States TriggerEvent(Event eEvent) = 0;
175
176
182 virtual void Run() = 0;
183
184
192 States GetState() const { return m_eState; }
193
194
202 virtual std::string ToString() const { return m_szStateName; }
203
204
214 virtual bool operator==(const State& other) const { return ToString() == other.ToString(); }
215
216
226 virtual bool operator!=(const State& other) const { return !operator==(other); }
227 };
228} // namespace statemachine
229
230#endif // STATE_HPP
The abstract state class. All states inherit from this class.
Definition State.hpp:110
virtual bool operator!=(const State &other) const
Checks to see if the current state is not equal to the passed state.
Definition State.hpp:226
virtual std::string ToString() const
Accessor for the State private member. Returns the state as a string.
Definition State.hpp:202
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:124
State(States eState)
Construct a new State object.
Definition State.hpp:145
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:192
virtual void Exit()
This method is called when the state is exited. It is used to clean up the state.
Definition State.hpp:134
virtual bool operator==(const State &other) const
Checks to see if the current state is equal to the passed state.
Definition State.hpp:214
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:85
Event
The events that can be triggered in the state machine.
Definition State.hpp:52
States
The states that the state machine can be in.
Definition State.hpp:31