47 static const char rcsid[] =
"$Id: trajectory.cpp,v 1.8 2006/05/16 19:24:26 gourdeau Exp $";
54 using namespace NEWMAT;
76 nb_path = pts.
Nrows()-1;
81 cerr <<
"Spl_cubic::Spl_cubic: size of time vector is zero." << endl;
87 cerr <<
"Spl_cubic::Spl_cubic: need at least 4 points to produce a cubic spline." << endl;
93 cerr <<
"Spl_cubic::Spl_cubic: No data for each points." << endl;
100 for(
int i = 1; i <= N-1; i++)
102 delta(i) = pts(1,i+1) - pts(1,i);
107 cerr <<
"Spl_cubic::Spl_cubic: time between input points is zero" << endl;
110 beta(i) = 1/delta(i);
113 Matrix A(N-2, N-2); A = 0;
114 A(1,1) = 2*(delta(1)+delta(2));
116 for(
int j = 2; j <= N-2; j++)
119 A(j,j) = 2*(delta(j)+delta(j+1));
120 if( (j+1) <= A.
Ncols())
121 A(j,j+1) = delta(j+1);
125 for(
int k = 1; k <= N-2; k++)
128 C(k,k+1) = -(beta(k)+beta(k+1));
129 C(k,k+2) = beta(k+1);
135 dd_s(1) = dd_s(N) = 0;
137 Ak =
Matrix(nb_path, N-1);
138 Bk =
Matrix(nb_path, N-1);
139 Ck =
Matrix(nb_path, N-1);
140 Dk =
Matrix(nb_path, N-1);
142 for(
int ii = 2; ii <= nb_path+1; ii++)
146 for(
int jj = 1; jj < N; jj++)
149 Ak(ii-1,jj) = 1/(6.0*delta(jj))*(dd_s(jj+1) - dd_s(jj));
150 Bk(ii-1,jj) = 1/2.0*dd_s(jj);
151 Ck(ii-1,jj) = (pts(ii,jj+1)-pts(ii,jj))/delta(jj) -
152 1/6.0*delta(jj)*(dd_s(jj+1) + 2*dd_s(jj));
153 Dk(ii-1,jj) = pts(ii,jj);
163 cerr <<
"Spl_cubic::interpolating: data is not good. Problems occur in constructor." << endl;
167 for(
int i = 1; i < tk.Ncols(); i++)
168 if( (t >= tk(i)) && (t < tk(i+1)) )
170 s = Ak.
SubMatrix(1,nb_path,i,i)*pow(t - tk(i), 3) +
171 Bk.
SubMatrix(1,nb_path,i,i)*pow(t - tk(i),2) +
172 Ck.
SubMatrix(1,nb_path,i,i)*(t - tk(i)) +
177 cerr <<
"Spl_cubic::interpolating: t is out of range." << endl;
187 cerr <<
"Spl_cubic::first_derivative: data is not good. Problems occur in constructor." << endl;
191 for(
int i = 1; i < tk.Ncols(); i++)
192 if( (t >= tk(i)) && (t < tk(i+1)) )
194 ds = 3*Ak.
SubMatrix(1,nb_path,i,i)*pow(t - tk(i), 2) +
195 2*Bk.
SubMatrix(1,nb_path,i,i)*(t - tk(i)) +
200 cerr <<
"Spl_cubic::first_derivative: t not in range." << endl;
210 cerr <<
"Spl_cubic::second_derivative: data is not good. Problems occur in constructor." << endl;
214 for(
int i = 1; i < tk.Ncols(); i++)
215 if( (t >= tk(i)) && (t < tk(i+1)) )
217 ds = 6*Ak.
SubMatrix(1,nb_path,i,i)*(t - tk(i)) +
222 cerr <<
"Spl_cubic::second_derivative: t not in range." << endl;
269 const char *ptr_filename = filename.c_str();
271 std::ifstream inpointfile(ptr_filename, std::ios::in);
281 getline(inpointfile, temp);
282 while(temp.substr(0,1) ==
"#") {
283 getline(inpointfile, temp);
286 if(temp ==
"JOINT_SPACE")
288 else if (temp ==
"CARTESIAN_SPACE")
292 cerr <<
"Spl_path::Spl_path: wrong selection type. Should be joint space or cartesian space." << endl;
296 getline(inpointfile, temp);
297 istringstream inputString1(temp);
298 inputString1 >> nb_path;
301 cerr <<
"Spl_path::Spl_path: number of splines should be >= 1." << endl;
307 while( !inpointfile.eof() )
309 getline(inpointfile, temp);
310 istringstream inputString2(temp);
312 if(temp.substr(0,1) ==
" ")
314 inputString2 >> time;
317 getline(inpointfile, temp);
318 istringstream inputString3(temp);
320 for(
int i = 1; i <= nb_path; i++)
321 inputString3 >> p(i);
323 pts_map.insert(point_map::value_type(time, p));
327 cerr <<
"Spl_path::Spl_path: can not open file " << filename.c_str() << endl;
331 nb_pts = pts_map.
size();
332 point_map::const_iterator iter = pts_map.begin();
333 nb_path = iter->second.Nrows();
335 Matrix pts(nb_path+1, nb_pts); pts = 0;
338 for(iter = pts_map.begin(); iter != pts_map.end(); ++iter)
340 pts(1,i) = iter->first;
341 pts.
SubMatrix(2, nb_path+1, i, i) = iter->second;
362 cerr <<
"Spl_path::p_pdot: problem with spline interpolating." << endl;
374 cerr <<
"Spl_path::p_pdot: problem with spline interpolating." << endl;
379 cerr <<
"Spl_path::p_pdot: problem with spline first_derivative." << endl;
392 cerr <<
"Spl_path::p_pdot_pdotdot: problem with spline interpolating." << endl;
397 cerr <<
"Spl_path::p_pdot_pdotdot: problem with spline first_derivative." << endl;
402 cerr <<
"Spl_path::p_pdot_pdotdot: problem with spline first_derivative." << endl;
438 const char *ptr_filename = filename.c_str();
440 std::ifstream inquatfile(ptr_filename, std::ios::in);
450 while( !inquatfile.eof() )
452 getline(inquatfile, temp);
453 while(temp.substr(0,1) ==
"#") {
454 getline(inquatfile, temp);
456 istringstream inputString(temp);
457 if(temp.substr(0,1) ==
" ")
461 getline(inquatfile, temp);
462 if( (temp.substr(0,1) ==
"r") || (temp.substr(0,1) ==
"R") )
464 getline(inquatfile, temp);
465 istringstream inputString(temp);
466 inputString >> R(1,1) >> R(1,2) >> R(1,3) >> R(2,1) >> R(2,2) >>
467 R(2,3) >> R(3,1) >> R(3,2) >> R(3,3);
470 else if( (temp.substr(0,1) ==
"q") || (temp.substr(0,1) ==
"Q") )
472 getline(inquatfile, temp);
473 istringstream inputString(temp);
474 inputString >> R(1,1) >> R(1,2) >> R(1,3) >> R(2,1);
475 q =
Quaternion(R(1,1), R(1,2), R(1,3), R(2,1));
477 else if(temp.substr(0,1) ==
"")
482 cerr <<
"Spl_Quaternion::Spl_Quaternion: format of input file " 483 << filename.c_str() <<
" is incorrect" << endl;
486 quat_data.insert( quat_map::value_type(time, q));
491 cerr <<
"Spl_Quaternion::Spl_Quaternion: can not open file " 492 << filename.c_str() << endl;
514 quat_map::const_iterator iter1 = quat_data.begin(), iter2;
516 if( t == iter1->first)
527 if( t < iter2->first)
529 dt = (t - iter1->first)/(iter2->first - iter1->first);
530 s =
Slerp(iter1->second, iter2->second, dt);
538 for(iter1 = iter2; iter1 != ----quat_data.end(); ++iter1)
542 if( (t >= iter1->first) && (t < iter2->first) )
544 dt = (t - iter1->first)/(iter2->first - iter1->first);
547 qn = (++iter1)->second,
548 qn_1 = (++iter1)->second,
549 qn_2 = (++iter1)->second,
554 Quaternion an = qn*(((qn.i()*qn_1).Log() + (qn.i()*qn_).Log())/-4).
exp(),
555 an_1 = qn_1*(((qn_1.i()*qn_2).Log() + (qn_1.i()*qn).Log())/-4).
exp();
557 s =
Squad(qn, an, an_1, qn_1, dt);
563 iter2 = iter1; iter2++;
564 if( (t >= iter1->first) && (t <= iter2->first) )
566 dt = (t - iter1->first)/(iter2->first - iter1->first);
567 s =
Slerp(iter1->second, iter2->second, dt);
571 cerr <<
"Spl_Quaternion::quat_w: t not in range." << endl;
580 quat_map::const_iterator iter1 = quat_data.begin(), iter2;
582 if( t == iter1->first)
594 if( t < iter2->first)
596 dt = (t - iter1->first)/(iter2->first - iter1->first);
597 s =
Slerp(iter1->second, iter2->second, dt);
598 ds =
Slerp_prime(iter1->second, iter2->second, dt);
607 for(iter1 = iter2; iter1 != ----quat_data.end(); ++iter1)
611 if( (t >= iter1->first) && (t < iter2->first) )
613 dt = (t - iter1->first)/(iter2->first - iter1->first);
616 qn = (++iter1)->second,
617 qn_1 = (++iter1)->second,
618 qn_2 = (++iter1)->second,
623 Quaternion an = qn*(((qn.i()*qn_1).Log() + (qn.i()*qn_).Log())/-4).
exp(),
624 an_1 = qn_1*(((qn_1.i()*qn_2).Log() + (qn_1.i()*qn).Log())/-4).
exp();
626 s =
Squad(qn, an, an_1, qn_1, dt);
634 iter2 = iter1; iter2++;
635 if( (t >= iter1->first) && (t <= iter2->first) )
637 dt = (t - iter1->first)/(iter2->first - iter1->first);
638 s =
Slerp(iter1->second, iter2->second, dt);
639 ds =
Slerp_prime(iter1->second, iter2->second, dt);
644 cerr <<
"Spl_Quaternion::quat_w: t not in range." << endl;
656 quaternion_active =
false;
669 set_trajectory(filename);
692 const char *ptr_filename = filename.c_str();
694 std::ifstream inpointfile(ptr_filename, std::ios::in);
700 getline(inpointfile, temp);
701 while(temp.substr(0,1) ==
"#") {
702 getline(inpointfile, temp);
705 if( (temp ==
"JOINT_SPACE") || (temp ==
"CARTESIAN_SPACE") )
708 type = path.get_type();
709 quaternion_active =
false;
715 quaternion_active =
true;
719 cerr <<
"Trajectory_Select::set_trajectory: invalid input file " << filename << endl;
Quaternion class definition.
short type
Cartesian or joint space.
Trajectory_Select()
Constructor.
short quat(const Real t, Quaternion &s)
Quaternion interpollation.
Header file for trajectory generation class.
short type
Cartesian space or joint space.
Quaternion exp() const
Exponential of a quaternion.
Quaternion Slerp(const Quaternion &q0, const Quaternion &q1, const Real t)
Spherical Linear Interpolation.
short second_derivative(const Real t, ColumnVector &dds)
Spline second derivative at time t.
Trajectory class selection.
Spl_path path
Spl_path instance.
short quat_w(const Real t, Quaternion &s, ColumnVector &w)
Quaternion interpollation and angular velocity.
Cubic quaternions spline.
short interpolating(const Real t, ColumnVector &s)
Interpolating the spline at time t. Extrapolating is not allowed.
Spl_Quaternion path_quat
Spl_Quaternion instance.
bool quaternion_active
Using Spl_Quaternion.
TransposedMatrix t() const
Quaternion Squad_prime(const Quaternion &p, const Quaternion &a, const Quaternion &b, const Quaternion &q, const Real t)
Spherical Cubic Interpolation derivative.
Quaternion Slerp_prime(const Quaternion &q0, const Quaternion &q1, const Real t)
Spherical Linear Interpolation derivative.
The usual rectangular matrix.
short p_pdot_pddot(const Real time, ColumnVector &p, ColumnVector &pdot, ColumnVector &pdotdot)
Position, velocity and acceleration vector at time t.
short p(const Real time, ColumnVector &p)
Position vector at time t.
Quaternion Squad(const Quaternion &p, const Quaternion &a, const Quaternion &b, const Quaternion &q, const Real t)
Spherical Cubic Interpolation.
std::map< Real, Quaternion, less< Real > > quat_map
Data at control points.
GetSubMatrix SubMatrix(int fr, int lr, int fc, int lc) const
Trajectory_Select & operator=(const Trajectory_Select &x)
Overload = operator.
std::map< Real, ColumnVector, less< Real > > point_map
Data at control points.
void set_trajectory(const std::string &filename)
Trajectory selection.
static const char rcsid[]
RCS/CVS version.
short p_pdot(const Real time, ColumnVector &p, ColumnVector &pdot)
Position and velocity vector at time t.
Natural cubic splines class.
short first_derivative(const Real t, ColumnVector &ds)
Spline first derivative at time t.
ReturnMatrix Omega(const Quaternion &q, const Quaternion &q_dot)
Return angular velocity from a quaternion and it's time derivative.