![]() |
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.
|
The StateMachineHandler class serves as the main state machine for Autonomy Software. It will handle all state transitions and run the logic for each state. More...
#include <StateMachineHandler.h>
Public Member Functions | |
StateMachineHandler () | |
Construct a new State Machine Handler object. | |
~StateMachineHandler () | |
Destroy the State Machine Handler:: State Machine Handler object. | |
void | StartStateMachine () |
This method will start the state machine. It will set the first state to Idle and start the thread pool. | |
void | StopStateMachine () |
This method will stop the state machine. It will signal whatever state is currently running to abort back to idle and then stop the main code running in the ThreadedContinuousCode() method. | |
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 state and run the state's HandleEvent() method. It will then check the state's transition conditions and transition to the next state if the conditions are met. | |
void | ClearSavedStates () |
Clear all saved states. | |
void | ClearSavedState (statemachine::States eState) |
Clear a saved state based on the given state. | |
statemachine::States | GetCurrentState () const |
Accessor for the Current State private member. | |
statemachine::States | GetPreviousState () const |
Accessor for the Previous State private member. | |
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. Realigning GPS latlon is not necessary since we're using the ZEDSDK's Fusion module. The heading of the camera's GeoPose is actually automatically realigned too depending on what direction We are headed, but this is just to be safe. | |
IPS & | GetIPS () |
Accessor for the Frame I P S private member. | |
Private Member Functions | |
std::shared_ptr< statemachine::State > | CreateState (statemachine::States eState) |
Create a State object based of of the State enum. | |
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 is already stored in the map of exited states. If it is, it loads the state from the map. If not, it creates a new state and stores it in the map. | |
void | SaveCurrentState () |
Save the current state to the map of exited states. This is used to store the state when the state machine is transitioning to a new state. And prevents the state from being deleted when the state machine transitions to a new state. | |
void | ThreadedContinuousCode () override |
This code will run continuously in a separate thread. The State Machine Handler will check the current state and run the state's logic. It will then check the state's transition conditions and transition to the next state if the conditions are met. | |
void | PooledLinearCode () override |
This method holds the code that is ran in the thread pool started by the ThreadedLinearCode() method. It currently does nothing and is not needed in the current implementation of the StateMachineHandler. | |
![]() | |
AutonomyThread () | |
Construct a new Autonomy Thread object. | |
virtual | ~AutonomyThread () |
Destroy the Autonomy Thread object. If the parent object or main thread is destroyed or exited while this thread is still running, a race condition will occur. Stopping and joining the thread here insures that the main program can't exit if the user forgot to stop and join the thread. | |
void | Start () |
When this method is called, it starts a new thread that runs the code within the ThreadedContinuousCode method. This is the users main code that will run the important and continuous code for the class. | |
void | RequestStop () |
Signals threads to stop executing user code, terminate. DOES NOT JOIN. This method will not force the thread to exit, if the user code is not written properly and contains WHILE statement or any other long-executing or blocking code, then the thread will not exit until the next iteration. | |
void | Join () |
Waits for thread to finish executing and then closes thread. This method will block the calling code until thread is finished. | |
bool | Joinable () const |
Check if the code within the thread and all pools created by it are finished executing and the thread is ready to be closed. | |
AutonomyThreadState | GetThreadState () const |
Accessor for the Threads State private member. | |
IPS & | GetIPS () |
Accessor for the Frame I P S private member. | |
void | RunPool (const unsigned int nNumTasksToQueue, const unsigned int nNumThreads=2, const bool bForceStopCurrentThreads=false) |
When this method is called, it starts/adds tasks to a thread pool that runs nNumTasksToQueue copies of the code within the PooledLinearCode() method using nNumThreads number of threads. This is meant to be used as an internal utility of the child class to further improve parallelization. Default value for nNumThreads is 2. | |
void | RunDetachedPool (const unsigned int nNumTasksToQueue, const unsigned int nNumThreads=2, const bool bForceStopCurrentThreads=false) |
When this method is called, it starts a thread pool full of threads that don't return std::futures (like a placeholder for the thread return type). This means the thread will not have a return type and there is no way to determine if the thread has finished other than calling the Join() method. Only use this if you want to 'set and forget'. It will be faster as it doesn't return futures. Runs PooledLinearCode() method code. This is meant to be used as an internal utility of the child class to further improve parallelization. | |
void | ParallelizeLoop (const int nNumThreads, const N tTotalIterations, F &&tLoopFunction) |
Given a ref-qualified looping function and an arbitrary number of iterations, this method will divide up the loop and run each section in a thread pool. This function must not return anything. This method will block until the loop has completed. | |
void | ClearPoolQueue () |
Clears any tasks waiting to be ran in the queue, tasks currently running will remain running. | |
void | JoinPool () |
Waits for pool to finish executing tasks. This method will block the calling code until thread is finished. | |
bool | PoolJoinable () const |
Check if the internal pool threads are done executing code and the queue is empty. | |
void | SetMainThreadIPSLimit (int nMaxIterationsPerSecond=0) |
Mutator for the Main Thread Max I P S private member. | |
int | GetPoolNumOfThreads () |
Accessor for the Pool Num Of Threads private member. | |
int | GetPoolQueueLength () |
Accessor for the Pool Queue Size private member. | |
std::vector< void > | GetPoolResults () |
Accessor for the Pool Results private member. The action of getting results will destroy and remove them from this object. This method blocks if the thread is not finished, so no need to call JoinPool() before getting results. | |
int | GetMainThreadMaxIPS () const |
Accessor for the Main Thread Max I P S private member. | |
Private Attributes | |
std::shared_ptr< statemachine::State > | m_pCurrentState |
std::shared_ptr< statemachine::State > | m_pPreviousState |
std::unordered_map< statemachine::States, std::shared_ptr< statemachine::State > > | m_umSavedStates |
std::shared_mutex | m_muStateMutex |
std::shared_mutex | m_muEventMutex |
std::atomic_bool | m_bSwitchingStates |
ZEDCamera * | m_pMainCam |
geoops::GPSCoordinate | m_stCurrentGPSLocation |
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, signal a StartAutonomy Event. | |
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, signal an Abort Event. | |
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. No matter what state we are in, signal an Abort Event. | |
![]() | |
IPS | m_IPS |
Additional Inherited Members | |
![]() | |
enum | AutonomyThreadState |
The StateMachineHandler class serves as the main state machine for Autonomy Software. It will handle all state transitions and run the logic for each state.
StateMachineHandler::StateMachineHandler | ( | ) |
Construct a new State Machine Handler object.
StateMachineHandler::~StateMachineHandler | ( | ) |
Destroy the State Machine Handler:: State Machine Handler object.
|
private |
Create a State object based of of the State enum.
eState | - The State enum to create a State object from. |
|
private |
Transition to a new state. This function is called by the HandleEvent and checks to see if this state is already stored in the map of exited states. If it is, it loads the state from the map. If not, it creates a new state and stores it in the map.
eNextState | - The State enum to transition to. |
bSaveCurrentState | - Whether or not to save the current state so it can be recalled next time it is triggered. Default value is false. |
|
private |
Save the current state to the map of exited states. This is used to store the state when the state machine is transitioning to a new state. And prevents the state from being deleted when the state machine transitions to a new state.
|
overrideprivatevirtual |
This code will run continuously in a separate thread. The State Machine Handler will check the current state and run the state's logic. It will then check the state's transition conditions and transition to the next state if the conditions are met.
Implements AutonomyThread< void >.
|
overrideprivatevirtual |
This method holds the code that is ran in the thread pool started by the ThreadedLinearCode() method. It currently does nothing and is not needed in the current implementation of the StateMachineHandler.
Implements AutonomyThread< void >.
void StateMachineHandler::StartStateMachine | ( | ) |
This method will start the state machine. It will set the first state to Idle and start the thread pool.
void StateMachineHandler::StopStateMachine | ( | ) |
This method will stop the state machine. It will signal whatever state is currently running to abort back to idle and then stop the main code running in the ThreadedContinuousCode() method.
void StateMachineHandler::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 state and run the state's HandleEvent() method. It will then check the state's transition conditions and transition to the next state if the conditions are met.
eEvent | - The Event enum to handle. |
bSaveCurrentState | - Whether or not to save the current state so it can be recalled next time it is triggered. Default value is false. |
void StateMachineHandler::ClearSavedStates | ( | ) |
Clear all saved states.
void StateMachineHandler::ClearSavedState | ( | statemachine::States | eState | ) |
Clear a saved state based on the given state.
eState | - The state to clear from the saved states. |
statemachine::States StateMachineHandler::GetCurrentState | ( | ) | const |
Accessor for the Current State private member.
statemachine::States StateMachineHandler::GetPreviousState | ( | ) | const |
Accessor for the Previous State private member.
void StateMachineHandler::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. Realigning GPS latlon is not necessary since we're using the ZEDSDK's Fusion module. The heading of the camera's GeoPose is actually automatically realigned too depending on what direction We are headed, but this is just to be safe.
eCameraName | - The camera name represented as an enum from the CameraHandler class. |
stNewCameraPosition | - The new UTM position of the ZED camera. |
dNewCameraHeading | - The new compass heading of the ZED camera. |
|
inline |
Accessor for the Frame I P S private member.
|
private |
Callback function used to trigger the start of autonomy. No matter what state we are in, signal a StartAutonomy Event.
|
private |
Callback function used to trigger autonomy to stop. No matter what state we are in, signal an Abort Event.
|
private |
Callback function used to force autonomy into Idle state if battery voltage gets too low. No matter what state we are in, signal an Abort Event.