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 "motorEncodersSignCheck.h"
31 using namespace robottestingframework;
33 using namespace yarp::os;
34 using namespace yarp::dev;
39 MotorEncodersSignCheck::MotorEncodersSignCheck() : yarp::robottestingframework::TestCase(
"MotorEncodersSignCheck") {
49 MotorEncodersSignCheck::~MotorEncodersSignCheck() { }
51 bool MotorEncodersSignCheck::setup(yarp::os::Property& property) {
53 if(property.check(
"name"))
54 setName(property.find(
"name").asString());
57 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(property.check(
"robot"),
"The robot name must be given as the test parameter!");
58 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(property.check(
"part"),
"The part name must be given as the test parameter!");
59 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(property.check(
"joints"),
"The joints list must be given as the test parameter!");
60 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(property.check(
"home"),
"The home position must be given as the test parameter!");
62 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(property.check(
"pwmStep"),
"The output_step must be given as the test parameter!");
63 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(property.check(
"pwmMax"),
"The output_max must be given as the test parameter!");
65 robotName =
property.find(
"robot").asString();
66 partName =
property.find(
"part").asString();
68 Bottle* homeBottle =
property.find(
"home").asList();
69 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(homeBottle!=0,
"unable to parse zero parameter");
71 Bottle* jointsBottle =
property.find(
"joints").asList();
72 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(jointsBottle!=0,
"unable to parse joints parameter");
74 Bottle* pwm_step_Bottle =
property.find(
"pwmStep").asList();
75 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(pwm_step_Bottle!=0,
"unable to parse pwmStep parameter");
77 Bottle* command_delay_Bottle =
property.find(
"commandDelay").asList();
78 if(command_delay_Bottle==0)
79 ROBOTTESTINGFRAMEWORK_TEST_REPORT(
"Command delay not configured. default value (0.1 sec) will be used ");
82 Bottle* pwm_max_Bottle =
property.find(
"pwmMax").asList();
83 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(pwm_max_Bottle!=0,
"unable to parse joints parameter");
85 Bottle* threshold_Bottle =
property.find(
"PosThreshold").asList();
86 if(threshold_Bottle==0)
87 ROBOTTESTINGFRAMEWORK_TEST_REPORT(
"Position threshold not configured. default value (5 deg) will be used ");
89 Bottle* pwm_start_Bottle =
property.find(
"pwmStart").asList();
90 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(pwm_start_Bottle!=0,
"unable to parse pwmStart parameter");
94 options.put(
"device",
"remote_controlboard");
95 options.put(
"remote",
"/"+robotName+
"/"+partName);
96 options.put(
"local",
"/mEncSignCheckTest/"+robotName+
"/"+partName);
98 dd =
new PolyDriver(options);
99 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->isValid(),
"Unable to open device driver");
100 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(ipwm),
"Unable to open pwm interface");
101 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(ienc),
"Unable to open encoders interface");
102 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(icmd),
"Unable to open control mode interface");
103 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(iimd),
"Unable to open interaction mode interface");
104 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(imenc),
"Unable to open interaction mode interface");
105 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(ipid),
"Unable to open ipidcontrol interface");
107 if (!ienc->getAxes(&n_part_joints))
109 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(
"unable to get the number of joints of the part");
112 int n_cmd_joints = jointsBottle->size();
113 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(n_cmd_joints>0 && n_cmd_joints<=n_part_joints,"invalid number of joints, it must be >0 & <= number of part joints
");
115 for (int i=0; i <n_cmd_joints; i++) jointsList.push_back(jointsBottle->get(i).asInt32());
117 home.resize (n_cmd_joints); for (int i=0; i< n_cmd_joints; i++) home[i]=homeBottle->get(i).asFloat64();
118 opl_step.resize (n_cmd_joints); for (int i=0; i< n_cmd_joints; i++) opl_step[i]=pwm_step_Bottle->get(i).asFloat64();
119 opl_max.resize (n_cmd_joints); for (int i=0; i< n_cmd_joints; i++) opl_max[i]=pwm_max_Bottle->get(i).asFloat64();
120 opl_start.resize(n_cmd_joints); for (int i=0; i< n_cmd_joints; i++) opl_start[i]=pwm_start_Bottle->get(i).asFloat64();
121 pos_threshold.resize (n_cmd_joints);
122 if(threshold_Bottle!=0)
124 for (int i=0; i< n_cmd_joints; i++)
125 pos_threshold[i]=threshold_Bottle->get(i).asFloat64();
129 for (int i=0; i< n_cmd_joints; i++)
133 opl_delay.resize (n_cmd_joints);
134 if(command_delay_Bottle!=0)
136 for (int i=0; i< n_cmd_joints; i++)
137 opl_delay[i]=command_delay_Bottle->get(i).asFloat64();
141 for (int i=0; i< n_cmd_joints; i++)
145 jPosMotion = new yarp::robottestingframework::jointsPosMotion(dd, jointsList);
146 jPosMotion->setTolerance(2.0);
147 jPosMotion->setTimeout(10); //10 sec
153 void MotorEncodersSignCheck::tearDown()
156 sprintf(buff,"Closing test module
");ROBOTTESTINGFRAMEWORK_TEST_REPORT(buff);
159 jPosMotion->setAndCheckPosControlMode();
160 jPosMotion->goTo(home);
164 if (dd) {delete dd; dd =0;}
168 void MotorEncodersSignCheck::setModeSingle(int i, int desired_control_mode, yarp::dev::InteractionModeEnum desired_interaction_mode)
170 icmd->setControlMode((int)jointsList[i],desired_control_mode);
171 iimd->setInteractionMode((int)jointsList[i],desired_interaction_mode);
172 yarp::os::Time::delay(0.010);
175 void MotorEncodersSignCheck::OplExecute(int i)
178 double time = yarp::os::Time::now();
179 double time_old = yarp::os::Time::now();
182 double const delta = 10;
183 bool not_moving = true;
184 double opl=opl_start[i];
186 ipwm->setRefDutyCycle((int)jointsList[i], opl);
187 double last_opl_cmd=yarp::os::Time::now();
188 yarp::os::Time::delay(3.0); //i need to wait a while because when i set ref output zero, joint may move (due to stiction or gravity) and I should save the position when pwm=0
190 imenc->getMotorEncoder((int)jointsList[i],&start_enc);
196 ipwm->setRefDutyCycle(jointsList[i], opl);
197 imenc->getMotorEncoder((int)jointsList[i],&enc);
199 if(enc > start_enc+pos_threshold[i])
201 ipwm->setRefDutyCycle((int)jointsList[i], 0.0);
203 sprintf(buff,"TEST SUCCESS (pwm=%f) enc=%f start_enc=%f
",opl, enc, start_enc);
204 ROBOTTESTINGFRAMEWORK_TEST_REPORT(buff);
206 else if(enc < start_enc-pos_threshold[i])
208 ipwm->setRefDutyCycle((int)jointsList[i], 0.0);
210 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(0, robottestingframework::Asserter::format("because enc readings drecrease enc=%f start_enc=%f (output=%f)
", enc, start_enc, opl));
212 else if (jPosMotion->checkJointLimitsReached((int)jointsList[i]))
214 ipwm->setRefDutyCycle((int)jointsList[i], 0.0);
216 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(0,robottestingframework::Asserter::format("Test failed because hw limit was touched (enc=%f)
",enc));
219 if (yarp::os::Time::now()-last_opl_cmd>opl_delay[i])
222 last_opl_cmd=yarp::os::Time::now();
226 ipwm->setRefDutyCycle((int)jointsList[i], 0.0);
228 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(0,robottestingframework::Asserter::format("Test failed failed because max output was reached(output=%f)
",opl));
231 yarp::os::Time::delay(0.010);
233 if (time-time_old>5.0 && not_moving==true)
235 sprintf(buff,"test in progress on joint %d, current output value = %f
",(int)jointsList[i],opl);ROBOTTESTINGFRAMEWORK_TEST_REPORT(buff);
241 void MotorEncodersSignCheck::run()
245 jPosMotion->setAndCheckPosControlMode();
246 jPosMotion->goTo(home);
249 for (unsigned int i=0 ; i<jointsList.size(); i++)
252 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE((ipid->getPidOutput(yarp::dev::PidControlTypeEnum::VOCAB_PIDTYPE_POSITION, (int)jointsList[i], &posout)),
253 robottestingframework::Asserter::format(" getOutput j %d
return false",(int)jointsList[i]));
255 setModeSingle(i,VOCAB_CM_PWM,VOCAB_IM_STIFF);
256 ipwm->setRefDutyCycle((int)jointsList[i],0.0);
258 ROBOTTESTINGFRAMEWORK_TEST_REPORT(robottestingframework::Asserter::format("Testing joint %d with starting pwm = %.2f. In position j had pwm = %.2f
",(int)jointsList[i], opl_start[i], posout));
261 jPosMotion->setAndCheckPosControlMode();
262 jPosMotion->goTo(home);