Skip to content

Commit 81280ed

Browse files
author
Olivier Stasse
authored
Merge pull request #54 from jmirabel/master
Update matrix operators and entity Event.
2 parents 5733488 + 273550d commit 81280ed

File tree

8 files changed

+456
-174
lines changed

8 files changed

+456
-174
lines changed

include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ SET(NEWHEADERS
8888
sot/core/periodic-call-entity.hh
8989
sot/core/trajectory.hh
9090
sot/core/switch.hh
91+
sot/core/variadic-op.hh
9192
)
9293
INSTALL(FILES ${NEWHEADERS}
9394
DESTINATION include/sot/core

include/sot/core/event.hh

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <dynamic-graph/pool.h>
2525
#include <dynamic-graph/command-bind.h>
2626
#include <dynamic-graph/command-getter.h>
27+
#include <dynamic-graph/command-setter.h>
2728

2829
#include <sot/core/config.hh>
2930

@@ -57,6 +58,12 @@ namespace dynamicgraph {
5758
" Get list of signals\n";
5859
addCommand ("list", new command::Getter<Event, std::string>
5960
(*this, &Event::getSignalsByName, docstring));
61+
62+
docstring =
63+
"\n"
64+
" Triggers an event only when condition goes from False to True\n";
65+
addCommand ("setOnlyUp", new command::Setter<Event, bool>
66+
(*this, &Event::setOnlyUp, docstring));
6067
}
6168

6269
~Event () {}
@@ -87,6 +94,11 @@ namespace dynamicgraph {
8794
return oss.str();
8895
}
8996

97+
void setOnlyUp (const bool& up)
98+
{
99+
onlyUp_ = up;
100+
}
101+
90102
private:
91103
typedef SignalBase<int>* Trigger_t;
92104
typedef std::vector<Trigger_t> Triggers_t;
@@ -95,11 +107,14 @@ namespace dynamicgraph {
95107
{
96108
const bool& val = conditionSIN (time);
97109
ret = (val != lastVal_);
110+
bool trigger = onlyUp_ ? (!lastVal_ && val) : ret;
98111
if (ret) {
99112
lastVal_ = val;
100-
for (Triggers_t::const_iterator _s = triggers.begin();
101-
_s != triggers.end(); ++_s)
102-
(*_s)->recompute (time);
113+
if (trigger) {
114+
for (Triggers_t::const_iterator _s = triggers.begin();
115+
_s != triggers.end(); ++_s)
116+
(*_s)->recompute (time);
117+
}
103118
}
104119
return ret;
105120
}
@@ -109,7 +124,7 @@ namespace dynamicgraph {
109124
Triggers_t triggers;
110125
SignalPtr <bool, int> conditionSIN;
111126

112-
bool lastVal_;
127+
bool lastVal_, onlyUp_;
113128
};
114129
} // namespace sot
115130
} // namespace dynamicgraph

