kinematics-dynamics
TrajGen.hpp
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 #ifndef __TRAJ_GEN_HPP__
4 #define __TRAJ_GEN_HPP__
5 
6 #include <yarp/os/Log.h>
7 
25 class Traj
26 {
27 public:
28  virtual ~Traj() {}
29  virtual bool configure(const double xi, const double xf, const double _T) = 0;
30  virtual bool configure(const double xi, const double xf, const double xdoti, const double xdotf, const double _T) = 0;
31  virtual double get(const double ti) const = 0;
32  virtual double getdot(const double ti) const = 0;
33  virtual double getdotdot(const double ti) const = 0;
34  virtual bool maxVelBelow(const double thresVel) const = 0;
35  virtual bool maxAccBelow(const double thresAcc) const = 0;
36  virtual double getT() const = 0;
37  virtual void dump(double samples) const = 0;
38 };
39 
45 class OrderOneTraj : public Traj
46 {
47 public:
48  OrderOneTraj() : T(0.0), m(0.0), b(0.0) {}
49 
50  bool configure(const double xi, const double xf, const double _T)
51  {
52  T = _T;
53  b = xi;
54  m = (xf - xi) / T;
55  return true;
56  }
57 
58  bool configure(const double xi, const double xf, const double xdoti, const double xdotf, const double _T)
59  {
60  return false;
61  }
62 
63  double get(const double ti) const
64  {
65  return b + (m * ti);
66  }
67 
68  double getdot(const double ti) const
69  {
70  return m;
71  }
72 
73  double getdotdot(const double ti) const
74  {
75  return 0;
76  }
77 
78  bool maxVelBelow(const double thresVel) const
79  {
80  return m < thresVel;
81  }
82 
83  bool maxAccBelow(const double thresAcc) const
84  {
85  return 0 < thresAcc;
86  }
87 
88  double getT() const
89  {
90  return T;
91  }
92 
93  void dump(double samples) const
94  {
95  for (double i = 0; i < T; i += (T / samples))
96  {
97  yInfo("%05.2f %+02.6f %+02.6f %+02.6f", i, get(i), getdot(i), getdotdot(i));
98  }
99  }
100 
101 private:
102  double T, m, b;
103 };
104 
110 class OrderThreeTraj : public Traj
111 {
112 public:
113  OrderThreeTraj() : a3(0.0), a2(0.0), a1(0.0), a0(0.0), T(0.0) {}
114 
118  bool configure(const double xi, const double xf, const double _T)
119  {
120  T = _T;
121  a0 = xi;
122  a1 = 0;
123  a3 = 2 * (xi - xf) / (T * T * T);
124  a2 = (xf - xi) / (T * T) - a3 * T;
125  return true;
126  }
127 
131  bool configure(const double xi, const double xf, const double xdoti, const double xdotf, const double _T)
132  {
133  T = _T;
134  a0 = xi;
135  a1 = xdoti;
136  a3 = 2.0 * (xi - xf) / (T * T * T) + (xdotf + xdoti) / (T * T);
137  a2 = (xf - xi - xdoti * T) / (T * T) - a3 * T;
138  return true;
139  }
140 
144  double get(const double ti) const
145  {
146  if (ti > T)
147  {
148  return a3 * T * T * T + a2 * T * T + a1 * T + a0; // Security hack
149  }
150  return a3 * ti * ti * ti + a2 * ti * ti + a1 * ti + a0;
151  }
152 
156  double getdot(const double ti) const
157  {
158  if (ti > T)
159  {
160  return 3 * a3 * T * T + 2 * a2 * T + a1; // Security hack
161  }
162  return 3 * a3 * ti * ti + 2 * a2 * ti + a1;
163  }
164 
168  double getdotdot(const double ti) const
169  {
170  if (ti > T)
171  {
172  return 6 * a3 * T + 2 * a2; // Security hack
173  }
174  return 6 * a3 * ti + 2 * a2;
175  }
176 
180  bool maxVelBelow(const double thresVel) const
181  {
182  return getdot(T / 2) < thresVel;
183  }
184 
188  bool maxAccBelow(const double thresAcc) const
189  {
190  return getdotdot(T / 2) < thresAcc;
191  }
192 
196  double getT() const
197  {
198  return T;
199  }
200 
205  void dump(double samples) const
206  {
207  for (double i = 0; i < T; i += (T / samples))
208  {
209  yInfo("%05.2f %+02.6f %+02.6f %+02.6f", i, get(i), getdot(i), getdotdot(i));
210  }
211  }
212 
213 private:
214  double a3, a2, a1, a0, T;
215 };
216 
217 #endif // __TRAJ_GEN_HPP__
218 
Generates a 1DOF order-one trajectory.
Definition: TrajGen.hpp:46
Generates a 1DOF order-three trajectory.
Definition: TrajGen.hpp:111
void dump(double samples) const
Definition: TrajGen.hpp:205
bool maxVelBelow(const double thresVel) const
Definition: TrajGen.hpp:180
bool maxAccBelow(const double thresAcc) const
Definition: TrajGen.hpp:188
double getdot(const double ti) const
Definition: TrajGen.hpp:156
double getT() const
Definition: TrajGen.hpp:196
double getdotdot(const double ti) const
Definition: TrajGen.hpp:168
double get(const double ti) const
Definition: TrajGen.hpp:144
bool configure(const double xi, const double xf, const double xdoti, const double xdotf, const double _T)
Definition: TrajGen.hpp:131
bool configure(const double xi, const double xf, const double _T)
Definition: TrajGen.hpp:118
A base class for 1 degree-of-freedom trajectories.
Definition: TrajGen.hpp:26