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
#define MC_CONTROL_FSM_DLLAPI
Definition: api.h:50
struct MC_CONTROL_FSM_DLLAPI Controller
Definition: State.h:23
std::shared_ptr< State > StatePtr
Definition: State.h:172
Definition: CompletionCriteria.h:11
std::chrono::duration< double, std::milli > duration_ms
Definition: clock.h:13
Definition: Controller.h:50
Definition: Executor.h:33
Executor & operator=(const Executor &)=delete
Executor(Executor &&)=delete
bool read_msg(std::string &msg)
Executor & operator=(Executor &&)=delete
Executor(const Executor &)=delete
bool read_write_msg(std::string &msg, std::string &out)
void teardown(Controller &ctl)
bool ready() const
Definition: Executor.h:96
void init(Controller &ctl, const mc_rtc::Configuration &config, const std::string &name="", const std::vector< std::string > &category={})
bool complete() const
Definition: Executor.h:99
void interrupt()
Definition: Executor.h:90
const std::string & next_state() const
Definition: Executor.h:120
const std::string & output() const
Definition: Executor.h:117
bool run(Controller &ctl, bool keep_state)
const std::string & state() const
Definition: Executor.h:114
bool resume(const std::string &state)
void stop(Controller &ctl)
bool running() const
Definition: Executor.h:93
Definition: TransitionMap.h:44
Simplify access to values hold within a JSON file.
Definition: Configuration.h:166