From d8fabca00814c4cceb9f0533252be8cbe1356234 Mon Sep 17 00:00:00 2001 From: ThomasAvans Date: Mon, 15 Apr 2024 15:48:55 +0200 Subject: Added Finite State Machine implementation --- main/CMakeLists.txt | 4 +-- main/puzzle/fsm/FSM.cpp | 3 ++ main/puzzle/fsm/FSM.h | 50 ++++++++++++++++++++++++++ main/puzzle/fsm/FSM.hpp | 68 +++++++++++++++++++++++++++++++++++ main/puzzle/fsm/IBehaviouralState.hpp | 25 +++++++++++++ test/.gitignore | 1 + 6 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 main/puzzle/fsm/FSM.cpp create mode 100644 main/puzzle/fsm/FSM.h create mode 100644 main/puzzle/fsm/FSM.hpp create mode 100644 main/puzzle/fsm/IBehaviouralState.hpp create mode 100644 test/.gitignore diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 59d1156..8af9402 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -11,8 +11,9 @@ set(PICO_BOARD pico_w) pico_sdk_init() -add_executable(main +add_executable(main main.cpp + FSM.cpp ) pico_enable_stdio_usb(main 1) @@ -20,4 +21,3 @@ pico_enable_stdio_usb(main 1) pico_add_extra_outputs(main) target_include_directories(main PRIVATE ${CMAKE_CURRENT_LIST_DIR}) target_link_libraries(main pico_cyw43_arch_lwip_threadsafe_background pico_stdlib) - diff --git a/main/puzzle/fsm/FSM.cpp b/main/puzzle/fsm/FSM.cpp new file mode 100644 index 0000000..fad7fb3 --- /dev/null +++ b/main/puzzle/fsm/FSM.cpp @@ -0,0 +1,3 @@ +#include "FSM.h" + +#include "IBehaviouralState.hpp" \ No newline at end of file diff --git a/main/puzzle/fsm/FSM.h b/main/puzzle/fsm/FSM.h new file mode 100644 index 0000000..a363c88 --- /dev/null +++ b/main/puzzle/fsm/FSM.h @@ -0,0 +1,50 @@ +#ifndef _FSM_H_ +#define _FSM_H_ + +#include +#include + +#include "IBehaviouralState.hpp" + +/// +/// +/// +/// IBehaviouralState is the only accepted +/// class. +template class FSM { +public: + template FSM(TPState &...args) { + int i = 0; + + ((void)_states.emplace(i++, args), ...); + } + + /// + /// Implement with FSM::act() + /// + void act(); + + /// + /// Used to check current state. + /// + /// Current state. + std::shared_ptr &get_state() { return _currentState; } + + /// + /// Used to get all states. + /// + /// Current states. + std::map> get_states(); + + /// + /// Sets current state, calls appropiate functions. + /// + /// State to transition into. + void set_state(std::shared_ptr); + +private: + std::map> _states; + std::shared_ptr _currentState; +}; + +#endif // _FSM_H_ \ No newline at end of file diff --git a/main/puzzle/fsm/FSM.hpp b/main/puzzle/fsm/FSM.hpp new file mode 100644 index 0000000..c86ed80 --- /dev/null +++ b/main/puzzle/fsm/FSM.hpp @@ -0,0 +1,68 @@ +#ifndef _FSM_HPP_ +#define _FSM_HPP_ + +#include "FSM.h" + +#include + +/// +/// This is a generic update method, unknown classes are not supported. +/// +/// IBehaviouralState is the only accepted +/// class. +template void FSM::act() { + // No implementation for unknown class. + std::cout << "No implementation for unknown class" << std::endl; +} +// +///// +///// Calls the IBehaviouralState act method. +///// +///// IBehaviouralState is the only accepted +/// class. +template <> inline void FSM::act() { _currentState->act(); } + +/// +/// This is a generic return of the type. +/// +/// Any class type works with this return. +/// The current IBehaviouralState map. +template +std::map> FSM::get_states() { + return _states; +} + +///// +///// State transitioning from current state to newState. +///// Calls Exit on the current state and Enter on the new state. +///// +///// IBehaviouralState is the only accepted +/// class. +///// New state to transition into. +template <> +inline void +FSM::set_state(std::shared_ptr newState) { + // We can guarantee all statemachines are based on IBehaviouralState + if (_currentState != nullptr) + _currentState->exit(); + + _currentState = nullptr; + _currentState = newState; + _currentState->enter(); +} + +/// +/// State transitioning from current state to newState. +/// Calls Exit on the current state and Enter on the new state. +/// +/// IBehaviouralState is the only accepted +/// class. New state to transition +/// into. +template +void FSM::set_state(std::shared_ptr newState) { + // We can guarantee all statemachines are based on IBehaviouralState + // No implementation for an unknown state change class. + std::cout << "No implementation for unknown state change class" << std::endl; +} + +#endif // _FSM_HPP_ \ No newline at end of file diff --git a/main/puzzle/fsm/IBehaviouralState.hpp b/main/puzzle/fsm/IBehaviouralState.hpp new file mode 100644 index 0000000..c41fef3 --- /dev/null +++ b/main/puzzle/fsm/IBehaviouralState.hpp @@ -0,0 +1,25 @@ +#ifndef _FSM_IBEHAVIOURALSTATE_HPP_ +#define _FSM_IBEHAVIOURALSTATE_HPP_ + +/// +/// Sub class used to define methods implemented by behavioural specific states. +/// +class IBehaviouralState { +public: + /// + /// Enters the current state. Used for setup. + /// + virtual void enter() = 0; + + /// + /// Updates the current state, used for physics, etc. + /// + virtual void act() = 0; + + /// + /// Exits the state, used for cleanup. + /// + virtual void exit() = 0; +}; + +#endif // _FSM_IBEHAVIOURALSTATE_HPP_ \ No newline at end of file diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 0000000..c795b05 --- /dev/null +++ b/test/.gitignore @@ -0,0 +1 @@ +build \ No newline at end of file -- cgit v1.2.3