mc_rtc  2.16.0
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  { addToLogger(ctl, logger, category + "_" + name_); }
108 
110  void removeFromLogger_(mc_rtc::Logger & logger, const std::string & category = "")
111  { removeFromLogger(logger, category + "_" + name_); }
112 
116  std::vector<std::string> category = {})
117  {
118  category.push_back(name_);
119  addToGUI(ctl, gui, category);
120  }
121 
126  void removeFromGUI_(mc_rtc::gui::StateBuilder & gui, std::vector<std::string> category = {})
127  {
128  category.push_back(name_);
129  removeFromGUI(gui, category);
130  };
131 
142  inline const std::string & desc() const noexcept { return desc_; }
143 
144  inline const std::string type() const noexcept { return type_; }
145 
147  inline const std::string & error() const noexcept { return error_; }
148 
150  inline double dt() const noexcept { return dt_; }
151 
152 protected:
160  virtual void addToLogger(const mc_control::MCController &, mc_rtc::Logger &, const std::string & /* category */) {}
161 
168  virtual void removeFromLogger(mc_rtc::Logger &, const std::string & /* category */);
169 
177  virtual void addToGUI(const mc_control::MCController &,
179  const std::vector<std::string> & /* category */)
180  {
181  }
182 
191  virtual void removeFromGUI(mc_rtc::gui::StateBuilder &, const std::vector<std::string> & category);
192 
193 protected:
194  std::string name_;
195  std::string type_;
196  std::string desc_;
197  std::string error_;
198 
199  double dt_;
200 };
201 
202 using ObserverPtr = std::shared_ptr<mc_observers::Observer>;
203 
204 } // namespace mc_observers
205 
206 #ifdef WIN32
207 # define OBSERVER_MODULE_API __declspec(dllexport)
208 #else
209 # if __GNUC__ >= 4
210 # define OBSERVER_MODULE_API __attribute__((visibility("default")))
211 # else
212 # define OBSERVER_MODULE_API
213 # endif
214 #endif
#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
Definition: Contact.h:88
MCController is the base class to implement all controllers. It assumes that at least two robots are ...
Definition: MCController.h:99
State observation API.
Definition: Observer.h:51
const std::string & desc() const noexcept
Short description of the observer.
Definition: Observer.h:142
std::string desc_
Short description of the observer and its configuration.
Definition: Observer.h:196
Observer(const std::string &type, double dt)
Definition: Observer.h:52
const std::string type() const noexcept
Definition: Observer.h:144
std::string name_
Observer name.
Definition: Observer.h:194
void removeFromLogger_(mc_rtc::Logger &logger, const std::string &category="")
Remove observer from logger.
Definition: Observer.h:110
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:114
virtual void removeFromGUI(mc_rtc::gui::StateBuilder &, const std::vector< std::string > &category)
Remove observer from GUI.
virtual bool run(const mc_control::MCController &ctl)=0
Compute observer state.
virtual ~Observer()=default
virtual void addToGUI(const mc_control::MCController &, mc_rtc::gui::StateBuilder &, const std::vector< std::string > &)
Add observer information the GUI.
Definition: Observer.h:177
std::string error_
Descriptive error message to show if the observer failed.
Definition: Observer.h:197
void name(const std::string &name)
Set the observer's name.
Definition: Observer.h:98
virtual void reset(const mc_control::MCController &ctl)=0
Reset estimator.
virtual void configure(const mc_control::MCController &, const mc_rtc::Configuration &)
Configure observer.
Definition: Observer.h:62
virtual void update(mc_control::MCController &ctl)=0
Update the real robot state from the observed state.
const std::string & error() const noexcept
Definition: Observer.h:147
virtual void removeFromLogger(mc_rtc::Logger &, const std::string &)
Remove observer from logger.
virtual void addToLogger(const mc_control::MCController &, mc_rtc::Logger &, const std::string &)
Add observer from logger.
Definition: Observer.h:160
void removeFromGUI_(mc_rtc::gui::StateBuilder &gui, std::vector< std::string > category={})
Remove observer from gui.
Definition: Observer.h:126
double dt() const noexcept
Definition: Observer.h:150
double dt_
Timestep.
Definition: Observer.h:199
std::string type_
Observer type.
Definition: Observer.h:195
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
const std::string & name() const noexcept
Returns the observer's name.
Definition: Observer.h:103
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