67 using namespace ROBOOP;
 
   70 void homogen_demo(
void);
 
   71 void kinematics_demo(
void);
 
   72 void dynamics_demo(
void);
 
   77    cout << 
"=====================================================\n";
 
   78    cout << 
" ROBOOP -- A robotics object oriented package in C++ \n";;
 
   79    cout << 
" DEMO program \n";
 
   80    cout << 
"=====================================================\n";
 
   91 void homogen_demo(
void)
 
   93    ColumnVector p1(3), p2(3), p3(3);
 
   94    Real ott[] = {1.0,2.0,3.0};
 
   95    Real tto[] = {3.0,2.0,1.0};
 
   98    cout << 
"=====================================================\n";
 
   99    cout << 
"Homogeneous transformations\n";
 
  100    cout << 
"=====================================================\n";
 
  102    cout << 
"Translation of [1;2;3]\n";
 
  104    cout << setw(7) << setprecision(3) << 
trans(p1);
 
  107    cout << 
"Rotation of pi/6 around axis X\n";
 
  108    cout << setw(7) << setprecision(3) << 
rotx(M_PI/6);
 
  112    cout << 
"Rotation of pi/8 around axis Y\n";
 
  113    cout << setw(7) << setprecision(3) << 
roty(M_PI/8);
 
  117    cout << 
"Rotation of pi/3 around axis Z\n";
 
  118    cout << setw(7) << setprecision(3) << 
rotz(M_PI/3);
 
  122    cout << 
"Rotation of pi/3 around axis [1;2;3]\n";
 
  123    cout << setw(7) << setprecision(3) << 
rotk(M_PI/3,p1);
 
  127    cout << 
"Rotation of pi/6 around line [1;2;3] -- [3;2;1]\n";
 
  129    cout << setw(7) << setprecision(3) << 
rotd(M_PI/3,p1,p2);
 
  133    cout << 
"Roll Pitch Yaw Rotation [1;2;3]\n";
 
  134    cout << setw(7) << setprecision(3) << 
rpy(p1);
 
  138    cout << 
"Euler ZXZ Rotation [3;2;1]\n";
 
  139    cout << setw(7) << setprecision(3) << 
eulzxz(p2);
 
  143    cout << 
"Inverse of Rotation around axis\n";
 
  144    cout << setw(7) << setprecision(3) << 
irotk(
rotk(M_PI/3,p1));
 
  148    cout << 
"Inverse of Roll Pitch Yaw Rotation\n";
 
  149    cout << setw(7) << setprecision(3) << 
irpy(
rpy(p1));
 
  153    cout << 
"Inverse of Euler ZXZ Rotation\n";
 
  158    cout << 
"Cross product between [1;2;3] and [3;2;1]\n";
 
  159    cout << setw(7) << setprecision(3) << CrossProduct(p1,p2);
 
  163    cout << 
"Rotation matrix from quaternion\n";
 
  164    ColumnVector axis(3); axis(1)=axis(2)=0; axis(3)=1.0;
 
  166    cout << setw(7) << setprecision(3) << q.R();
 
  170    cout << 
"Transformation matrix from quaternion\n";
 
  171    cout << setw(7) << setprecision(3) << q.T();
 
  174    cout << 
"Quaternion from rotation matrix\n";
 
  176    cout << setw(7) << setprecision(3) << qq.
s() << endl;
 
  177    cout << setw(7) << setprecision(3) << qq.v() << endl;
 
  181    cout << 
"Quaternion from transformation matrix\n";
 
  183    cout << setw(7) << setprecision(3) << qq.
