mc_rtc  2.16.0
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 
115  /* Load the observers */
116  void create(const mc_rtc::Configuration & config, double dt);
117 
118  /* Initialize based on the current robot state */
119  void reset();
120 
121  /* Run this observservation pipeline
122  *
123  * If an observer is unable to estimate the robot's state, it is expected to
124  * return false. In this case, the pipeline execution is considered invalid,
125  * and this status is reflected by the return value of this function. This
126  * state may later be retrieved by success().
127  *
128  * @return True when the pipeline exectued properly
129  * False otherwise (one or more observers failed to execute)
130  **/
131  bool run();
132 
134  inline bool runObservers() const noexcept { return runObservers_; }
135 
141  inline void runObservers(bool status) { runObservers_ = status; }
142 
145  inline bool updateObservers() const { return updateObservers_; }
146 
154  inline void updateObservers(bool status) { updateObservers_ = status; }
155 
161  inline bool success() const noexcept { return success_; }
162 
163  /* Const accessor to an observer
164  *
165  * @param name Name of the observer
166  * @throws std::runtime_error if the observer is not part of the pipeline
167  */
168  const PipelineObserver & observer(const std::string & name) const
169  {
170  auto it = std::find_if(pipelineObservers_.begin(), pipelineObservers_.end(),
171  [&name](const PipelineObserver & obs) { return obs.observer().name() == name; });
172  if(it == pipelineObservers_.end())
173  {
174  mc_rtc::log::error_and_throw("Observer pipeline \"{}\" does not have any observer named \"{}\"", name_, name);
175  }
176  return *it;
177  }
178 
179  /* Non-const variant */
180  PipelineObserver & observer(const std::string & name)
181  { return const_cast<PipelineObserver &>(static_cast<const ObserverPipeline *>(this)->observer(name)); }
182 
183  const std::vector<PipelineObserver> & observers() const { return pipelineObservers_; }
184 
185  std::vector<PipelineObserver> & observers() { return pipelineObservers_; }
186 
194  bool hasObserver(const std::string & name) const
195  {
196  return std::find_if(pipelineObservers_.begin(), pipelineObservers_.end(),
197  [&name](const PipelineObserver & obs) { return obs.observer().name() == name; })
198  != pipelineObservers_.end();
199  }
200 
210  bool hasObserverType(const std::string & type) const
211  {
212  return std::find_if(pipelineObservers_.begin(), pipelineObservers_.end(),
213  [&type](const PipelineObserver & obs) { return obs.observer().type() == type; })
214  != pipelineObservers_.end();
215  }
216 
218  inline const std::string & desc() const noexcept { return desc_; }
219 
220  /* Name used to identify this pipeline */
221  inline const std::string & name() const noexcept { return name_; }
222 
227 
228 protected:
230  std::string name_ = {"DefaultObserverPipeline"};
231  /* Short descriptive description of the observer used for CLI logging */
232  std::string desc_ = {""};
233  bool runObservers_ = true;
234  bool updateObservers_ = true;
235  bool success_ = false;
236 
238  std::vector<PipelineObserver> pipelineObservers_;
239 };
240 
241 } // 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:202
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:145
const PipelineObserver & observer(const std::string &name) const
Definition: ObserverPipeline.h:168
void runObservers(bool status)
Whether to run the observers in this pipeline.
Definition: ObserverPipeline.h:141
ObserverPipeline(mc_control::MCController &ctl, const std::string &name)
mc_control::MCController & ctl_
Definition: ObserverPipeline.h:229
void removeFromGUI(mc_rtc::gui::StateBuilder &)
void updateObservers(bool status)
Whether to update the observers in this pipeline.
Definition: ObserverPipeline.h:154
bool hasObserverType(const std::string &type) const
Checks if there is an observer of a specific type in the pipeline.
Definition: ObserverPipeline.h:210
bool runObservers() const noexcept
Definition: ObserverPipeline.h:134
std::vector< PipelineObserver > & observers()
Definition: ObserverPipeline.h:185
ObserverPipeline(mc_control::MCController &ctl)
PipelineObserver & observer(const std::string &name)
Definition: ObserverPipeline.h:180
void create(const mc_rtc::Configuration &config, double dt)
bool success() const noexcept
Checks whether the last run of the pipeline succeeded.
Definition: ObserverPipeline.h:161
void addToLogger(mc_rtc::Logger &)
const std::string & desc() const noexcept
Short description of the pipeline.
Definition: ObserverPipeline.h:218
const std::string & name() const noexcept
Definition: ObserverPipeline.h:221
void addToGUI(mc_rtc::gui::StateBuilder &)
const std::vector< PipelineObserver > & observers() const
Definition: ObserverPipeline.h:183
std::vector< PipelineObserver > pipelineObservers_
Definition: ObserverPipeline.h:238
bool hasObserver(const std::string &name) const
Checks whether this pipeline has an observer.
Definition: ObserverPipeline.h:194
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