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 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'main/CMakeLists.txt') 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) - -- cgit v1.2.3 From 1640a4fb4bb0c831dd3f2a28bf99a49f3f41e829 Mon Sep 17 00:00:00 2001 From: ThomasAvans Date: Mon, 22 Apr 2024 10:40:59 +0200 Subject: Processed review --- .gitmodules | 2 ++ main/CMakeLists.txt | 1 - main/puzzle/fsm/FSM.cpp | 3 -- main/puzzle/fsm/FSM.h | 50 -------------------------- main/puzzle/fsm/FSM.hpp | 68 ----------------------------------- main/puzzle/fsm/IBehaviouralState.hpp | 25 ------------- shared/FSM.cpp | 3 ++ shared/FSM.h | 50 ++++++++++++++++++++++++++ shared/FSM.hpp | 68 +++++++++++++++++++++++++++++++++++ shared/IBehaviouralState.hpp | 25 +++++++++++++ 10 files changed, 148 insertions(+), 147 deletions(-) delete mode 100644 main/puzzle/fsm/FSM.cpp delete mode 100644 main/puzzle/fsm/FSM.h delete mode 100644 main/puzzle/fsm/FSM.hpp delete mode 100644 main/puzzle/fsm/IBehaviouralState.hpp create mode 100644 shared/FSM.cpp create mode 100644 shared/FSM.h create mode 100644 shared/FSM.hpp create mode 100644 shared/IBehaviouralState.hpp (limited to 'main/CMakeLists.txt') diff --git a/.gitmodules b/.gitmodules index 5c66c79..c951407 100644 --- a/.gitmodules +++ b/.gitmodules @@ -6,3 +6,5 @@ [submodule "test/lib/googletest"] path = test/lib/googletest url = https://github.com/google/googletest + branch = v1.14.0 + shallow = true diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 8af9402..a50a9a6 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -13,7 +13,6 @@ pico_sdk_init() add_executable(main main.cpp - FSM.cpp ) pico_enable_stdio_usb(main 1) diff --git a/main/puzzle/fsm/FSM.cpp b/main/puzzle/fsm/FSM.cpp deleted file mode 100644 index fad7fb3..0000000 --- a/main/puzzle/fsm/FSM.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#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 deleted file mode 100644 index 792a44f..0000000 --- a/main/puzzle/fsm/FSM.h +++ /dev/null @@ -1,50 +0,0 @@ -#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 deleted file mode 100644 index c86ed80..0000000 --- a/main/puzzle/fsm/FSM.hpp +++ /dev/null @@ -1,68 +0,0 @@ -#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 deleted file mode 100644 index c41fef3..0000000 --- a/main/puzzle/fsm/IBehaviouralState.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#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/shared/FSM.cpp b/shared/FSM.cpp new file mode 100644 index 0000000..fad7fb3 --- /dev/null +++ b/shared/FSM.cpp @@ -0,0 +1,3 @@ +#include "FSM.h" + +#include "IBehaviouralState.hpp" \ No newline at end of file diff --git a/shared/FSM.h b/shared/FSM.h new file mode 100644 index 0000000..792a44f --- /dev/null +++ b/shared/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/shared/FSM.hpp b/shared/FSM.hpp new file mode 100644 index 0000000..c86ed80 --- /dev/null +++ b/shared/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/shared/IBehaviouralState.hpp b/shared/IBehaviouralState.hpp new file mode 100644 index 0000000..c41fef3 --- /dev/null +++ b/shared/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 -- cgit v1.2.3 From 6dd2000fd69fb54240159b1b950036e745e17bcc Mon Sep 17 00:00:00 2001 From: ThomasAvans Date: Mon, 22 Apr 2024 10:42:24 +0200 Subject: removed trailing space --- main/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'main/CMakeLists.txt') diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index a50a9a6..e24d9a5 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -11,7 +11,7 @@ set(PICO_BOARD pico_w) pico_sdk_init() -add_executable(main +add_executable(main main.cpp ) -- cgit v1.2.3