Skip to content
Snippets Groups Projects
Unverified Commit 98bec86a authored by Florent Lamiraux's avatar Florent Lamiraux Committed by GitHub
Browse files

Merge pull request #197 from florent-lamiraux/devel

[Event] Allow to periodically broadcast event when input remains to t…
parents df86d2a9 bbbed270
No related branches found
No related tags found
No related merge requests found
Pipeline #20252 failed
...@@ -45,7 +45,8 @@ class SOT_CORE_DLLAPI Event : public dynamicgraph::Entity { ...@@ -45,7 +45,8 @@ class SOT_CORE_DLLAPI Event : public dynamicgraph::Entity {
Event(const std::string &name) Event(const std::string &name)
: Entity(name), checkSOUT("Event(" + name + ")::output(bool)::check"), : Entity(name), checkSOUT("Event(" + name + ")::output(bool)::check"),
conditionSIN(NULL, "Event(" + name + ")::input(bool)::condition"), conditionSIN(NULL, "Event(" + name + ")::input(bool)::condition"),
lastVal_(2) // lastVal_ should be different true and false. lastVal_(2), // lastVal_ should be different true and false.
timeSinceUp_(0), repeatAfterNIterations_(0)
{ {
checkSOUT.setFunction(boost::bind(&Event::check, this, _1, _2)); checkSOUT.setFunction(boost::bind(&Event::check, this, _1, _2));
signalRegistration(conditionSIN); signalRegistration(conditionSIN);
...@@ -63,10 +64,12 @@ class SOT_CORE_DLLAPI Event : public dynamicgraph::Entity { ...@@ -63,10 +64,12 @@ class SOT_CORE_DLLAPI Event : public dynamicgraph::Entity {
*this, &Event::getSignalsByName, docstring)); *this, &Event::getSignalsByName, docstring));
docstring = docstring =
"\n" "\n"
" Triggers an event only when condition goes from False to True\n"; " Repease event if input signal remains True for a while\n"
addCommand("setOnlyUp", new command::Setter<Event, bool>( " Input: number of iterations before repeating output\n."
*this, &Event::setOnlyUp, docstring)); " 0 for no repetition";
addCommand("repeat", new command::Setter<Event, int>(*this, &Event::repeat,
docstring));
} }
~Event() {} ~Event() {}
...@@ -94,8 +97,10 @@ class SOT_CORE_DLLAPI Event : public dynamicgraph::Entity { ...@@ -94,8 +97,10 @@ class SOT_CORE_DLLAPI Event : public dynamicgraph::Entity {
return oss.str(); return oss.str();
} }
void setOnlyUp(const bool &up) { onlyUp_ = up; } void repeat(const int& nbIterations)
{
repeatAfterNIterations_ = nbIterations;
}
private: private:
typedef SignalBase<int> *Trigger_t; typedef SignalBase<int> *Trigger_t;
typedef std::vector<Trigger_t> Triggers_t; typedef std::vector<Trigger_t> Triggers_t;
...@@ -107,7 +112,8 @@ private: ...@@ -107,7 +112,8 @@ private:
Triggers_t triggers; Triggers_t triggers;
SignalPtr<bool, int> conditionSIN; SignalPtr<bool, int> conditionSIN;
bool lastVal_, onlyUp_; bool lastVal_;
int timeSinceUp_, repeatAfterNIterations_;
}; };
} // namespace sot } // namespace sot
} // namespace dynamicgraph } // namespace dynamicgraph
......
...@@ -11,14 +11,23 @@ namespace sot { ...@@ -11,14 +11,23 @@ namespace sot {
bool &Event::check(bool &ret, const int &time) { bool &Event::check(bool &ret, const int &time) {
const bool &val = conditionSIN(time); const bool &val = conditionSIN(time);
ret = (val != lastVal_); ret = (val != lastVal_);
bool trigger = onlyUp_ ? (!lastVal_ && val) : ret; bool up = (!lastVal_ && val);
if (up) {
timeSinceUp_ = 0;
} else if (val) {
++timeSinceUp_;
}
// If repetition is activated, trigger again after given number of iterations
bool trigger = up || (repeatAfterNIterations_ > 0 &&
timeSinceUp_ >= repeatAfterNIterations_);
if (ret) { if (ret) {
lastVal_ = val; lastVal_ = val;
if (trigger) { }
for (Triggers_t::const_iterator _s = triggers.begin(); if (trigger) {
_s != triggers.end(); ++_s) for (Triggers_t::const_iterator _s = triggers.begin();
(*_s)->recompute(time); _s != triggers.end(); ++_s)
} (*_s)->recompute(time);
timeSinceUp_ = 0;
} }
return ret; return ret;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment