|
| 1 | +// Copyright (c) 2018, Joseph Mirabel |
| 2 | +// Authors: Joseph Mirabel (joseph.mirabel@laas.fr) |
| 3 | +// |
| 4 | +// This file is part of sot-core. |
| 5 | +// sot-core is free software: you can redistribute it |
| 6 | +// and/or modify it under the terms of the GNU Lesser General Public |
| 7 | +// License as published by the Free Software Foundation, either version |
| 8 | +// 3 of the License, or (at your option) any later version. |
| 9 | +// |
| 10 | +// sot-core is distributed in the hope that it will be |
| 11 | +// useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 12 | +// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | +// General Lesser Public License for more details. You should have |
| 14 | +// received a copy of the GNU Lesser General Public License along with |
| 15 | +// sot-core. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +#ifndef __SOT_EVENT_H__ |
| 18 | +# define __SOT_EVENT_H__ |
| 19 | + |
| 20 | +#include <dynamic-graph/entity.h> |
| 21 | +#include <dynamic-graph/signal.h> |
| 22 | +#include <dynamic-graph/signal-ptr.h> |
| 23 | +#include <dynamic-graph/signal-time-dependent.h> |
| 24 | +#include <dynamic-graph/pool.h> |
| 25 | +#include <dynamic-graph/command-bind.h> |
| 26 | +#include <dynamic-graph/command-getter.h> |
| 27 | + |
| 28 | +#include <sot/core/config.hh> |
| 29 | + |
| 30 | +namespace dynamicgraph { |
| 31 | + namespace sot { |
| 32 | + /// Event |
| 33 | + class SOT_CORE_DLLAPI Event : public dynamicgraph::Entity |
| 34 | + { |
| 35 | + DYNAMIC_GRAPH_ENTITY_DECL(); |
| 36 | + |
| 37 | + Event (const std::string& name) : |
| 38 | + Entity (name), |
| 39 | + checkSOUT ("Event("+name+")::output(bool)::check"), |
| 40 | + conditionSIN(NULL,"Event("+name+")::input(bool)::condition"), |
| 41 | + lastVal_ (2) // lastVal_ should be different true and false. |
| 42 | + { |
| 43 | + checkSOUT.setFunction |
| 44 | + (boost::bind (&Event::check, this, _1, _2)); |
| 45 | + signalRegistration (conditionSIN); |
| 46 | + signalRegistration (checkSOUT); |
| 47 | + |
| 48 | + using command::makeCommandVoid1; |
| 49 | + std::string docstring = |
| 50 | + "\n" |
| 51 | + " Add a signal\n"; |
| 52 | + addCommand ("addSignal", makeCommandVoid1 |
| 53 | + (*this, &Event::addSignal, docstring)); |
| 54 | + |
| 55 | + docstring = |
| 56 | + "\n" |
| 57 | + " Get list of signals\n"; |
| 58 | + addCommand ("list", new command::Getter<Event, std::string> |
| 59 | + (*this, &Event::getSignalsByName, docstring)); |
| 60 | + } |
| 61 | + |
| 62 | + ~Event () {} |
| 63 | + |
| 64 | + /// Header documentation of the python class |
| 65 | + virtual std::string getDocString () const |
| 66 | + { |
| 67 | + return |
| 68 | + "Send an event when the input changes\n\n" |
| 69 | + " The signal triggered is called whenever the condition is satisfied.\n"; |
| 70 | + } |
| 71 | + |
| 72 | + void addSignal (const std::string& signal) |
| 73 | + { |
| 74 | + std::istringstream iss (signal); |
| 75 | + triggers.push_back(&PoolStorage::getInstance()->getSignal (iss)); |
| 76 | + } |
| 77 | + |
| 78 | + // Returns the Python string representation of the list of signal names. |
| 79 | + std::string getSignalsByName () const |
| 80 | + { |
| 81 | + std::ostringstream oss; |
| 82 | + oss << "("; |
| 83 | + for (Triggers_t::const_iterator _sig = triggers.begin(); |
| 84 | + _sig != triggers.end(); ++_sig) |
| 85 | + oss << '\'' << (*_sig)->getName() << "\', "; |
| 86 | + oss << ")"; |
| 87 | + return oss.str(); |
| 88 | + } |
| 89 | + |
| 90 | + private: |
| 91 | + typedef SignalBase<int>* Trigger_t; |
| 92 | + typedef std::vector<Trigger_t> Triggers_t; |
| 93 | + |
| 94 | + bool& check (bool& ret, const int& time) |
| 95 | + { |
| 96 | + const bool& val = conditionSIN (time); |
| 97 | + ret = (val != lastVal_); |
| 98 | + if (ret) { |
| 99 | + lastVal_ = val; |
| 100 | + for (Triggers_t::const_iterator _s = triggers.begin(); |
| 101 | + _s != triggers.end(); ++_s) |
| 102 | + (*_s)->recompute (time); |
| 103 | + } |
| 104 | + return ret; |
| 105 | + } |
| 106 | + |
| 107 | + Signal <bool, int> checkSOUT; |
| 108 | + |
| 109 | + Triggers_t triggers; |
| 110 | + SignalPtr <bool, int> conditionSIN; |
| 111 | + |
| 112 | + bool lastVal_; |
| 113 | + }; |
| 114 | + } // namespace sot |
| 115 | +} // namespace dynamicgraph |
| 116 | +#endif // __SOT_EVENT_H__ |
0 commit comments