23 #include <robottestingframework/dll/Plugin.h>
24 #include <robottestingframework/TestAssert.h>
25 #include <yarp/os/Time.h>
26 #include <yarp/os/Network.h>
27 #include <yarp/os/BufferedPort.h>
28 #include <yarp/os/ResourceFinder.h>
29 #include <yarp/dev/GazeControl.h>
30 #include <yarp/sig/Matrix.h>
31 #include <yarp/math/Math.h>
33 #include <iCub/ctrl/filters.h>
35 #include "DemoRedBallTest.h"
38 using namespace robottestingframework;
39 using namespace yarp::os;
40 using namespace yarp::dev;
41 using namespace yarp::sig;
42 using namespace yarp::math;
43 using namespace iCub::ctrl;
50 DemoRedBallTest::DemoRedBallTest() : yarp::robottestingframework::TestCase(
"DemoRedBallTest")
56 DemoRedBallTest::~DemoRedBallTest()
62 bool DemoRedBallTest::setup(Property &property)
64 Time::useNetworkClock(
"/clock");
66 string context=
property.check(
"context",Value(
"demoRedBall")).asString();
67 string from=
property.check(
"from",Value(
"config-test.ini")).asString();
71 rf.setDefaultContext(context.c_str());
72 rf.setDefaultConfigFile(from.c_str());
76 params.robot=
"icubSim";
78 params.reach_tol=0.01;
79 params.use_torso=
true;
81 params.use_right=
true;
82 params.home_arm.resize(7,0.0);
84 Bottle &general=rf.findGroup(
"general");
85 if (!general.isNull())
87 params.robot=general.check(
"robot",Value(params.robot)).asString();
88 params.eye=general.check(
"eye",Value(params.eye)).asString();
89 params.reach_tol=general.check(
"reach_tol",Value(params.reach_tol)).asFloat64();
90 params.use_torso=(general.check(
"torso",Value(params.use_torso?
"on":
"off")).asString()==
"on");
91 params.use_left=(general.check(
"left_arm",Value(params.use_left?
"on":
"off")).asString()==
"on");
92 params.use_right=(general.check(
"right_arm",Value(params.use_right?
"on":
"off")).asString()==
"on");
95 Bottle &home_arm=rf.findGroup(
"home_arm");
96 if (!home_arm.isNull())
98 if (home_arm.check(
"poss"))
100 Bottle &poss=home_arm.findGroup(
"poss");
101 for (
size_t i=0; i<std::min(params.home_arm.length(),(
size_t)poss.size()-1); i++)
102 params.home_arm[i]=poss.get(1+i).asFloat64();
106 ROBOTTESTINGFRAMEWORK_TEST_REPORT(
"Opening Clients");
110 optJoint.put(
"device",
"remote_controlboard");
111 optJoint.put(
"remote",(
"/"+params.robot+
"/"+
"left_arm"));
112 optJoint.put(
"local",(
"/"+getName()+
"/joint/left_arm"));
115 optCart.put(
"device",
"cartesiancontrollerclient");
116 optCart.put(
"remote",(
"/"+params.robot+
"/"+
"cartesianController/left_arm"));
117 optCart.put(
"local",(
"/"+getName()+
"/cartesian/left_arm"));
119 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(drvJointArmL.open(optJoint)&&drvCartArmL.open(optCart),
120 "Unable to open clients for left_arm!");
123 if (params.use_right)
126 optJoint.put(
"device",
"remote_controlboard");
127 optJoint.put(
"remote",(
"/"+params.robot+
"/"+
"right_arm"));
128 optJoint.put(
"local",(
"/"+getName()+
"/joint/right_arm"));
131 optCart.put(
"device",
"cartesiancontrollerclient");
132 optCart.put(
"remote",(
"/"+params.robot+
"/"+
"cartesianController/right_arm"));
133 optCart.put(
"local",(
"/"+getName()+
"/cartesian/right_arm"));
135 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(drvJointArmR.open(optJoint)&&drvCartArmR.open(optCart),
136 "Unable to open clients for right_arm!");
141 optJoint.put(
"device",
"remote_controlboard");
142 optJoint.put(
"remote",(
"/"+params.robot+
"/"+
"head"));
143 optJoint.put(
"local",(
"/"+getName()+
"/joint/head"));
146 optGaze.put(
"device",
"gazecontrollerclient");
147 optGaze.put(
"remote",
"/iKinGazeCtrl");
148 optGaze.put(
"local",(
"/"+getName()+
"/gaze"));
150 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(drvJointHead.open(optJoint)&&drvGaze.open(optGaze),
151 "Unable to open clients for head!");
154 if (params.use_torso)
157 optJoint.put(
"device",
"remote_controlboard");
158 optJoint.put(
"remote",(
"/"+params.robot+
"/"+
"torso"));
159 optJoint.put(
"local",(
"/"+getName()+
"/joint/torso"));
161 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(drvJointTorso.open(optJoint),
162 "Unable to open clients for torso!");
165 rpcPort.open(
"/"+getName()+
"/rpc");
166 string dest=
"/demoRedBall/rpc";
167 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(Network::connect(rpcPort.getName(),dest),
168 Asserter::format(
"Unable to connect to %s!",dest.c_str()));
170 guiPort.open(
"/"+getName()+
"/gui:i");
171 string src=
"/demoRedBall/gui:o";
172 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(Network::connect(src,guiPort.getName()),
173 Asserter::format(
"Unable to connect to %s!",src.c_str()));
180 void DemoRedBallTest::tearDown()
182 ROBOTTESTINGFRAMEWORK_TEST_REPORT(
"Closing Clients");
187 ROBOTTESTINGFRAMEWORK_ASSERT_FAIL_IF_FALSE(drvJointArmL.close()&&drvCartArmL.close(),
188 "Unable to close client for left_arm!");
190 if (params.use_right)
192 ROBOTTESTINGFRAMEWORK_ASSERT_FAIL_IF_FALSE(drvJointArmR.close()&&drvCartArmR.close(),
193 "Unable to close client for right_arm!");
195 ROBOTTESTINGFRAMEWORK_ASSERT_FAIL_IF_FALSE(drvJointHead.close()&&drvGaze.close(),
196 "Unable to close client for head!");
197 if (params.use_torso)
199 ROBOTTESTINGFRAMEWORK_ASSERT_FAIL_IF_FALSE(drvJointTorso.close(),
"Unable to close client for left_arm!");
205 bool DemoRedBallTest::getBallPosition(
const Bottle* b, Vector& pos)
209 if (b->get(0).isString() && (b->get(0).asString()==
"object"))
212 pos[0]=b->get(5).asFloat64()/1000.;
213 pos[1]=b->get(6).asFloat64()/1000.;
214 pos[2]=b->get(7).asFloat64()/1000.;
223 void DemoRedBallTest::testBallPosition(
const Vector &dpos)
226 int nEncs; IEncoders* ienc;
231 cmd.addString(
"update_pose");
232 cmd.addFloat64(dpos[0]);
233 cmd.addFloat64(dpos[1]);
234 cmd.addFloat64(dpos[2]);
235 rpcPort.write(cmd,rep);
240 cmd.addString(
"start");
242 cmd.addFloat64(-50.);
244 rpcPort.write(cmd,rep);
246 auto filt = make_unique<MedianFilter>(5, Vector{0., 0., 0.});
252 while (Time::now()-t0<10.0)
254 if (
auto* gui=guiPort.read(
false))
257 if (getBallPosition(gui,pos))
262 igaze->getFixationPoint(x);
263 if (norm(filt->output()-x)<2.0*params.reach_tol)
270 ROBOTTESTINGFRAMEWORK_TEST_CHECK(done,
"Ball gazed at with the eyes!");
272 filt->init(Vector{0., 0., 0.});
275 while (Time::now()-t0<10.0)
277 if (
auto* gui=guiPort.read(
false))
280 if (getBallPosition(gui,pos))
285 arm_under_test.iarm->getPose(x,o);
286 if (norm(filt->output()-x)<params.reach_tol)
293 ROBOTTESTINGFRAMEWORK_TEST_CHECK(done,
"Ball reached with the hand!");
295 ROBOTTESTINGFRAMEWORK_TEST_REPORT(
"Going home");
297 cmd.addString(
"stop");
298 rpcPort.write(cmd,rep);
300 arm_under_test.ienc->getAxes(&nEncs);
301 encs.resize(nEncs,0.0);
304 while (Time::now()-t0<10.0)
306 arm_under_test.ienc->getEncoders(encs.data());
307 if (norm(params.home_arm-encs.subVector(0,params.home_arm.length()-1))<5.0)
314 ROBOTTESTINGFRAMEWORK_TEST_CHECK(done,
"Arm has reached home!");
316 drvJointHead.view(ienc);
317 ienc->getAxes(&nEncs);
318 encs.resize(nEncs,0.0);
321 while (Time::now()-t0<10.0)
323 ienc->getEncoders(encs.data());
324 if (norm(encs.subVector(0,3))<5.0)
331 ROBOTTESTINGFRAMEWORK_TEST_CHECK(done,
"Head has reached home!");
333 if (params.use_torso)
335 drvJointTorso.view(ienc);
336 ienc->getAxes(&nEncs);
337 encs.resize(nEncs,0.0);
340 while (Time::now()-t0<10.0)
342 ienc->getEncoders(encs.data());
343 if (norm(encs.subVector(0,3))<5.0)
350 ROBOTTESTINGFRAMEWORK_TEST_CHECK(done,
"Torso has reached home!");
356 void DemoRedBallTest::run()
358 if (params.use_torso || params.use_left)
363 drvJointArmL.view(arm_under_test.ienc);
364 drvCartArmL.view(arm_under_test.iarm);
365 ROBOTTESTINGFRAMEWORK_TEST_REPORT(
"Reaching with the left hand");
366 testBallPosition(dpos);
369 if (params.use_torso || params.use_right)
374 drvJointArmR.view(arm_under_test.ienc);
375 drvCartArmR.view(arm_under_test.iarm);
376 ROBOTTESTINGFRAMEWORK_TEST_REPORT(
"Reaching with the right hand");
377 testBallPosition(dpos);
This test verifies the point-to-point cartesian movement.