2727
2828/* SOT */
2929#include < sot/core/integrator-abstract.hh>
30+ #include < dynamic-graph/command-setter.h>
31+ #include < dynamic-graph/command-getter.h>
3032
3133/* --------------------------------------------------------------------- */
3234/* --- CLASS ----------------------------------------------------------- */
@@ -50,7 +52,7 @@ class IntegratorEuler
5052{
5153
5254 public:
53- virtual const std::string& getClassName ( void ) const { return dg::Entity::getClassName (); }
55+ virtual const std::string& getClassName ( void ) const ;
5456 static std::string getTypeName ( void ) { return " Unknown" ; }
5557 static const std::string CLASS_NAME ;
5658
@@ -63,8 +65,29 @@ class IntegratorEuler
6365 public:
6466 IntegratorEuler ( const std::string& name )
6567 : IntegratorAbstract<sigT,coefT>( name )
68+ , derivativeSOUT(boost::bind(&IntegratorEuler<sigT,coefT>::derivative,this ,_1,_2),
69+ SOUT ,
70+ " sotIntegratorEuler(" +name+" )::output(vector)::derivativesout" )
6671 {
67- SOUT .addDependency (SIN );
72+ this ->signalRegistration ( derivativeSOUT );
73+
74+ using namespace dg ::command;
75+
76+ setSamplingPeriod (0.005 );
77+
78+ this ->addCommand (" setSamplingPeriod" ,
79+ new Setter<IntegratorEuler,double > (*this ,
80+ &IntegratorEuler::setSamplingPeriod,
81+ " Set the time during two sampling." ));
82+ this ->addCommand (" getSamplingPeriod" ,
83+ new Getter<IntegratorEuler,double > (*this ,
84+ &IntegratorEuler::getSamplingPeriod,
85+ " Get the time during two sampling." ));
86+
87+ this ->addCommand (" initialize" ,
88+ makeCommandVoid0 (*this , &IntegratorEuler::initialize,
89+ docCommandVoid0 (" Initialize internal memory from current value of input" )
90+ ));
6891 }
6992
7093 virtual ~IntegratorEuler ( void ) {}
@@ -73,14 +96,16 @@ protected:
7396 std::vector<sigT> inputMemory;
7497 std::vector<sigT> outputMemory;
7598
99+ dg::SignalTimeDependent<sigT, int > derivativeSOUT;
100+
101+ double dt;
102+ double invdt;
103+
76104public:
77105 sigT& integrate ( sigT& res, int time )
78106 {
79107 sotDEBUG (15 )<<" # In {" <<std::endl;
80108
81- const double dt = 0.005 ;
82- const double invdt = 200 ;
83-
84109 sigT sum;
85110 sigT tmp1, tmp2;
86111 const std::vector<coefT>& num = numerator;
@@ -89,33 +114,32 @@ public:
89114 // Step 1
90115 tmp1 = inputMemory[0 ];
91116 inputMemory[0 ] = SIN .access (time);
92- sum.resize (tmp1.size ());
93- sum = denom[0 ] * inputMemory[0 ];
117+ sum = num[0 ] * inputMemory[0 ];
94118 // End of step 1. Here, sum is b_0 X
95119
96120 // Step 2
97- int denomsize = denom .size ();
98- for (int i = 1 ; i < denomsize ; ++i)
121+ int numsize = ( int )num .size ();
122+ for (int i = 1 ; i < numsize ; ++i)
99123 {
100124 tmp2 = inputMemory[i-1 ] - tmp1;
101125 tmp2 *= invdt;
102126 tmp1 = inputMemory[i];
103127 inputMemory[i] = tmp2;
104- sum += (denom [i] * inputMemory[i]);
128+ sum += (num [i] * inputMemory[i]);
105129 }
106130 // End of step 2. Here, sum is b_m * d(m)X / dt^m + ... - b_0 X
107131
108132 // Step 3
109- int numsize = num .size () - 1 ;
110- for (int i = 0 ; i < numsize ; ++i)
133+ int denomsize = ( int )denom .size () - 1 ;
134+ for (int i = 0 ; i < denomsize ; ++i)
111135 {
112- sum -= (num [i] * outputMemory[i]);
136+ sum -= (denom [i] * outputMemory[i]);
113137 }
114138 // End of step 3. Here, sum is b_m * d(m)X / dt^m + ... - b_0 X - a_0 Y - ... a_n-1 d(n-1)Y / dt^(n-1)
115139
116140 // Step 4
117- outputMemory[numsize ] = sum;
118- for (int i = numsize - 1 ; i >= 0 ; --i)
141+ outputMemory[denomsize ] = sum;
142+ for (int i = denomsize- 1 ; i >= 0 ; --i)
119143 {
120144 outputMemory[i] += (outputMemory[i+1 ] * dt);
121145 }
@@ -127,6 +151,47 @@ public:
127151 sotDEBUG (15 )<<" # Out }" <<std::endl;
128152 return res;
129153 }
154+
155+ sigT& derivative ( sigT& res, int time )
156+ {
157+ if (outputMemory.size () < 2 )
158+ throw dg::ExceptionSignal (dg::ExceptionSignal::GENERIC ,
159+ " Integrator does not compute the derivative." );
160+
161+ SOUT .recompute (time);
162+ res = outputMemory[1 ];
163+ return res;
164+ }
165+
166+ void setSamplingPeriod (const double & period)
167+ {
168+ dt = period;
169+ invdt = 1 /period;
170+ }
171+
172+ double getSamplingPeriod () const
173+ {
174+ return dt;
175+ }
176+
177+ void initialize ()
178+ {
179+ std::size_t numsize = numerator.size ();
180+ inputMemory.resize (numsize);
181+
182+ inputMemory[0 ] = SIN .accessCopy ();
183+ for (std::size_t i = 1 ; i < numsize; ++i)
184+ {
185+ inputMemory[i] = inputMemory[0 ];
186+ }
187+
188+ std::size_t denomsize = denominator.size ();
189+ outputMemory.resize (denomsize);
190+ for (std::size_t i = 0 ; i < denomsize; ++i)
191+ {
192+ outputMemory[i] = inputMemory[0 ];
193+ }
194+ }
130195};
131196
132197} /* namespace sot */ } /* namespace dynamicgraph */
0 commit comments