22 #include <robottestingframework/TestAssert.h>
23 #include <robottestingframework/dll/Plugin.h>
24 #include <yarp/os/Time.h>
25 #include <yarp/os/Property.h>
27 #include "TorqueControlConsistency.h"
33 using namespace robottestingframework;
34 using namespace yarp::os;
35 using namespace yarp::dev;
38 ROBOTTESTINGFRAMEWORK_PREPARE_PLUGIN(TorqueControlConsistency)
40 TorqueControlConsistency::TorqueControlConsistency() : yarp::robottestingframework::TestCase(
"TorqueControlConsistency") {
56 TorqueControlConsistency::~TorqueControlConsistency() { }
58 bool TorqueControlConsistency::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(
"zero"),
"The zero position must be given as the test parameter!");
70 robotName =
property.find(
"robot").asString();
71 partName =
property.find(
"part").asString();
73 zero =
property.find(
"zero").asFloat64();
75 Bottle* jointsBottle =
property.find(
"joints").asList();
76 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(jointsBottle!=0,
"unable to parse joints parameter");
77 n_cmd_joints = jointsBottle->size();
78 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(n_cmd_joints>0,
"invalid number of joints, it must be >0");
81 options.put(
"device",
"remote_controlboard");
82 options.put(
"remote",
"/"+robotName+
"/"+partName);
83 options.put(
"local",
"/TorqueControlConsistencyTest/"+robotName+
"/"+partName);
85 dd =
new PolyDriver(options);
86 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->isValid(),
"Unable to open device driver");
87 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(itrq),
"Unable to open torque control interface");
88 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(ienc),
"Unable to open encoders interface");
89 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(iamp),
"Unable to open ampliefier interface");
90 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(ipos),
"Unable to open position interface");
91 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(icmd),
"Unable to open control mode interface");
92 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(iimd),
"Unable to open interaction mode interface");
94 if (!ienc->getAxes(&n_part_joints))
96 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(
"unable to get the number of joints of the part");
100 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(
"Error this part has in invalid (<=0) number of jonits");
101 else if (jointsBottle->size() == 1)
102 cmd_mode=single_joint;
103 else if (jointsBottle->size() < n_part_joints)
104 cmd_mode=some_joints;
105 else if (jointsBottle->size() == n_part_joints)
108 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(
"invalid joint selection?");
110 cmd_tot =
new double[n_part_joints];
111 pos_tot=
new double[n_part_joints];
112 jointsList=
new int[n_cmd_joints];
113 cmd_some=
new double[n_cmd_joints];
114 prevcurr_tot=
new double[n_part_joints];
115 prevcurr_some=
new double[n_cmd_joints];
116 for (
int i=0; i <n_cmd_joints; i++) jointsList[i]=jointsBottle->get(i).asInt32();
121 void TorqueControlConsistency::tearDown()
123 if (jointsList) {
delete jointsList; jointsList =0;}
124 if (dd) {
delete dd; dd =0;}
127 void TorqueControlConsistency::setMode(
int desired_control_mode, yarp::dev::InteractionModeEnum desired_interaction_mode)
129 for (
int i=0; i<n_cmd_joints; i++)
131 icmd->setControlMode(jointsList[i],desired_control_mode);
132 iimd->setInteractionMode(jointsList[i],desired_interaction_mode);
133 yarp::os::Time::delay(0.010);
137 void TorqueControlConsistency::verifyMode(
int desired_control_mode, yarp::dev::InteractionModeEnum desired_interaction_mode, std::string title)
140 yarp::dev::InteractionModeEnum imode;
146 for (
int i=0; i<n_cmd_joints; i++)
148 icmd->getControlMode (jointsList[i],&cmode);
149 iimd->getInteractionMode(jointsList[i],&imode);
150 if (cmode==desired_control_mode && imode==desired_interaction_mode) ok++;
152 if (ok==n_cmd_joints)
break;
156 sprintf(sbuf,
"Test (%s) failed: current mode is (%d,%d), it should be (%d,%d)",title.c_str(), desired_control_mode,desired_interaction_mode,cmode,imode);
157 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(sbuf);
159 yarp::os::Time::delay(0.2);
163 sprintf(sbuf,
"Test (%s) passed: current mode is (%d,%d)",title.c_str(), desired_control_mode,desired_interaction_mode);
164 ROBOTTESTINGFRAMEWORK_TEST_REPORT(sbuf);
167 void TorqueControlConsistency::goHome()
169 for (
int i=0; i<n_cmd_joints; i++)
171 ipos->setRefSpeed(jointsList[i],20.0);
172 ipos->positionMove(jointsList[i],zero);
179 for (
int i=0; i<n_cmd_joints; i++)
181 ienc->getEncoder(jointsList[i],&pos_tot[jointsList[i]]);
182 if (fabs(pos_tot[jointsList[i]]-zero)<0.5) in_position++;
184 if (in_position==n_cmd_joints)
break;
187 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(
"Timeout while reaching zero position");
189 yarp::os::Time::delay(0.2);
194 void TorqueControlConsistency::setRefTorque(
double value)
197 if (cmd_mode==single_joint)
199 for (
int i=0; i<n_cmd_joints; i++)
201 itrq->setRefTorque(jointsList[i],cmd_single);
204 else if (cmd_mode==some_joints)
207 for (
int i=0; i<n_cmd_joints; i++)
209 itrq->setRefTorque(jointsList[i],cmd_single);
212 else if (cmd_mode==all_joints)
214 for (
int i=0; i<n_part_joints; i++)
216 cmd_tot[i]=cmd_single;
218 itrq->setRefTorques(cmd_tot);
222 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(
"Invalid cmd_mode");
224 yarp::os::Time::delay(0.010);
227 void TorqueControlConsistency::verifyRefTorque(
double verify_val, std::string title)
231 if (cmd_mode==single_joint)
233 for (
int i=0; i<n_cmd_joints; i++)
235 itrq->getRefTorque(jointsList[i],&value);
236 if (value==verify_val)
238 sprintf(sbuf,
"Test (%s) passed, j%d current reference is (%f)",title.c_str(),i, verify_val);
239 ROBOTTESTINGFRAMEWORK_TEST_REPORT(sbuf);
243 sprintf(sbuf,
"Test (%s) failed: current reference is (%f), it should be (%f)",title.c_str(), value, verify_val);
244 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(sbuf);
248 else if (cmd_mode==some_joints)
251 for (
int i=0; i<n_cmd_joints; i++)
253 itrq->getRefTorque(jointsList[i],&value);
254 if (value==verify_val)
256 sprintf(sbuf,
"Test (%s) passed j%d current reference is (%f)",title.c_str(),i, verify_val);
257 ROBOTTESTINGFRAMEWORK_TEST_REPORT(sbuf);
261 sprintf(sbuf,
"Test (%s) failed: current reference is (%f), it should be (%f)",title.c_str(), value, verify_val);
262 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(sbuf);
266 else if (cmd_mode==all_joints)
269 itrq->getRefTorques(cmd_tot);
270 for (
int i=0; i<n_part_joints; i++)
272 if (verify_val==cmd_tot[i]) ok++;
274 if (ok==n_part_joints)
276 sprintf(sbuf,
"Test (%s) passed, current reference is (%f)",title.c_str(), verify_val);
277 ROBOTTESTINGFRAMEWORK_TEST_REPORT(sbuf);
281 sprintf(sbuf,
"Test (%s) failed: only %d joints (of %d) are ok",title.c_str(),ok,n_part_joints);
282 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(sbuf);
287 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(
"Invalid cmd_mode");
289 yarp::os::Time::delay(0.010);
292 void TorqueControlConsistency::run()
294 setMode(VOCAB_CM_POSITION,VOCAB_IM_STIFF);
295 verifyMode(VOCAB_CM_POSITION,VOCAB_IM_STIFF,
"test0");
298 setMode(VOCAB_CM_TORQUE,VOCAB_IM_STIFF);
299 verifyMode(VOCAB_CM_TORQUE,VOCAB_IM_STIFF,
"test1");
301 verifyMode(VOCAB_CM_TORQUE,VOCAB_IM_STIFF,
"test2");
302 verifyRefTorque(0.1,
"test2a");
303 verifyMode(VOCAB_CM_TORQUE,VOCAB_IM_STIFF,
"test2b");
306 verifyMode(VOCAB_CM_TORQUE,VOCAB_IM_STIFF,
"test3");
307 verifyRefTorque(0,
"test3a");
308 verifyMode(VOCAB_CM_TORQUE,VOCAB_IM_STIFF,
"test3b");
310 verifyMode(VOCAB_CM_TORQUE,VOCAB_IM_STIFF,
"test4");
311 verifyRefTorque(-0.1,
"test4a");
312 verifyMode(VOCAB_CM_TORQUE,VOCAB_IM_STIFF,
"test4b");
314 setMode(VOCAB_CM_POSITION,VOCAB_IM_STIFF);
315 verifyMode(VOCAB_CM_POSITION,VOCAB_IM_STIFF,
"test5");