mc_rtc  2.17.1
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  { return const_cast<T &>(static_cast<const PipelineObserver *>(this)->observer<T>()); }
87 
88  bool update() const noexcept { return update_; }
89 
90  bool log() const noexcept { return log_; }
91 
92  bool gui() const noexcept { return gui_; }
93 
95  bool success() const noexcept { return success_; }
96 
100  bool successRequired() const noexcept { return successRequired_; }
101 
102  protected:
103  ObserverPtr observer_ = nullptr; //< Observer
104  bool update_ = true; //< Whether to update the real robot instance from this observer
105  bool log_ = true; //< Whether to log this observer
106  bool gui_ = true; //< Whether to display the gui
107  bool successRequired_ = true; //< Whether this observer must succeed or is allowed to fail
108  bool success_ = true; //< Whether this observer succeeded
109  };
110 
111  ObserverPipeline(mc_control::MCController & ctl, const std::string & name);
113  ~ObserverPipeline() = default;
114 
144  void create(const mc_rtc::Configuration & config, double dt);
145 
146  /* Initialize based on the current robot state */
147  void reset();
148 
149  /* Run this observservation pipeline
150  *
151  * If an observer is unable to estimate the robot's state, it is expected to
152  * return false. In this case, the pipeline execution is considered invalid,
153  * and this status is reflected by the return value of this function. This
154  * state may later be retrieved by success().
155  *
156  * @return True when the pipeline exectued properly
157  * False otherwise (one or more observers failed to execute)
158  **/
159  bool run();
160 
162  inline bool runObservers() const noexcept { return runObservers_; }
163 
169  inline void runObservers(bool status) { runObservers_ = status; }
170 
173  inline bool updateObservers() const { return updateObservers_; }
174 
182  inline void updateObservers(bool status) { updateObservers_ = status; }
183 
189  inline bool success() const noexcept { return success_; }
190 
191  /* Const accessor to an observer
192  *
193  * @param name Name of the observer
194  * @throws std::runtime_error if the observer is not part of the pipeline
195  */
196  const PipelineObserver & observer(const std::string & name) const
197  {
198  auto it = std::find_if(pipelineObservers_.begin(), pipelineObservers_.end(),
199  [&name](const PipelineObserver & obs) { return obs.observer().name() == name; });
200  if(it == pipelineObservers_.end())
201  {
202  mc_rtc::log::error_and_throw("Observer pipeline \"{}\" does not have any observer named \"{}\"", name_, name);
203  }
204  return *it;
205  }
206 
207  /* Non-const variant */
208  PipelineObserver & observer(const std::string & name)
209  { return const_cast<PipelineObserver &>(static_cast<const ObserverPipeline *>(this)->observer(name)); }
210 
211  const std::vector<PipelineObserver> & observers() const { return pipelineObservers_; }
212 
213  std::vector<PipelineObserver> & observers() { return pipelineObservers_; }
214 
222  bool hasObserver(const std::string & name) const
223  {
224  return std::find_if(pipelineObservers_.begin(), pipelineObservers_.end(),
225  [&name](const PipelineObserver & obs) { return obs.observer().name() == name; })
226  != pipelineObservers_.end();
227  }
228 
238  bool hasObserverType(const std::string & type) const
239  {
240  return std::find_if(pipelineObservers_.begin(), pipelineObservers_.end(),
241  [&type](const PipelineObserver & obs) { return obs.observer().type() == type; })
242  != pipelineObservers_.end();
243  }
244 
246  inline const std::string & desc() const noexcept { return desc_; }
247 
248  /* Name used to identify this pipeline */
249  inline const std::string & name() const noexcept { return name_; }
250 
255 
256 protected:
258  std::string name_ = {"DefaultObserverPipeline"};
259  /* Short descriptive description of the observer used for CLI logging */
260  std::string desc_ = {""};
261  bool runObservers_ = true;
262  bool updateObservers_ = true;
263  bool success_ = false;
264 
266  std::vector<PipelineObserver> pipelineObservers_;
267 };
268 
269 } // namespace mc_observers
#define MC_OBSERVERS_DLLAPI
Definition: api.h:47
Definition: CompletionCriteria.h:11
Definition: BodySensorObserver.h:14
std::shared_ptr< mc_observers::Observer > ObserverPtr
Definition: Observer.h:212
Definition: generic_gripper.h:15
void error_and_throw(const S &format, Args &&... args)
Definition: logging.h:40
MCController is the base class to implement all controllers. It assumes that at least two robots are ...
Definition: MCController.h:99
Definition: ObserverPipeline.h:41
PipelineObserver(const mc_observers::ObserverPtr &observer, const mc_rtc::Configuration &config)
Definition: ObserverPipeline.h:44
bool update() const noexcept
Definition: ObserverPipeline.h:88
bool log() const noexcept
Definition: ObserverPipeline.h:90
bool successRequired() const noexcept
Definition: ObserverPipeline.h:100
T & observer()
Definition: ObserverPipeline.h:85
const Observer & observer() const
Definition: ObserverPipeline.h:58
bool gui() const noexcept
Definition: ObserverPipeline.h:92
bool success() const noexcept
Definition: ObserverPipeline.h:95
Observer & observer()
Definition: ObserverPipeline.h:61
const T & observer() const
Definition: ObserverPipeline.h:73
State observation pipeline.
Definition: ObserverPipeline.h:38
bool updateObservers() const
Definition: ObserverPipeline.h:173
const PipelineObserver & observer(const std::string &name) const
Definition: ObserverPipeline.h:196
void runObservers(bool status)
Whether to run the observers in this pipeline.
Definition: ObserverPipeline.h:169
ObserverPipeline(mc_control::MCController &ctl, const std::string &name)
mc_control::MCController & ctl_
Definition: ObserverPipeline.h:257
void removeFromGUI(mc_rtc::gui::StateBuilder &)
void updateObservers(bool status)
Whether to update the observers in this pipeline.
Definition: ObserverPipeline.h:182
bool hasObserverType(const std::string &type) const
Checks if there is an observer of a specific type in the pipeline.
Definition: ObserverPipeline.h:238
bool runObservers() const noexcept
Definition: ObserverPipeline.h:162
std::vector< PipelineObserver > & observers()
Definition: ObserverPipeline.h:213
ObserverPipeline(mc_control::MCController &ctl)
PipelineObserver & observer(const std::string &name)
Definition: ObserverPipeline.h:208
void create(const mc_rtc::Configuration &config, double dt)
Load the observers and their configuration.
bool success() const noexcept
Checks whether the last run of the pipeline succeeded.
Definition: ObserverPipeline.h:189
void addToLogger(mc_rtc::Logger &)
const std::string & desc() const noexcept
Short description of the pipeline.
Definition: ObserverPipeline.h:246
const std::string & name() const noexcept
Definition: ObserverPipeline.h:249
void addToGUI(mc_rtc::gui::StateBuilder &)
const std::vector< PipelineObserver > & observers() const
Definition: ObserverPipeline.h:211
std::vector< PipelineObserver > pipelineObservers_
Definition: ObserverPipeline.h:266
bool hasObserver(const std::string &name) const
Checks whether this pipeline has an observer.
Definition: ObserverPipeline.h:222
void removeFromLogger(mc_rtc::Logger &)
State observation API.
Definition: Observer.h:51
Simplify access to values hold within a JSON file.
Definition: Configuration.h:166
Logs controller data to disk.
Definition: Logger.h:30
Definition: StateBuilder.h:28