Observer.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 
9 #include <mc_observers/api.h>
11 #include <mc_rtc/log/Logger.h>
12 
13 namespace mc_rtc
14 {
15 namespace gui
16 {
17 struct StateBuilder;
18 } // namespace gui
19 } // namespace mc_rtc
20 
21 namespace mc_control
22 {
23 struct MCController;
24 } // namespace mc_control
25 
26 namespace mc_rbdyn
27 {
28 
29 struct Robots;
30 }
31 
32 namespace mc_observers
33 {
34 
51 {
52  Observer(const std::string & type, double dt) : type_(type), dt_(dt) {}
53  virtual ~Observer() = default;
54 
62  virtual void configure(const mc_control::MCController & /*ctl*/, const mc_rtc::Configuration & /*config*/) {}
63 
71  virtual void reset(const mc_control::MCController & ctl) = 0;
72 
77  virtual bool run(const mc_control::MCController & ctl) = 0;
78 
91  virtual void update(mc_control::MCController & ctl) = 0;
92 
98  inline void name(const std::string & name) { name_ = name; }
99 
103  inline const std::string & name() const noexcept { return name_; }
104 
106  void addToLogger_(const mc_control::MCController & ctl, mc_rtc::Logger & logger, const std::string & category = "")
107  {
108  addToLogger(ctl, logger, category + "_" + name_);
109  }
110 
112  void removeFromLogger_(mc_rtc::Logger & logger, const std::string & category = "")
113  {
114  removeFromLogger(logger, category + "_" + name_);
115  }
116 
120  std::vector<std::string> category = {})
121  {
122  category.push_back(name_);
123  addToGUI(ctl, gui, category);
124  }
125 
130  void removeFromGUI_(mc_rtc::gui::StateBuilder & gui, std::vector<std::string> category = {})
131  {
132  category.push_back(name_);
133  removeFromGUI(gui, category);
134  };
135 
146  inline const std::string & desc() const noexcept { return desc_; }
147 
148  inline const std::string type() const noexcept { return type_; }
149 
151  inline const std::string & error() const noexcept { return error_; }
152 
154  inline double dt() const noexcept { return dt_; }
155 
156 protected:
164  virtual void addToLogger(const mc_control::MCController &, mc_rtc::Logger &, const std::string & /* category */) {}
165 
172  virtual void removeFromLogger(mc_rtc::Logger &, const std::string & /* category */);
173 
181  virtual void addToGUI(const mc_control::MCController &,
183  const std::vector<std::string> & /* category */)
184  {
185  }
186 
195  virtual void removeFromGUI(mc_rtc::gui::StateBuilder &, const std::vector<std::string> & category);
196 
197 protected:
198  std::string name_;
199  std::string type_;
200  std::string desc_;
201  std::string error_;
202 
203  double dt_;
204 };
205 
206 using ObserverPtr = std::shared_ptr<mc_observers::Observer>;
207 
208 } // namespace mc_observers
209 
210 #ifdef WIN32
211 # define OBSERVER_MODULE_API __declspec(dllexport)
212 #else
213 # if __GNUC__ >= 4
214 # define OBSERVER_MODULE_API __attribute__((visibility("default")))
215 # else
216 # define OBSERVER_MODULE_API
217 # endif
218 #endif
mc_rtc::Configuration
Simplify access to values hold within a JSON file.
Definition: Configuration.h:165
mc_observers::Observer::type_
std::string type_
Observer type.
Definition: Observer.h:199
mc_observers::Observer::type
const std::string type() const noexcept
Definition: Observer.h:148
MC_OBSERVERS_DLLAPI
#define MC_OBSERVERS_DLLAPI
Definition: api.h:47
mc_observers::Observer::name
void name(const std::string &name)
Set the observer's name.
Definition: Observer.h:98
mc_observers::Observer::dt
double dt() const noexcept
Definition: Observer.h:154
mc_observers::Observer::addToGUI
virtual void addToGUI(const mc_control::MCController &, mc_rtc::gui::StateBuilder &, const std::vector< std::string > &)
Add observer information the GUI.
Definition: Observer.h:181
mc_observers::Observer::desc
const std::string & desc() const noexcept
Short description of the observer.
Definition: Observer.h:146
mc_observers::Observer::name_
std::string name_
Observer name.
Definition: Observer.h:198
mc_observers::Observer::error
const std::string & error() const noexcept
Definition: Observer.h:151
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
mc_observers::Observer::name
const std::string & name() const noexcept
Returns the observer's name.
Definition: Observer.h:103
StateBuilder.h
mc_observers::Observer::desc_
std::string desc_
Short description of the observer and its configuration.
Definition: Observer.h:200
mc_observers::Observer::addToGUI_
void addToGUI_(const mc_control::MCController &ctl, mc_rtc::gui::StateBuilder &gui, std::vector< std::string > category={})
Add observer to the gui under category {category, name()}.
Definition: Observer.h:118
Logger.h
mc_observers::Observer
State observation API.
Definition: Observer.h:50
mc_rtc::gui::StateBuilder
Definition: StateBuilder.h:27
mc_observers::Observer::removeFromLogger_
void removeFromLogger_(mc_rtc::Logger &logger, const std::string &category="")
Remove observer from logger.
Definition: Observer.h:112
mc_observers
Definition: BodySensorObserver.h:13
mc_observers::Observer::dt_
double dt_
Timestep.
Definition: Observer.h:203
mc_observers::Observer::addToLogger_
void addToLogger_(const mc_control::MCController &ctl, mc_rtc::Logger &logger, const std::string &category="")
Add observer entries to the logger under the categrory "category + name()".
Definition: Observer.h:106
mc_observers::Observer::addToLogger
virtual void addToLogger(const mc_control::MCController &, mc_rtc::Logger &, const std::string &)
Add observer from logger.
Definition: Observer.h:164
mc_control
Definition: CompletionCriteria.h:10
mc_rbdyn
Definition: generic_gripper.h:14
mc_observers::Observer::Observer
Observer(const std::string &type, double dt)
Definition: Observer.h:52
mc_observers::Observer::configure
virtual void configure(const mc_control::MCController &, const mc_rtc::Configuration &)
Configure observer.
Definition: Observer.h:62
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
mc_observers::Observer::removeFromGUI_
void removeFromGUI_(mc_rtc::gui::StateBuilder &gui, std::vector< std::string > category={})
Remove observer from gui.
Definition: Observer.h:130
mc_rtc
Definition: Contact.h:87
mc_observers::Observer::error_
std::string error_
Descriptive error message to show if the observer failed.
Definition: Observer.h:201