23 #include <robottestingframework/TestAssert.h>
24 #include <robottestingframework/dll/Plugin.h>
25 #include <yarp/os/Time.h>
26 #include <yarp/os/Property.h>
28 #include "OpenloopConsistency.h"
34 using namespace robottestingframework;
35 using namespace yarp::os;
36 using namespace yarp::dev;
39 ROBOTTESTINGFRAMEWORK_PREPARE_PLUGIN(OpenLoopConsistency)
41 OpenLoopConsistency::OpenLoopConsistency() : yarp::robottestingframework::TestCase(
"OpenLoopConsistency") {
57 OpenLoopConsistency::~OpenLoopConsistency() { }
59 bool OpenLoopConsistency::setup(yarp::os::Property& property) {
61 if(property.check(
"name"))
62 setName(property.find(
"name").asString());
65 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(property.check(
"robot"),
"The robot name must be given as the test parameter!");
66 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(property.check(
"part"),
"The part name must be given as the test parameter!");
67 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(property.check(
"joints"),
"The joints list must be given as the test parameter!");
68 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(property.check(
"home"),
"The home positions must be given as the test parameter!");
70 robotName =
property.find(
"robot").asString();
71 partName =
property.find(
"part").asString();
73 Bottle* homeBottle =
property.find(
"home").asList();
74 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(homeBottle!=0,
"unable to parse home parameter");
76 Bottle* jointsBottle =
property.find(
"joints").asList();
77 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(jointsBottle!=0,
"unable to parse joints parameter");
78 n_cmd_joints = jointsBottle->size();
79 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(n_cmd_joints>0,
"invalid number of joints, it must be >0");
82 options.put(
"device",
"remote_controlboard");
83 options.put(
"remote",
"/"+robotName+
"/"+partName);
84 options.put(
"local",
"/OpenLoopConsistencyTest/"+robotName+
"/"+partName);
86 dd =
new PolyDriver(options);
87 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->isValid(),
"Unable to open device driver");
88 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(ipwm),
"Unable to open pwm control interface");
89 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(ienc),
"Unable to open encoders interface");
90 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(iamp),
"Unable to open ampliefier interface");
91 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(ipos),
"Unable to open position interface");
92 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(icmd),
"Unable to open control mode interface");
93 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(iimd),
"Unable to open interaction mode interface");
95 if (!ienc->getAxes(&n_part_joints))
97 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(
"unable to get the number of joints of the part");
100 if (n_part_joints<=0)
101 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(
"Error this part has in invalid (<=0) number of jonits");
102 else if (jointsBottle->size() == 1)
103 cmd_mode=single_joint;
104 else if (jointsBottle->size() < n_part_joints)
105 cmd_mode=some_joints;
106 else if (jointsBottle->size() == n_part_joints)
109 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(
"invalid joint selection?");
111 cmd_tot =
new double[n_part_joints];
112 pos_tot=
new double[n_part_joints];
113 jointsList=
new int[n_cmd_joints];
114 cmd_some=
new double[n_cmd_joints];
115 prevcurr_tot=
new double[n_part_joints];
116 prevcurr_some=
new double[n_cmd_joints];
117 home=
new double[n_cmd_joints];
118 for (
int i=0; i <n_cmd_joints; i++) jointsList[i]=jointsBottle->get(i).asInt32();
119 for (
int i=0; i <n_cmd_joints; i++) home[i]=homeBottle->get(i).asFloat64();
123 void OpenLoopConsistency::tearDown()
126 setMode(VOCAB_CM_POSITION,VOCAB_IM_STIFF);
130 if (jointsList) {
delete jointsList; jointsList =0;}
131 if(home){
delete [] home; home=0;}
132 if (dd) {
delete dd; dd =0;}
135 void OpenLoopConsistency::setMode(
int desired_control_mode, yarp::dev::InteractionModeEnum desired_interaction_mode)
137 for (
int i=0; i<n_cmd_joints; i++)
139 icmd->setControlMode(jointsList[i],desired_control_mode);
140 iimd->setInteractionMode(jointsList[i],desired_interaction_mode);
141 yarp::os::Time::delay(0.010);
145 void OpenLoopConsistency::verifyMode(
int desired_control_mode, yarp::dev::InteractionModeEnum desired_interaction_mode, std::string title)
148 yarp::dev::InteractionModeEnum imode;
154 for (
int i=0; i<n_cmd_joints; i++)
156 icmd->getControlMode (jointsList[i],&cmode);
157 iimd->getInteractionMode(jointsList[i],&imode);
158 if (cmode==desired_control_mode && imode==desired_interaction_mode) ok++;
160 if (ok==n_cmd_joints)
break;
164 sprintf(sbuf,
"Test (%s) failed: current mode is (%s,%s), it should be (%s,%s)",title.c_str(), Vocab32::decode((NetInt32)desired_control_mode).c_str(),Vocab32::decode((NetInt32)desired_interaction_mode).c_str(), Vocab32::decode((NetInt32)cmode).c_str(),Vocab32::decode((NetInt32)imode).c_str());
165 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(sbuf);
167 yarp::os::Time::delay(0.2);
171 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());
172 ROBOTTESTINGFRAMEWORK_TEST_REPORT(sbuf);
175 void OpenLoopConsistency::goHome()
177 for (
int i=0; i<n_cmd_joints; i++)
179 ipos->setRefSpeed(jointsList[i],20.0);
180 ipos->positionMove(jointsList[i],home[i]);
187 for (
int i=0; i<n_cmd_joints; i++)
189 ienc->getEncoder(jointsList[i],&pos_tot[jointsList[i]]);
190 if (fabs(pos_tot[jointsList[i]]-home[i])<0.5) in_position++;
192 if (in_position==n_cmd_joints)
break;
195 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(
"Timeout while reaching home position");
197 yarp::os::Time::delay(0.2);
202 void OpenLoopConsistency::setRefOpenloop(
double value)
205 if (cmd_mode==single_joint)
207 for (
int i=0; i<n_cmd_joints; i++)
209 ipwm->setRefDutyCycle(jointsList[i], cmd_single);
212 else if (cmd_mode==some_joints)
215 for (
int i=0; i<n_cmd_joints; i++)
217 ipwm->setRefDutyCycle(jointsList[i], cmd_single);
220 else if (cmd_mode==all_joints)
222 for (
int i=0; i<n_part_joints; i++)
224 cmd_tot[i]=cmd_single;
226 ipwm->setRefDutyCycles(cmd_tot);
230 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(
"Invalid cmd_mode");
232 yarp::os::Time::delay(0.010);
235 void OpenLoopConsistency::verifyRefOpenloop(
double verify_val, std::string title)
239 if (cmd_mode==single_joint)
241 for (
int i=0; i<n_cmd_joints; i++)
243 ipwm->getRefDutyCycle(jointsList[i], &value);
244 if (value==verify_val)
246 sprintf(sbuf,
"Test (%s) passed, j%d current reference is (%f)",title.c_str(),i, verify_val);
247 ROBOTTESTINGFRAMEWORK_TEST_REPORT(sbuf);
251 sprintf(sbuf,
"Test (%s) failed: current reference is (%f), it should be (%f)",title.c_str(), value, verify_val);
252 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(sbuf);
256 else if (cmd_mode==some_joints)
259 for (
int i=0; i<n_cmd_joints; i++)
261 ipwm->getRefDutyCycle(jointsList[i], &value);
262 if (value==verify_val)
264 sprintf(sbuf,
"Test (%s) passed j%d current reference is (%f)",title.c_str(),i, verify_val);
265 ROBOTTESTINGFRAMEWORK_TEST_REPORT(sbuf);
269 sprintf(sbuf,
"Test (%s) failed: current reference is (%f), it should be (%f)",title.c_str(), value, verify_val);
270 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(sbuf);
274 else if (cmd_mode==all_joints)
277 ipwm->getRefDutyCycles(cmd_tot);
278 for (
int i=0; i<n_part_joints; i++)
280 if (verify_val==cmd_tot[i]) ok++;
282 if (ok==n_part_joints)
284 sprintf(sbuf,
"Test (%s) passed, current reference is (%f)",title.c_str(), verify_val);
285 ROBOTTESTINGFRAMEWORK_TEST_REPORT(sbuf);
289 sprintf(sbuf,
"Test (%s) failed: only %d joints (of %d) are ok",title.c_str(),ok,n_part_joints);
290 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(sbuf);
295 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(
"Invalid cmd_mode");
297 yarp::os::Time::delay(0.010);
300 void OpenLoopConsistency::verifyOutputEqual(
double verify_val, std::string title)
304 if (cmd_mode==single_joint)
306 for (
int i=0; i<n_cmd_joints; i++)
308 ipwm->getDutyCycle(jointsList[i], &value);
309 if (value==verify_val)
311 sprintf(sbuf,
"Test (%s) passed, j%d current output is (%f)",title.c_str(),i, verify_val);
312 ROBOTTESTINGFRAMEWORK_TEST_REPORT(sbuf);
316 sprintf(sbuf,
"Test (%s) failed: current output is (%f), it should be (%f)",title.c_str(), value, verify_val);
317 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(sbuf);
321 else if (cmd_mode==some_joints)
324 for (
int i=0; i<n_cmd_joints; i++)
326 ipwm->getDutyCycle(jointsList[i], &value);
327 if (value==verify_val)
329 sprintf(sbuf,
"Test (%s) passed j%d current output is (%f)",title.c_str(),i,verify_val);
330 ROBOTTESTINGFRAMEWORK_TEST_REPORT(sbuf);
334 sprintf(sbuf,
"Test (%s) failed: current output is (%f), it should be (%f)",title.c_str(), value, verify_val);
335 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(sbuf);
339 else if (cmd_mode==all_joints)
342 ipwm->getDutyCycles(cmd_tot);
343 for (
int i=0; i<n_part_joints; i++)
345 if (verify_val==cmd_tot[i]) ok++;
348 ROBOTTESTINGFRAMEWORK_TEST_REPORT(robottestingframework::Asserter::format(
"verify_val=%.2f, read_val=%.2f j=%d",verify_val, cmd_tot[i], i ));
351 if (ok==n_part_joints)
353 sprintf(sbuf,
"Test (%s) passed current output is (%f)",title.c_str(), verify_val);
354 ROBOTTESTINGFRAMEWORK_TEST_REPORT(sbuf);
358 sprintf(sbuf,
"Test (%s) failed: only %d joints (of %d) are ok",title.c_str(),ok,n_part_joints);
359 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(sbuf);
364 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(
"Invalid cmd_mode");
366 yarp::os::Time::delay(0.010);
369 void OpenLoopConsistency::verifyOutputDiff(
double verify_val, std::string title)
373 if (cmd_mode==single_joint)
375 for (
int i=0; i<n_cmd_joints; i++)
377 ipwm->getDutyCycle(jointsList[i], &value);
378 if (value!=verify_val)
380 sprintf(sbuf,
"Test (%s) passed j%d, current output is (%f!=%f)",title.c_str(),i,value,verify_val);
381 ROBOTTESTINGFRAMEWORK_TEST_REPORT(sbuf);
385 sprintf(sbuf,
"Test (%s) failed: current output is (%f), it should be (%f)",title.c_str(), value, verify_val);
386 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(sbuf);
390 else if (cmd_mode==some_joints)
393 for (
int i=0; i<n_cmd_joints; i++)
395 ipwm->getDutyCycle(jointsList[i], &value);
396 if (value!=verify_val)
398 sprintf(sbuf,
"Test (%s) passed j%d current output is (%f!=%f)",title.c_str(), i,value,verify_val);
399 ROBOTTESTINGFRAMEWORK_TEST_REPORT(sbuf);
403 sprintf(sbuf,
"Test (%s) failed: current output is (%f), it should be (%f)",title.c_str(), value, verify_val);
404 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(sbuf);
408 else if (cmd_mode==all_joints)
411 ipwm->getDutyCycles(cmd_tot);
412 for (
int i=0; i<n_part_joints; i++)
414 if (verify_val!=cmd_tot[i]) ok++;
416 if (ok==n_part_joints)
418 sprintf(sbuf,
"Test (%s) passed current output is (%f!=%f)",title.c_str(),value,verify_val);
419 ROBOTTESTINGFRAMEWORK_TEST_REPORT(sbuf);
423 sprintf(sbuf,
"Test (%s) failed: only %d joints (of %d) are ok",title.c_str(),ok,n_part_joints);
424 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(sbuf);
429 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(
"Invalid cmd_mode");
431 yarp::os::Time::delay(0.010);
434 void OpenLoopConsistency::run()
436 setMode(VOCAB_CM_POSITION,VOCAB_IM_STIFF);
437 verifyMode(VOCAB_CM_POSITION,VOCAB_IM_STIFF,
"test0");
442 setMode(VOCAB_CM_PWM,VOCAB_IM_STIFF);
443 verifyMode(VOCAB_CM_PWM, VOCAB_IM_STIFF,
"test1");
444 verifyRefOpenloop(0,
"test0a");
445 verifyOutputEqual(0,
"test1a");
447 verifyMode(VOCAB_CM_PWM, VOCAB_IM_STIFF,
"test2");
448 verifyRefOpenloop(1,
"test2a");
449 verifyOutputEqual(1,
"test2b");
450 verifyMode(VOCAB_CM_PWM, VOCAB_IM_STIFF,
"test3");
452 verifyMode(VOCAB_CM_PWM, VOCAB_IM_STIFF,
"test4");
453 verifyRefOpenloop(0,
"test3a");
454 verifyOutputEqual(0,
"test3b");
455 verifyMode(VOCAB_CM_PWM, VOCAB_IM_STIFF,
"test5");
457 verifyMode(VOCAB_CM_PWM, VOCAB_IM_STIFF,
"test6");
458 verifyRefOpenloop(-1,
"test6a");
459 verifyOutputEqual(-1,
"test6b");
461 setMode(VOCAB_CM_POSITION,VOCAB_IM_STIFF);
462 verifyMode(VOCAB_CM_POSITION,VOCAB_IM_STIFF,
"test7");
464 verifyRefOpenloop(-1,
"test7a");