ObserverPipeline.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 
8 #include <mc_observers/api.h>
10 #include <mc_rtc/log/Logger.h>
11 #include <mc_rtc/type_name.h>
12 
13 namespace mc_control
14 {
15 struct MCController;
16 } // namespace mc_control
17 
18 namespace mc_rbdyn
19 {
20 struct Robots;
21 }
22 
23 namespace mc_observers
24 {
25 
38 {
41  {
42  friend struct ObserverPipeline;
43 
45  : observer_(observer)
46  {
47  config("update", update_);
48  config("log", log_);
49  config("gui", gui_);
50  config("successRequired", successRequired_);
51  }
52 
53  /* Const accessor to an observer generic interface
54  *
55  * @param name Name of the observer
56  * @throws std::runtime_error if the observer is not part of the pipeline
57  */
58  const Observer & observer() const { return *observer_; }
59 
60  /* Non-const variant */
61  Observer & observer() { return const_cast<Observer &>(static_cast<const PipelineObserver *>(this)->observer()); }
62 
72  template<typename T>
73  const T & observer() const
74  {
75  auto ptr = dynamic_cast<T *>(observer_.get());
76  if(!ptr)
77  {
78  mc_rtc::log::error_and_throw("{} observer type did not match the requested one: {}", observer_->type(),
79  mc_rtc::type_name<T>());
80  }
81  return *ptr;
82  }
83 
84  template<typename T>
85  T & observer()
86  {
87  return const_cast<T &>(static_cast<const PipelineObserver *>(this)->observer<T>());
88  }
89 
90  bool update() const noexcept { return update_; }
91 
92  bool log() const noexcept { return log_; }
93 
94  bool gui() const noexcept { return gui_; }
95 
97  bool success() const noexcept { return success_; }
98 
102  bool successRequired() const noexcept { return successRequired_; }
103 
104  protected:
105  ObserverPtr observer_ = nullptr; //< Observer
106  bool update_ = true; //< Whether to update the real robot instance from this observer
107  bool log_ = true; //< Whether to log this observer
108  bool gui_ = true; //< Whether to display the gui
109  bool successRequired_ = true; //< Whether this observer must succeed or is allowed to fail
110  bool success_ = true; //< Whether this observer succeeded
111  };
112 
113  ObserverPipeline(mc_control::MCController & ctl, const std::string & name);
115  ~ObserverPipeline() = default;
116 
117  /* Load the observers */
118  void create(const mc_rtc::Configuration & config, double dt);
119 
120  /* Initialize based on the current robot state */
121  void reset();
122 
123  /* Run this observservation pipeline
124  *
125  * If an observer is unable to estimate the robot's state, it is expected to
126  * return false. In this case, the pipeline execution is considered invalid,
127  * and this status is reflected by the return value of this function. This
128  * state may later be retrieved by success().
129  *
130  * @return True when the pipeline exectued properly
131  * False otherwise (one or more observers failed to execute)
132  **/
133  bool run();
134 
136  inline bool runObservers() const noexcept { return runObservers_; }
137 
143  inline void runObservers(bool status) { runObservers_ = status; }
144 
147  inline bool updateObservers() const { return updateObservers_; }
148 
156  inline void updateObservers(bool status) { updateObservers_ = status; }
157 
163  inline bool success() const noexcept { return success_; }
164 
165  /* Const accessor to an observer
166  *
167  * @param name Name of the observer
168  * @throws std::runtime_error if the observer is not part of the pipeline
169  */
170  const PipelineObserver & observer(const std::string & name) const
171  {
172  auto it = std::find_if(pipelineObservers_.begin(), pipelineObservers_.end(),
173  [&name](const PipelineObserver & obs) { return obs.observer().name() == name; });
174  if(it == pipelineObservers_.end())
175  {
176  mc_rtc::log::error_and_throw("Observer pipeline \"{}\" does not have any observer named \"{}\"", name_, name);
177  }
178  return *it;
179  }
180 
181  /* Non-const variant */
182  PipelineObserver & observer(const std::string & name)
183  {
184  return const_cast<PipelineObserver &>(static_cast<const ObserverPipeline *>(this)->observer(name));
185  }
186 
187  const std::vector<PipelineObserver> & observers() const { return pipelineObservers_; }
188 
189  std::vector<PipelineObserver> & observers() { return pipelineObservers_; }
190 
198  bool hasObserver(const std::string & name) const
199  {
200  return std::find_if(pipelineObservers_.begin(), pipelineObservers_.end(),
201  [&name](const PipelineObserver & obs) { return obs.observer().name() == name; })
202  != pipelineObservers_.end();
203  }
204 
214  bool hasObserverType(const std::string & type) const
215  {
216  return std::find_if(pipelineObservers_.begin(), pipelineObservers_.end(),
217  [&type](const PipelineObserver & obs) { return obs.observer().type() == type; })
218  != pipelineObservers_.end();
219  }
220 
222  inline const std::string & desc() const noexcept { return desc_; }
223 
224  /* Name used to identify this pipeline */
225  inline const std::string & name() const noexcept { return name_; }
226 
227  void addToLogger(mc_rtc::Logger &);
228  void removeFromLogger(mc_rtc::Logger &);
229  void addToGUI(mc_rtc::gui::StateBuilder &);
230  void removeFromGUI(mc_rtc::gui::StateBuilder &);
231 
232 protected:
234  std::string name_ = {"DefaultObserverPipeline"};
235  /* Short descriptive description of the observer used for CLI logging */
236  std::string desc_ = {""};
237  bool runObservers_ = true;
238  bool updateObservers_ = true;
239  bool success_ = false;
240 
242  std::vector<PipelineObserver> pipelineObservers_;
243 };
244 
245 } // namespace mc_observers
mc_rtc::Configuration
Simplify access to values hold within a JSON file.
Definition: Configuration.h:165
mc_observers::ObserverPipeline::hasObserver
bool hasObserver(const std::string &name) const
Checks whether this pipeline has an observer.
Definition: ObserverPipeline.h:198
mc_observers::ObserverPipeline::observer
PipelineObserver & observer(const std::string &name)
Definition: ObserverPipeline.h:182
mc_observers::ObserverPipeline::runObservers
bool runObservers() const noexcept
Definition: ObserverPipeline.h:136
MC_OBSERVERS_DLLAPI
#define MC_OBSERVERS_DLLAPI
Definition: api.h:47
type_name.h
mc_observers::ObserverPipeline::desc
const std::string & desc() const noexcept
Short description of the pipeline.
Definition: ObserverPipeline.h:222
mc_observers::ObserverPipeline::PipelineObserver::observer
T & observer()
Definition: ObserverPipeline.h:85
Observer.h
mc_observers::ObserverPipeline::hasObserverType
bool hasObserverType(const std::string &type) const
Checks if there is an observer of a specific type in the pipeline.
Definition: ObserverPipeline.h:214
mc_observers::ObserverPipeline::PipelineObserver::observer
Observer & observer()
Definition: ObserverPipeline.h:61
mc_observers::ObserverPipeline::observers
std::vector< PipelineObserver > & observers()
Definition: ObserverPipeline.h:189
mc_observers::ObserverPtr
std::shared_ptr< mc_observers::Observer > ObserverPtr
Definition: Observer.h:206
mc_rtc::Logger
Logs controller data to disk.
Definition: Logger.h:29
StateBuilder.h
mc_observers::ObserverPipeline
State observation pipeline.
Definition: ObserverPipeline.h:37
mc_observers::ObserverPipeline::pipelineObservers_
std::vector< PipelineObserver > pipelineObservers_
Definition: ObserverPipeline.h:242
mc_observers::ObserverPipeline::ctl_
mc_control::MCController & ctl_
Definition: ObserverPipeline.h:233
Logger.h
mc_observers::ObserverPipeline::PipelineObserver::update
bool update() const noexcept
Definition: ObserverPipeline.h:90
mc_observers::Observer
State observation API.
Definition: Observer.h:50
mc_rtc::gui::StateBuilder
Definition: StateBuilder.h:27
mc_observers::ObserverPipeline::name
const std::string & name() const noexcept
Definition: ObserverPipeline.h:225
mc_observers::ObserverPipeline::PipelineObserver::PipelineObserver
PipelineObserver(const mc_observers::ObserverPtr &observer, const mc_rtc::Configuration &config)
Definition: ObserverPipeline.h:44
mc_rtc::log::error_and_throw
void error_and_throw(Args &&... args)
Definition: logging.h:47
mc_observers::ObserverPipeline::observer
const PipelineObserver & observer(const std::string &name) const
Definition: ObserverPipeline.h:170
mc_observers::ObserverPipeline::PipelineObserver::success
bool success() const noexcept
Definition: ObserverPipeline.h:97
mc_observers::ObserverPipeline::runObservers
void runObservers(bool status)
Whether to run the observers in this pipeline.
Definition: ObserverPipeline.h:143
mc_observers::ObserverPipeline::PipelineObserver
Definition: ObserverPipeline.h:40
mc_observers
Definition: BodySensorObserver.h:13
mc_observers::ObserverPipeline::PipelineObserver::observer
const Observer & observer() const
Definition: ObserverPipeline.h:58
mc_observers::ObserverPipeline::PipelineObserver::log
bool log() const noexcept
Definition: ObserverPipeline.h:92
mc_observers::ObserverPipeline::PipelineObserver::observer
const T & observer() const
Definition: ObserverPipeline.h:73
mc_observers::ObserverPipeline::updateObservers
void updateObservers(bool status)
Whether to update the observers in this pipeline.
Definition: ObserverPipeline.h:156
mc_observers::ObserverPipeline::updateObservers
bool updateObservers() const
Definition: ObserverPipeline.h:147
mc_observers::ObserverPipeline::PipelineObserver::successRequired
bool successRequired() const noexcept
Definition: ObserverPipeline.h:102
mc_control
Definition: CompletionCriteria.h:10
mc_observers::ObserverPipeline::PipelineObserver::gui
bool gui() const noexcept
Definition: ObserverPipeline.h:94
mc_rbdyn
Definition: generic_gripper.h:14
mc_observers::ObserverPipeline::observers
const std::vector< PipelineObserver > & observers() const
Definition: ObserverPipeline.h:187
mc_observers::ObserverPipeline::success
bool success() const noexcept
Checks whether the last run of the pipeline succeeded.
Definition: ObserverPipeline.h:163
mc_control::MCController
MCController is the base class to implement all controllers. It assumes that at least two robots are ...
Definition: MCController.h:98
api.h