s() << endl;
 
  184    cout << setw(7) << setprecision(3) << qq.v() << endl;
 
  187 const Real RR_data[] =
 
  188   {0, 0, 0, 1.0, 0, 0, 0, 0, 2.0,-0.5, 0, 0, 0, 0, 0, 0.1666666, 0, 0.1666666, 0, 0, 0, 0, 0,
 
  189    0, 0, 0, 1.0, 0, 0, 0, 0, 1.0,-0.5, 0, 0, 0, 0, 0, 0.0833333, 0, 0.0833333, 0, 0, 0, 0, 0};
 
  190 const Real RR_data_mdh[] =
 
  191   {0, 0, 0, 1.0, 0, 0, 0, 0, 2.0, 0.5, 0, 0, 0, 0, 0, 0.1666666, 0, 0.1666666, 0, 0, 0, 0, 0,
 
  192    0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0.5, 0, 0, 0, 0, 0, 0.0833333, 0, 0.0833333, 0, 0, 0, 0, 0};
 
  193 const Real RR_data_mdh_min_para[] =
 
  194   {0, 0, 0, 1.0, 0, 0, 0, 0, 0, 0.0, 0, 0,     0, 0, 0, 0, 0.0, 1.666666, 0, 0, 0, 0, 0,
 
  195    0, 0, 0, 1.0, 0, 0, 0, 0, 0, 0.5, 0, 0, -0.25, 0, 0, 0, 0.0, 0.3333333, 0, 0, 0, 0, 0};
 
  197 const Real RP_data[] =
 
  198   {0, 0, 0, 0, -M_PI/2.0, 0, 0, 0, 2.0, 0, 0, 0.0, 1.0, 0, 0, 1.0, 0, 1.0, 0, 0, 0, 0, 0,
 
  199    1, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0,-1.0, 0.0833333, 0, 0, 0.0833333, 0, 0.0833333, 0, 0, 0, 0, 0};
 
  200 const Real PUMA560_data[] =
 
  201   {0, 0, 0, 0, M_PI/2.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.35, 0, 0, 0, 
 
  202    0, 0, 0, 0.4318, 0, 0, 0, 0, 17.4, -0.3638, 0.006, 0.2275, 0.13, 0, 0, 0.524, 0, 0.539, 0, 
 
  203    0, 0, 0.15005, 0.0203, -M_PI/2.0, 0, 0, 0, 4.8, -0.0203, -0.0141, 0.07, 0.066, 0, 0, 0.086, 0, 0.0125, 0,
 
  204    0, 0, 0.4318, 0.0, M_PI/2.0, 0, 0, 0, 0.82, 0, 0.019, 0, 0.0018, 0, 0, 0.0013, 0, 0.0018, 0, 
 
  205    0, 0, 0, 0.0, -M_PI/2.0, 0, 0, 0, 0.34, 0.0, 0.0, 0.0, 0.0003, 0.0, 0.0, 0.0004, 0.0, 0.0003, 0, 
 
  206    0, 0, 0, 0, 0, 0, 0, 0, 0.09, 0.0, 0.0, 0.032, 0.00015, 0.0, 0.0, 0.00015, 0.0, 0.00004, 0};
 
  207 const Real PUMA560_motor[] =
 
  208   {200e-6, -62.6111, 1.48e-3, (.395 +.435)/2, 
 
  209    200e-6, 107.815, .817e-3, (.126 + .071)/2,
 
  210    200e-6, -53.7063, 1.38e-3, (.132     + .105)/2,
 
  211    33e-6,   76.0364, 71.2e-6, (11.2e-3 + 16.9e-3)/2,
 
  212    33e-6,   71.923, 82.6e-6, (9.26e-3 + 14.5e-3)/2,
 
  213    33e-6,   76.686, 36.7e-6, (3.96e-3 + 10.5e-3)/2};
 
  215 const Real STANFORD_data[] =
 
  216   {0.0, 0.0, 0.4120, 0.0, -M_PI/2, 0,0,0,9.29, 0.0, 0.0175, -0.1105, 0.276, 0.0, 0, 0.255, 0.0, 0.071,0,0,0,0,0,
 
  217    0.0, 0.0, 0.1540, 0.0, M_PI/2.0, 0,0,0,5.01, 0.0, -1.054, 0.0, 0.108, 0.0, 0.0, 0.018, 0.0, 0.1,0,0,0,0,0,
 
  218    1.0, -M_PI/2.0, 0.0, 0.0, 0.0, 0,0,0,4.25, 0.0, 0.0, -6.447, 2.51, 0.0, 0.0, 2.51, 0.0, 0.006,0,0,0,0,0,
 
  219    0.0, 0.0, 0.0, 0.0, -M_PI/2.0, 0,0,0,1.08, 0.0, 0.092, -0.054, 0.002, 0.0, 0.0, 0.001, 0.0, 0.001,0,0,0,0,0,
 
  220    0.0, 0.0, 0.0, 0.0,  M_PI/2.0, 0,0,0,0.63, 0.0, 0.0, 0.566, 0.003, 0.0, 0.0, 0.003, 0.0, 0.0004,0,0,0,0,0,
 
  221    0.0, 0.0, 0.2630, 0.0, 0.0, 0,0,0,0.51, 0.0, 0.0, 1.5540, 0.013, 0.0, 0.0, 0.013, 0.0, 0.0003,0,0,0,0,0};
 
  227 ReturnMatrix xdot(Real t, 
const Matrix & x)
 
  230    ColumnVector q, qp, qpp; 
 
  231    ColumnVector tau, dx;              
 
  235    q = x.SubMatrix(1,ndof,1,1);               
 
  236    qp = x.SubMatrix(ndof+1,2*ndof,1,1);          
 
  237    qpp = ColumnVector(ndof);
 
  240    tau = ColumnVector(ndof);
 
  248    xd.Release(); 
