Executor.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2019 CNRS-UM LIRMM, CNRS-AIST JRL
3  */
4 
5 #pragma once
6 
7 #include <mc_control/api.h>
8 #include <mc_control/fsm/State.h>
11 
12 #include <chrono>
13 #include <memory>
14 
15 namespace mc_control
16 {
17 
18 namespace fsm
19 {
20 
21 struct Controller;
22 
33 {
40  Executor() = default;
41  Executor(const Executor &) = delete;
42  Executor(Executor &&) = delete;
43  Executor & operator=(const Executor &) = delete;
44  Executor & operator=(Executor &&) = delete;
45 
59  void init(Controller & ctl,
60  const mc_rtc::Configuration & config,
61  const std::string & name = "",
62  const std::vector<std::string> & category = {});
63 
73  bool run(Controller & ctl, bool keep_state);
74 
80  void stop(Controller & ctl);
81 
87  void teardown(Controller & ctl);
88 
90  inline void interrupt() { interrupt_triggered_ = true; }
91 
93  inline bool running() const { return state_ != nullptr; }
94 
96  inline bool ready() const { return ready_; }
97 
99  inline bool complete() const { return complete_; }
100 
105  bool resume(const std::string & state);
106 
111  bool next();
112 
114  const std::string & state() const { return curr_state_; }
115 
117  const std::string & output() const { return state_output_; }
118 
120  const std::string & next_state() const { return next_state_; }
121 
123  bool read_msg(std::string & msg);
124 
126  bool read_write_msg(std::string & msg, std::string & out);
127 
128 private:
129  using duration_ms = std::chrono::duration<double, std::milli>;
131  mc_rtc::Configuration config_;
133  std::string name_;
135  std::vector<std::string> category_;
137  bool managed_ = false;
139  bool step_by_step_ = true;
141  bool reset_postures_ = true;
142 
144  TransitionMap transition_map_;
145 
147  StatePtr state_ = nullptr;
149  std::string curr_state_ = "";
151  std::string state_output_ = "";
152 
154  bool interrupt_triggered_ = false;
156  bool ready_ = true;
158  bool transition_triggered_ = false;
160  bool complete_ = false;
162  std::string next_state_ = "";
163 
165  duration_ms state_create_dt_{0};
167  duration_ms state_run_dt_{0};
169  duration_ms state_teardown_dt_{0};
170 
171 private:
173  bool complete(Controller & ctl, bool keep_state);
174 
176  void next(Controller & ctl);
177 };
178 
179 } // namespace fsm
180 
181 } // namespace mc_control
mc_rtc::Configuration
Simplify access to values hold within a JSON file.
Definition: Configuration.h:165
MC_CONTROL_FSM_DLLAPI
#define MC_CONTROL_FSM_DLLAPI
Definition: api.h:50
mc_control::fsm::Executor::running
bool running() const
Definition: Executor.h:93
TransitionMap.h
mc_control::fsm::Executor::ready
bool ready() const
Definition: Executor.h:96
mc_control::fsm::TransitionMap
Definition: TransitionMap.h:43
mc_control::fsm::Controller
struct MC_CONTROL_FSM_DLLAPI Controller
Definition: State.h:23
api.h
StateFactory.h
mc_rtc::duration_ms
std::chrono::duration< double, std::milli > duration_ms
Definition: clock.h:13
mc_control::fsm::Executor::state
const std::string & state() const
Definition: Executor.h:114
mc_control::fsm::Executor
Definition: Executor.h:32
mc_control::fsm::Executor::complete
bool complete() const
Definition: Executor.h:99
mc_control
Definition: CompletionCriteria.h:10
State.h
mc_control::fsm::StatePtr
std::shared_ptr< State > StatePtr
Definition: State.h:172
mc_control::fsm::Executor::interrupt
void interrupt()
Definition: Executor.h:90
mc_control::fsm::Executor::output
const std::string & output() const
Definition: Executor.h:117
mc_control::fsm::Executor::next_state
const std::string & next_state() const
Definition: Executor.h:120
mc_control::fsm::Controller
Definition: Controller.h:49