7 #include <coil/Guard.h> 27 for (
int i=0;
i<dim;
i++){
28 gx[
i] = gv[
i] = ga[
i] =
x[
i] = v[
i] =
a[
i] = 0.0;
32 default_avg_vel = default_avg_vel_;
61 double a0,
double a1,
double a2,
62 double a3,
double a4,
double a5,
63 double &xx,
double &vv,
double &aa)
66 if (remain_t_ > dt+
EPS){
71 double t = target_t - remain_t_;
72 xx=a0+a1*t+a2*t*t+a3*t*t*t+a4*t*t*t*t+a5*t*t*t*t*t;
73 vv=a1+2*a2*t+3*a3*t*t+4*a4*t*t*t+5*a5*t*t*t*t;
74 aa=2*a2+6*a3*t+12*a4*t*t+20*a5*t*t*t;
79 double &xx,
double &vv,
double &aa)
81 if (remain_t_ > dt+
EPS){
83 vv = (gx-xx)/remain_t_;
102 double max_diff = 0, diff;
103 for (
int i=0;
i<dim;
i++){
104 diff = fabs(newg[
i]-gx[
i]);
105 if (diff > max_diff) max_diff = diff;
107 remain_t_ = max_diff/default_avg_vel;
108 #define MIN_INTERPOLATION_TIME (1.0) 110 std::cerr <<
"[interpolator][" <<
name <<
"] MIN_INTERPOLATION_TIME violated!! Limit remain_t (" << remain_t <<
") by MIN_INTERPOLATION_TIME (" <<
MIN_INTERPOLATION_TIME <<
")." 111 <<
"(max_diff = " << max_diff <<
", default_avg_vel = " << default_avg_vel <<
")" << std::endl;;
119 if (i_mode_ != LINEAR && i_mode_ != HOFFARBIB &&
120 i_mode_ != QUINTICSPLINE && i_mode_ != CUBICSPLINE)
return false;
128 setGoal(newg, NULL, time, online);
134 memcpy(gx, newg,
sizeof(
double)*dim);
135 if ( newv != NULL ) memcpy(gv, newv,
sizeof(
double)*dim);
136 else {
for(
int i = 0;
i < dim;
i++) { gv[
i] = 0; } }
140 for (
int i=0;
i<dim;
i++){
143 A=(gx[
i]-(
x[
i]+v[
i]*target_t+(
a[
i]/2.0)*target_t*target_t))/(target_t*target_t*target_t);
144 B=(gv[
i]-(v[
i]+
a[
i]*target_t))/(target_t*target_t);
145 C=(ga[
i]-
a[
i])/target_t;
150 a3[
i]=10*A-4*B+0.5*C;
151 a4[
i]=(-15*A+7*B-C)/target_t;
152 a5[
i]=(6*A-3*B+0.5*C)/(target_t*target_t);
158 a3[
i]=(-20*
x[
i] + 20*gx[
i] - 3*
a[
i]*target_t*target_t + ga[
i]*target_t*target_t -
159 12*v[
i]*target_t - 8*gv[
i]*target_t) / (2*target_t*target_t*target_t);
160 a4[
i]=(30*
x[
i] - 30*gx[
i] + 3*
a[
i]*target_t*target_t - 2*ga[
i]*target_t*target_t +
161 16*v[
i]*target_t + 14*gv[
i]*target_t) / (2*target_t*target_t*target_t*target_t);
162 a5[
i]=(-12*
x[
i] + 12*gx[
i] -
a[
i]*target_t*target_t + ga[
i]*target_t*target_t -
163 6*v[
i]*target_t - 6*gv[
i]*target_t) / (2*target_t*target_t*target_t*target_t*target_t);
168 a2[
i]=(-3*
x[
i] + 3*gx[
i] - 2*v[
i]*target_t - gv[
i]*target_t) / (target_t*target_t);
169 a3[
i]=( 2*
x[
i] - 2*gx[
i] + v[
i]*target_t + gv[
i]*target_t) / (target_t*target_t*target_t);
174 if (online) remain_t = time;
179 if (remain_t_ <= 0)
return;
182 for (
int i=0;
i<dim;
i++){
186 linear_interpolation(tm,
194 a0[i], a1[i], a2[i], a3[i], a4[i], a5[i],
205 go(newg, NULL, time, immediate);
210 if (time == 0) time = calc_interpolation_time(newg);
211 setGoal(newg, newv, time,
false);
216 if (immediate) sync();
220 bool immediate,
size_t offset1,
size_t offset2)
222 ifstream strm(fname);
223 if (!strm.is_open()) {
224 cerr <<
"[interpolator " <<
name <<
"] file not found(" << fname <<
")" << endl;
227 double *vs,
ptime=-1,time, tmp;
228 vs =
new double[dim];
230 while(strm.eof()==0){
231 for (
size_t i=0;
i<offset1;
i++){
234 for (
int i=0;
i<dim;
i++){
237 for (
size_t i=0;
i<offset2;
i++){
241 go(vs, time_to_start,
false);
243 go(vs, scale*(time-ptime),
false);
250 if (immediate) sync();
254 bool immediate,
size_t offset1,
size_t offset2)
256 load(fname.c_str(), time_to_start, scale, immediate, offset1, offset2);
261 double *p =
new double[dim];
262 double *dp =
new double[dim];
263 double *ddp =
new double[dim];
264 memcpy(p, x_,
sizeof(
double)*dim);
265 memcpy(dp, v_,
sizeof(
double)*dim);
266 memcpy(ddp, a_,
sizeof(
double)*dim);
270 if (immediate) sync();
278 double *&vs = q.front();
281 double *&dvs = dq.front();
284 double *&ddvs = ddq.front();
295 double *&vs = q.back();
299 memcpy(
x, q.back(),
sizeof(double)*dim);
301 memcpy(
x, gx,
sizeof(
double)*dim);
303 double *&dvs = dq.back();
307 memcpy(v, dq.back(),
sizeof(double)*dim);
309 memcpy(v, gv,
sizeof(
double)*dim);
311 double *&ddvs = ddq.back();
315 memcpy(
a, ddq.back(),
sizeof(double)*dim);
317 memcpy(
a, ga,
sizeof(
double)*dim);
319 }
else if (remain_t > 0) {
326 for (
int i=0;
i<dim;
i++){
327 gx[
i] =
x[
i] = x_[
i];
329 gv[
i] = v[
i] = v_[
i];
348 get(x_, NULL, NULL, popp);
353 get(x_, v_, NULL, popp);
358 interpolate(remain_t);
361 double *&vs = q.front();
363 cerr <<
"[interpolator " <<
name <<
"] interpolator::get vs = NULL, q.size() = " << q.size()
364 <<
", length = " <<
length << endl;
366 double *&dvs = dq.front();
368 cerr <<
"[interpolator " <<
name <<
"] interpolator::get dvs = NULL, dq.size() = " << dq.size()
369 <<
", length = " <<
length << endl;
371 double *&ddvs = ddq.front();
373 cerr <<
"[interpolator " <<
name <<
"] interpolator::get ddvs = NULL, ddq.size() = " << ddq.size()
374 <<
", length = " <<
length << endl;
376 memcpy(x_, vs,
sizeof(
double)*dim);
377 if ( v_ != NULL ) memcpy(v_, dvs,
sizeof(
double)*dim);
378 if ( a_ != NULL ) memcpy(a_, ddvs,
sizeof(
double)*dim);
381 memcpy(x_, gx,
sizeof(
double)*dim);
382 if ( v_ != NULL) memcpy(v_, gv,
sizeof(
double)*dim);
383 if ( a_ != NULL) memcpy(a_, ga,
sizeof(
double)*dim);
389 return length==0 && remain_t <= 0;
void push(const double *x_, const double *v_, const double *a_, bool immediate=true)
bool setInterpolationMode(interpolation_mode i_mode_)
void interpolate(double &remain_t_)
void load(string fname, double time_to_start=1.0, double scale=1.0, bool immediate=true, size_t offset1=0, size_t offset2=0)
void clear(CorbaSequence &seq)
void linear_interpolation(double &remain_t_, double gx, double &xx, double &vv, double &aa)
png_infop png_charpp name
png_bytep png_bytep png_size_t length
void hoffarbib(double &remain_t_, double a0, double a1, double a2, double a3, double a4, double a5, double &xx, double &vv, double &aa)
void go(const double *gx, const double *gv, double time, bool immediate=true)
double calc_interpolation_time(const double *g)
#define MIN_INTERPOLATION_TIME
interpolator(int dim_, double dt_, interpolation_mode imode_=HOFFARBIB, double default_avg_vel_=0.5)
void get(double *x_, bool popp=true)
void setGoal(const double *gx, const double *gv, double time, bool online=true)
void set(const double *x, const double *v=NULL)