State.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2020 CNRS-UM LIRMM, CNRS-AIST JRL
3  */
4 
5 #pragma once
6 
7 #include <mc_control/fsm/api.h>
9 
11 
12 #include <mc_tasks/MetaTask.h>
13 #include <mc_tasks/PostureTask.h>
14 
15 #include <mc_rtc/Configuration.h>
16 
17 namespace mc_control
18 {
19 
20 namespace fsm
21 {
22 
24 
59 {
60  virtual ~State() {}
61 
75  void configure_(const mc_rtc::Configuration & config);
76 
78  void start_(Controller & ctl);
79 
81  void teardown_(Controller & ctl);
82 
84  virtual bool run(Controller & ctl) = 0;
85 
87  virtual void stop(Controller &) {}
88 
90  virtual bool read_msg(std::string &) { return false; }
91 
93  virtual bool read_write_msg(std::string &, std::string &) { return false; }
94 
97  const std::string & output() const noexcept { return output_; }
98 
100  const std::string & name() { return name_; }
101 
102  void name(const std::string & n) { name_ = n; }
103 
104 protected:
106  void output(const std::string & o) { output_ = o; }
107 
122  virtual void configure(const mc_rtc::Configuration & config);
123 
129  virtual void start(Controller & ctl) = 0;
130 
132  virtual void teardown(Controller & ctl) = 0;
133 
134 protected:
159 
161  std::vector<mc_solver::ConstraintSetPtr> constraints_;
163  std::vector<std::pair<mc_tasks::MetaTaskPtr, mc_rtc::Configuration>> tasks_;
165  std::vector<mc_tasks::PostureTaskPtr> postures_;
166 
167 private:
168  std::string name_ = "";
169  std::string output_ = "";
170 };
171 
172 using StatePtr = std::shared_ptr<State>;
173 
174 } // namespace fsm
175 
176 } // namespace mc_control
177 
178 /* The following macros are used to simplify the required symbol exports */
179 
180 #ifndef MC_RTC_BUILD_STATIC
181 
182 # ifdef WIN32
183 # define FSM_STATE_API __declspec(dllexport)
184 # else
185 # if __GNUC__ >= 4
186 # define FSM_STATE_API __attribute__((visibility("default")))
187 # else
188 # define FSM_STATE_API
189 # endif
190 # endif
191 
192 # define EXPORT_SINGLE_STATE(NAME, TYPE) \
193  extern "C" \
194  { \
195  FSM_STATE_API void MC_RTC_FSM_STATE(std::vector<std::string> & names) \
196  { \
197  names = {NAME}; \
198  } \
199  FSM_STATE_API void destroy(mc_control::fsm::State * ptr) \
200  { \
201  delete ptr; \
202  } \
203  FSM_STATE_API mc_control::fsm::State * create(const std::string &) \
204  { \
205  return new TYPE(); \
206  } \
207  }
208 
209 #else
210 
211 # define EXPORT_SINGLE_STATE(NAME, TYPE) \
212  namespace \
213  { \
214  static auto registered = []() \
215  { \
216  using fn_t = std::function<TYPE *()>; \
217  mc_control::fsm::Controller::factory().register_object(NAME, fn_t([]() { return new TYPE(); })); \
218  return true; \
219  }(); \
220  }
221 
222 #endif
mc_rtc::Configuration
Simplify access to values hold within a JSON file.
Definition: Configuration.h:165
PostureTask.h
mc_control::fsm::State::read_msg
virtual bool read_msg(std::string &)
Definition: State.h:90
mc_control::fsm::State::read_write_msg
virtual bool read_write_msg(std::string &, std::string &)
Definition: State.h:93
MC_CONTROL_FSM_DLLAPI
#define MC_CONTROL_FSM_DLLAPI
Definition: api.h:50
ConstraintSet.h
mc_control::fsm::State::postures_
std::vector< mc_tasks::PostureTaskPtr > postures_
Definition: State.h:165
mc_control::fsm::State::name
void name(const std::string &n)
Definition: State.h:102
api.h
mc_control::fsm::State::constraints_
std::vector< mc_solver::ConstraintSetPtr > constraints_
Definition: State.h:161
mc_control::fsm::State::constraints_config_
mc_rtc::Configuration constraints_config_
Definition: State.h:152
mc_control::fsm::State::tasks_
std::vector< std::pair< mc_tasks::MetaTaskPtr, mc_rtc::Configuration > > tasks_
Definition: State.h:163
mc_control::fsm::State
Definition: State.h:58
mc_control::fsm::State::config_
mc_rtc::Configuration config_
Definition: State.h:158
mc_control::fsm::State::tasks_config_
mc_rtc::Configuration tasks_config_
Definition: State.h:154
mc_control::fsm::State::~State
virtual ~State()
Definition: State.h:60
mc_control::fsm::State::add_collisions_after_config_
mc_rtc::Configuration add_collisions_after_config_
Definition: State.h:148
Configuration.h
mc_control::fsm::State::add_collisions_config_
mc_rtc::Configuration add_collisions_config_
Definition: State.h:144
mc_control::fsm::State::remove_collisions_config_
mc_rtc::Configuration remove_collisions_config_
Definition: State.h:146
mc_control
Definition: CompletionCriteria.h:10
mc_control::fsm::State::add_contacts_after_config_
mc_rtc::Configuration add_contacts_after_config_
Definition: State.h:140
MetaTask.h
mc_control::fsm::State::output
const std::string & output() const noexcept
Definition: State.h:97
mc_control::fsm::State::output
void output(const std::string &o)
Definition: State.h:106
mc_control::fsm::StatePtr
std::shared_ptr< State > StatePtr
Definition: State.h:172
mc_control::fsm::State::add_contacts_config_
mc_rtc::Configuration add_contacts_config_
Definition: State.h:136
mc_control::fsm::State::remove_contacts_after_config_
mc_rtc::Configuration remove_contacts_after_config_
Definition: State.h:142
mc_control::fsm::State::remove_posture_task_
mc_rtc::Configuration remove_posture_task_
Definition: State.h:156
mc_control::fsm::State::remove_collisions_after_config_
mc_rtc::Configuration remove_collisions_after_config_
Definition: State.h:150
mc_control::fsm::Controller
Definition: Controller.h:49
mc_control::fsm::State::stop
virtual void stop(Controller &)
Definition: State.h:87
api.h
mc_control::fsm::State::name
const std::string & name()
Definition: State.h:100
mc_control::fsm::State::remove_contacts_config_
mc_rtc::Configuration remove_contacts_config_
Definition: State.h:138