17 #include "orientationThread.h" 28 OrientationThread::OrientationThread() : RateThread(20)
38 bool OrientationThread::open(
string &name,
string &hand,
string &robot,
int &nAngles)
40 this->nAngles=nAngles;
43 string remoteName=
"/"+robot+
"/cartesianController/"+hand;
45 optCtrl.put(
"device",
"cartesiancontrollerclient");
46 optCtrl.put(
"remote",remoteName.c_str());
47 optCtrl.put(
"local",(
"/"+name+
"/orientationThread/"+hand+
"/cartesian").c_str());
49 if (!dCtrl.open(optCtrl))
51 fprintf(stdout,
"%s Cartesian Interface is not open\n", hand.c_str());
59 string remoteArmName=
"/"+robot+
"/"+hand;
60 optArm.put(
"device",
"remote_controlboard");
61 optArm.put(
"remote",remoteArmName.c_str());
62 optArm.put(
"local",(
"/"+name+
"/localArm/"+hand).c_str());
64 string remoteTorsoName=
"/"+robot+
"/torso";
65 optTorso.put(
"device",
"remote_controlboard");
66 optTorso.put(
"remote",remoteTorsoName.c_str());
67 optTorso.put(
"local",(
"/"+name+
"/grasplocalTorso/"+hand).c_str());
69 robotTorso.open(optTorso);
70 robotArm.open(optArm);
72 if (!robotTorso.isValid() || !robotArm.isValid())
74 fprintf(stdout,
"Device not available\n");
79 robotArm.view(limArm);
80 robotTorso.view(limTorso);
82 if (hand==
"right_arm")
83 arm=
new iCubArm(
"right");
85 arm=
new iCubArm(
"left");
89 chain->releaseLink(0);
90 chain->releaseLink(1);
91 chain->releaseLink(2);
93 deque<IControlLimits*> lim;
94 lim.push_back(limTorso);
95 lim.push_back(limArm);
96 arm->alignJointsBounds(lim);
98 arm->setAllConstraints(
false);
100 thetaMin.resize(10,0.0);
101 thetaMax.resize(10,0.0);
102 for (
unsigned int i=0; i<chain->getDOF(); i++)
104 thetaMin[i]=(*chain)(i).getMin();
105 thetaMax[i]=(*chain)(i).getMax();
108 getAngles(angles, nAngles);
110 ones.resize(q.size()); ones=1.0;
119 void OrientationThread::preAskForPose()
121 iCtrl->storeContext(¤tContext);
122 yarp::sig::Vector dof;
124 yarp::sig::Vector newDof;
129 iCtrl->setDOF(newDof,dof);
133 void OrientationThread::postAskForPose()
135 iCtrl->restoreContext(currentContext);
136 iCtrl->deleteContext(currentContext);
140 void OrientationThread::reset()
147 void OrientationThread::setInfo(yarp::sig::Vector &eePos, yarp::sig::Vector &px, yarp::sig::Vector &py, yarp::sig::Vector &pointNormal, yarp::sig::Vector ¢er, yarp::sig::Vector &biggestAxis)
152 this->pointNormal=pointNormal;
154 this->biggestAxis=biggestAxis;
160 void OrientationThread::getAngles(yarp::sig::Vector &angles,
int nAngles)
162 angles.resize(nAngles);
163 double factor=360.0/nAngles;
165 for (
int i=0; i<nAngles; i++)
173 void OrientationThread::run()
175 bestOrientation=eye(4,4);
177 Matrix orientation=eye(4,4);
181 orientation(0,3)=center[0];
182 orientation(1,3)=center[1];
183 orientation(2,3)=center[2];
184 orientation(0,2)=pointNormal[0];
185 orientation(1,2)=pointNormal[1];
186 orientation(2,2)=pointNormal[2];
188 for (
int j=0; j<nAngles; j++)
190 orientation(0,0)=px[0]*cos(angles[j])-py[0]*sin(angles[j]);
191 orientation(1,0)=px[1]*cos(angles[j])-py[1]*sin(angles[j]);
192 orientation(2,0)=px[2]*cos(angles[j])-py[2]*sin(angles[j]);
193 orientation(0,1)=px[0]*sin(angles[j])+py[0]*cos(angles[j]);
194 orientation(1,1)=px[1]*sin(angles[j])+py[1]*cos(angles[j]);
195 orientation(2,1)=px[2]*sin(angles[j])+py[2]*cos(angles[j]);
197 yarp::sig::Vector x(3); x[0]=orientation(0,0); x[1]=orientation(1,0); x[2]=orientation(2,0);
200 if (dot(x,biggestAxis)<-0.4 || dot(x,biggestAxis)>0.4)
203 if (hand==
"right_arm")
205 if (orientation(0,0)>0.1 || orientation(2,1)>0.5 || orientation(2,0)<-0.1 || orientation(2,0)>0.9)
210 if (orientation(0,0)>0.1 || orientation(2,1)>0.5 || orientation(2,0)<-0.1 || orientation(2,0)>0.9)
216 od=dcm2axis(orientation);
220 iCtrl->askForPose(eePos,od,xdhat,odhat,q);
230 odhat[0]=odhat[0]*odhat[3];
231 odhat[1]=odhat[1]*odhat[3];
232 odhat[2]=odhat[2]*odhat[3];
235 double xdist=norm(eePos-xdhat);
236 double odist=norm(od-odhat);
238 if (xdist>0.01 && odist>0.1)
241 Jacobian=arm->GeoJacobian();
242 mulJac=Jacobian*(Jacobian.transposed());
244 manip=sqrt(det(mulJac));
247 for (
unsigned int k=0; k<thetaMin.size(); k++)
248 limits+=(q[k]-thetaMin[k])*(thetaMax[k]-q[k])/((thetaMax[k]-thetaMin[k])*(thetaMax[k]-thetaMin[k]));
250 manip*=(1-exp(-limits));
255 bestOrientation=orientation;
265 void OrientationThread::getBestManip(
double &manip, yarp::sig::Matrix &orientation)
267 manip=this->bestManip;
268 orientation=this->bestOrientation;
272 bool OrientationThread::checkDone()
278 bool OrientationThread::getResult()
284 bool OrientationThread::normalDirection(
string &hand, yarp::sig::Vector &normal)
286 if (normal[1]<0 && hand==
"left_arm")
288 if (normal[1]>0 && hand==
"right_arm")
295 void OrientationThread::close()
300 if (robotArm.isValid())
303 if (robotTorso.isValid())
310 void OrientationThread::threadRelease()