Skip to content

Commit 0ee01a7

Browse files
Add command to periodic call to recompute a signal every n iterations, where n is specified by the user. This could be useful for signals that just need to be plotted or dumped on file.
1 parent e854acb commit 0ee01a7

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

include/sot/core/periodic-call.hh

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,25 @@ namespace sot {
5454
class SOT_CORE_EXPORT PeriodicCall
5555
{
5656
protected:
57-
typedef std::map< std::string,dynamicgraph::SignalBase<int>* > SignalMapType;
57+
struct SignalToCall
58+
{
59+
dynamicgraph::SignalBase<int>* signal;
60+
unsigned int downsamplingFactor;
61+
62+
SignalToCall()
63+
{
64+
signal = NULL;
65+
downsamplingFactor = 1;
66+
}
67+
68+
SignalToCall(dynamicgraph::SignalBase<int>* s, unsigned int df=1)
69+
{
70+
signal = s;
71+
downsamplingFactor = df;
72+
}
73+
};
74+
75+
typedef std::map< std::string,SignalToCall > SignalMapType;
5876
SignalMapType signalMap;
5977

6078
typedef std::list< std::string > CmdListType;
@@ -68,6 +86,9 @@ class SOT_CORE_EXPORT PeriodicCall
6886
PeriodicCall( void );
6987
virtual ~PeriodicCall( void ) {}
7088

89+
void addDownsampledSignal( const std::string &name, dynamicgraph::SignalBase<int>& sig, const unsigned int& downsamplingFactor );
90+
void addDownsampledSignal( const std::string& sigpath, const unsigned int& downsamplingFactor );
91+
7192
void addSignal( const std::string &name, dynamicgraph::SignalBase<int>& sig );
7293
void addSignal( const std::string& args );
7394
void rmSignal( const std::string &name );

src/tools/periodic-call.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ setPyInterpreter( dynamicgraph::python::Interpreter* ptr )
6262
void PeriodicCall::
6363
addSignal( const std::string &name, SignalBase<int>& sig )
6464
{
65-
signalMap[ name ] = &sig;
65+
signalMap[ name ] = SignalToCall(&sig);
6666
return ;
6767
}
6868

@@ -75,6 +75,22 @@ addSignal( const std::string& sigpath )
7575
return ;
7676
}
7777

78+
void PeriodicCall::
79+
addDownsampledSignal( const std::string &name, SignalBase<int>& sig, const unsigned int& downsamplingFactor )
80+
{
81+
signalMap[ name ] = SignalToCall(&sig,downsamplingFactor);
82+
return ;
83+
}
84+
85+
void PeriodicCall::
86+
addDownsampledSignal( const std::string& sigpath, const unsigned int& downsamplingFactor )
87+
{
88+
istringstream sigISS( sigpath );
89+
SignalBase<int>& signal = ::dynamicgraph::PoolStorage::getInstance()->getSignal( sigISS );
90+
addDownsampledSignal( sigpath,signal,downsamplingFactor );
91+
return ;
92+
}
93+
7894
void PeriodicCall::
7995
rmSignal( const std::string &name )
8096
{
@@ -111,7 +127,8 @@ runSignals( const int& t )
111127
for( SignalMapType::iterator iter = signalMap.begin();
112128
signalMap.end()!=iter; ++iter )
113129
{
114-
(*iter).second ->recompute( t );
130+
if(t%iter->second.downsamplingFactor == 0)
131+
(*iter).second.signal->recompute( t );
115132
}
116133
return ;
117134
}
@@ -240,6 +257,8 @@ void PeriodicCall::addSpecificCommands(Entity& ent,
240257
boost::function< void( const std::string& ) >
241258
addSignal = boost::bind( &PeriodicCall::addSignal, this,_1 ),
242259
rmSignal = boost::bind( &PeriodicCall::rmSignal, this,_1 );
260+
boost::function< void( const std::string&, const unsigned int& ) >
261+
addDownsampledSignal = boost::bind( &PeriodicCall::addDownsampledSignal, this,_1,_2);
243262
boost::function< void( void ) >
244263
clear = boost::bind( &PeriodicCall::clear, this );
245264
boost::function< void( std::ostream& ) >
@@ -249,6 +268,11 @@ void PeriodicCall::addSpecificCommands(Entity& ent,
249268
makeCommandVoid1(ent,addSignal,
250269
docCommandVoid1("Add the signal to the refresh list",
251270
"string (sig name)")));
271+
ADD_COMMAND("addDownsampledSignal",
272+
makeCommandVoid2(ent,addDownsampledSignal,
273+
docCommandVoid2("Add the signal to the refresh list",
274+
"string (sig name)",
275+
"unsigned int (downsampling factor, 1 means every time, 2 means every other time, etc...")));
252276
ADD_COMMAND("rmSignal",
253277
makeCommandVoid1(ent,rmSignal,
254278
docCommandVoid1("Remove the signal to the refresh list",

0 commit comments

Comments
 (0)