integrator-euler.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2010,
3  * François Bleibel,
4  * Olivier Stasse,
5  *
6  * CNRS/AIST
7  *
8  */
9 
10 #ifndef __SOT_INTEGRATOR_EULER_H__
11 #define __SOT_INTEGRATOR_EULER_H__
12 
13 /* --------------------------------------------------------------------- */
14 /* --- INCLUDE --------------------------------------------------------- */
15 /* --------------------------------------------------------------------- */
16 
17 /* SOT */
18 #include <dynamic-graph/command-getter.h>
19 #include <dynamic-graph/command-setter.h>
21 
22 /* --------------------------------------------------------------------- */
23 /* --- CLASS ----------------------------------------------------------- */
24 /* --------------------------------------------------------------------- */
25 
26 namespace dynamicgraph {
27 namespace sot {
28 namespace dg = dynamicgraph;
29 
30 namespace internal {
31 template <class coefT> bool integratorEulerCoeffIsIdentity(const coefT c) {
32  return c == 1;
33 }
34 
35 bool integratorEulerCoeffIsIdentity(const Vector c) { return c.isOnes(); }
36 
37 bool integratorEulerCoeffIsIdentity(const Matrix c) { return c.isIdentity(); }
38 } // namespace internal
39 
49 template <class sigT, class coefT>
50 class IntegratorEuler : public IntegratorAbstract<sigT, coefT> {
51 
52 public:
53  virtual const std::string &getClassName(void) const;
54  static std::string getTypeName(void) { return "Unknown"; }
55  static const std::string CLASS_NAME;
56 
57 public:
62 
63 public:
64  IntegratorEuler(const std::string &name)
65  : IntegratorAbstract<sigT, coefT>(name),
66  derivativeSOUT(boost::bind(&IntegratorEuler<sigT, coefT>::derivative,
67  this, _1, _2),
68  SOUT,
69  "sotIntegratorEuler(" + name +
70  ")::output(vector)::derivativesout") {
71  this->signalRegistration(derivativeSOUT);
72 
73  using namespace dg::command;
74 
75  setSamplingPeriod(0.005);
76 
77  this->addCommand("setSamplingPeriod",
78  new Setter<IntegratorEuler, double>(
80  "Set the time during two sampling."));
81  this->addCommand("getSamplingPeriod",
82  new Getter<IntegratorEuler, double>(
84  "Get the time during two sampling."));
85 
86  this->addCommand(
87  "initialize",
88  makeCommandVoid0(
90  docCommandVoid0(
91  "Initialize internal memory from current value of input")));
92  }
93 
94  virtual ~IntegratorEuler(void) {}
95 
96 protected:
97  std::vector<sigT> inputMemory;
98  std::vector<sigT> outputMemory;
99 
100  dg::SignalTimeDependent<sigT, int> derivativeSOUT;
101 
102  double dt;
103  double invdt;
104 
105 public:
106  sigT &integrate(sigT &res, int time) {
107  sotDEBUG(15) << "# In {" << std::endl;
108 
109  sigT sum;
110  sigT tmp1, tmp2;
111  const std::vector<coefT> &num = numerator;
112  const std::vector<coefT> &denom = denominator;
113 
114  // Step 1
115  tmp1 = inputMemory[0];
116  inputMemory[0] = SIN.access(time);
117  sum = num[0] * inputMemory[0];
118  // End of step 1. Here, sum is b_0 X
119 
120  // Step 2
121  int numsize = (int)num.size();
122  for (int i = 1; i < numsize; ++i) {
123  tmp2 = inputMemory[i - 1] - tmp1;
124  tmp2 *= invdt;
125  tmp1 = inputMemory[i];
126  inputMemory[i] = tmp2;
127  sum += (num[i] * inputMemory[i]);
128  }
129  // End of step 2. Here, sum is b_m * d(m)X / dt^m + ... + b_0 X
130 
131  // Step 3
132  int denomsize = (int)denom.size() - 1;
133  for (int i = 0; i < denomsize; ++i) {
134  sum -= (denom[i] * outputMemory[i]);
135  }
136  // End of step 3. Here, sum is b_m * d(m)X / dt^m + ... + b_0 X
137  // - a_0 Y - ... a_n-1 d(n-1)Y / dt^(n-1)
138 
139  // Step 4
140  outputMemory[denomsize] = sum;
141  for (int i = denomsize - 1; i >= 0; --i) {
142  outputMemory[i] += (outputMemory[i + 1] * dt);
143  }
144  // End of step 4. The ODE is integrated
145 
146  inputMemory[0] = SIN.access(time);
147  res = outputMemory[0];
148 
149  sotDEBUG(15) << "# Out }" << std::endl;
150  return res;
151  }
152 
153  sigT &derivative(sigT &res, int time) {
154  if (outputMemory.size() < 2)
155  throw dg::ExceptionSignal(dg::ExceptionSignal::GENERIC,
156  "Integrator does not compute the derivative.");
157 
158  SOUT.recompute(time);
159  res = outputMemory[1];
160  return res;
161  }
162 
163  void setSamplingPeriod(const double &period) {
164  dt = period;
165  invdt = 1 / period;
166  }
167 
168  double getSamplingPeriod() const { return dt; }
169 
170  void initialize() {
171  std::size_t numsize = numerator.size();
172  inputMemory.resize(numsize);
173 
174  inputMemory[0] = SIN.accessCopy();
175  for (std::size_t i = 1; i < numsize; ++i) {
176  inputMemory[i] = inputMemory[0];
177  }
178 
179  std::size_t denomsize = denominator.size();
180  outputMemory.resize(denomsize);
181  for (std::size_t i = 0; i < denomsize; ++i) {
182  outputMemory[i] = inputMemory[0];
183  }
184 
185  // Check that denominator.back is the identity
187  throw dg::ExceptionSignal(
188  dg::ExceptionSignal::GENERIC,
189  "The coefficient of the highest order derivative of denominator "
190  "should be 1 (the last pushDenomCoef should be the identity).");
191  }
192 };
193 
194 } /* namespace sot */
195 } /* namespace dynamicgraph */
196 
197 #endif
dynamicgraph::sot::IntegratorEuler::derivative
sigT & derivative(sigT &res, int time)
Definition: integrator-euler.hh:153
dynamicgraph::sot::IntegratorEuler::getClassName
virtual const std::string & getClassName(void) const
dynamicgraph::sot::IntegratorAbstract::denominator
std::vector< coefT > denominator
Definition: integrator-abstract.hh:101
dynamicgraph::sot::IntegratorEuler::getTypeName
static std::string getTypeName(void)
Definition: integrator-euler.hh:54
dynamicgraph::sot::IntegratorEuler::CLASS_NAME
static const std::string CLASS_NAME
Definition: integrator-euler.hh:55
dynamicgraph
Definition: abstract-sot-external-interface.hh:17
dynamicgraph::sot::IntegratorEuler
integrates an ODE using a naive Euler integration. TODO: change the integration method....
Definition: integrator-euler.hh:50
dynamicgraph::sot::IntegratorEuler::getSamplingPeriod
double getSamplingPeriod() const
Definition: integrator-euler.hh:168
dynamicgraph::sot::IntegratorEuler::IntegratorEuler
IntegratorEuler(const std::string &name)
Definition: integrator-euler.hh:64
dynamicgraph::sot::IntegratorEuler::integrate
sigT & integrate(sigT &res, int time)
Definition: integrator-euler.hh:106
dynamicgraph::sot::IntegratorEuler::setSamplingPeriod
void setSamplingPeriod(const double &period)
Definition: integrator-euler.hh:163
dynamicgraph::sot::IntegratorEuler::~IntegratorEuler
virtual ~IntegratorEuler(void)
Definition: integrator-euler.hh:94
integrator-abstract.hh
dynamicgraph::sot::IntegratorAbstract::numerator
std::vector< coefT > numerator
Definition: integrator-abstract.hh:100
dynamicgraph::sot::IntegratorEuler::inputMemory
std::vector< sigT > inputMemory
Definition: integrator-euler.hh:97
dynamicgraph::sot::IntegratorEuler::outputMemory
std::vector< sigT > outputMemory
Definition: integrator-euler.hh:98
dynamicgraph::sot::IntegratorAbstract
integrates an ODE. If Y is the output and X the input, the following equation is integrated: a_p * d(...
Definition: integrator-abstract.hh:46
dynamicgraph::sot::IntegratorEuler::dt
double dt
Definition: integrator-euler.hh:102
dynamicgraph::sot::IntegratorAbstract::SOUT
dg::SignalTimeDependent< sigT, int > SOUT
Definition: integrator-abstract.hh:97
dynamicgraph::sot::IntegratorAbstract::SIN
dg::SignalPtr< sigT, int > SIN
Definition: integrator-abstract.hh:95
dynamicgraph::sot::IntegratorEuler::initialize
void initialize()
Definition: integrator-euler.hh:170
dynamicgraph::sot::IntegratorEuler::invdt
double invdt
Definition: integrator-euler.hh:103
dynamicgraph::sot::IntegratorEuler::derivativeSOUT
dg::SignalTimeDependent< sigT, int > derivativeSOUT
Definition: integrator-euler.hh:100
dynamicgraph::sot::internal::integratorEulerCoeffIsIdentity
bool integratorEulerCoeffIsIdentity(const coefT c)
Definition: integrator-euler.hh:31
sotDEBUG
#define sotDEBUG(level)
Definition: debug.hh:167