return xd;
 
  251 void kinematics_demo(
void)
 
  253    Matrix initrob(2,23), Tobj;
 
  259    cout << 
"=====================================================\n";
 
  260    cout << 
"Robot RR kinematics\n";
 
  261    cout << 
"=====================================================\n";
 
  263    robot = 
Robot(initrob);
 
  267    cout << 
"Robot D-H parameters\n";
 
  268    cout << 
"   type     theta      d        a      alpha\n";
 
  269    cout << setw(7) << setprecision(3) << initrob.SubMatrix(1,dof,1,5);
 
  271    cout << 
"Robot joints variables\n";
 
  272    cout << setw(7) << setprecision(3) << robot.
get_q();
 
  274    cout << 
"Robot position\n";
 
  275    cout << setw(7) << setprecision(3) << robot.
kine();
 
  277    qr = ColumnVector(dof);
 
  280    cout << 
"Robot joints variables\n";
 
  281    cout << setw(7) << setprecision(3) << robot.
get_q();
 
  283    cout << 
"Robot position\n";
 
  284    cout << setw(7) << setprecision(3) << robot.
kine();
 
  286    cout << 
"Robot Jacobian w/r to base frame\n";
 
  287    cout << setw(7) << setprecision(3) << robot.
jacobian();
 
  289    cout << 
"Robot Jacobian w/r to tool frame\n";
 
  290    cout << setw(7) << setprecision(3) << robot.
