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 {
Event(const std::string &name)
: Entity(name), checkSOUT("Event(" + name + ")::output(bool)::check"),
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));
signalRegistration(conditionSIN);
......@@ -63,10 +64,12 @@ class SOT_CORE_DLLAPI Event : public dynamicgraph::Entity {
*this, &Event::getSignalsByName, docstring));
docstring =
"\n"
" Triggers an event only when condition goes from False to True\n";
addCommand("setOnlyUp", new command::Setter<Event, bool>(
*this, &Event::setOnlyUp, docstring));
"\n"
" Repease event if input signal remains True for a while\n"
" Input: number of iterations before repeating output\n."
" 0 for no repetition";
addCommand("repeat", new command::Setter<Event, int>(*this, &Event::repeat,
docstring));
}
~Event() {}
......@@ -94,8 +97,10 @@ class SOT_CORE_DLLAPI Event : public dynamicgraph::Entity {
return oss.str();
}
void setOnlyUp(const bool &up) { onlyUp_ = up; }
void repeat(const int& nbIterations)
{
repeatAfterNIterations_ = nbIterations;
}
private:
typedef SignalBase<int> *Trigger_t;
typedef std::vector<Trigger_t> Triggers_t;
......@@ -107,7 +112,8 @@ private:
Triggers_t triggers;
SignalPtr<bool, int> conditionSIN;
bool lastVal_, onlyUp_;
bool lastVal_;
int timeSinceUp_, repeatAfterNIterations_;
};
} // namespace sot
} // namespace dynamicgraph
......
......@@ -11,14 +11,23 @@ namespace sot {
bool &Event::check(bool &ret, const int &time) {
const bool &val = conditionSIN(time);
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) {
lastVal_ = val;
if (trigger) {
for (Triggers_t::const_iterator _s = triggers.begin();
_s != triggers.end(); ++_s)
(*_s)->recompute(time);
}
}
if (trigger) {
for (Triggers_t::const_iterator _s = triggers.begin();
_s != triggers.end(); ++_s)
(*_s)->recompute(time);
timeSinceUp_ = 0;
}
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