// Copyright (c) 2025, Unitree Robotics Co., Ltd. // All rights reserved. #pragma once #include #include "BaseState.h" #include #include class CtrlFSM { public: CtrlFSM(std::shared_ptr initstate) { // Initialize FSM states states.push_back(std::move(initstate)); } CtrlFSM(YAML::Node cfg) { auto fsms = cfg["_"]; // enabled FSMs // register FSM string map; used for state transition for (auto it = fsms.begin(); it != fsms.end(); ++it) { std::string fsm_name = it->first.as(); int id = it->second["id"].as(); FSMStringMap.insert({id, fsm_name}); } // Initialize FSM states for (auto it = fsms.begin(); it != fsms.end(); ++it) { std::string fsm_name = it->first.as(); int id = it->second["id"].as(); std::string fsm_type = it->second["type"] ? it->second["type"].as() : fsm_name; auto fsm_class = getFsmMap().find("State_" + fsm_type); if (fsm_class == getFsmMap().end()) { throw std::runtime_error("FSM: Unknown FSM type " + fsm_type); } auto state_instance = fsm_class->second(id, fsm_name); add(state_instance); } } void start() { // Start From State_Passive currentState = states[0]; currentState->enter(); fsm_thread_ = std::make_shared( "FSM", 0, this->dt * 1e6, &CtrlFSM::run_, this); spdlog::info("FSM: Start {}", currentState->getStateString()); } void add(std::shared_ptr state) { for(auto & s : states) { if(s->isState(state->getState())) { spdlog::error("FSM: State_{} already exists", state->getStateString()); std::exit(0); } } states.push_back(std::move(state)); } ~CtrlFSM() { states.clear(); } std::vector> states; private: const double dt = 0.001; void run_() { currentState->pre_run(); currentState->run(); currentState->post_run(); // Check if need to change state int nextStateMode = 0; for(int i(0); iregistered_checks.size(); i++) { if(currentState->registered_checks[i].first()) { nextStateMode = currentState->registered_checks[i].second; break; } } if(nextStateMode != 0 && !currentState->isState(nextStateMode)) { for(auto & state : states) { if(state->isState(nextStateMode)) { spdlog::info("FSM: Change state from {} to {}", currentState->getStateString(), state->getStateString()); currentState->exit(); currentState = state; currentState->enter(); break; } } } } std::shared_ptr currentState; unitree::common::RecurrentThreadPtr fsm_thread_; };