include/sot/core/sot.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ namespace dynamicgraph {
201201
defineFreeFloatingJoints(const unsigned int& jointIdFirst,
202202
const unsigned int& jointIdLast = -1);
203203
virtual void defineNbDof( const unsigned int& nbDof );
204+
virtual const unsigned int& getNbDof() const { return nbJoints; }
204205

205206
/*! @} */
206207
public: /* --- CONTROL --- */

include/sot/core/switch.hh

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,40 @@
2828
#include <dynamic-graph/command-getter.h>
2929

3030
#include <sot/core/config.hh>
31+
#include <sot/core/variadic-op.hh>
3132

3233
namespace dynamicgraph {
3334
namespace sot {
3435
/// Switch
3536
template <typename Value, typename Time = int>
36-
class SOT_CORE_DLLAPI Switch : public dynamicgraph::Entity
37+
class SOT_CORE_DLLAPI Switch : public VariadicAbstract<Value,Value,Time>
3738
{
3839
DYNAMIC_GRAPH_ENTITY_DECL();
3940

41+
typedef VariadicAbstract<Value,Value,Time> Base;
42+
4043
Switch (const std::string& name) :
41-
Entity (name),
44+
Base (name,CLASS_NAME),
4245
selectionSIN(NULL,"Switch("+name+")::input(int)::selection"),
43-
boolSelectionSIN(NULL,"Switch("+name+")::input(bool)::boolSelection"),
44-
signalSOUT ("Switch("+name+")::output(" + typeName() + ")::sout")
46+
boolSelectionSIN(NULL,"Switch("+name+")::input(bool)::boolSelection")
4547
{
46-
signalSOUT.setFunction (boost::bind (&Switch::signal, this, _1, _2));
47-
signalRegistration (selectionSIN << boolSelectionSIN << signalSOUT);
48+
this->signalRegistration (selectionSIN << boolSelectionSIN);
49+
this->SOUT.setFunction (boost::bind(&Switch::signal,this,_1,_2));
50+
this->SOUT.addDependency (selectionSIN);
51+
this->SOUT.addDependency (boolSelectionSIN);
4852

4953
using command::makeCommandVoid1;
5054
std::string docstring =
5155
"\n"
5256
" Set number of input signals\n";
53-
addCommand ("setSignalNumber", makeCommandVoid1
54-
(*this, &Switch::setSignalNumber, docstring));
57+
this->addCommand ("setSignalNumber", makeCommandVoid1
58+
(*(Base*)this, &Base::setSignalNumber, docstring));
5559

5660
docstring =
5761
"\n"
5862
" Get number of input signals\n";
59-
addCommand ("getSignalNumber",
60-
new command::Getter<Switch, int> (*this, &Switch::getSignalNumber, docstring));
63+
this->addCommand ("getSignalNumber",
64+
new command::Getter<Base, int> (*this, &Base::getSignalNumber, docstring));
6165
}
6266

6367
~Switch () {}
@@ -69,39 +73,7 @@ namespace dynamicgraph {
6973
"Dynamically select a given signal based on a input information.\n";
7074
}
7175

72-
void setSignalNumber (const int& n)
73-
{
74-
assert (n>=0);
75-
const std::size_t oldSize = signals.size();
76-
for (std::size_t i = n; i < oldSize; ++i)
77-
{
78-
std::ostringstream oss; oss << "sin" << i;
79-
signalDeregistration(oss.str());
80-
delete signals[i];
81-
}
82-
signals.resize(n,NULL);
83-
84-
for (std::size_t i = oldSize; i < (std::size_t)n; ++i)
85-
{
86-
assert (signals[i]==NULL);
87-
std::ostringstream oss;
88-
oss << "Switch("<< getName()<< ")::input(" << typeName() << ")::sin" << i;
89-
signals[i] = new Signal_t (NULL,oss.str());
90-
signalRegistration(*signals[i]);
91-
}
92-
}
93-
94-
int getSignalNumber () const
95-
{
96-
return (int)signals.size();
97-
}
98-
9976
private:
100-
typedef SignalPtr<Value, Time> Signal_t;
101-
typedef std::vector<Signal_t*> Signals_t;
102-
103-
static const std::string& typeName ();
104-
10577
Value& signal (Value& ret, const Time& time)
10678
{
10779
int sel;
@@ -111,18 +83,15 @@ namespace dynamicgraph {
11183
const bool& b = boolSelectionSIN(time);
11284
sel = b ? 1 : 0;
11385
}
114-
if (sel < 0 || sel >= int(signals.size()))
86+
if (sel < 0 || sel >= int(this->signalsIN.size()))
11587
throw std::runtime_error ("Signal selection is out of range.");
11688

117-
ret = (*signals[sel]) (time);
89+
ret = this->signalsIN[sel]->access (time);
11890
return ret;
11991
}
12092

121-
Signals_t signals;
12293
SignalPtr <int, Time> selectionSIN;
12394
SignalPtr <bool, Time> boolSelectionSIN;
124-
125-
Signal <Value, Time> signalSOUT;
12695
};
12796
} // namespace sot
12897
} // namespace dynamicgraph

include/sot/core/variadic-op.hh

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
/*
2+
* Copyright 2018,
3+
* Mirabel Joseph
4+
*
5+
* CNRS/AIST
6+
*
7+
* This file is part of sot-core.
8+
* sot-core is free software: you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public License
10+
* as published by the Free Software Foundation, either version 3 of
11+
* the License, or (at your option) any later version.
12+
* sot-core is distributed in the hope that it will be
13+
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14+
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Lesser General Public License for more details. You should
16+
* have received a copy of the GNU Lesser General Public License along
17+
* with sot-core. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef SOT_CORE_VARIADICOP_HH
21+
#define SOT_CORE_VARIADICOP_HH
22+
23+
/* --------------------------------------------------------------------- */
24+
/* --- INCLUDE --------------------------------------------------------- */
25+
/* --------------------------------------------------------------------- */
26+
27+
/* Matrix */
28+
#include <dynamic-graph/linear-algebra.h>
29+
30+
/* SOT */
31+
#include <sot/core/flags.hh>
32+
#include <dynamic-graph/entity.h>
33+
#include <sot/core/pool.hh>
34+
#include <dynamic-graph/all-signals.h>
35+
#include <sot/core/matrix-geometry.hh>
36+
37+
/* STD */
38+
#include <string>
39+
40+
#include <boost/function.hpp>
41+
42+
namespace dynamicgraph {
43+
namespace sot {
44+
45+
/* --------------------------------------------------------------------- */
46+
/* --- CLASS ----------------------------------------------------------- */
47+
/* --------------------------------------------------------------------- */
48+
49+
template< typename Tin, typename Tout, typename Time >
50+
class VariadicAbstract
51+
:public Entity
52+
{
53+
public: /* --- CONSTRUCTION --- */
54+
55+
static std::string getTypeInName ( void );
56+
static std::string getTypeOutName( void );
57+
58+
VariadicAbstract( const std::string& name , const std::string& className)
59+
: Entity(name)
60+
,SOUT( className+"("+name+")::output("+getTypeOutName()+")::sout")
61+
,baseSigname(className+"("+name+")::input("+getTypeInName()+")::")
62+
{
63+
signalRegistration( SOUT );
64+
}
65+
66+
virtual ~VariadicAbstract( void )
67+
{
68+
for (std::size_t i = 0; i < signalsIN.size(); ++i) {
69+
_removeSignal (i);
70+
delete signalsIN[i];
71+
}
72+
};
73+
74+
public: /* --- SIGNAL --- */
75+
76+
SignalTimeDependent<Tout,int> SOUT;
77+
78+
std::size_t addSignal ()
79+
{
80+
std::ostringstream oss; oss << "sin" << signalsIN.size();
81+
return addSignal (oss.str());
82+
}
83+
84+
std::size_t addSignal (const std::string& name)
85+
{
86+
signal_t* sig = new signal_t (NULL, baseSigname + name);
87+
try {
88+
_declareSignal(sig);
89+
signalsIN.push_back (sig);
90+
// names.push_back (name);
91+
return signalsIN.size()-1;
92+
} catch (const ExceptionAbstract&) {
93+
delete sig;
94+
throw;
95+
}
96+
}
97+
98+
void removeSignal ()
99+
{
100+
assert (signalsIN.size()>0);
101+
_removeSignal (signalsIN().size()-1);
102+
// names.pop_back();
103+
signalsIN.pop_back();
104+
}
105+
106+
void setSignalNumber (const int& n)
107+
{
108+
assert (n>=0);
109+
const std::size_t oldSize = signalsIN.size();
110+
for (std::size_t i = n; i < oldSize; ++i)
111+
_removeSignal (i);
112+
signalsIN.resize(n,NULL);
113+
// names.resize(n);
114+
115+
for (std::size_t i = oldSize; i < (std::size_t)n; ++i)
116+
{
117+
assert (signalsIN[i]==NULL);
118+
std::ostringstream oss;
119+
oss << baseSigname << "sin" << i;
120+
// names[i] = oss.str();
121+
// signal_t* s = new signal_t (NULL,names[i]);
122+
signal_t* s = new signal_t (NULL,oss.str());
123+
signalsIN[i] = s;
124+
_declareSignal (s);
125+
}
126+
}
127+
128+
int getSignalNumber () const
129+
{
130+
return (int)signalsIN.size();
131+
}
132+
133+
protected:
134+
typedef SignalPtr<Tin,int> signal_t;
135+
std::vector< signal_t* > signalsIN;
136+
// Use signal->shortName instead
137+
// std::vector< std::string > names;
138+
139+
private:
140+
void _removeSignal (const std::size_t i)
141+
{
142+
// signalDeregistration(names[i]);
143+
signalDeregistration(signalsIN[i]->shortName());
144+
SOUT.removeDependency (*signalsIN[i]);
145+
delete signalsIN[i];
146+
}
147+
void _declareSignal (signal_t* s)
148+
{
149+
signalRegistration(*s);
150+
SOUT.addDependency (*s);
151+
}
152+
const std::string baseSigname;
153+
};
154+
155+
template< typename Operator >
156+
class VariadicOp
157+
:public VariadicAbstract<typename Operator::Tin, typename Operator::Tout, int>
158+
{
159+
Operator op;
160+
typedef typename Operator::Tin Tin;
161+
typedef typename Operator::Tout Tout;
162+
typedef VariadicOp<Operator> Self;
163+
164+
public: /* --- CONSTRUCTION --- */
165+
typedef VariadicAbstract<Tin,Tout,int> Base;
166+
167+
// static std::string getTypeInName ( void ) { return Operator::nameTypeIn (); }
168+
// static std::string getTypeOutName( void ) { return Operator::nameTypeOut(); }
169+
static const std::string CLASS_NAME;
170+
virtual const std::string& getClassName () const { return CLASS_NAME; }
171+
std::string getDocString () const { return op.getDocString ();}
172+
173+
VariadicOp( const std::string& name )
174+
: Base(name, CLASS_NAME)
175+
{
176+
this->SOUT.setFunction (boost::bind(&Self::computeOperation,this,_1,_2));
177+
op.initialize(this,this->commandMap);
178+
}
179+
180+
virtual ~VariadicOp( void ) {};
181+
182+
protected:
183+
Tout& computeOperation( Tout& res,int time )
184+
{
185+
std::vector< const Tin* > in (this->signalsIN.size());
186+
for (std::size_t i = 0; i < this->signalsIN.size(); ++i) {
187+
const Tin& x = this->signalsIN[i]->access (time);
188+
in[i] = &x;
189+
}
190+
op(in,res);
191+
return res;
192+
}
193+
};
194+
} // namespace sot
195+
} // namespace dynamicgraph
196+
197+
198+
#endif // #ifndef SOT_CORE_VARIADICOP_HH

0 commit comments

Comments
 (0)