jacobian(qr.Nrows());
 
  292    for (i = 1; i <= qr.Nrows(); i++) {
 
  293       cout << 
"Robot position partial derivative with respect to joint " << i << 
" \n";
 
  294       cout << setw(7) << setprecision(3) << robot.
dTdqi(i);
 
  298    qs = ColumnVector(dof);
 
  301    cout << 
"Robot inverse kinematics\n";
 
  302    cout << 
"  q start  q final  q real\n";
 
  303    cout << setw(7) << setprecision(3) << (qs | robot.
inv_kin(Tobj) | qr);
 
  307    cout << 
"=====================================================\n";
 
  308    cout << 
"Robot RP kinematics\n";
 
  309    cout << 
"=====================================================\n";
 
  311    robot = 
Robot(initrob);
 
  314    cout << 
"Robot D-H parameters\n";
 
  315    cout << 
"  type    theta     d       a     alpha\n";
 
  316    cout << setw(7) << setprecision(3) << initrob.SubMatrix(1,dof,1,5);
 
  318    cout << 
"Robot joints variables\n";
 
  319    cout << setw(7) << setprecision(3) << robot.
get_q();
 
  321    cout << 
"Robot position\n";
 
  322    cout << setw(7) << setprecision(3) << robot.
kine();
 
  324    robot.
set_q(M_PI/4.0,1);
 
  326    cout << 
"Robot joints variables\n";
 
  327    cout << setw(7) << setprecision(3) << robot.
get_q();
 
  329    cout << 
"Robot position\n";
 
  330    cout << setw(7) << setprecision(3) << robot.
kine();
 
  332    cout << 
"Robot Jacobian w/r to base frame\n";
 
  333    cout << setw(7) << setprecision(3) << robot.
jacobian();
 
  336    cout << 
"Robot Jacobian w/r to tool frame\n";
 
  337    cout << setw(7) << setprecision(3) << robot.
jacobian(qr.Nrows());
 
  339    for (i = 1; i <= qr.Nrows(); i++) {
 
  340       cout << 
"Robot position partial derivative with respect to joint " << i << 
" \n";
 
  341       cout << setw(7) << setprecision(3) << robot.
dTdqi(i);
 
  345    robot.
set_q(M_PI/16.0,1);
 
  348    cout << 
"Robot inverse kinematics\n";
 
  349    cout << 
" q start q final q real\n";
 
  350    cout << setw(7) << setprecision(3) << (qs | robot.
inv_kin(Tobj) | qr);
 
  354    cout << 
"=====================================================\n";
 
  355    cout << 
"Robot PUMA560 kinematics\n";
 
  356    cout << 
"=====================================================\n";
 
  357    robot = 
Robot(
"conf/puma560_dh.conf", 
"PUMA560_DH");
 
  360    cout << 
"Robot joints variables\n";
 
  361    cout << setw(7) << setprecision(3) << robot.
get_q();
 
  363    cout << 
"Robot position\n";
 
  364    cout << setw(7) << setprecision(3) << robot.
kine();
 
  366    qr = ColumnVector(dof);
 
  369    cout << 
"Robot joints variables\n";
 
  370    cout << setw(7) << setprecision(3) << robot.
get_q();
 
  372    cout << 
"Robot position\n";
 
  373    cout << setw(7) << setprecision(3) << robot.
kine();
 
  375    cout << 
"Robot Jacobian w/r to base frame\n";
 
  376    cout << setw(7) << setprecision(3) << robot.
jacobian();
 
  378    cout << 
"Robot Jacobian w/r to tool frame\n";
 
  379    cout << setw(7) << setprecision(3) << robot.
jacobian(qr.Nrows());
 
  381    for (i = 1; i <= qr.Nrows(); i++) {
 
  382       cout << 
"Robot position partial derivative with respect to joint " << i << 
" \n";
 
  383       cout << setw(7) << setprecision(3) << robot.
dTdqi(i);
 
  387    qs = ColumnVector(dof);
 
  391    cout << 
"Robot inverse kinematics\n";
 
  392    cout << 
" q start q final q real\n";
 
  393    cout << setw(7) << setprecision(3) << (qs | robot.
inv_kin(Tobj) | qr);
 
  396    cout << 
"=====================================================\n";
 
  397    cout << 
"Robot STANFORD kinematics\n";
 
  398    cout << 
"=====================================================\n";
 
  399    initrob = Matrix(6,23);
 
  400    initrob << STANFORD_data;
 
  401    robot = 
Robot(initrob);
 
  404    cout << 
"Robot D-H parameters\n";
 
  405    cout << 
"  type    theta     d       a     alpha\n";
 
  406    cout << setw(7) << setprecision(3) << initrob.SubMatrix(1,dof,1,5);
 
  408    cout << 
"Robot joints variables\n";
 
  409    cout << setw(7) << setprecision(3) << robot.
get_q();
 
  411    cout << 
"Robot position\n";
 
  412    cout << setw(7) << setprecision(3) << robot.
kine();
 
  414    qr = ColumnVector(dof);
 
  417    cout << 
"Robot joints variables\n";
 
  418    cout << setw(7) << setprecision(3) << robot.
get_q();
 
  420    cout << 
"Robot position\n";
 
  421    cout << setw(7) << setprecision(3) << robot.
kine();
 
  423    cout << 
"Robot Jacobian w/r to base frame\n";
 
  424    cout << setw(7) << setprecision(3) << robot.
jacobian();
 
  426    cout << 
"Robot Jacobian w/r to tool frame\n";
 
  427    cout << setw(7) << setprecision(3) << robot.
jacobian(qr.Nrows());
 
  429    for (i = 1; i <= qr.Nrows(); i++) {
 
  430       cout << 
"Robot position partial derivative with respect to joint " << i << 
" \n";
 
  431       cout << setw(7) << setprecision(3) << robot.
dTdqi(i);
 
  435    qs = ColumnVector(dof);
 
  439    cout << 
"Robot inverse kinematics\n";
 
  440    cout << 
" q start q final q real\n";
 
  441    cout << setw(7) << setprecision(3) << (qs | robot.
inv_kin(Tobj) | qr);
 
  446 void dynamics_demo(
void)
 
  448    int nok, nbad, dof = 0;
 
  449    Matrix initrob(2,23), Tobj, xout;
 
  455    cout << 
"=====================================================\n";
 
  456    cout << 
"Robot RR dynamics\n";
 
  457    cout << 
"=====================================================\n";
 
  459    robot = 
Robot(initrob);
 
  462    cout << 
"Robot D-H parameters\n";
 
  463    cout << 
"  type    theta     d       a     alpha\n";
 
  464    cout << setw(7) << setprecision(3) << initrob.SubMatrix(1,dof,1,5);
 
  466    cout << 
"Robot D-H inertial parameters\n";
 
  467    cout << 
"  mass     cx       cy      cz     Ixx     Ixy     Ixz     Iyy     Iyz     Izz\n";
 
  468    cout << setw(7) << setprecision(3) << initrob.SubMatrix(1,dof,9,18);
 
  470    cout << 
"Robot joints variables\n";
 
  471    cout << setw(7) << setprecision(3) << robot.
get_q();
 
  473    cout << 
"Robot Inertia matrix\n";
 
  474    cout << setw(7) << setprecision(3) << robot.
inertia(robot.
get_q());
 
  477    K = Matrix(dof,2*dof);
 
  479    K(1,1) = K(2,2) = 25.0;      
 
  480    K(1,3) = K(2,4) = 7.071;
 
  481    cout << 
"Feedback gain matrix K\n";
 
  482    cout << setw(7) << setprecision(3) << K;
 
  484    q0 = ColumnVector(dof);
 
  486    qs = ColumnVector(2*dof);
 
  490    for(i = 1; i <= dof; i++)
 
  491       cout <<
"q" << i << 
"      ";
 
  492    for(i = 1; i <= dof; i++)
 
  493       cout <<
"qp" << i << 
"     ";
 
  498    odeint(xdot, qs, 0.0, 4.0, 1e-4,0.1, 1e-12, nok, nbad, tout, xout, 0.05);
 
  499    cout << setw(7) << setprecision(3) << (tout & xout).t();
 
  504    set_plot2d(
"Robot joints position", 
"time (sec)", 
"q(i) (rad)", 
"q", DATAPOINTS,
 
  512    set_plot2d(
"Robot joints velocity", 
"time (sec)", 
"qp(i) (rad/s)", 
"qp", DATAPOINTS,
 
  513               tout, xout, dof+1, 2*dof);
 
  516    cout << 
"=====================================================\n";
 
  517    cout << 
"Robot RP dynamics\n";
 
  518    cout << 
"=====================================================\n";
 
  520    robot = 
Robot(initrob);
 
  523    cout << 
"Robot D-H parameters\n";
 
  524    cout << 
"  type    theta     d       a     alpha\n";
 
  525    cout << setw(7) << setprecision(3) << initrob.SubMatrix(1,dof,1,5);
 
  527    cout << 
"Robot D-H inertial parameters\n";
 
  528    cout << 
"  mass     cx       cy      cz     Ixx     Ixy     Ixz     Iyy     Iyz     Izz\n";
 
  529    cout << setw(7) << setprecision(3) << initrob.SubMatrix(1,dof,9,18);
 
  531    cout << 
"Robot joints variables\n";
 
  532    cout << setw(7) << setprecision(3) << robot.
get_q();
 
  534    cout << 
"Robot Inertia matrix\n";
 
  535    cout << setw(7) << setprecision(3) << robot.
inertia(robot.
get_q());
 
  539    cout << 
"=====================================================\n";
 
  540    cout << 
"Robot PUMA560 dynamics\n";
 
  541    cout << 
"=====================================================\n";
 
  542    initrob = Matrix(6,19);
 
  543    initrob << PUMA560_data;
 
  546    robot = 
Robot((initrob | temp));
 
  549    cout << 
"Robot D-H parameters\n";
 
  550    cout << 
"  type    theta     d       a     alpha\n";
 
  551    cout << setw(7) << setprecision(3) << initrob.SubMatrix(1,dof,1,5);
 
  553    cout << 
"Robot D-H inertial parameters\n";
 
  554    cout << 
"  mass     cx       cy      cz     Ixx     Ixy     Ixz     Iyy     Iyz     Izz\n";
 
  555    cout << setw(7) << setprecision(3) << initrob.SubMatrix(1,dof,9,18);
 
  557    cout << 
"Robot joints variables\n";
 
  558    cout << setw(7) << setprecision(3) << robot.
get_q();
 
  560    cout << 
"Robot Inertia matrix\n";
 
  561    cout << setw(8) << setprecision(4) << robot.
inertia(robot.
get_q());
 
  563    qs = ColumnVector(dof);
 
  564    qr = ColumnVector(dof);
 
  567    cout << 
"Robot Torque\n";
 
  568    cout << setw(8) << setprecision(4) << robot.
torque(robot.
get_q(),qs,qr);
 
  570    cout << 
"Robot acceleration\n";
 
  575    cout << 
"=====================================================\n";
 
  576    cout << 
"Robot STANFORD dynamics\n";
 
  577    cout << 
"=====================================================\n";
 
  578    initrob = Matrix(6,23);
 
  579    initrob << STANFORD_data;
 
  580    robot = 
Robot(initrob);
 
  583    cout << 
"Robot D-H parameters\n";
 
  584    cout << 
"  type    theta     d       a     alpha\n";
 
  585    cout << setw(7) << setprecision(3) << initrob.SubMatrix(1,dof,1,5);
 
  587    cout << 
"Robot D-H inertial parameters\n";
 
  588    cout << 
"  mass     cx       cy      cz     Ixx     Ixy     Ixz     Iyy     Iyz     Izz\n";
 
  589    cout << setw(7) << setprecision(3) << initrob.SubMatrix(1,dof,9,18);
 
  591    cout << 
"Robot joints variables\n";
 
  592    cout << setw(7) << setprecision(3) << robot.
get_q();
 
  594    cout << 
"Robot Inertia matrix\n";
 
  595    cout << setw(7) << setprecision(3) << robot.
inertia(robot.
get_q());
 
  599    cout << 
"=====================================================\n";
 
  600    cout << 
"Robot PUMA560 with motors dynamics\n";
 
  601    cout << 
"=====================================================\n";
 
  602    initrob = Matrix(6,19);
 
  603    initrob << PUMA560_data;
 
  604    Matrix initrobm = Matrix(6,4);
 
  605    initrobm << PUMA560_motor;
 
  606    robot = 
Robot(initrob,initrobm);
 
  609    cout << 
"Robot D-H parameters\n";
 
  610    cout << 
"  type    theta     d       a     alpha\n";
 
  611    cout << setw(7) << setprecision(3) << initrob.SubMatrix(1,dof,1,5);
 
  613    cout << 
"Robot D-H inertial parameters\n";
 
  614    cout << 
"  mass     cx       cy      cz     Ixx     Ixy     Ixz     Iyy     Iyz     Izz\n";
 
  615    cout << setw(7) << setprecision(3) << initrob.SubMatrix(1,dof,9,18);
 
  617    cout << 
"Robot motors inertia, gear ratio, viscous and Coulomb friction coefficients\n";
 
  618    cout << 
"  Im       Gr       B       Cf\n";
 
  619    cout << setw(7) << setprecision(3) << initrobm;
 
  621    cout << 
"Robot joints variables\n";
 
  622    cout << setw(7) << setprecision(3) << robot.
get_q();
 
  624    cout << 
"Robot Inertia matrix\n";
 
  625    cout << setw(8) << setprecision(4) << robot.
inertia(robot.
get_q());
 
  627    qs = ColumnVector(dof);
 
  628    qr = ColumnVector(dof);
 
  632    cout << 
"Robot Torque\n";
 
  633    cout << setw(8) << setprecision(4) << robot.
torque(robot.
get_q(),qs,qr);
 
  635    cout << 
"Robot acceleration\n";