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;
76 [
this](
const rovecomm::RoveCommPacket<uint8_t>& stPacket,
const sockaddr_in& stdAddr)
83 LOG_INFO(logging::g_qSharedLogger,
"Incoming Packet: Start Autonomy!");
97 const std::function<void(
const rovecomm::RoveCommPacket<uint8_t>&,
const sockaddr_in&)>
AutonomyStopCallback =
98 [
this](
const rovecomm::RoveCommPacket<uint8_t>& stPacket,
const sockaddr_in& stdAddr)
105 LOG_INFO(logging::g_qSharedLogger,
"Incoming Packet: Abort Autonomy!");
108 this->
HandleEvent(statemachine::Event::eAbort,
true);
119 [
this](
const rovecomm::RoveCommPacket<uint8_t>& stPacket,
const sockaddr_in& stdAddr)
135 LOG_NOTICE(logging::g_qSharedLogger,
"Incoming Clear Waypoints packet: Deleting all saved states in StateMachineHandler...");
142 LOG_WARNING(logging::g_qSharedLogger,
143 "Incoming Clear Waypoints packet: Cannot clear saved states in StateMachineHandler unless in Idle state. Current state is {}.",
157 [
this](
const rovecomm::RoveCommPacket<float>& stPacket,
const sockaddr_in& stdAddr)
163 double dTotalCellVoltages = 0.0;
164 int nValidCellVoltageValues = 0;
167 for (
int nIter = 0; nIter < stPacket.unDataCount; ++nIter)
170 if (stPacket.vData[nIter] >= 0.1)
173 dTotalCellVoltages += stPacket.vData[nIter];
175 ++nValidCellVoltageValues;
179 double dAverageCellVoltage = dTotalCellVoltages / nValidCellVoltageValues;
182 LOG_DEBUG(logging::g_qSharedLogger,
"Incoming Packet: PMS Cell Voltages. Average voltage is: {}", dAverageCellVoltage);
185 if (constants::BATTERY_CHECKS_ENABLED && dAverageCellVoltage < constants::BATTERY_MINIMUM_CELL_VOLTAGE &&
189 LOG_CRITICAL(logging::g_qSharedLogger,
190 "Incoming PMS Packet: Average cell voltage is {} which is below the safe minimum of {}. Entering Idle state...",
192 constants::BATTERY_MINIMUM_CELL_VOLTAGE);
195 this->
HandleEvent(statemachine::Event::eAbort,
true);
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: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:187
void ClearSavedState(statemachine::States eState)
Clear a saved state based on the given state.
Definition StateMachineHandler.cpp:397
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:75
double SmartRetrieveAngularVelocity()
Retrieve the rover's current velocity. Currently there is no easy way to get the velocity of the ZEDC...
Definition StateMachineHandler.cpp:628
void ClearSavedStates()
Clear all saved states.
Definition StateMachineHandler.cpp:379
statemachine::States GetCurrentState() const
Accessor for the Current State private member.
Definition StateMachineHandler.cpp:413
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:156
void PooledLinearCode() override
This method holds the code that is ran in the thread pool started by the ThreadedLinearCode() method....
Definition StateMachineHandler.cpp:337
std::shared_ptr< statemachine::State > CreateState(statemachine::States eState)
Create a State object based of of the State enum.
Definition StateMachineHandler.cpp:75
double SmartRetrieveVelocity()
Retrieve the rover's current velocity. Currently there is no easy way to get the velocity of the ZEDC...
Definition StateMachineHandler.cpp:612
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:648
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:97
void ThreadedContinuousCode() override
This code will run continuously in a separate thread. The State Machine Handler will check the curren...
Definition StateMachineHandler.cpp:239
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:118
statemachine::States GetPreviousState() const
Accessor for the Previous State private member.
Definition StateMachineHandler.cpp:426
~StateMachineHandler()
Destroy the State Machine Handler:: State Machine Handler object.
Definition StateMachineHandler.cpp:56
void StopStateMachine()
This method will stop the state machine. It will signal whatever state is currently running to abort ...
Definition StateMachineHandler.cpp:212
geoops::RoverPose SmartRetrieveRoverPose(bool bVIOHeading=true, bool bVIOTracking=false)
Retrieve the rover's current position and heading. Automatically picks between getting the position/h...
Definition StateMachineHandler.cpp:445
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:353
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:172
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
This struct stores/contains information about a UTM coordinate.
Definition GeospatialOperations.hpp:211