Download third-party/unitree_rl_lab/deploy/include/FSM/CtrlFSM.h from hk239/v2d: direct link, hf CLI and curl.
- Browser
- Download file 3.37 kB
-
https://huggingface.co/datasets/hk239/v2d/resolve/main/third-party/unitree_rl_lab/deploy/include/FSM/CtrlFSM.h
- Command line
-
hf download hf://datasets/hk239/v2d/third-party/unitree_rl_lab/deploy/include/FSM/CtrlFSM.h
-
curl -L -o CtrlFSM.h https://huggingface.co/datasets/hk239/v2d/resolve/main/third-party/unitree_rl_lab/deploy/include/FSM/CtrlFSM.h
3.37 kB
| // Copyright (c) 2025, Unitree Robotics Co., Ltd. | |
| // All rights reserved. | |
| class CtrlFSM | |
| { | |
| public: | |
| CtrlFSM(std::shared_ptr<BaseState> 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<std::string>(); | |
| int id = it->second["id"].as<int>(); | |
| FSMStringMap.insert({id, fsm_name}); | |
| } | |
| // Initialize FSM states | |
| for (auto it = fsms.begin(); it != fsms.end(); ++it) | |
| { | |
| std::string fsm_name = it->first.as<std::string>(); | |
| int id = it->second["id"].as<int>(); | |
| std::string fsm_type = it->second["type"] ? it->second["type"].as<std::string>() : 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<unitree::common::RecurrentThread>( | |
| "FSM", 0, this->dt * 1e6, &CtrlFSM::run_, this); | |
| spdlog::info("FSM: Start {}", currentState->getStateString()); | |
| } | |
| void add(std::shared_ptr<BaseState> 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<std::shared_ptr<BaseState>> 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); i<currentState->registered_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<BaseState> currentState; | |
| unitree::common::RecurrentThreadPtr fsm_thread_; | |
| }; |