23 #include <robottestingframework/dll/Plugin.h>
24 #include <robottestingframework/TestAssert.h>
25 #include <yarp/robottestingframework/TestAsserter.h>
27 #include "movementReferencesTest.h"
30 using namespace robottestingframework;
32 using namespace yarp::os;
38 MovementReferencesTest::MovementReferencesTest() : yarp::robottestingframework::TestCase(
"MovementReferencesTest")
44 MovementReferencesTest::~MovementReferencesTest()
48 bool MovementReferencesTest::setup(yarp::os::Property &config)
62 if(config.check(
"name"))
63 setName(config.find(
"name").asString());
65 setName(
"MovementReferencesTest");
69 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(config.check(
"robot"),
"The robot name must be given as the test parameter!");
70 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(config.check(
"part"),
"The part name must be given as the test parameter!");
71 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(config.check(
"joints"),
"The joints list must be given as the test parameter!");
72 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(config.check(
"home"),
"The home position must be given as the test parameter!");
73 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(config.check(
"target"),
"Missing 'target' parameter, cannot open device");
75 robotName = config.find(
"robot").asString();
76 partName = config.find(
"part").asString();
80 options.put(
"device",
"remote_controlboard");
81 options.put(
"remote",
"/"+robotName+
"/"+partName);
82 options.put(
"local",
"/moveRefTest/"+robotName+
"/"+partName);
84 dd =
new yarp::dev::PolyDriver(options);
85 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->isValid(),
"Unable to open device driver");
86 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(iPosition),
"Unable to open position interface");
87 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(iEncoders),
"Unable to open encoders interface");
88 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(iControlMode),
"Unable to open control mode interface");
89 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(iPWM),
"Unable to open PWM interface");
90 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(iPosDirect),
"Unable to open OpnLoop interface");
91 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(iVelocity),
"Unable to open velocity2 interface");
95 if (!iEncoders->getAxes(&numJointsInPart))
97 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(
"unable to get the number of joints of the part");
100 Bottle* homeBottle = config.find(
"home").asList();
101 if(homeBottle == NULL)
104 ROBOTTESTINGFRAMEWORK_TEST_REPORT(
"home bottle is null");
109 ROBOTTESTINGFRAMEWORK_TEST_REPORT(
"home bottle is not null");
112 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(homeBottle!=0,
"unable to parse home parameter");
114 Bottle* jointsBottle = config.find(
"joints").asList();
115 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(jointsBottle!=0,
"unable to parse joints parameter");
117 Bottle* targetBottle = config.find(
"target").asList();
118 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(targetBottle!=0,
"unable to parse target parameter");
120 Bottle* refAccBottle = config.find(
"refAcc").asList();
121 if(refAccBottle == NULL)
122 ROBOTTESTINGFRAMEWORK_TEST_REPORT(
"refAcc param not present. Test will use deafoult values.");
124 Bottle* refVelBottle = config.find(
"refVel").asList();
125 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(refVelBottle!=0,
"unable to parse refVel parameter");
128 numJoints = jointsBottle->size();
129 ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format(
"num joints: %d", numJoints));
132 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(numJoints>0 && numJoints<=numJointsInPart,"invalid number of joints, it must be >0 & <= number of part joints
");
134 //jointsList.clear();
137 jointsList.resize(numJoints);
138 for (int i=0; i< numJoints; i++)
140 jointsList[i]=jointsBottle->get(i).asInt32();
143 jList = new int[numJoints];
144 for (int i=0; i< numJoints; i++)
146 jList[i]=jointsBottle->get(i).asInt32();
151 //for (int i=0; i <numJoints; i++) jointsList.push_back(jointsBottle->get(i).asInt32());
153 homePos.resize (numJoints); for (int i=0; i< numJoints; i++) homePos[i]=homeBottle->get(i).asFloat64();
154 targetPos.resize (numJoints); for (int i=0; i< numJoints; i++) targetPos[i]=targetBottle->get(i).asFloat64();
155 refVel.resize (numJoints); for (int i=0; i< numJoints; i++) refVel[i]=refVelBottle->get(i).asFloat64();
156 if(refAccBottle != NULL)
158 refAcc.resize (numJoints);
159 for (int i=0; i< numJoints; i++) refAcc[i]=refAccBottle->get(i).asFloat64();
166 jPosMotion = new yarp::robottestingframework::jointsPosMotion(dd, jointsList);
167 jPosMotion->setTolerance(2.0);
168 jPosMotion->setTimeout(5); //5 sec
170 // ROBOTTESTINGFRAMEWORK_TEST_REPORT(jointsList.toString().c_str());
171 // ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("startup num joints: %d partnj=%d
", numJoints, numJointsInPart));
172 // ROBOTTESTINGFRAMEWORK_TEST_REPORT(targetPos.toString().c_str());
173 // ROBOTTESTINGFRAMEWORK_TEST_REPORT(refVel.toString().c_str());
174 // ROBOTTESTINGFRAMEWORK_TEST_REPORT(refAcc.toString().c_str());
176 // for(int k=0; k<numJoints; k++)
178 // ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("targetpos [%d]=%.2f
", k, targetPos[k]));
181 /* for(int k=0; k<numJoints; k++)
183 ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("jointlist [%d]=%d
", k, (int)jointsList[k]));
184 ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("jlist [%d]=%d
", k, jList[k]));
189 void MovementReferencesTest::tearDown() {
191 // finalization goes her ...
193 for (int i=0; i<numJoints; ++i)
196 // 1) check get reference position returns the target position set by positionMove(..)
198 ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("tear down: joint %d is going to home
", jList[i]));
200 setAndCheckControlMode(jList[i], VOCAB_CM_POSITION);
202 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(jPosMotion->goToSingle(jList[i], homePos[i]),
203 Asserter::format(("go to target pos
for j %d
"),jList[i]));
212 void MovementReferencesTest::setAndCheckControlMode(int j, int mode)
214 int rec_mode=VOCAB_CM_IDLE;
215 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iControlMode->setControlMode(j, mode),
216 Asserter::format(("setting control mode
for j %d
"),j));
218 yarp::os::Time::delay(0.1);
220 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iControlMode->getControlMode(j, &rec_mode),
221 Asserter::format(("getting control mode
for j %d
"),j));
223 ROBOTTESTINGFRAMEWORK_ASSERT_FAIL_IF_FALSE((rec_mode == mode),
224 Asserter::format(("joint %d: is not in position
"),j));
228 void MovementReferencesTest::run() {
234 // ROBOTTESTINGFRAMEWORK_TEST_REPORT(jointsList.toString().c_str());
235 // for(int k=0; k<numJoints; k++)
237 // ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("targetpos [%d]=%.2f
", k, targetPos[k]));
240 // for(int k=0; k<numJoints; k++)
242 // ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("jointlist [%d]=%d
", k, jointsList[k]));
246 ROBOTTESTINGFRAMEWORK_TEST_REPORT("setting velocity refs
for all joints...
");
248 if(refAcc.size() != 0)
250 ROBOTTESTINGFRAMEWORK_TEST_REPORT("setting acceleration refs
for all joints...
");
251 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iVelocity->setRefAccelerations(numJoints, jList, refAcc.data()),
252 Asserter::format("setting reference acceleration on joints
"));
255 ROBOTTESTINGFRAMEWORK_TEST_REPORT("setting velocity refs
for all joints...
");
256 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iPosition->setRefSpeeds(numJoints, jList, refVel.data()),
257 Asserter::format("setting reference speed on joints
"));
262 ROBOTTESTINGFRAMEWORK_TEST_REPORT("all joints are going to home...
");
263 for (int i=0; i<numJoints; ++i)
266 // 1) check get reference position returns the target position set by positionMove(..)
268 ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format(" joint %d is going to home
", jList[i]));
270 setAndCheckControlMode(jList[i], VOCAB_CM_POSITION);
272 ROBOTTESTINGFRAMEWORK_ASSERT_FAIL_IF_FALSE(jPosMotion->goToSingle(jList[i], homePos[i]),
273 Asserter::format(("go to target pos
for j %d
"),jList[i]));
276 yarp::os::Time::delay(5);
278 ROBOTTESTINGFRAMEWORK_TEST_REPORT("Checking individual joints...
");
280 const double res_th = 0.01; //resolution threshold
281 //numJoints=numJointsInPart;
282 for (int i=0; i<numJoints; ++i)
284 //double reached_pos;
285 double rec_targetPos=200.0;
287 // 1) check get reference position returns the target position set by positionMove(..)
289 ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("Checking PosReference joint %d with resolution threshold %.3f
", jList[i], res_th));
291 setAndCheckControlMode(jList[i], VOCAB_CM_POSITION);
293 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(jPosMotion->goToSingle(jList[i], targetPos[i]),
294 Asserter::format(("go to target pos
for j %d
"),jList[i])); //Note: gotosingle use IPositioncontrol2::PositionMove
296 yarp::os::Time::delay(0.5);
298 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iPosition->getTargetPosition(jList[i], &rec_targetPos),
299 Asserter::format(("getting target pos
for j %d
"),jList[i]));
301 bool res = yarp::robottestingframework::TestAsserter::isApproxEqual(targetPos[i], rec_targetPos, res_th, res_th);
302 ROBOTTESTINGFRAMEWORK_TEST_CHECK(res, Asserter::format(
303 ("IPositionControl: getting target pos
for j %d: setval =%.2f received %.2f
"),
304 jList[i], targetPos[i],rec_targetPos));
307 // std::cout <<"ERRORE: getTargetPosition: j
" << jList[i] << "sent
" << targetPos[i] << "rec
" << rec_targetPos;
309 // std::cout <<"OK: getTargetPosition: j
" << jList[i] << "sent
" << targetPos[i] << "rec
" << rec_targetPos;
311 yarp::os::Time::delay(3);
313 //2) check get reference output (pwm mode) returns the ouput set by setRefOutput
314 ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("Checking pwm reference joint %d
", jList[i]));
317 setAndCheckControlMode(jList[i], VOCAB_CM_PWM);
320 double rec_output = 0;
321 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iPWM->setRefDutyCycle(jList[i], output),
322 Asserter::format(("set ref output
for j %d
"),jList[i]));
323 yarp::os::Time::delay(0.5);
325 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iPWM->getRefDutyCycle(jList[i], &rec_output),
326 Asserter::format(("get ref output
for j %d
"),jList[i]));
328 ROBOTTESTINGFRAMEWORK_TEST_CHECK((output == rec_output),
329 Asserter::format(("getting target output
for j %d: setval =%.2f received %.2f
"),jList[i], output,rec_output));
331 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iPosition->positionMove(jList[i], homePos[i]),
332 Asserter::format(("go to home
for j %d
"),jList[i]));
333 yarp::os::Time::delay(0.5);
334 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iPosition->getTargetPosition(jList[i], &rec_targetPos),
335 Asserter::format(("getting target pos
for j %d
"),jList[i]));
337 //here I expect getTargetPosition returns targetPos[j] and not homePos[j] because joint is in pwm control mode and
338 //the positionMove(homepos) command should be discarded by firmware motor controller
339 res = yarp::robottestingframework::TestAsserter::isApproxEqual(homePos[i], rec_targetPos, res_th, res_th);
340 ROBOTTESTINGFRAMEWORK_TEST_CHECK(!res,
341 Asserter::format(("joint %d discards PosotinMove command
while it is in opnLoop mode. Set=%.2f rec=%.2f
"),jList[i], homePos[i], rec_targetPos));
343 //3) check get reference pos (directPosition mode) returns the target position set by setPosition()
345 ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("Checking directPos reference joint %d
", jList[i]));
347 setAndCheckControlMode(jList[i], VOCAB_CM_POSITION_DIRECT);
349 double curr_pos=targetPos[i];
350 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iEncoders->getEncoder(jList[i], &curr_pos),
351 Asserter::format(("get encoders
for j %d
"),jList[i]));
354 double new_directPos = curr_pos+delta;
355 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iPosDirect->setPosition(jList[i], new_directPos),
356 Asserter::format(("Direct:setPosition
for j %d
"),jList[i]));
357 yarp::os::Time::delay(0.5);
359 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iPosDirect->getRefPosition(jList[i], &rec_targetPos),
360 Asserter::format(("getting target pos
for j %d
"),jList[i]));
362 res = yarp::robottestingframework::TestAsserter::isApproxEqual(new_directPos, rec_targetPos, res_th, res_th);
363 ROBOTTESTINGFRAMEWORK_TEST_CHECK(res,
364 Asserter::format(("iDirect: getting target direct pos
for j %d: setval =%.2f received %.2f
"),jList[i], new_directPos,rec_targetPos));
366 //here I'm going to check the position reference is not changed.
367 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iPosition->getTargetPosition(jList[i], &rec_targetPos),
368 Asserter::format(("getting target pos
for j %d
"),jList[i]));
370 res = yarp::robottestingframework::TestAsserter::isApproxEqual(targetPos[i], rec_targetPos, res_th, res_th);
371 ROBOTTESTINGFRAMEWORK_TEST_CHECK(res, Asserter::format(
372 ("IPositionControl: getting target pos
for j %d: setval =%.2f received %.2f
"),
373 jList[i], targetPos[i],rec_targetPos));
375 //4) check get reference velocity (velocity mode) returns the target velocity set by velocityMove()
377 ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("Checking velocity reference joint %d
", jList[i]));
379 setAndCheckControlMode(jList[i], VOCAB_CM_VELOCITY);
383 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iVelocity->velocityMove(jList[i], vel),
384 Asserter::format(("IVelocity:velocityMove
for j %d
"),jList[i]));
386 yarp::os::Time::delay(0.5);
387 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iVelocity->getRefVelocity(jList[i], &rec_vel),
388 Asserter::format(("IVelocity:getting target velocity
for j %d
"),jList[i]));
389 res = yarp::robottestingframework::TestAsserter::isApproxEqual(vel, rec_vel, res_th, res_th);
390 ROBOTTESTINGFRAMEWORK_TEST_CHECK(res,
391 Asserter::format(("iVelocity: getting target vel
for j %d: setval =%.2f received %.2f
"),jList[i], vel,rec_vel));
Check IPositionControl, IVelocityControl, IPWMControl, IPositionDirect.