icub-test
CartesianControlReachingToleranceTest.cpp
1 /*
2  * iCub Robot Unit Tests (Robot Testing Framework)
3  *
4  * Copyright (C) 2015-2019 Istituto Italiano di Tecnologia (IIT)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <string>
22 #include <robottestingframework/TestAssert.h>
23 #include <robottestingframework/dll/Plugin.h>
24 #include <yarp/os/Time.h>
25 #include <yarp/dev/CartesianControl.h>
26 #include <yarp/dev/IEncoders.h>
27 #include <yarp/sig/Matrix.h>
28 #include <yarp/math/Math.h>
29 
30 #include "CartesianControlReachingToleranceTest.h"
31 
32 using namespace std;
33 using namespace robottestingframework;
34 using namespace yarp::os;
35 using namespace yarp::dev;
36 using namespace yarp::sig;
37 using namespace yarp::math;
38 
39 // prepare the plugin
40 ROBOTTESTINGFRAMEWORK_PREPARE_PLUGIN(CartesianControlReachingToleranceTest)
41 
42 
43 /***********************************************************************************/
44 CartesianControlReachingToleranceTest::CartesianControlReachingToleranceTest() :
45  yarp::robottestingframework::TestCase("CartesianControlReachingToleranceTest")
46 {
47 }
48 
49 
50 /***********************************************************************************/
51 CartesianControlReachingToleranceTest::~CartesianControlReachingToleranceTest()
52 {
53 }
54 
55 
56 /***********************************************************************************/
57 bool CartesianControlReachingToleranceTest::setup(Property &property)
58 {
59  string robot=property.check("robot",Value("icubSim")).asString();
60  string arm=property.check("arm-type",Value("left")).asString();
61 
62  Property optCart;
63  optCart.put("device","cartesiancontrollerclient");
64  optCart.put("remote",("/"+robot+"/"+"cartesianController/"+arm+"_arm"));
65  optCart.put("local",("/"+getName()+"/cartesian/"+arm+"_arm"));
66 
67  Property optJoint;
68  optJoint.put("device","remote_controlboard");
69  optJoint.put("remote",("/"+robot+"/"+arm+"_arm"));
70  optJoint.put("local",("/"+getName()+"/joint/"+arm+"_arm"));
71 
72  ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("Opening Cartesian Controller Client for %s_arm",arm.c_str()));
73  ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(drvCart.open(optCart),"Unable to open the client!");
74 
75  ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("Opening Joint Controller Client for %s_arm",arm.c_str()));
76  ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(drvJoint.open(optJoint),"Unable to open the client!");
77  return true;
78 }
79 
80 
81 /***********************************************************************************/
82 void CartesianControlReachingToleranceTest::tearDown()
83 {
84  ROBOTTESTINGFRAMEWORK_TEST_REPORT("Closing Cartesian Controller Client");
85  ROBOTTESTINGFRAMEWORK_ASSERT_FAIL_IF_FALSE(drvCart.close(),"Unable to close the client!");
86 
87  ROBOTTESTINGFRAMEWORK_TEST_REPORT("Closing Joint Controller Client");
88  ROBOTTESTINGFRAMEWORK_ASSERT_FAIL_IF_FALSE(drvJoint.close(),"Unable to close the client!");
89 }
90 
91 
92 /***********************************************************************************/
93 double CartesianControlReachingToleranceTest::compute_error(const Vector &xh, const Vector &oh,
94  const Vector &x, const Vector &o)
95 {
96  Matrix H=axis2dcm(o);
97  H(0,3)=x[0];
98  H(1,3)=x[1];
99  H(2,3)=x[2];
100 
101  Vector e(6,0.0);
102  e[0]=xh[0]-H(0,3);
103  e[1]=xh[1]-H(1,3);
104  e[2]=xh[2]-H(2,3);
105  Matrix Des=axis2dcm(oh);
106  Vector ax=dcm2axis(Des*SE3inv(H));
107  e[3]=ax[3]*ax[0];
108  e[4]=ax[3]*ax[1];
109  e[5]=ax[3]*ax[2];
110 
111  return norm(e);
112 }
113 
114 
115 /***********************************************************************************/
116 void CartesianControlReachingToleranceTest::run()
117 {
118  ICartesianControl *iarm;
119  ROBOTTESTINGFRAMEWORK_TEST_CHECK(drvCart.view(iarm),"Opening the view on the Cartesian device!");
120 
121  IEncoders *ienc;
122  ROBOTTESTINGFRAMEWORK_TEST_CHECK(drvJoint.view(ienc),"Opening the view on the Joint device!");
123 
124  bool done;
125 
126  Vector x,o;
127  double t0=Time::now();
128  while (Time::now()-t0<5.0)
129  {
130  done=iarm->getPose(x,o);
131  if (done)
132  break;
133  Time::delay(0.1);
134  }
135  ROBOTTESTINGFRAMEWORK_TEST_CHECK(done,"Initial pose retrieved!");
136 
137  ROBOTTESTINGFRAMEWORK_TEST_REPORT("Setting up the context");
138  int context;
139  iarm->storeContext(&context);
140 
141  Vector dof;
142  iarm->getDOF(dof); dof=1.0;
143  dof[0]=dof[1]=dof[2]=0.0;
144  iarm->setDOF(dof,dof);
145  iarm->setTrajTime(1.0);
146  iarm->setInTargetTol(0.02);
147 
148  double tol;
149  iarm->getInTargetTol(&tol);
150 
151  Vector pos2reach(3,0.0);
152  pos2reach[0]=-0.35;
153  pos2reach[1]=0.0;
154  pos2reach[2]=0.15;
155 
156  Matrix dcm2reach=zeros(3,3);
157  dcm2reach(0,0)=dcm2reach(2,1)=dcm2reach(1,2)=-1.0;
158  Vector ori2reach=dcm2axis(dcm2reach);
159 
160  ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("Reaching for the target: (%s, %s)",
161  pos2reach.toString(3,3).c_str(),ori2reach.toString(3,3).c_str()));
162  Vector xh,oh,qh;
163  iarm->goToPoseSync(pos2reach,ori2reach);
164  iarm->getDesired(xh,oh,qh);
165 
166  ROBOTTESTINGFRAMEWORK_TEST_REPORT("Waiting");
167  done=iarm->waitMotionDone(0.1,10.0);
168  iarm->stopControl();
169 
170  Vector xf,of;
171  iarm->getPose(xf,of);
172 
173  int nJoints;
174  ienc->getAxes(&nJoints);
175  Vector qf(nJoints);
176  ienc->getEncoders(qf.data());
177 
178  ROBOTTESTINGFRAMEWORK_TEST_CHECK(done,"Target reached!");
179  ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("Reaching tolerance: %g",tol));
180  ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("Solved pose: (%s, %s)",
181  xh.toString(3,3).c_str(),oh.toString(3,3).c_str()));
182  ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("Reached pose: (%s, %s)",
183  xf.toString(3,3).c_str(),of.toString(3,3).c_str()));
184  ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("Solved joints qh: (%s)",
185  qh.subVector(3,dof.length()-1).toString(3,3).c_str()));
186  ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("Reached joints qf: (%s)",
187  qf.subVector(0,6).toString(3,3).c_str()));
188  ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("Error: %g",compute_error(xh,oh,xf,of)));
189 
190  ROBOTTESTINGFRAMEWORK_TEST_REPORT("Going back to starting pose");
191  iarm->goToPoseSync(x,o);
192 
193  ROBOTTESTINGFRAMEWORK_TEST_REPORT("Waiting");
194  done=iarm->waitMotionDone(1.0,5.0);
195  ROBOTTESTINGFRAMEWORK_TEST_CHECK(done,"Starting pose reached!");
196 
197  ROBOTTESTINGFRAMEWORK_TEST_REPORT("Cleaning up the context");
198  iarm->restoreContext(context);
199  iarm->deleteContext(context);
200 }
201 
This test verifies the point-to-point cartesian movement.