iCub-main
main.cpp
Go to the documentation of this file.
1 
7 //
8 // The most basic example on the use of iDyn when the chain is provided with
9 // a force/torque sensor, and the sensor is placed inside the chain.
10 //
11 // Author: Serena Ivaldi - <serena.ivaldi@iit.it>
12 
13 #include <iostream>
14 #include <iomanip>
15 #include <yarp/sig/Vector.h>
16 #include <yarp/sig/Matrix.h>
17 #include <iCub/iDyn/iDyn.h>
18 #include <iCub/iDyn/iDynInv.h>
19 #include <iCub/skinDynLib/common.h>
20 
21 using namespace std;
22 using namespace yarp::sig;
23 using namespace iCub::iDyn;
24 using namespace iCub::skinDynLib;
25 
26 // useful print functions
27 // print a matrix nicely
28 void printMatrix(const string &s, const Matrix &m)
29 {
30  cout<<s<<endl;
31  for(int i=0;i<m.rows();i++)
32  {
33  for(int j=0;j<m.cols();j++)
34  cout<<setw(15)<<m(i,j);
35  cout<<endl;
36  }
37 }
38 // print a vector nicely
39 void printVector(const string &s, const Vector &v)
40 {
41  cout<<s<<endl;
42  for(size_t j=0;j<v.length();j++)
43  cout<<setw(15)<<v(j);
44  cout<<endl;
45 }
46 
47 
49 // MAIN
51 
52 int main()
53 {
54  // we create a iDyn::iCubArmNoTorsoDyn = arm only
55  // if you are familiar with iKin's iCubArm, beware this one is different!
56  // iDyn::iCubArmDyn is exactly like iKin::iCubArm, with the same kinematic properties and so on.
57  // iDyn::iCubArmNoTorsoDyn, as the name says, does not have the 3 torso links: so here N==DOF.
58  iCubArmNoTorsoDyn *arm = new iCubArmNoTorsoDyn("right");
59 
60  // we create a iDyn::iDynSensorArmNoTorso for the right arm, with force/moment computation in the static case
61  // (STATIC) and verbosity flag on (VERBOSE).
62  // if you want dynamic computations you can substitute STATIC with DYNAMIC.
63  // if you want to disable verbosity, just put NO_VERBOSE.
64  // note that there is no need to specify the sensor properties for the icub arm, since the sensor
65  // type is automatically set after getting the arm information
66  iDynSensorArmNoTorso *armWSensorSolver = new iDynSensorArmNoTorso(arm,STATIC,VERBOSE);
67 
68  // the arm must be "prepared" for Newton-Euler computation, i.e. for computing the dynamics...
69  // since we already chose STATIC computations, we tell the arm to getting ready..
70  // in order to perform the kinematic and wrench computation in a chain, we must first tell the limb
71  // that we want to use the iDyn::RecursiveNewtonEuler library.
72  // the reason is twofold:
73  // 1) one can use a iDynLimb without dynamics, since it includes the kinematics of iKin
74  // for example one may only want to set joint angles and compute the jacobian
75  // 2) one may prefer to use its own dynamic library to compute internal forces/torques, and not
76  // necessarily the Newton-Euler one we provide. to use our method, a new chain, parallel to the
77  // iDynChain, is built: it is a OneChainNewtonEuler, and it is made of OneLinkNewtonEuler virtual
78  // links, each having its "real" corresponding iDynLink: in addition, a BaseLink and a FinalLink
79  // elements are added to the chain, defining the entry points for the kinematic and wrench phases
80  // of Newton-Euler computations.
81  // the method to call is prepareNewtonEuler(), and we must specify the type of computations we want
82  // to perform, which are basically:
83  // - STATIC: we only need q (joints positions)
84  // - DYNAMIC: we need q,dq,ddq (joints position, velocity, acceleration) --> default
86 
87  // now the chain must be updated with all the necessary informations!!!!
88 
89  // 1) the joint angles (pos, vel, acc) of the limbs: we can take this values from the encoders..
90  Vector q(arm->getN()); q.zero();
91  Vector dq(q); Vector ddq(q);
92  // now we set the joints values
93  // note: if the joint angles exceed the joint limits, their value is saturated to the min/max automatically
94  // note: STATIC computations only use q, while DYNAMIC computations use q,dq,ddq.. but we set everything
95  arm->setAng(q);
96  arm->setDAng(dq);
97  arm->setD2Ang(ddq);
98 
99  // 2) the inertial information to initialize the kinematic phase
100  // this information must be set at the base of the chain, where the base is intended to be the first link (first.. in the Denavit
101  // Hartenberg convention)
102  // note: if nothing is moving, it can be simply zero, but ddp0 must provide the gravity information :)
103  Vector w0(3); Vector dw0(3); Vector ddp0(3);
104  w0=dw0=ddp0=0.0; ddp0[2]=9.81;
105  // here we set the kinematic information on the arm chain
106  arm->initKinematicNewtonEuler(w0,dw0,ddp0);
107 
108  // 3) the wrench (force/moment) measured by the FT sensor placed inside the chain
109  Vector Fsens(3); Fsens.zero();
110  Fsens=2.0; // or whatever number you like... here its is 2 (Newton) per force component..
111  Vector Musens(3); Musens.zero();
112  Musens=0.1; //or whatever number you like... here its is 0.1 (Newton per meter) per moment component..
113 
114  // finally we set the measurements on the sensor, and we start the computation...
115  // this method autonomously perform a kinematic phase (to this we set the kinematics information before..)
116  // then the wrench phase is "split": from the sensor back to the base, the computations are performed in the
117  // backward sense, while from sensor to the end-effector in the forward sense.
118  armWSensorSolver->computeFromSensorNewtonEuler(Fsens,Musens);
119 
120  // then we can retrieve our results...
121  // forces moments and torques
122  Matrix F = arm->getForces();
123  Matrix Mu = arm->getMoments();
124  Vector Tau = arm->getTorques();
125 
126  // now print the information
127 
128  cout<<"iCubArmNoTorsoDyn has "<<arm->getN()<<" links."<<endl;
129 
130  cout<<"\n\n";
131 
132  printVector("Joint Angles - command ", q);
133  printVector("Joint Angles - real ", arm->getAng());
134 
135  cout<<"\n\n";
136 
137  printVector("Measured Force in the FT sensor ",Fsens);
138  printVector("Measured Moment in the FT sensor ",Musens);
139 
140  cout<<"\n\n";
141 
142  printMatrix("Forces ",F);
143  printMatrix("Moments ",Mu);
144  printVector("Torques ",Tau);
145 
146  // remember to delete everything before exiting :)
147  cout<<"\nDestroying sensor..."<<endl;
148  delete armWSensorSolver; armWSensorSolver=NULL;
149  cout<<"Destroying arm..."<<endl;
150  delete arm; arm=NULL;
151 
152  return 0;
153 }
154 
155 
A class for defining the 7-DOF iCub Arm in the iDyn framework.
Definition: iDyn.h:1369
bool initKinematicNewtonEuler(const yarp::sig::Vector &w0, const yarp::sig::Vector &dw0, const yarp::sig::Vector &ddp0)
Calls the proper method to set kinematics variables in OneChainNewtonEuler: either initKinematicBase(...
Definition: iDyn.cpp:1144
yarp::sig::Matrix getForces() const
Returns the links forces as a matrix, where the i-th col is the i-th force.
Definition: iDyn.cpp:859
yarp::sig::Vector setD2Ang(const yarp::sig::Vector &ddq)
Sets the free joint angles acceleration to values of ddq[i].
void prepareNewtonEuler(const NewEulMode NewEulMode_s=DYNAMIC)
Prepare for the Newton-Euler recursive computation of forces and torques.
Definition: iDyn.cpp:916
yarp::sig::Matrix getMoments() const
Returns the links moments as a matrix, where the i-th col is the i-th moment.
Definition: iDyn.cpp:867
yarp::sig::Vector getTorques() const
Returns the links torque as a vector.
Definition: iDyn.cpp:875
yarp::sig::Vector setAng(const yarp::sig::Vector &q)
Sets the free joint angles to values of q[i].
yarp::sig::Vector setDAng(const yarp::sig::Vector &dq)
Sets the free joint angles velocity to values of dq[i].
A class for computing joint force/moment/torque of an iCub arm (left/right) given the FT measurements...
Definition: iDynInv.h:1785
virtual bool computeFromSensorNewtonEuler(const yarp::sig::Vector &F, const yarp::sig::Vector &Mu)
The main computation method: given the FT sensor measurements, compute forces moments and torques in ...
yarp::sig::Vector getAng()
Returns the current free joint angles values.
Definition: iKinFwd.cpp:611
unsigned int getN() const
Returns the number of Links belonging to the Chain.
Definition: iKinFwd.h:550
int main(int argc, char *argv[])
Definition: main.cpp:31
void printVector(const string &s, const Vector &v)
Definition: main.cpp:162
void printMatrix(const string &s, const Matrix &m)
Definition: main.cpp:152
@ STATIC
Definition: iDynInv.h:64