test_robot_lib.cpp
Go to the documentation of this file.
00001 
00027 #include "sr_robot_lib/sr_motor_hand_lib.hpp"
00028 #include <sr_mechanism_model/simple_transmission.hpp>
00029 #include <gtest/gtest.h>
00030 #include <ros/ros.h>
00031 #include <utility>
00032 #include <string>
00033 #include <vector>
00034 
00035 #define error_flag_names palm_0200_edc_error_flag_names
00036 #define STATUS_TYPE ETHERCAT_DATA_STRUCTURE_0230_PALM_EDC_STATUS
00037 #define COMMAND_TYPE ETHERCAT_DATA_STRUCTURE_0230_PALM_EDC_COMMAND
00038 
00039 class HandLibTestProtected :
00040         public shadow_robot::SrMotorHandLib<STATUS_TYPE, COMMAND_TYPE>
00041 {
00042 public:
00043   HandLibTestProtected(hardware_interface::HardwareInterface *hw, ros::NodeHandle nh)
00044           : shadow_robot::SrMotorHandLib<STATUS_TYPE, COMMAND_TYPE>(hw, nh, nh, "", "rh_")
00045   {
00046   };
00047 
00048 public:
00049   using shadow_robot::SrMotorHandLib<STATUS_TYPE, COMMAND_TYPE>::joints_vector;
00050   using shadow_robot::SrMotorHandLib<STATUS_TYPE, COMMAND_TYPE>::humanize_flags;
00051 };
00052 
00053 class HandLibTest
00054 {
00055   ros::NodeHandle nh;
00056 public:
00057   ros_ethercat_model::RobotState *hw = NULL;
00058   boost::shared_ptr<HandLibTestProtected> sr_hand_lib;
00059   sr_actuator::SrMotorActuator *actuator;
00060   XmlRpc::XmlRpcValue joint_to_sensor_mapping;
00061 
00062   HandLibTest()
00063   {
00064     TiXmlElement *root;
00065     TiXmlElement *root_element;
00066     TiXmlDocument xml;
00067     std::string robot_description;
00068     if (ros::param::get("/robot_description", robot_description))
00069     {
00070       xml.Parse(robot_description.c_str());
00071     }
00072     else
00073     {
00074       ROS_ERROR_STREAM("Could not load the xml from parameter server");
00075     }
00076     root_element = xml.RootElement();
00077     root = xml.FirstChildElement("robot");
00078 
00079     nh.getParam("joint_to_sensor_mapping", joint_to_sensor_mapping);
00080     hw = new ros_ethercat_model::RobotState(root);
00081 
00082     hardware_interface::HardwareInterface *ehw = static_cast<hardware_interface::HardwareInterface *>(hw);
00083     sr_hand_lib.reset(new HandLibTestProtected(ehw, nh));
00084   }
00085 
00086   ~HandLibTest()
00087   {
00088     delete hw;
00089   }
00090 
00091   void check_hw_actuator(std::string name, int motor_id, int id_in_enum, double expected_pos)
00092   {
00093     actuator = static_cast<sr_actuator::SrMotorActuator *>(hw->getActuator(name));
00094 
00095     EXPECT_EQ(actuator->state_.device_id_, motor_id);
00096     // EXPECT_EQ(state.position_ , expected_pos);
00097   }
00098 };
00099 
00103 TEST(SrRobotLib, Initialization)
00104 {
00105   boost::shared_ptr<HandLibTest> lib_test = boost::shared_ptr<HandLibTest>(new HandLibTest());
00106 
00107 //  pr2_hardware_interface::HardwareInterface *hw;
00108 //  hw = new pr2_hardware_interface::HardwareInterface();
00109 //  boost::shared_ptr< shadow_robot::SrMotorHandLib<STATUS_TYPE, COMMAND_TYPE> > lib_test =
00110 // boost::shared_ptr< shadow_robot::SrMotorHandLib<STATUS_TYPE, COMMAND_TYPE> >(
00111 // new shadow_robot::SrMotorHandLib<STATUS_TYPE, COMMAND_TYPE>(hw) );
00112 
00113   EXPECT_TRUE(true);
00114 }
00115 
00119 TEST(SrRobotLib, UpdateMotor)
00120 {
00121   boost::shared_ptr<HandLibTest> lib_test = boost::shared_ptr<HandLibTest>(new HandLibTest());
00122 
00123   STATUS_TYPE *status_data = new STATUS_TYPE();
00124   // add growing sensors values
00125   for (unsigned int i = 1; i < SENSORS_NUM_0220 + 2; ++i)
00126   {
00127     // position = id in joint enum
00128     status_data->sensors[i] = i;
00129   }
00130 
00131   status_data->motor_data_type = MOTOR_DATA_SGR;
00132 
00133   // even motors
00134   status_data->which_motors = 0;
00135 
00136   // all motor data arrived with no errors
00137   status_data->which_motor_data_arrived = 0x00055555;
00138   status_data->which_motor_data_had_errors = 0;
00139 
00140   // add growing motor data packet values
00141   for (unsigned int i = 0; i < 10; ++i)
00142   {
00143     status_data->motor_data_packet[i].torque = 4;
00144     status_data->motor_data_packet[i].misc = 2 * i;
00145   }
00146   // filling the status data with known values
00147   status_data->idle_time_us = 1;
00148 
00149   // update the library
00150   lib_test->sr_hand_lib->update(status_data);
00151 
00152   // check the data we read back are correct.
00153   EXPECT_EQ(lib_test->sr_hand_lib->main_pic_idle_time, 1);
00154   EXPECT_EQ(lib_test->sr_hand_lib->main_pic_idle_time_min, 1);
00155 
00156   // check the sensors etc..
00157   std::vector<shadow_joints::Joint>::iterator joint_tmp = lib_test->sr_hand_lib->joints_vector.begin();
00158   for (; joint_tmp != lib_test->sr_hand_lib->joints_vector.end(); ++joint_tmp)
00159   {
00160     if (joint_tmp->has_actuator)
00161     {
00162       boost::shared_ptr<shadow_joints::MotorWrapper> motor_wrapper =
00163               boost::static_pointer_cast<shadow_joints::MotorWrapper>(joint_tmp->actuator_wrapper);
00164       // we updated the even motors
00165       if (motor_wrapper->motor_id % 2 == 0)
00166       {
00167         const sr_actuator::SrMotorActuator *sr_actuator =
00168                 static_cast<sr_actuator::SrMotorActuator *>(motor_wrapper->actuator);
00169 
00170         ROS_ERROR_STREAM("last measured effort: " << sr_actuator->state_.last_measured_effort_ << " actuator: " <<
00171                          motor_wrapper->actuator);
00172 
00173         EXPECT_FLOAT_EQ(sr_actuator->motor_state_.force_unfiltered_, 4.0);  // (double)motor_wrapper->motor_id/2.0);
00174         EXPECT_EQ(sr_actuator->motor_state_.strain_gauge_right_, motor_wrapper->motor_id);
00175       }
00176     }
00177   }
00178   delete status_data;
00179 }
00180 
00186 TEST(SrRobotLib, UpdateActuators)
00187 {
00188   boost::shared_ptr<HandLibTest> lib_test = boost::shared_ptr<HandLibTest>(new HandLibTest());
00189 
00190   STATUS_TYPE *status_data = new STATUS_TYPE();
00191   // add growing sensors values
00192   for (unsigned int i = 0; i < SENSORS_NUM_0220 + 1; ++i)
00193   {
00194     // position = id in joint enum
00195     status_data->sensors[i] = i + 1;
00196   }
00197 
00198   status_data->motor_data_type = MOTOR_DATA_VOLTAGE;
00199 
00200   // even motors
00201   status_data->which_motors = 0;
00202 
00203   // all motor data arrived with no errors
00204   status_data->which_motor_data_arrived = 0x00055555;
00205   status_data->which_motor_data_had_errors = 0;
00206 
00207   // add growing motor data packet values
00208   for (unsigned int i = 0; i < 10; ++i)
00209   {
00210     status_data->motor_data_packet[i].torque = i;
00211     status_data->motor_data_packet[i].misc = 10 * i;
00212   }
00213   // filling the status data with known values
00214   status_data->idle_time_us = 1;
00215 
00216   // update the library
00217   lib_test->sr_hand_lib->update(status_data);
00218 
00219   // name, motor_id, id_in_enum, expected_pos
00220   lib_test->check_hw_actuator("rh_FFJ4", 2, 3, 4.0);
00221 
00222   // cleanup
00223   delete status_data;
00224 }
00225 
00232 class TestHandLib
00233         :
00234                 public HandLibTestProtected
00235 {
00236 public:
00237   TestHandLib(hardware_interface::HardwareInterface *hw, ros::NodeHandle nh)
00238           : HandLibTestProtected(hw, nh)
00239   {
00240   }
00241 
00242   using HandLibTestProtected::calibrate_joint;
00243 
00244   // using HandLibTestProtected::status_data;
00245 
00246   // using HandLibTestProtected::actuator;
00247 
00248   using HandLibTestProtected::humanize_flags;
00249 };
00250 
00251 
00257 // TEST(SrRobotLib, CalibrationOneMotor)
00258 // {
00259 
00260 //   pr2_hardware_interface::HardwareInterface *hw;
00261 //   boost::shared_ptr<TestHandLib> sr_hand_lib = boost::shared_ptr<TestHandLib>( new TestHandLib(hw) );
00262 
00263 //   STATUS_TYPE status_data;
00264 
00265 //  // set all the sensors to 0
00266 //   for(unsigned int i = 0; i < SENSORS_NUM_0220 + 1; ++i)
00267 //     status_data.sensors[i] = 0;
00268 
00269 //   sr_hand_lib->status_data = &status_data;
00270 //   for(unsigned int i = 0; i < SENSORS_NUM_0220 + 1; ++i)
00271 //     EXPECT_EQ(sr_hand_lib->status_data->sensors[i], 0);
00272 
00273 //  // let's find FFJ3 in the vector
00274 //   boost::ptr_vector<shadow_joints::Joint>::iterator ffj3 = sr_hand_lib->joints_vector.begin();
00275 //   std::string name_tmp = ffj3->joint_name;
00276 //   bool ffj3_found = false;
00277 //   int index_ffj3 = 0;
00278 
00279 //   for(; ffj3 != sr_hand_lib->joints_vector.end(); ++ffj3)
00280 //   {
00281 //     name_tmp = ffj3->joint_name;
00282 
00283 //     if( name_tmp.compare("FFJ3") == 0)
00284 //     {
00285 //       ffj3_found = true;
00286 //       break;
00287 //     }
00288 
00289 //     ++index_ffj3;
00290 //   }
00291 
00292 //   EXPECT_TRUE(ffj3_found);
00293 //   EXPECT_EQ(index_ffj3, 3);
00294 
00295 //   sr_hand_lib->actuator = (ffj3->motor->actuator);
00296 
00297 //   sr_hand_lib->calibrate_joint(ffj3);
00298 //  // all the sensors at 0 -> should be 0
00299 //   EXPECT_EQ( ffj3->motor->actuator->state_.position_unfiltered_ , 0.0);
00300 
00301 //   for(unsigned int i = 0; i < SENSORS_NUM_0220 + 1; ++i)
00302 //   sr_hand_lib->status_data->sensors[i] = 1;
00303 
00304 //  // now ffj3 position should be 1
00305 //   sr_hand_lib->calibrate_joint(ffj3);
00306 //  // all the sensors at 1 -> should be 1
00307 //   EXPECT_EQ( ffj3->motor->actuator->state_.position_unfiltered_ , 1.0);
00308 
00309 //   delete hw;
00310 // }
00311 
00312 
00313 // /**
00314 //  * Testing the calibration procedure for
00315 //  * a joint having calibrating the sensors first
00316 //  * and then combining them (FFJ0)
00317 //  *
00318 //  */
00319 // TEST(SrRobotLib, CalibrationFFJ0)
00320 // {
00321 
00322 //   pr2_hardware_interface::HardwareInterface *hw;
00323 //   boost::shared_ptr<TestHandLib> sr_hand_lib = boost::shared_ptr<TestHandLib>( new TestHandLib(hw) );
00324 
00325 //   STATUS_TYPE status_data;
00326 
00327 //  // set all the sensors to 0
00328 //   for(unsigned int i = 0; i < SENSORS_NUM_0220 + 1; ++i)
00329 //     status_data.sensors[i] = 0;
00330 
00331 //   sr_hand_lib->status_data = &status_data;
00332 //   for(unsigned int i = 0; i < SENSORS_NUM_0220 + 1; ++i)
00333 //     EXPECT_EQ(sr_hand_lib->status_data->sensors[i], 0);
00334 
00335 //  // let's find FFJ0 in the vector
00336 //   boost::ptr_vector<shadow_joints::Joint>::iterator ffj0 = sr_hand_lib->joints_vector.begin();
00337 //   std::string name_tmp = ffj0->joint_name;
00338 //   bool ffj0_found = false;
00339 //   int index_ffj0 = 0;
00340 
00341 //   for(; ffj0 != sr_hand_lib->joints_vector.end(); ++ffj0)
00342 //   {
00343 //     name_tmp = ffj0->joint_name;
00344 
00345 //     if( name_tmp.compare("FFJ0") == 0)
00346 //     {
00347 //       ffj0_found = true;
00348 //       break;
00349 //     }
00350 
00351 //     ++index_ffj0;
00352 //   }
00353 
00354 //   EXPECT_TRUE(ffj0_found);
00355 //   EXPECT_EQ(index_ffj0, 0);
00356 
00357 //   sr_hand_lib->actuator = (ffj0->motor->actuator);
00358 
00359 //   sr_hand_lib->calibrate_joint(ffj0);
00360 //  // all the sensors at 0 -> should be 0
00361 //   EXPECT_EQ( ffj0->motor->actuator->state_.position_unfiltered_ , 0.0);
00362 
00363 //   for(unsigned int i = 0; i < SENSORS_NUM_0220 + 1; ++i)
00364 //     sr_hand_lib->status_data->sensors[i] = 1;
00365 
00366 //  // now ffj0 position should be 1
00367 //   sr_hand_lib->calibrate_joint(ffj0);
00368 //  // all the sensors at 1 -> should be 2
00369 //   EXPECT_EQ( ffj0->motor->actuator->state_.position_unfiltered_ , 2.0);
00370 
00371 //   delete hw;
00372 // }
00373 
00374 // /**
00375 //  * Testing the calibration procedure for
00376 //  * a compound joint combining the sensors
00377 //  * and then calibrating the total (THJ5)
00378 //  *
00379 //  */
00380 // TEST(SrRobotLib, CalibrationTHJ5)
00381 // {
00382 
00383 //   pr2_hardware_interface::HardwareInterface *hw;
00384 //   boost::shared_ptr<TestHandLib> sr_hand_lib = boost::shared_ptr<TestHandLib>( new TestHandLib(hw) );
00385 
00386 //   STATUS_TYPE status_data;
00387 
00388 //  // set all the sensors to 0
00389 //   for(unsigned int i = 0; i < SENSORS_NUM_0220 + 1; ++i)
00390 //     status_data.sensors[i] = 0;
00391 
00392 //   sr_hand_lib->status_data = &status_data;
00393 //   for(unsigned int i = 0; i < SENSORS_NUM_0220 + 1; ++i)
00394 //     EXPECT_EQ(sr_hand_lib->status_data->sensors[i], 0);
00395 
00396 //  // let's find THJ5 in the vector
00397 //   boost::ptr_vector<shadow_joints::Joint>::iterator thj5 = sr_hand_lib->joints_vector.begin();
00398 //   std::string name_tmp = thj5->joint_name;
00399 //   bool thj5_found = false;
00400 //   int index_thj5 = 0;
00401 
00402 //   for(; thj5 != sr_hand_lib->joints_vector.end(); ++thj5)
00403 //   {
00404 //     name_tmp = thj5->joint_name;
00405 
00406 //     if( name_tmp.compare("THJ5") == 0)
00407 //     {
00408 //       thj5_found = true;
00409 //       break;
00410 //     }
00411 
00412 //     ++index_thj5;
00413 //   }
00414 
00415 //   EXPECT_TRUE(thj5_found);
00416 //   EXPECT_EQ(index_thj5, 25);
00417 
00418 //   sr_hand_lib->actuator = (thj5->motor->actuator);
00419 
00420 //   sr_hand_lib->calibrate_joint(thj5);
00421 //  // all the sensors at 0 -> should be 0
00422 //   EXPECT_EQ( thj5->motor->actuator->state_.position_unfiltered_ , 0.0);
00423 
00424 //   for(unsigned int i = 0; i < SENSORS_NUM_0220 + 1; ++i)
00425 //     sr_hand_lib->status_data->sensors[i] = 1;
00426 
00427 //  // now thj5 position should be 1
00428 //   sr_hand_lib->calibrate_joint(thj5);
00429 //  // all the sensors at 1 -> should be 1 (THJ5 = .5 THJ5A + .5 THJ5B)
00430 //   EXPECT_EQ( thj5->motor->actuator->state_.position_unfiltered_ , 1.0);
00431 
00432 //   delete hw;
00433 // }
00434 
00439 TEST(SrRobotLib, HumanizeFlags)
00440 {
00441   boost::shared_ptr<HandLibTest> lib_test = boost::shared_ptr<HandLibTest>(new HandLibTest());
00442 
00443   std::vector<std::pair<std::string, bool> > flags;
00444   // all flags set
00445   flags = lib_test->sr_hand_lib->humanize_flags(0xFFFF);
00446 
00447   EXPECT_EQ(flags.size(), 16);
00448 
00449   for (unsigned int i = 0; i < 16; ++i)
00450   {
00451     EXPECT_EQ(flags[i].first.compare(error_flag_names[i]), 0);
00452   }
00453 
00454   // The last three flags are serious
00455   EXPECT_TRUE(flags[13].second);
00456   EXPECT_TRUE(flags[14].second);
00457   EXPECT_TRUE(flags[15].second);
00458 }
00459 
00461 //     MAIN       //
00463 
00464 int main(int argc, char **argv)
00465 {
00466   ros::init(argc, argv, "calibration_test");
00467   testing::InitGoogleTest(&argc, argv);
00468   return RUN_ALL_TESTS();
00469 }
00470 /* For the emacs weenies in the crowd.
00471    Local Variables:
00472    c-basic-offset: 2
00473    End:
00474 */
00475 


sr_robot_lib
Author(s): Ugo Cupcic, Toni Oliver
autogenerated on Mon Jul 1 2019 20:06:26