11#ifndef STATEMACHINEHANDLER_H
12#define STATEMACHINEHANDLER_H
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"
27#include <RoveComm/RoveComm.h>
28#include <RoveComm/RoveCommManifest.h>
30#include <shared_mutex>
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;
58 double m_dZEDHeadingOffset;
61 double m_dLastRawZEDHeading;
62 double m_dLastFusedHeading;
63 bool m_bFirstHeadingLoop;
83 [
this](
const rovecomm::RoveCommPacket<uint8_t>& stPacket,
const sockaddr_in& stdAddr)
90 LOG_INFO(logging::g_qSharedLogger,
"Incoming Packet: Start Autonomy!");
105 [
this](
const rovecomm::RoveCommPacket<uint8_t>& stPacket,
const sockaddr_in& stdAddr)
112 LOG_INFO(logging::g_qSharedLogger,
"Incoming Packet: Abort Autonomy!");
115 this->
HandleEvent(statemachine::Event::eAbort,
true);
126 [
this](
const rovecomm::RoveCommPacket<uint8_t>& stPacket,
const sockaddr_in& stdAddr)
142 LOG_NOTICE(logging::g_qSharedLogger,
"Incoming Clear Waypoints packet: Deleting all saved states in StateMachineHandler...");
149 LOG_WARNING(logging::g_qSharedLogger,
150 "Incoming Clear Waypoints packet: Cannot clear saved states in StateMachineHandler unless in Idle state. Current state is {}.",
164 [
this](
const rovecomm::RoveCommPacket<float>& stPacket,
const sockaddr_in& stdAddr)
170 double dTotalCellVoltages = 0.0;
171 int nValidCellVoltageValues = 0;
174 for (
int nIter = 0; nIter < stPacket.unDataCount; ++nIter)
177 if (stPacket.vData[nIter] >= 0.1)
180 dTotalCellVoltages += stPacket.vData[nIter];
182 ++nValidCellVoltageValues;
186 double dAverageCellVoltage = dTotalCellVoltages / nValidCellVoltageValues;
189 LOG_DEBUG(logging::g_qSharedLogger,
"Incoming Packet: PMS Cell Voltages. Average voltage is: {}", dAverageCellVoltage);
192 if (constants::BATTERY_CHECKS_ENABLED && dAverageCellVoltage < constants::BATTERY_MINIMUM_CELL_VOLTAGE &&
196 LOG_CRITICAL(logging::g_qSharedLogger,
197 "Incoming PMS Packet: Average cell voltage is {} which is below the safe minimum of {}. Entering Idle state...",
199 constants::BATTERY_MINIMUM_CELL_VOLTAGE);
202 this->
HandleEvent(statemachine::Event::eAbort,
true);
224 void RealignZEDHeading(
const double dNewActualHeading,
const double dCurrentZEDHeading);
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