23 #include <robottestingframework/dll/Plugin.h>
24 #include <robottestingframework/TestAssert.h>
25 #include <yarp/robottestingframework/TestAsserter.h>
27 #include "MotorTest.h"
30 using namespace robottestingframework;
32 using namespace yarp::os;
35 ROBOTTESTINGFRAMEWORK_PREPARE_PLUGIN(
MotorTest)
37 MotorTest::MotorTest() : yarp::robottestingframework::TestCase(
"MotorTest") {
40 MotorTest::~MotorTest() {
43 bool MotorTest::setup(yarp::os::Property &configuration) {
57 if(configuration.check(
"name"))
58 setName(configuration.find(
"name").asString());
60 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(configuration.check(
"portname"),
61 "Missing 'portname' parameter, cannot open device");
62 m_portname = configuration.find(
"portname").asString();
64 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(configuration.check(
"joints"),
65 "Missing 'joints' parameter, cannot open device");
66 m_NumJoints=configuration.find(
"joints").asInt32();
68 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(configuration.check(
"target"),
69 "Missing 'target' parameter, cannot open device");
70 yarp::os::Bottle bot=configuration.findGroup(
"target").tail();
71 int n = m_NumJoints<bot.size()? m_NumJoints:bot.size();
72 m_aTargetVal=
new double[m_NumJoints];
73 m_aHome=
new double [m_NumJoints];
74 for (
int i=0; i<n; ++i) {
75 m_aTargetVal[i]=bot.get(i).asFloat64();
79 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(configuration.check(
"min"),
80 "Missing 'min' parameter, cannot open device");
81 bot = configuration.findGroup(
"min").tail();
82 n = m_NumJoints<bot.size()?m_NumJoints:bot.size();
83 m_aMinErr=
new double[m_NumJoints];
84 for (
int i=0; i<n; ++i)
85 m_aMinErr[i]=bot.get(i).asFloat64();
87 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(configuration.check(
"max"),
88 "Missing 'max' parameter, cannot open device");
89 bot=configuration.findGroup(
"max").tail();
90 n = m_NumJoints<bot.size()? m_NumJoints:bot.size();
91 m_aMaxErr=
new double[m_NumJoints];
92 for (
int i=0; i<n; ++i)
93 m_aMaxErr[i]=bot.get(i).asFloat64();
95 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(configuration.check(
"refvel"),
96 "Missing 'max' parameter, cannot open device");
97 bot = configuration.findGroup(
"refvel").tail();
98 n = m_NumJoints<bot.size()?m_NumJoints:bot.size();
99 m_aRefVel=
new double[m_NumJoints];
100 for (
int i=0; i<n; ++i)
101 m_aRefVel[i]=bot.get(i).asFloat64();
103 if(configuration.check(
"refacc")) {
104 bot = configuration.findGroup(
"refacc").tail();
105 n = m_NumJoints<bot.size()?m_NumJoints:bot.size();
106 m_aRefAcc=
new double[m_NumJoints];
107 for (
int i=0; i<n; ++i)
108 m_aRefAcc[i]=bot.get(i).asFloat64();
111 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(configuration.check(
"timeout"),
112 "Missing 'timeout' parameter, cannot open device");
113 bot = configuration.findGroup(
"timeout").tail();
114 n = m_NumJoints<bot.size()?m_NumJoints:bot.size();
115 m_aTimeout =
new double[m_NumJoints];
116 for (
int i=0; i<n; ++i)
117 m_aTimeout[i]=bot.get(i).asFloat64();
120 yarp::os::Property options;
121 options.put(
"device",
"remote_controlboard");
122 options.put(
"local",m_portname+
"/client");
123 options.put(
"remote",m_portname);
125 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(m_driver.open(options),
126 "cannot open driver");
128 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(m_driver.view(iEncoders),
"cannot view iEncoder");
129 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(m_driver.view(iPosition),
"cannot view iPosition");
134 void MotorTest::tearDown() {
137 ROBOTTESTINGFRAMEWORK_TEST_REPORT(
"Homing robot");
138 iPosition->positionMove(m_aHome);
141 double timeStart=yarp::os::Time::now();
142 double timeNow=timeStart;
143 while(timeNow<timeStart+m_aTimeout[0] && !reached) {
144 iPosition->checkMotionDone(&reached);
145 timeNow=yarp::os::Time::now();
146 yarp::os::Time::delay(0.1);
150 if (m_aTargetVal)
delete [] m_aTargetVal;
151 if (m_aMaxErr)
delete [] m_aMaxErr;
152 if (m_aMinErr)
delete [] m_aMinErr;
153 if (m_aRefVel)
delete [] m_aRefVel;
154 if (m_aRefAcc)
delete [] m_aRefAcc;
155 if (m_aTimeout)
delete [] m_aTimeout;
156 if (m_aHome)
delete [] m_aHome;
159 void MotorTest::run() {
165 ROBOTTESTINGFRAMEWORK_TEST_REPORT(
"checking joints number");
166 iEncoders->getAxes(&nJoints);
167 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(m_NumJoints==nJoints,
"expected number of joints is inconsistent");
169 ROBOTTESTINGFRAMEWORK_TEST_REPORT(
"Checking individual joints...");
170 for (
int joint=0; joint<m_NumJoints; ++joint) {
171 ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format(
"Checking joint %d", joint));
172 if (m_aRefAcc!=NULL) {
173 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iPosition->setRefAcceleration(joint, m_aRefAcc[joint]),
174 Asserter::format(
"setting reference acceleration on joint %d", joint));
177 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iPosition->setRefSpeed(joint, m_aRefVel[joint]),
178 Asserter::format(
"setting reference speed on joint %d", joint));
181 double timeStart=yarp::os::Time::now();
182 double timeNow=timeStart;
185 ROBOTTESTINGFRAMEWORK_TEST_REPORT(
"Checking encoders");
186 while(timeNow<timeStart+m_aTimeout[joint] && !read) {
188 read=iEncoders->getEncoder(joint,m_aHome+joint);
189 yarp::os::Time::delay(0.1);
191 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(read,
"getEncoder() returned true");
193 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iPosition->positionMove(joint, m_aTargetVal[joint]),
194 Asserter::format(
"moving joint %d to %.2lf", joint, m_aTargetVal[joint]));
197 ret=iPosition->checkMotionDone(joint, &doneAll);
198 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(!doneAll&&ret,
"checking checkMotionDone returns false after position move");
200 ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format(
"Waiting timeout %.2lf", m_aTimeout[joint]));
202 while(timeNow<timeStart+m_aTimeout[joint] && !reached) {
204 iEncoders->getEncoder(joint,&pos);
205 reached = yarp::robottestingframework::TestAsserter::isApproxEqual(pos, m_aTargetVal[joint], m_aMinErr[joint], m_aMaxErr[joint]);
206 timeNow=yarp::os::Time::now();
207 yarp::os::Time::delay(0.1);
209 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(reached,
"reached position");
213 ROBOTTESTINGFRAMEWORK_TEST_REPORT(
"Checking multiple joints...");
214 if (m_aRefAcc!=NULL) {
215 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iPosition->setRefAccelerations(m_aRefAcc),
216 "setting reference acceleration on all joints");
218 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iPosition->setRefSpeeds(m_aRefVel),
219 "setting reference speed on all joints");
221 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iPosition->positionMove(m_aHome),
222 "moving all joints to home");
226 ret=iPosition->checkMotionDone(&doneAll);
227 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(!doneAll&&ret,
"checking checkMotionDone returns false after position move");
230 double timeStart=yarp::os::Time::now();
231 double timeNow=timeStart;
233 double timeout=m_aTimeout[0];
234 for(
int j=0; j<m_NumJoints; j++)
236 if (timeout<m_aTimeout[j])
237 timeout=m_aTimeout[j];
240 ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format(
"Waiting timeout %.2lf", timeout));
243 encoders=
new double [m_NumJoints];
244 while(timeNow<timeStart+timeout && !reached) {
245 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iEncoders->getEncoders(encoders),
"getEncoders()");
246 reached = yarp::robottestingframework::TestAsserter::isApproxEqual(encoders, m_aHome, m_aMinErr, m_aMaxErr, m_NumJoints);
247 timeNow=yarp::os::Time::now();
248 yarp::os::Time::delay(0.1);
251 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(reached,
"reached position");
262 while(times>0 && !doneAll) {
263 ret=iPosition->checkMotionDone(&doneAll);
265 yarp::os::Time::delay(0.1);
269 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(doneAll&&ret,
"checking checkMotionDone returns true");
273 ROBOTTESTINGFRAMEWORK_TEST_REPORT(
"Now checking group interface");
276 int *jmap=
new int [m_NumJoints];
277 double *swapped_refvel=
new double [m_NumJoints];
278 double *swapped_target=
new double [m_NumJoints];
280 for(
int kk=0;kk<m_NumJoints;kk++)
282 swapped_refvel[kk]=m_aRefVel[m_NumJoints-kk-1];
283 swapped_target[kk]=m_aTargetVal[m_NumJoints-kk-1];
284 jmap[kk]=m_NumJoints-kk-1;
287 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iPosition->setRefSpeeds(m_NumJoints, jmap, swapped_refvel),
288 "setting reference speed on all joints using group interface");
290 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(iPosition->positionMove(m_NumJoints, jmap, swapped_target),
291 "moving all joints to home using group interface");
293 ret=iPosition->checkMotionDone(m_NumJoints, jmap, &doneAll);
294 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(!doneAll&&ret,
"checking checkMotionDone returns false after position move");
296 timeStart=yarp::os::Time::now();
299 ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format(
"Waiting timeout %.2lf", timeout));
301 while(timeNow<timeStart+timeout && !reached) {
302 iEncoders->getEncoders(encoders);
303 reached = yarp::robottestingframework::TestAsserter::isApproxEqual(encoders, m_aTargetVal, m_aMinErr, m_aMaxErr, m_NumJoints);
304 timeNow=yarp::os::Time::now();
305 yarp::os::Time::delay(0.1);
307 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(reached,
"reached position");
310 bool *done_vector=
new bool [m_NumJoints];
318 while(times>0 && !doneAll) {
319 ret=iPosition->checkMotionDone(m_NumJoints, jmap, &doneAll);
321 yarp::os::Time::delay(0.1);
325 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(doneAll&&ret,
"checking checkMotionDone");
326 delete [] done_vector;
331 delete [] swapped_refvel;
332 delete [] swapped_target;
Check IPositionControl and IEncoders.