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 "./CameraHandler.h"
15
16#include "../states/ApproachingMarkerState.h"
17#include "../states/ApproachingObjectState.h"
18#include "../states/AvoidanceState.h"
19#include "../states/IdleState.h"
20#include "../states/NavigatingState.h"
21#include "../states/ReversingState.h"
22#include "../states/SearchPatternState.h"
23#include "../states/StuckState.h"
24#include "../states/VerifyingMarkerState.h"
25#include "../states/VerifyingObjectState.h"
26#include "../states/VerifyingPositionState.h"
27
29#include <RoveComm/RoveComm.h>
30#include <RoveComm/RoveCommManifest.h>
31#include <atomic>
32#include <shared_mutex>
33
35
36
46{
47 private:
49 // Declare private class member variables.
51 std::shared_ptr<statemachine::State> m_pCurrentState;
52 std::shared_ptr<statemachine::State> m_pPreviousState;
53 std::unordered_map<statemachine::States, std::shared_ptr<statemachine::State>> m_umSavedStates;
54 std::shared_mutex m_muStateMutex;
55 std::shared_mutex m_muEventMutex;
56 std::atomic_bool m_bSwitchingStates;
57 ZEDCamera* m_pMainCam;
58 geoops::GPSCoordinate m_stCurrentGPSLocation;
59
61 // Declare private class methods.
63 std::shared_ptr<statemachine::State> CreateState(statemachine::States eState);
64 void ChangeState(statemachine::States eNextState, const bool bSaveCurrentState = false);
65 void SaveCurrentState();
66 void ThreadedContinuousCode() override;
67 void PooledLinearCode() override;
68
69
77 const std::function<void(const rovecomm::RoveCommPacket<uint8_t>&, const sockaddr_in&)> AutonomyStartCallback =
78 [this](const rovecomm::RoveCommPacket<uint8_t>& stPacket, const sockaddr_in& stdAddr)
79 {
80 // Not using this.
81 (void) stPacket;
82 (void) stdAddr;
83
84 // Submit logger message.
85 LOG_INFO(logging::g_qSharedLogger, "Incoming Packet: Start Autonomy!");
86
87 // Signal statemachine handler with Start event.
88 this->HandleEvent(statemachine::Event::eStart);
89 };
90
91
99 const std::function<void(const rovecomm::RoveCommPacket<uint8_t>&, const sockaddr_in&)> AutonomyStopCallback =
100 [this](const rovecomm::RoveCommPacket<uint8_t>& stPacket, const sockaddr_in& stdAddr)
101 {
102 // Not using this.
103 (void) stPacket;
104 (void) stdAddr;
105
106 // Submit logger message.
107 LOG_INFO(logging::g_qSharedLogger, "Incoming Packet: Abort Autonomy!");
108
109 // Signal statemachine handler with stop event.
110 this->HandleEvent(statemachine::Event::eAbort, true);
111 };
112
113
121 const std::function<void(const rovecomm::RoveCommPacket<float>&, const sockaddr_in&)> PMSCellVoltageCallback =
122 [this](const rovecomm::RoveCommPacket<float>& stPacket, const sockaddr_in& stdAddr)
123 {
124 // Not using this.
125 (void) stdAddr;
126
127 // Create instance variables.
128 double dTotalCellVoltages = 0.0;
129 int nValidCellVoltageValues = 0;
130
131 // Loop through voltage values and average all of the valid ones.
132 for (int nIter = 0; nIter < stPacket.unDataCount; ++nIter)
133 {
134 // Check if the voltage values is greater than at least 0.1.
135 if (stPacket.vData[nIter] >= 0.1)
136 {
137 // Add cell voltage value to total.
138 dTotalCellVoltages += stPacket.vData[nIter];
139 // Increment voltage voltage counter.
140 ++nValidCellVoltageValues;
141 }
142 }
143 // Calculate average cell voltage.
144 double dAverageCellVoltage = dTotalCellVoltages / nValidCellVoltageValues;
145
146 // Submit logger message.
147 LOG_DEBUG(logging::g_qSharedLogger, "Incoming Packet: PMS Cell Voltages. Average voltage is: {}", dAverageCellVoltage);
148
149 // Check if voltage is above the safe minimum for lithium ion batteries.
150 if (constants::BATTERY_CHECKS_ENABLED && dAverageCellVoltage < constants::BATTERY_MINIMUM_CELL_VOLTAGE &&
151 this->GetCurrentState() != statemachine::States::eIdle)
152 {
153 // Submit logger message.
154 LOG_CRITICAL(logging::g_qSharedLogger,
155 "Incoming PMS Packet: Average cell voltage is {} which is below the safe minimum of {}. Entering Idle state...",
156 dAverageCellVoltage,
157 constants::BATTERY_MINIMUM_CELL_VOLTAGE);
158
159 // Signal statemachine handler with stop event.
160 this->HandleEvent(statemachine::Event::eAbort, true);
161 }
162 };
163
164 public:
166 // Declare public class methods and variables.
170
171 void StartStateMachine();
172 void StopStateMachine();
173
174 void HandleEvent(statemachine::Event eEvent, const bool bSaveCurrentState = false);
175
176 void ClearSavedStates();
180
181 void RealignZEDPosition(CameraHandler::ZEDCamName eCameraName, const geoops::UTMCoordinate& stNewCameraPosition, const double dNewCameraHeading);
183};
184
185#endif // STATEMACHINEHANDLER_H
Defines the CameraHandler class.
Interface class used to easily multithread a child class.
Definition AutonomyThread.hpp:38
IPS & GetIPS()
Accessor for the Frame I P S private member.
Definition AutonomyThread.hpp:224
The StateMachineHandler class serves as the main state machine for Autonomy Software....
Definition StateMachineHandler.h:46
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:178
void ClearSavedState(statemachine::States eState)
Clear a saved state based on the given state.
Definition StateMachineHandler.cpp:378
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:77
void ClearSavedStates()
Clear all saved states.
Definition StateMachineHandler.cpp:360
statemachine::States GetCurrentState() const
Accessor for the Current State private member.
Definition StateMachineHandler.cpp:394
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:121
void PooledLinearCode() override
This method holds the code that is ran in the thread pool started by the ThreadedLinearCode() method....
Definition StateMachineHandler.cpp:321
std::shared_ptr< statemachine::State > CreateState(statemachine::States eState)
Create a State object based of of the State enum.
Definition StateMachineHandler.cpp:74
void RealignZEDPosition(CameraHandler::ZEDCamName eCameraName, const geoops::UTMCoordinate &stNewCameraPosition, const double dNewCameraHeading)
This is used to realign the ZEDs forward direction with the rover's current compass heading....
Definition StateMachineHandler.cpp:427
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:99
void ThreadedContinuousCode() override
This code will run continuously in a separate thread. The State Machine Handler will check the curren...
Definition StateMachineHandler.cpp:227
StateMachineHandler()
Construct a new State Machine Handler object.
Definition StateMachineHandler.cpp:23
statemachine::States GetPreviousState() const
Accessor for the Previous State private member.
Definition StateMachineHandler.cpp:407
~StateMachineHandler()
Destroy the State Machine Handler:: State Machine Handler object.
Definition StateMachineHandler.cpp:55
void StopStateMachine()
This method will stop the state machine. It will signal whatever state is currently running to abort ...
Definition StateMachineHandler.cpp:200
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:337
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:110
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:163
This class serves as a middle inheritor between the Camera interface and the ZEDCam class....
Definition ZEDCamera.hpp:33
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
This struct stores/contains information about a GPS data.
Definition GeospatialOperations.hpp:148
This struct stores/contains information about a UTM coordinate.
Definition GeospatialOperations.hpp:244