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
StateMachineHandler.h
Go to the documentation of this file.
1
11#ifndef STATEMACHINEHANDLER_H
12#define STATEMACHINEHANDLER_H
13
14#include "../states/ApproachingMarkerState.h"
15#include "../states/ApproachingObjectState.h"
16#include "../states/IdleState.h"
17#include "../states/NavigatingState.h"
18#include "../states/ReversingState.h"
19#include "../states/SearchPatternState.h"
20#include "../states/StuckState.h"
21#include "../states/VerifyingMarkerState.h"
22#include "../states/VerifyingObjectState.h"
23#include "../states/VerifyingPositionState.h"
24#include "./CameraHandler.h"
25
27#include <RoveComm/RoveComm.h>
28#include <RoveComm/RoveCommManifest.h>
29#include <atomic>
30#include <shared_mutex>
31
33
34
44{
45 private:
47 // Declare private class member variables.
49 std::shared_ptr<statemachine::State> m_pCurrentState;
50 std::shared_ptr<statemachine::State> m_pPreviousState;
51 std::unordered_map<statemachine::States, std::shared_ptr<statemachine::State>> m_umSavedStates;
52 std::shared_mutex m_muStateMutex;
53 std::shared_mutex m_muEventMutex;
54 std::atomic_bool m_bSwitchingStates;
55 std::shared_ptr<ZEDCamera> m_pMainCam;
56 std::shared_ptr<ZEDCamera> m_pRearCam;
57 geoops::GPSCoordinate m_stCurrentGPSLocation;
58 double m_dZEDHeadingOffset; // This is the offset that is applied to the ZED's heading to align it with the actual heading of the rover.
59
60 // Kinematic tracking variables for heading recovery.
61 double m_dLastRawZEDHeading;
62 double m_dLastFusedHeading;
63 bool m_bFirstHeadingLoop;
64
66 // Declare private class methods.
68 std::shared_ptr<statemachine::State> CreateState(statemachine::States eState);
69 void ChangeState(statemachine::States eNextState, const bool bSaveCurrentState = false);
70 void SaveCurrentState();
71 void ThreadedContinuousCode() override;
72 void PooledLinearCode() override;
73
74
82 const std::function<void(const rovecomm::RoveCommPacket<uint8_t>&, const sockaddr_in&)> AutonomyStartCallback =
83 [this](const rovecomm::RoveCommPacket<uint8_t>& stPacket, const sockaddr_in& stdAddr)
84 {
85 // Not using this.
86 (void) stPacket;
87 (void) stdAddr;
88
89 // Submit logger message.
90 LOG_INFO(logging::g_qSharedLogger, "Incoming Packet: Start Autonomy!");
91
92 // Signal statemachine handler with Start event.
93 this->HandleEvent(statemachine::Event::eStart);
94 };
95
96
104 const std::function<void(const rovecomm::RoveCommPacket<uint8_t>&, const sockaddr_in&)> AutonomyStopCallback =
105 [this](const rovecomm::RoveCommPacket<uint8_t>& stPacket, const sockaddr_in& stdAddr)
106 {
107 // Not using this.
108 (void) stPacket;
109 (void) stdAddr;
110
111 // Submit logger message.
112 LOG_INFO(logging::g_qSharedLogger, "Incoming Packet: Abort Autonomy!");
113
114 // Signal statemachine handler with stop event.
115 this->HandleEvent(statemachine::Event::eAbort, true);
116 };
117
118
125 const std::function<void(const rovecomm::RoveCommPacket<uint8_t>&, const sockaddr_in&)> ClearWaypointsCallback =
126 [this](const rovecomm::RoveCommPacket<uint8_t>& stPacket, const sockaddr_in& stdAddr)
127 {
128 // Not using this.
129 (void) stPacket;
130 (void) stdAddr;
131
132 /*
133 The clear waypoints command will clear all waypoints in the WaypointHandler, but it also clears all saved states in the StateMachineHandler
134 to prevent any conflicts when restarting autonomy with previously saved states that may have waypoints associated with them.
135 However, we only want to delete our saved states if we are currently in IdleState.
136 */
137
138 // Check if the current state is IdleState.
139 if (this->GetCurrentState() == statemachine::States::eIdle)
140 {
141 // Submit logger message.
142 LOG_NOTICE(logging::g_qSharedLogger, "Incoming Clear Waypoints packet: Deleting all saved states in StateMachineHandler...");
143 // Clear the saved states.
144 this->ClearSavedStates();
145 }
146 else
147 {
148 // Submit logger message.
149 LOG_WARNING(logging::g_qSharedLogger,
150 "Incoming Clear Waypoints packet: Cannot clear saved states in StateMachineHandler unless in Idle state. Current state is {}.",
151 static_cast<int>(this->GetCurrentState()));
152 }
153 };
154
155
163 const std::function<void(const rovecomm::RoveCommPacket<float>&, const sockaddr_in&)> PMSCellVoltageCallback =
164 [this](const rovecomm::RoveCommPacket<float>& stPacket, const sockaddr_in& stdAddr)
165 {
166 // Not using this.
167 (void) stdAddr;
168
169 // Create instance variables.
170 double dTotalCellVoltages = 0.0;
171 int nValidCellVoltageValues = 0;
172
173 // Loop through voltage values and average all of the valid ones.
174 for (int nIter = 0; nIter < stPacket.unDataCount; ++nIter)
175 {
176 // Check if the voltage values is greater than at least 0.1.
177 if (stPacket.vData[nIter] >= 0.1)
178 {
179 // Add cell voltage value to total.
180 dTotalCellVoltages += stPacket.vData[nIter];
181 // Increment voltage voltage counter.
182 ++nValidCellVoltageValues;
183 }
184 }
185 // Calculate average cell voltage.
186 double dAverageCellVoltage = dTotalCellVoltages / nValidCellVoltageValues;
187
188 // Submit logger message.
189 LOG_DEBUG(logging::g_qSharedLogger, "Incoming Packet: PMS Cell Voltages. Average voltage is: {}", dAverageCellVoltage);
190
191 // Check if voltage is above the safe minimum for lithium ion batteries.
192 if (constants::BATTERY_CHECKS_ENABLED && dAverageCellVoltage < constants::BATTERY_MINIMUM_CELL_VOLTAGE &&
193 this->GetCurrentState() != statemachine::States::eIdle)
194 {
195 // Submit logger message.
196 LOG_CRITICAL(logging::g_qSharedLogger,
197 "Incoming PMS Packet: Average cell voltage is {} which is below the safe minimum of {}. Entering Idle state...",
198 dAverageCellVoltage,
199 constants::BATTERY_MINIMUM_CELL_VOLTAGE);
200
201 // Signal statemachine handler with stop event.
202 this->HandleEvent(statemachine::Event::eAbort, true);
203 }
204 };
205
206 public:
208 // Declare public class methods and variables.
212 void StartStateMachine();
213 void StopStateMachine();
214 void HandleEvent(statemachine::Event eEvent, const bool bSaveCurrentState = false);
215 void ClearSavedStates();
219
220 // Smart location retrieving.
221 geoops::RoverPose SmartRetrieveRoverPose(bool bIMUHeading = true);
222 double SmartRetrieveVelocity();
224 void RealignZEDHeading(const double dNewActualHeading, const double dCurrentZEDHeading);
225
227};
228
229#endif // STATEMACHINEHANDLER_H
Defines the CameraHandler class.
Interface class used to easily multithread a child class.
Definition AutonomyThread.hpp:40
IPS & GetIPS()
Accessor for the Frame I P S private member.
Definition AutonomyThread.hpp:257
The StateMachineHandler class serves as the main state machine for Autonomy Software....
Definition StateMachineHandler.h:44
void StartStateMachine()
This method will start the state machine. It will set the first state to Idle and start the thread po...
Definition StateMachineHandler.cpp:192
void RealignZEDHeading(const double dNewActualHeading, const double dCurrentZEDHeading)
This method is used to realign the ZED camera's heading with the actual heading of the rover....
Definition StateMachineHandler.cpp:467
void ClearSavedState(statemachine::States eState)
Clear a saved state based on the given state.
Definition StateMachineHandler.cpp:329
const std::function< void(const rovecomm::RoveCommPacket< uint8_t > &, const sockaddr_in &)> AutonomyStartCallback
Callback function used to trigger the start of autonomy. No matter what state we are in,...
Definition StateMachineHandler.h:82
double SmartRetrieveAngularVelocity()
Retrieve the rover's current velocity. Currently there is no easy way to get the velocity of the ZEDC...
Definition StateMachineHandler.cpp:450
void ClearSavedStates()
Clear all saved states.
Definition StateMachineHandler.cpp:311
statemachine::States GetCurrentState() const
Accessor for the Current State private member.
Definition StateMachineHandler.cpp:345
const std::function< void(const rovecomm::RoveCommPacket< float > &, const sockaddr_in &)> PMSCellVoltageCallback
Callback function used to force autonomy into Idle state if battery voltage gets too low....
Definition StateMachineHandler.h:163
void PooledLinearCode() override
This method holds the code that is ran in the thread pool started by the ThreadedLinearCode() method....
Definition StateMachineHandler.cpp:269
std::shared_ptr< statemachine::State > CreateState(statemachine::States eState)
Create a State object based of of the State enum.
Definition StateMachineHandler.cpp:80
double SmartRetrieveVelocity()
Retrieve the rover's current velocity. Currently there is no easy way to get the velocity of the ZEDC...
Definition StateMachineHandler.cpp:434
const std::function< void(const rovecomm::RoveCommPacket< uint8_t > &, const sockaddr_in &)> AutonomyStopCallback
Callback function used to trigger autonomy to stop. No matter what state we are in,...
Definition StateMachineHandler.h:104
void ThreadedContinuousCode() override
This code will run continuously in a separate thread. The State Machine Handler will check the curren...
Definition StateMachineHandler.cpp:244
StateMachineHandler()
Construct a new State Machine Handler object.
Definition StateMachineHandler.cpp:23
const std::function< void(const rovecomm::RoveCommPacket< uint8_t > &, const sockaddr_in &)> ClearWaypointsCallback
Callback function that is called whenever RoveComm receives new CLEARWAYPOINTS packet.
Definition StateMachineHandler.h:125
statemachine::States GetPreviousState() const
Accessor for the Previous State private member.
Definition StateMachineHandler.cpp:358
~StateMachineHandler()
Destroy the State Machine Handler:: State Machine Handler object.
Definition StateMachineHandler.cpp:61
void StopStateMachine()
This method will stop the state machine. It will signal whatever state is currently running to abort ...
Definition StateMachineHandler.cpp:217
geoops::RoverPose SmartRetrieveRoverPose(bool bIMUHeading=true)
This method is used to retrieve the rover's current position and heading. It uses the GPS data from t...
Definition StateMachineHandler.cpp:375
void HandleEvent(statemachine::Event eEvent, const bool bSaveCurrentState=false)
This method Handles Events that are passed to the State Machine Handler. It will check the current st...
Definition StateMachineHandler.cpp:285
void ChangeState(statemachine::States eNextState, const bool bSaveCurrentState=false)
Transition to a new state. This function is called by the HandleEvent and checks to see if this state...
Definition StateMachineHandler.cpp:115
void SaveCurrentState()
Save the current state to the map of exited states. This is used to store the state when the state ma...
Definition StateMachineHandler.cpp:177
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
This struct stores/contains information about a GPS data.
Definition GeospatialOperations.hpp:100
This struct is used by the WaypointHandler to provide an easy way to store all pose data about the ro...
Definition GeospatialOperations.hpp:708