sot-core  4.11.8
Hierarchical task solver plug-in for dynamic-graph.
event.hh
Go to the documentation of this file.
1 // Copyright (c) 2018, Joseph Mirabel
2 // Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3 
4 #ifndef __SOT_EVENT_H__
5 #define __SOT_EVENT_H__
6 
7 #include <dynamic-graph/command-bind.h>
8 #include <dynamic-graph/command-getter.h>
9 #include <dynamic-graph/command-setter.h>
10 #include <dynamic-graph/entity.h>
11 #include <dynamic-graph/pool.h>
12 #include <dynamic-graph/signal-ptr.h>
13 #include <dynamic-graph/signal-time-dependent.h>
14 #include <dynamic-graph/signal.h>
15 
16 #include <sot/core/config.hh>
17 
18 namespace dynamicgraph {
19 namespace sot {
43 
44 class SOT_CORE_DLLAPI Event : public dynamicgraph::Entity {
45  DYNAMIC_GRAPH_ENTITY_DECL();
46 
47  Event(const std::string &name)
48  : Entity(name),
49  checkSOUT("Event(" + name + ")::output(bool)::check"),
50  conditionSIN(NULL, "Event(" + name + ")::input(bool)::condition"),
51  lastVal_(2), // lastVal_ should be different true and false.
52  timeSinceUp_(0),
53  repeatAfterNIterations_(0) {
54  checkSOUT.setFunction(boost::bind(&Event::check, this, _1, _2));
55  signalRegistration(conditionSIN);
56  signalRegistration(checkSOUT);
57 
58  using command::makeCommandVoid1;
59  std::string docstring =
60  "\n"
61  " Add a signal\n";
62  addCommand("addSignal",
63  makeCommandVoid1(*this, &Event::addSignal, docstring));
64 
65  docstring =
66  "\n"
67  " Get list of signals\n";
68  addCommand("list", new command::Getter<Event, std::string>(
69  *this, &Event::getSignalsByName, docstring));
70 
71  docstring =
72  "\n"
73  " Repease event if input signal remains True for a while\n"
74  " Input: number of iterations before repeating output\n."
75  " 0 for no repetition";
76  addCommand("repeat", new command::Setter<Event, int>(*this, &Event::repeat,
77  docstring));
78  }
79 
80  ~Event() {}
81 
83  virtual std::string getDocString() const {
84  return "Send an event when the input changes\n\n"
85  " The signal triggered is called whenever the condition is "
86  "satisfied.\n";
87  }
88 
89  void addSignal(const std::string &signal) {
90  std::istringstream iss(signal);
91  triggers.push_back(&PoolStorage::getInstance()->getSignal(iss));
92  }
93 
94  // Returns the Python string representation of the list of signal names.
95  std::string getSignalsByName() const {
96  std::ostringstream oss;
97  oss << "(";
98  for (Triggers_t::const_iterator _sig = triggers.begin();
99  _sig != triggers.end(); ++_sig)
100  oss << '\'' << (*_sig)->getName() << "\', ";
101  oss << ")";
102  return oss.str();
103  }
104 
105  void repeat(const int &nbIterations) {
106  repeatAfterNIterations_ = nbIterations;
107  }
108 
109  private:
110  typedef SignalBase<int> *Trigger_t;
111  typedef std::vector<Trigger_t> Triggers_t;
112 
113  bool &check(bool &ret, const int &time);
114 
115  Signal<bool, int> checkSOUT;
116 
117  Triggers_t triggers;
118  SignalPtr<bool, int> conditionSIN;
119 
120  bool lastVal_;
121  int timeSinceUp_, repeatAfterNIterations_;
122 };
123 } // namespace sot
124 } // namespace dynamicgraph
125 #endif // __SOT_EVENT_H__
dynamicgraph
Definition: abstract-sot-external-interface.hh:17
dynamicgraph::sot::PoolStorage::getInstance
static PoolStorage * getInstance()
Get unique instance of the class.
SOT_CORE_DLLAPI
#define SOT_CORE_DLLAPI
Definition: config.hh:64
dynamicgraph::sot::Event
Definition: event.hh:44
config.hh