RIT VEXU Core API
Loading...
Searching...
No Matches
StateMachine< System, IDType, Message, delay_ms, do_log > Class Template Reference

State Machine :)))))) A fun fun way of controlling stateful subsystems - used in the 2023-2024 Over Under game for our overly complex intake-cata subsystem (see there for an example) The statemachine runs in a background thread and a user thread can interact with it through current_state and send_message. More...

#include <state_machine.h>

Classes

class  MaybeMessage
 MaybeMessage a message of Message type or nothing MaybeMessage m = {}; // empty MaybeMessage m = Message::EnumField1. More...
 
struct  State
 

Public Member Functions

 StateMachine (State *initial)
 Construct a state machine and immediatly start running it.
 
IDType current_state () const
 retrieve the current state of the state machine. This is safe to call from external threads
 
void send_message (Message msg)
 send a message to the state machine from outside
 

Detailed Description

template<typename System, typename IDType, typename Message, int32_t delay_ms, bool do_log = false>
class StateMachine< System, IDType, Message, delay_ms, do_log >

State Machine :)))))) A fun fun way of controlling stateful subsystems - used in the 2023-2024 Over Under game for our overly complex intake-cata subsystem (see there for an example) The statemachine runs in a background thread and a user thread can interact with it through current_state and send_message.

Designwise: the System class should hold onto any motors, feedback controllers, etc that are persistent in the system States themselves should hold any data that only that state needs. For example if a state should be exitted after a certain amount of time, it should hold a timer rather than the System holding that timer. (see Junder from 2024 for an example of this design)

Template Parameters
SystemThe system that this is the base class of class Thing : public StateMachine<Thing> @tparam IDType The ID enum that recognizes states. Hint hint, use anenum class`
Messagethe message enum that a state or an outside can send and that states respond to
delay_msthe delay to wait between each state processing to allow other threads to work
do_logtrue if you want print statements describing incoming messages and current states. If true, it is expected that IDType and Message have a function called to_string that takes them as its only parameter and returns a std::string

Constructor & Destructor Documentation

◆ StateMachine()

template<typename System , typename IDType , typename Message , int32_t delay_ms, bool do_log = false>
StateMachine< System, IDType, Message, delay_ms, do_log >::StateMachine ( State * initial)
inline

Construct a state machine and immediatly start running it.

Parameters
initialthe state that the machine will begin in

Member Function Documentation

◆ current_state()

template<typename System , typename IDType , typename Message , int32_t delay_ms, bool do_log = false>
IDType StateMachine< System, IDType, Message, delay_ms, do_log >::current_state ( ) const
inline

retrieve the current state of the state machine. This is safe to call from external threads

Returns
the current state

◆ send_message()

template<typename System , typename IDType , typename Message , int32_t delay_ms, bool do_log = false>
void StateMachine< System, IDType, Message, delay_ms, do_log >::send_message ( Message msg)
inline

send a message to the state machine from outside

Parameters
msgthe message to send This is safe to call from external threads

The documentation for this class was generated from the following file: