22 #include <robottestingframework/TestAssert.h>
23 #include <robottestingframework/dll/Plugin.h>
24 #include <yarp/os/Time.h>
25 #include <yarp/os/Property.h>
26 #include <yarp/os/Vocab.h>
28 #include "ControlModes.h"
30 using namespace robottestingframework;
31 using namespace yarp::os;
32 using namespace yarp::dev;
37 ControlModes::ControlModes() : yarp::robottestingframework::TestCase(
"ControlModes") {
55 ControlModes::~ControlModes() { }
57 bool ControlModes::setup(yarp::os::Property& property) {
59 if(property.check(
"name"))
60 setName(property.find(
"name").asString());
63 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(property.check(
"robot"),
"The robot name must be given as the test parameter!");
64 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(property.check(
"part"),
"The part name must be given as the test parameter!");
65 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(property.check(
"joints"),
"The joints list must be given as the test parameter!");
66 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(property.check(
"home"),
"The home positions must be given as the test parameter!");
68 robotName =
property.find(
"robot").asString();
69 partName =
property.find(
"part").asString();
70 if(property.check(
"tolerance"))
71 tolerance =
property.find(
"tolerance").asFloat64();
75 ROBOTTESTINGFRAMEWORK_TEST_REPORT(robottestingframework::Asserter::format(
"Tolerance of %.2f is used to check home position", tolerance));
77 Bottle* jointsBottle =
property.find(
"joints").asList();
78 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(jointsBottle!=0,
"unable to parse joints parameter");
79 n_cmd_joints = jointsBottle->size();
80 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(n_cmd_joints>0,
"invalid number of joints, it must be >0");
83 options.put(
"device",
"remote_controlboard");
84 options.put(
"remote",
"/"+robotName+
"/"+partName);
85 options.put(
"local",
"/ControlModesTest/"+robotName+
"/"+partName);
87 dd =
new PolyDriver(options);
88 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->isValid(),
"Unable to open device driver");
89 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(idir),
"Unable to open position direct interface");
90 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(ienc),
"Unable to open encoders interface");
91 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(iamp),
"Unable to open ampliefier interface");
92 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(ipos),
"Unable to open position interface");
93 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(icmd),
"Unable to open control mode interface");
94 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(iimd),
"Unable to open interaction mode interface");
95 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(ivel),
"Unable to open velocity control interface");
96 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(itrq),
"Unable to open torque control interface");
97 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(ivar),
"Unable to open remote variables interface");
99 if (!ienc->getAxes(&n_part_joints))
101 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(
"unable to get the number of joints of the part");
104 if (n_part_joints<=0)
105 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(
"Error this part has in invalid (<=0) number of jonits");
106 else if (jointsBottle->size() == 1)
107 cmd_mode=single_joint;
108 else if (jointsBottle->size() < n_part_joints)
109 cmd_mode=some_joints;
110 else if (jointsBottle->size() == n_part_joints)
113 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(
"invalid joint selection?");
115 cmd_tot =
new double[n_part_joints];
116 pos_tot=
new double[n_part_joints];
117 jointsList=
new int[n_cmd_joints];
118 cmd_some=
new double[n_cmd_joints];
119 prevcurr_tot=
new double[n_part_joints];
120 prevcurr_some=
new double[n_cmd_joints];
121 jointTorqueCtrlEnabled =
new int[n_part_joints];
122 home_pos =
new double[n_cmd_joints];
124 for (
int i=0; i <n_cmd_joints; i++) jointsList[i]=jointsBottle->get(i).asInt32();
126 Bottle* homePosBottle =
property.find(
"home").asList();
127 for (
int i=0; i <n_cmd_joints; i++) home_pos[i]=homePosBottle->get(i).asFloat64();
129 checkJointWithTorqueMode();
133 void ControlModes::tearDown()
135 if (jointsList) {
delete jointsList; jointsList =0;}
136 if (dd) {
delete dd; dd =0;}
139 void ControlModes::getOriginalCurrentLimits()
141 for (
int i=0; i<n_cmd_joints; i++)
143 iamp->getMaxCurrent(jointsList[i],&prevcurr_some[i]);
145 for (
int i=0; i<n_part_joints; i++)
147 iamp->getMaxCurrent(i,&prevcurr_tot[i]);
149 yarp::os::Time::delay(0.010);
152 void ControlModes::resetOriginalCurrentLimits()
154 for (
int i=0; i<n_part_joints; i++)
156 iamp->setMaxCurrent(i,prevcurr_tot[i]);
158 yarp::os::Time::delay(0.010);
161 void ControlModes::zeroCurrentLimits()
163 if (cmd_mode==single_joint)
165 for (
int i=0; i<n_cmd_joints; i++)
167 iamp->setMaxCurrent(jointsList[i],0.0);
170 else if (cmd_mode==some_joints)
172 for (
int i=0; i<n_cmd_joints; i++)
174 iamp->setMaxCurrent(jointsList[i],0.0);
177 else if (cmd_mode==all_joints)
179 for (
int i=0; i<n_part_joints; i++)
181 iamp->setMaxCurrent(i,0.0);
186 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(
"Invalid cmd_mode");
188 yarp::os::Time::delay(0.010);
191 void ControlModes::setMode(
int desired_control_mode, yarp::dev::InteractionModeEnum desired_interaction_mode)
193 for (
int i=0; i<n_cmd_joints; i++)
195 setModeSingle(jointsList[i], desired_control_mode, desired_interaction_mode);
199 void ControlModes::setModeSingle(
int joint,
int desired_control_mode, yarp::dev::InteractionModeEnum desired_interaction_mode)
201 icmd->setControlMode(joint,desired_control_mode);
202 iimd->setInteractionMode(joint,desired_interaction_mode);
203 yarp::os::Time::delay(0.010);
206 void ControlModes::verifyMode(
int desired_control_mode, yarp::dev::InteractionModeEnum desired_interaction_mode, std::string title)
209 yarp::dev::InteractionModeEnum imode;
215 for (
int i=0; i<n_cmd_joints; i++)
217 icmd->getControlMode (jointsList[i],&cmode);
218 iimd->getInteractionMode(jointsList[i],&imode);
219 if (cmode==desired_control_mode && imode==desired_interaction_mode) ok++;
221 if (ok==n_cmd_joints)
break;
225 sprintf(sbuf,
"Test (%s) failed: current mode is (%s,%s), it should be (%s,%s)",title.c_str(), Vocab32::decode((NetInt32)cmode).c_str(), Vocab32::decode((NetInt32)imode).c_str(), Vocab32::decode((NetInt32)desired_control_mode).c_str(),Vocab32::decode((NetInt32)desired_interaction_mode).c_str());
226 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(sbuf);
228 yarp::os::Time::delay(0.2);
232 sprintf(sbuf,
"Test (%s) passed: current mode is (%s,%s)",title.c_str(),Vocab32::decode((NetInt32)desired_control_mode).c_str(), Vocab32::decode((NetInt32)desired_interaction_mode).c_str());
233 ROBOTTESTINGFRAMEWORK_TEST_REPORT(sbuf);
236 void ControlModes::verifyModeSingle(
int joint,
int desired_control_mode, yarp::dev::InteractionModeEnum desired_interaction_mode, std::string title)
239 yarp::dev::InteractionModeEnum imode;
245 icmd->getControlMode (joint,&cmode);
246 iimd->getInteractionMode(joint,&imode);
247 if (cmode==desired_control_mode && imode==desired_interaction_mode) ok=
true;
253 sprintf(sbuf,
"Test (%s) failed: current mode is (%s,%s), it should be (%s,%s)",title.c_str(), Vocab32::decode((NetInt32)cmode).c_str(), Vocab32::decode((NetInt32)imode).c_str(), Vocab32::decode((NetInt32)desired_control_mode).c_str(),Vocab32::decode((NetInt32)desired_interaction_mode).c_str());
254 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(sbuf);
256 yarp::os::Time::delay(0.2);
260 sprintf(sbuf,
"Test (%s) passed: current mode is (%s,%s)",title.c_str(),Vocab32::decode((NetInt32)desired_control_mode).c_str(), Vocab32::decode((NetInt32)desired_interaction_mode).c_str());
261 ROBOTTESTINGFRAMEWORK_TEST_REPORT(sbuf);
264 void ControlModes::checkJointWithTorqueMode()
267 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(ivar->getRemoteVariable(
"torqueControlEnabled", b),
"unable to get torqueControlEnabled");
269 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE((b.size() != n_part_joints),
"torqueControlEnabled var returns joint num not corret.");
272 for(
int i=0; i<b.size() && j<n_part_joints; i++)
274 yarp::os::Bottle *baux = b.get(i).asList();
275 for(
int k=0; k<baux->size()&& j<n_part_joints; k++)
277 jointTorqueCtrlEnabled[j] = baux->get(k).asInt32();
283 void ControlModes::verifyAmplifier(
int desired_amplifier_mode, std::string title)
291 for (
int i=0; i<n_cmd_joints; i++)
293 iamp->getAmpStatus (jointsList[i],&amode);
297 if (amode==desired_amplifier_mode) ok++;
299 if (ok==n_cmd_joints)
break;
303 sprintf(sbuf,
"Test (%s) failed: amplifier mode is (%d), it should be (%d)",title.c_str(), amode, desired_amplifier_mode);
304 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(sbuf);
306 yarp::os::Time::delay(0.2);
310 sprintf(sbuf,
"Test (%s) passed: amplifier mode is (%d)",title.c_str(), desired_amplifier_mode);
311 ROBOTTESTINGFRAMEWORK_TEST_REPORT(sbuf);
314 void ControlModes::executeCmd()
316 if (cmd_mode==single_joint)
318 for (
int i=0; i<n_cmd_joints; i++)
320 idir->setPosition(jointsList[i],cmd_single);
323 else if (cmd_mode==some_joints)
325 for (
int i=0; i<n_cmd_joints; i++)
327 cmd_some[i]=cmd_single;
329 idir->setPositions(n_cmd_joints,jointsList, cmd_some);
331 else if (cmd_mode==all_joints)
333 for (
int i=0; i<n_part_joints; i++)
335 cmd_tot[i]=cmd_single;
337 idir->setPositions(cmd_tot);
341 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(
"Invalid cmd_mode");
345 void ControlModes::goHome()
347 for (
int i=0; i<n_cmd_joints; i++)
349 ipos->setRefSpeed(jointsList[i],20.0);
350 ipos->positionMove(jointsList[i],home_pos[i]);
357 for (
int i=0; i<n_cmd_joints; i++)
359 ienc->getEncoder(jointsList[i],&pos_tot[jointsList[i]]);
360 if (fabs(pos_tot[jointsList[i]]-home_pos[i])<tolerance) in_position++;
362 if (in_position==n_cmd_joints)
break;
365 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(
"Timeout while reaching zero position");
367 yarp::os::Time::delay(0.2);
373 void ControlModes::checkControlModeWithImCompliant(
int desired_control_mode, std::string title)
376 for (
int i=0; i<n_cmd_joints; i++)
378 if(jointTorqueCtrlEnabled[jointsList[i]])
380 sprintf(buff,
"joint %d has torque enabled. try to set %s and im_comp", jointsList[i], Vocab32::decode((NetInt32)desired_control_mode).c_str());
381 ROBOTTESTINGFRAMEWORK_TEST_REPORT(buff);
382 setModeSingle(jointsList[i],desired_control_mode,VOCAB_IM_COMPLIANT);
383 verifyModeSingle(jointsList[i], desired_control_mode,VOCAB_IM_COMPLIANT,title);
387 sprintf(buff,
"joint %d doesn't support compliant interaction mode. test %s skipped", jointsList[i], title.c_str());
388 ROBOTTESTINGFRAMEWORK_TEST_REPORT(buff);
393 void ControlModes::run()
396 getOriginalCurrentLimits();
397 setMode(VOCAB_CM_POSITION,VOCAB_IM_STIFF);
398 verifyMode(VOCAB_CM_POSITION,VOCAB_IM_STIFF,
"test0");
399 verifyAmplifier(0,
"test0b");
403 setMode(VOCAB_CM_POSITION,VOCAB_IM_STIFF);
404 verifyMode(VOCAB_CM_POSITION,VOCAB_IM_STIFF,
"test1");
405 verifyAmplifier(0,
"test1b");
407 setMode(VOCAB_CM_POSITION_DIRECT,VOCAB_IM_STIFF);
408 verifyMode(VOCAB_CM_POSITION_DIRECT,VOCAB_IM_STIFF,
"test2");
409 verifyAmplifier(0,
"test2b");
411 setMode(VOCAB_CM_VELOCITY,VOCAB_IM_STIFF);
412 verifyMode(VOCAB_CM_VELOCITY,VOCAB_IM_STIFF,
"test3");
413 verifyAmplifier(0,
"test3b");
416 for (
int i=0; i<n_cmd_joints; i++)
418 if(jointTorqueCtrlEnabled[jointsList[i]])
420 sprintf(buff,
"joint %d has torque enabled. try to set cm_torque and im_stiff", jointsList[i]);
421 ROBOTTESTINGFRAMEWORK_TEST_REPORT(buff);
422 setModeSingle(jointsList[i],VOCAB_CM_TORQUE,VOCAB_IM_STIFF);
423 verifyModeSingle(jointsList[i], VOCAB_CM_TORQUE,VOCAB_IM_STIFF,
"test4");
424 verifyAmplifier(0,
"test4b");
428 sprintf(buff,
"joint %d doesn't support torque control mode. test test4 skipped", jointsList[i]);
429 ROBOTTESTINGFRAMEWORK_TEST_REPORT(buff);
434 setMode(VOCAB_CM_MIXED,VOCAB_IM_STIFF);
435 verifyMode(VOCAB_CM_MIXED,VOCAB_IM_STIFF,
"test5");
436 verifyAmplifier(0,
"test5b");
438 setMode(VOCAB_CM_PWM,VOCAB_IM_STIFF);
439 verifyMode(VOCAB_CM_PWM, VOCAB_IM_STIFF,
"test6");
440 verifyAmplifier(0,
"test6b");
442 setMode(VOCAB_CM_IDLE,VOCAB_IM_STIFF);
443 verifyMode(VOCAB_CM_IDLE,VOCAB_IM_STIFF,
"test7");
444 verifyAmplifier(0,
"test7b");
446 setMode(VOCAB_CM_POSITION,VOCAB_IM_STIFF);
447 verifyMode(VOCAB_CM_POSITION,VOCAB_IM_STIFF,
"test8");
448 verifyAmplifier(0,
"test8b");
450 setMode(VOCAB_CM_FORCE_IDLE,VOCAB_IM_STIFF);
451 verifyMode(VOCAB_CM_IDLE,VOCAB_IM_STIFF,
"test9");
452 verifyAmplifier(0,
"test9b");
454 setMode(VOCAB_CM_POSITION,VOCAB_IM_STIFF);
455 verifyMode(VOCAB_CM_POSITION,VOCAB_IM_STIFF,
"test10");
456 verifyAmplifier(0,
"test10b");
461 for (
int i=0; i<n_cmd_joints; i++)
463 if(jointTorqueCtrlEnabled[jointsList[i]])
465 sprintf(buff,
"joint %d has torque enabled. try to set cm_torque and im_compliant", jointsList[i]);
466 ROBOTTESTINGFRAMEWORK_TEST_REPORT(buff);
467 setModeSingle(jointsList[i],VOCAB_CM_POSITION,VOCAB_IM_COMPLIANT);
468 verifyModeSingle(jointsList[i], VOCAB_CM_POSITION,VOCAB_IM_COMPLIANT,
"test11");
473 checkControlModeWithImCompliant(VOCAB_CM_POSITION,
"test11");
474 verifyAmplifier(0,
"test11b");
477 checkControlModeWithImCompliant(VOCAB_CM_POSITION_DIRECT,
"test12");
478 verifyAmplifier(0,
"test12b");
480 checkControlModeWithImCompliant(VOCAB_CM_VELOCITY,
"test13");
481 verifyAmplifier(0,
"test13b");
483 checkControlModeWithImCompliant(VOCAB_CM_TORQUE,
"test4");
484 verifyAmplifier(0,
"test14b");
486 checkControlModeWithImCompliant(VOCAB_CM_MIXED,
"test15");
487 verifyAmplifier(0,
"test15b");
489 checkControlModeWithImCompliant(VOCAB_CM_PWM,
"test16");
490 verifyAmplifier(0,
"test16b");
492 checkControlModeWithImCompliant(VOCAB_CM_IDLE,
"test17");
493 verifyAmplifier(0,
"test17b");
495 checkControlModeWithImCompliant(VOCAB_CM_POSITION,
"test18");
496 verifyAmplifier(0,
"test18b");
498 checkControlModeWithImCompliant(VOCAB_CM_IDLE,
"test19");
499 verifyAmplifier(0,
"test19b");
501 checkControlModeWithImCompliant(VOCAB_CM_POSITION,
"test20");
502 verifyAmplifier(0,
"test20b");
507 verifyMode(VOCAB_CM_HW_FAULT, VOCAB_IM_STIFF,
"test21");
508 verifyAmplifier(0,
"test21b");
509 resetOriginalCurrentLimits();
512 setMode(VOCAB_CM_POSITION,VOCAB_IM_COMPLIANT);
513 checkControlModeWithImCompliant(VOCAB_CM_HW_FAULT,
"test22");
514 verifyAmplifier(0,
"test22b");
516 setMode(VOCAB_CM_POSITION_DIRECT,VOCAB_IM_COMPLIANT);
517 checkControlModeWithImCompliant(VOCAB_CM_HW_FAULT,
"test23");
518 verifyAmplifier(0,
"test23b");
520 setMode(VOCAB_CM_VELOCITY,VOCAB_IM_COMPLIANT);
521 checkControlModeWithImCompliant(VOCAB_CM_HW_FAULT,
"test24");
522 verifyAmplifier(0,
"test24b");
524 setMode(VOCAB_CM_TORQUE,VOCAB_IM_COMPLIANT);
525 checkControlModeWithImCompliant(VOCAB_CM_HW_FAULT,
"test25");
526 verifyAmplifier(0,
"test25b");
528 setMode(VOCAB_CM_MIXED,VOCAB_IM_COMPLIANT);
529 checkControlModeWithImCompliant(VOCAB_CM_HW_FAULT,
"test26");
530 verifyAmplifier(0,
"test26b");
532 setMode(VOCAB_CM_PWM, VOCAB_IM_COMPLIANT);
533 checkControlModeWithImCompliant(VOCAB_CM_HW_FAULT,
"test27");
534 verifyAmplifier(0,
"test27b");
536 setMode(VOCAB_CM_IDLE,VOCAB_IM_COMPLIANT);
537 checkControlModeWithImCompliant(VOCAB_CM_HW_FAULT,
"test28");
538 verifyAmplifier(0,
"test28b");
540 setMode(VOCAB_CM_POSITION,VOCAB_IM_COMPLIANT);
541 checkControlModeWithImCompliant(VOCAB_CM_HW_FAULT,
"test29");
542 verifyAmplifier(0,
"test29b");
544 setMode(VOCAB_CM_FORCE_IDLE,VOCAB_IM_COMPLIANT);
545 for (
int i=0; i<n_cmd_joints; i++)
547 if(jointTorqueCtrlEnabled[jointsList[i]])
549 verifyModeSingle(jointsList[i], VOCAB_CM_IDLE,VOCAB_IM_COMPLIANT,
"test30-comp");
553 verifyModeSingle(jointsList[i], VOCAB_CM_IDLE,VOCAB_IM_STIFF,
"test30-stiff");
556 verifyAmplifier(0,
"test30b");
558 setMode(VOCAB_CM_POSITION,VOCAB_IM_STIFF);
559 verifyMode(VOCAB_CM_POSITION,VOCAB_IM_STIFF,
"test31");
560 verifyAmplifier(0,
"test31b");
The test checks if the joint is able to go in all the available control/interaction modes and if tran...