EEROS  0.4.1.0
API for the EEROS Real-Time Robotics Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ConstantAccTrajectoryGenerator.hpp
Go to the documentation of this file.
1 #ifndef ORG_EEROS_CONTROL_CONSTANTACCTRAJECTORYGENERATOR_HPP_
2 #define ORG_EEROS_CONTROL_CONSTANTACCTRAJECTORYGENERATOR_HPP_
3 
5 #include <cmath>
6 #include <mutex>
8 
9 
10 namespace eeros {
11  namespace control {
12 
13  template<typename T>
15 
16  public:
17 
18  using E = typename T::value_type;
19 
20  ConstantAccTrajectoryGenerator(T velMax, T accMax, T decMax, double dt) : finish(true), velMax(velMax), accMax(accMax), decMax(decMax), dt(dt) { }
21 
22  virtual bool finished() {
23  std::lock_guard<std::mutex> lck(mtx);
24  return finish;
25  }
26 
27  virtual std::array<T, 3> get(double dt) {
28  std::lock_guard<std::mutex> lck(mtx);
29  std::array<T, 3> y = this->last;
30  t += dt;
31 
32  for(unsigned int i = 0; i < a1p.size(); i++) {
33  if(!finish) {
34  if(t >= 0 && t < dT1) {
35  y[0][i] = a1p[i] * pow(t, 2) + c1p[i];
36  y[1][i] = b1v[i] * t;
37  y[2][i] = c1a[i];
38  }
39  else if(t >= dT1 && t < dT1 + dT2) {
40  y[0][i] = b2p[i] * t + c2p[i];
41  y[1][i] = c2v[i];
42  y[2][i] = 0;
43  }
44  else if(t >= dT1 + dT2 && t < dT1 + dT2 + dT3) {
45  y[0][i] = a3p[i] * pow(t, 2) + b3p[i] * t + c3p[i];
46  y[1][i] = b3v[i] * t + c3v[i];
47  y[2][i] = c3a[i];
48  }
49  else if(t >= dT1 + dT2 + dT3) {
50  finish = true;
51  y[0][i] = this->last[0][i];
52  y[1][i] = 0.0;
53  y[2][i] = 0.0;
54  }
55  else { // t < 0
56  throw EEROSException("get() failed, t < 0");
57  }
58  }
59  // set last value
60  this->last[0][i] = y[0][i];
61  this->last[1][i] = y[1][i];
62  this->last[2][i] = y[2][i];
63  }
64 
65  return y;
66  }
67 
68  virtual bool push(std::array<T, 3> start, std::array<T, 3> end) {
69  if(!finish) return false;
70  T calcVelNorm, calcAccNorm, calcDecNorm;
71  E velNorm, accNorm, decNorm, squareNormVel;
72  T distance = end[0] - start[0];
73 
74  T zero;
75  zero = 0;
76  if (distance == zero)
77  return false;
78 
79  // Define speeds and accelerations
80  for(unsigned int i = 0; i < calcVelNorm.size(); i++) {
81  calcVelNorm[i] = fabs(velMax[i] / distance[i]);
82  calcAccNorm[i] = fabs(accMax[i] / distance[i]);
83  calcDecNorm[i] = fabs(decMax[i] / distance[i]);
84  }
85 
86  // Init velNorm, accNorm, decNorm and find minimum
87  velNorm = calcVelNorm[0]; accNorm = calcAccNorm[0]; decNorm = calcDecNorm[0];
88  for(unsigned int i = 0; i < calcVelNorm.size(); i++) {
89  if(calcVelNorm[i] < velNorm) velNorm = calcVelNorm[i];
90  if(calcAccNorm[i] < accNorm) accNorm = calcAccNorm[i];
91  if(calcDecNorm[i] < decNorm) decNorm = calcDecNorm[i];
92  }
93 
94  // Minimize velocity
95  squareNormVel = sqrt(2 * (accNorm * decNorm) / (accNorm + decNorm));
96  if(velNorm > squareNormVel) velNorm = squareNormVel;
97 
98  // Calculate time intervals
99  dT1 = velNorm / accNorm;
100  dT3 = velNorm / decNorm;
101  dT2 = 1 / velNorm - (dT1 + dT3) * 0.5;
102  if (dT2 < 0) dT2 = 0;
103 
104  // Adaptation to timestamps
105  dT1 = ceil(dT1 / dt) * dt;
106  dT2 = ceil(dT2 / dt) * dt;
107  dT3 = ceil(dT3 / dt) * dt;
108  // Adaptation of speed to new timestamps
109  velNorm = 1/((dT2 + (dT1 + dT3)*0.5)*dt);
110 
111  a1p = 0.5 * velNorm / dT1 * distance * dt;
112  c1p = start[0];
113  b1v = velNorm / dT1 * dt * distance * dt;
114  c1a = velNorm * dt / dT1 * distance * pow(dt, 2);
115 
116  b2p = velNorm * dt * distance;
117  c2p = start[0] - 0.5 * velNorm * dT1 * dt * distance;
118  c2v = velNorm * distance * dt * dt;
119  c2a = 0;
120 
121  a3p = (-1) * velNorm * 0.5 * dt / dT3 * distance;
122  b3p = velNorm * dt / dT3 * (dT1 + dT2 + dT3) * distance;
123  c3p = start[0] + (1 - velNorm * 0.5 * dt / dT3 * pow(dT1 + dT2 + dT3, 2)) * distance;
124  b3v = (-1) *velNorm * dt * distance / dT3 * dt;
125  c3v = velNorm * (dT1 + dT2 + dT3) * distance * dt / dT3 * dt;
126  c3a = (-1) * velNorm * dt / dT3 * distance * dt * dt;
127 
128  std::lock_guard<std::mutex> lck(mtx);
129  finish = false;
130  t = 0;
131 
132  return true;
133  }
134 
135  virtual void reset(std::array<T, 3> last) {
136  std::lock_guard<std::mutex> lck(mtx);
137  this->last = last;
138  this->finish = true;
139  }
140 
141  protected:
142  std::mutex mtx;
143  double dt;
145 
146  double dT1, dT2, dT3;
147  T a1p, c1p, b1v, c1a, b2p, c2p, c2v, c2a, a3p, b3p, c3p, b3v, c3v, c3a;
148 
149  double t;
150  bool finish;
151  };
152  };
153 };
154 
155 #endif /* ORG_EEROS_CONTROL_CONSTANTACCTRAJECTORYGENERATOR_HPP_ */
156 
T velMax
Definition: ConstantAccTrajectoryGenerator.hpp:144
virtual void reset(std::array< T, 3 > last)
Definition: ConstantAccTrajectoryGenerator.hpp:135
T a1p
Definition: ConstantAccTrajectoryGenerator.hpp:147
T b1v
Definition: ConstantAccTrajectoryGenerator.hpp:147
T c3a
Definition: ConstantAccTrajectoryGenerator.hpp:147
typename T::value_type E
Definition: ConstantAccTrajectoryGenerator.hpp:18
virtual bool finished()
Definition: ConstantAccTrajectoryGenerator.hpp:22
Definition: EEROSException.hpp:9
T c2p
Definition: ConstantAccTrajectoryGenerator.hpp:147
T b3p
Definition: ConstantAccTrajectoryGenerator.hpp:147
Definition: TrajectoryGenerator.hpp:10
T c1p
Definition: ConstantAccTrajectoryGenerator.hpp:147
virtual bool push(std::array< T, 3 > start, std::array< T, 3 > end)
Definition: ConstantAccTrajectoryGenerator.hpp:68
Definition: ConstantAccTrajectoryGenerator.hpp:14
T b2p
Definition: ConstantAccTrajectoryGenerator.hpp:147
std::mutex mtx
Definition: ConstantAccTrajectoryGenerator.hpp:142
std::array< T, N > last
Definition: TrajectoryGenerator.hpp:46
T c3v
Definition: ConstantAccTrajectoryGenerator.hpp:147
T a3p
Definition: ConstantAccTrajectoryGenerator.hpp:147
double t
Definition: ConstantAccTrajectoryGenerator.hpp:149
T c1a
Definition: ConstantAccTrajectoryGenerator.hpp:147
T c2a
Definition: ConstantAccTrajectoryGenerator.hpp:147
double dT1
Definition: ConstantAccTrajectoryGenerator.hpp:146
double dT2
Definition: ConstantAccTrajectoryGenerator.hpp:146
bool finish
Definition: ConstantAccTrajectoryGenerator.hpp:150
T c3p
Definition: ConstantAccTrajectoryGenerator.hpp:147
T accMax
Definition: ConstantAccTrajectoryGenerator.hpp:144
T b3v
Definition: ConstantAccTrajectoryGenerator.hpp:147
ConstantAccTrajectoryGenerator(T velMax, T accMax, T decMax, double dt)
Definition: ConstantAccTrajectoryGenerator.hpp:20
int i
Definition: RingBufferTest.cpp:12
T decMax
Definition: ConstantAccTrajectoryGenerator.hpp:144
T c2v
Definition: ConstantAccTrajectoryGenerator.hpp:147
double dT3
Definition: ConstantAccTrajectoryGenerator.hpp:146
double dt
Definition: ConstantAccTrajectoryGenerator.hpp:143