sr_muscle_hand_lib.cpp
Go to the documentation of this file.
00001 
00028 #include "sr_robot_lib/sr_muscle_hand_lib.hpp"
00029 #include <algorithm>
00030 #include <boost/foreach.hpp>
00031 #include <boost/algorithm/string.hpp>
00032 
00033 #include <sr_utilities/sr_math_utils.hpp>
00034 
00035 #include "sr_robot_lib/shadow_PSTs.hpp"
00036 
00037 namespace shadow_robot
00038 {
00039   template <class StatusType, class CommandType>
00040   const int SrMuscleHandLib<StatusType, CommandType>::nb_muscle_data = 3;
00041 
00042   template <class StatusType, class CommandType>
00043   const char* SrMuscleHandLib<StatusType, CommandType>::human_readable_muscle_data_types[nb_muscle_data] = {"muscle_data_pressure",
00044                                                                                                             "muscle_data_can_stats",
00045                                                                                                             "muscle_data_slow_misc"};
00046 
00047   template <class StatusType, class CommandType>
00048   const int32u SrMuscleHandLib<StatusType, CommandType>::muscle_data_types[nb_muscle_data] = {MUSCLE_DATA_PRESSURE,
00049                                                                                             MUSCLE_DATA_CAN_STATS,
00050                                                                                             MUSCLE_DATA_SLOW_MISC};
00051 
00052 
00053   template <class StatusType, class CommandType>
00054   SrMuscleHandLib<StatusType, CommandType>::SrMuscleHandLib(pr2_hardware_interface::HardwareInterface *hw) :
00055     SrMuscleRobotLib<StatusType, CommandType>(hw)
00056   {
00057     //read the muscle polling frequency from the parameter server
00058     this->muscle_update_rate_configs_vector = this->read_update_rate_configs("muscle_data_update_rate/", nb_muscle_data, human_readable_muscle_data_types, muscle_data_types);
00059     this->muscle_updater_ = boost::shared_ptr<generic_updater::MuscleUpdater<CommandType> >(new generic_updater::MuscleUpdater<CommandType>(this->muscle_update_rate_configs_vector, operation_mode::device_update_state::INITIALIZATION));
00060 
00061     for(unsigned int i=0; i< this->muscle_update_rate_configs_vector.size(); ++i)
00062     {
00063       //The initialization parameters (assigned a -2 in the config file) are introduced in the flags map that will allow us to determine
00064       //if the data has been received from every muscle driver
00065       if(this->muscle_update_rate_configs_vector[i].when_to_update == -2)
00066         this->from_muscle_driver_data_received_flags_[this->muscle_update_rate_configs_vector[i].what_to_update] = 0;
00067     }
00068 
00069     for(unsigned int i=0; i< NUM_MUSCLE_DRIVERS; ++i)
00070     {
00071       this->muscle_drivers_vector_.push_back(new shadow_joints::MuscleDriver(i) );
00072 
00073       //get the last inserted driver
00074       boost::ptr_vector<shadow_joints::MuscleDriver>::reverse_iterator driver = this->muscle_drivers_vector_.rbegin();
00075 
00076       std::stringstream ss;
00077       ss << "reset_muscle_driver_" << i;
00078       //initialize the reset muscle driver service
00079       driver->reset_driver_service = this->nh_tilde.template advertiseService<std_srvs::Empty::Request, std_srvs::Empty::Response>( ss.str().c_str(),
00080                                                                                                                                 boost::bind( &SrMuscleHandLib<StatusType, CommandType>::reset_muscle_driver_callback, this, _1, _2, i ) );
00081     }
00082 
00083     std::vector<shadow_joints::JointToSensor > joint_to_sensor_vect = this->read_joint_to_sensor_mapping();
00084 
00085     //initializing the joints vector
00086     std::vector<std::string> joint_names_tmp;
00087     std::vector<shadow_joints::JointToMuscle> joint_to_muscle_map = read_joint_to_muscle_mapping();
00088     std::vector<shadow_joints::JointToSensor > joints_to_sensors;
00089     std::vector<sr_actuator::SrGenericActuator*> actuators;
00090 
00091     ROS_ASSERT(joint_to_muscle_map.size() == JOINTS_NUM_0320);
00092     ROS_ASSERT(joint_to_sensor_vect.size() == JOINTS_NUM_0320);
00093 
00094     for(unsigned int i=0; i< JOINTS_NUM_0320; ++i)
00095     {
00096       joint_names_tmp.push_back(std::string(joint_names[i]));
00097       shadow_joints::JointToSensor tmp_jts = joint_to_sensor_vect[i];
00098       joints_to_sensors.push_back(tmp_jts);
00099 
00100       //initializing the actuators.
00101       sr_actuator::SrMuscleActuator* actuator = new sr_actuator::SrMuscleActuator(joint_names[i]);
00102       ROS_INFO_STREAM("adding actuator: "<<joint_names[i]);
00103       actuators.push_back( actuator );
00104 
00105       if(hw)
00106       {
00107         if(!hw->addActuator(actuator) )
00108         {
00109           ROS_FATAL("An actuator of the name '%s' already exists.", actuator->name_.c_str());
00110         }
00111       }
00112     }
00113     initialize(joint_names_tmp, joint_to_muscle_map, joint_to_sensor_vect, actuators);
00114 /*
00115 #ifdef DEBUG_PUBLISHER
00116     //advertise the debug service, used to set which data we want to publish on the debug topics
00117     debug_service = this->nh_tilde.advertiseService( "set_debug_publishers", &SrMuscleHandLib::set_debug_data_to_publish, this);
00118 #endif
00119 */
00120   }
00121 
00122   template <class StatusType, class CommandType>
00123   SrMuscleHandLib<StatusType, CommandType>::~SrMuscleHandLib()
00124   {
00125     boost::ptr_vector<shadow_joints::Joint>::iterator joint = this->joints_vector.begin();
00126     for(;joint != this->joints_vector.end(); ++joint)
00127     {
00128       delete joint->actuator_wrapper->actuator;
00129     }
00130   }
00131 
00132   template <class StatusType, class CommandType>
00133   void SrMuscleHandLib<StatusType, CommandType>::initialize(std::vector<std::string> joint_names,
00134                              std::vector<int> actuator_ids,
00135                              std::vector<shadow_joints::JointToSensor> joint_to_sensors,
00136                              std::vector<sr_actuator::SrGenericActuator*> actuators)
00137   {
00138     ROS_ERROR("This version of SrMuscleHandLib<StatusType, CommandType>::initialize should not be used");
00139   }
00140 
00141   template <class StatusType, class CommandType>
00142   void SrMuscleHandLib<StatusType, CommandType>::initialize(std::vector<std::string> joint_names,
00143                              std::vector<shadow_joints::JointToMuscle> actuator_ids,
00144                              std::vector<shadow_joints::JointToSensor> joint_to_sensors,
00145                              std::vector<sr_actuator::SrGenericActuator*> actuators)
00146   {
00147     for(unsigned int index = 0; index < joint_names.size(); ++index)
00148     {
00149       //add the joint and the vector of joints.
00150       this->joints_vector.push_back( new shadow_joints::Joint() );
00151 
00152       //get the last inserted joint
00153       boost::ptr_vector<shadow_joints::Joint>::reverse_iterator joint = this->joints_vector.rbegin();
00154 
00155       //update the joint variables
00156       joint->joint_name = joint_names[index];
00157       joint->joint_to_sensor = joint_to_sensors[index];
00158 
00159       if(actuator_ids[index].muscle_driver_id[0] == -1) //no muscles associated to this joint. We only check the driver 0 assuming a joint with -1 will have -1 in the driver 1 as well
00160         joint->has_actuator = false;
00161       else
00162         joint->has_actuator = true;
00163 
00164       boost::shared_ptr<shadow_joints::MuscleWrapper> muscle_wrapper ( new shadow_joints::MuscleWrapper() );
00165       joint->actuator_wrapper    = muscle_wrapper;
00166       muscle_wrapper->muscle_driver_id[0] = actuator_ids[index].muscle_driver_id[0];
00167       muscle_wrapper->muscle_driver_id[1] = actuator_ids[index].muscle_driver_id[1];
00168       muscle_wrapper->muscle_id[0] = actuator_ids[index].muscle_id[0];
00169       muscle_wrapper->muscle_id[1] = actuator_ids[index].muscle_id[1];
00170       muscle_wrapper->actuator = actuators[index];
00171     } //end for joints.
00172   }
00173 
00174   template <class StatusType, class CommandType>
00175   bool SrMuscleHandLib<StatusType, CommandType>::reset_muscle_driver_callback(std_srvs::Empty::Request& request,
00176                                                                               std_srvs::Empty::Response& response,
00177                                                                               int muscle_driver_index)
00178   {
00179     ROS_INFO_STREAM(" resetting muscle driver " << muscle_driver_index);
00180 
00181     this->reset_muscle_driver_queue.push(muscle_driver_index);
00182 
00183     return true;
00184   }
00185 
00186 
00187   template <class StatusType, class CommandType>
00188   std::vector<shadow_joints::JointToMuscle> SrMuscleHandLib<StatusType, CommandType>::read_joint_to_muscle_mapping()
00189   {
00190     std::vector<shadow_joints::JointToMuscle> muscle_map;
00191     std::string param_name = "joint_to_muscle_mapping";
00192 
00193     XmlRpc::XmlRpcValue mapping;
00194     this->nodehandle_.getParam(param_name, mapping);
00195     ROS_ASSERT(mapping.getType() == XmlRpc::XmlRpcValue::TypeArray);
00196     //iterate on all the joints
00197     for(int32_t i = 0; i < mapping.size(); ++i)
00198     {
00199       ROS_ASSERT(mapping[i].getType() == XmlRpc::XmlRpcValue::TypeArray);
00200 
00201       ROS_ASSERT(mapping[i][0].getType() == XmlRpc::XmlRpcValue::TypeArray);
00202       ROS_ASSERT(mapping[i][0][0].getType() == XmlRpc::XmlRpcValue::TypeInt);
00203       ROS_ASSERT(mapping[i][0][1].getType() == XmlRpc::XmlRpcValue::TypeInt);
00204 
00205       ROS_ASSERT(mapping[i][1].getType() == XmlRpc::XmlRpcValue::TypeArray);
00206       ROS_ASSERT(mapping[i][1][0].getType() == XmlRpc::XmlRpcValue::TypeInt);
00207       ROS_ASSERT(mapping[i][1][1].getType() == XmlRpc::XmlRpcValue::TypeInt);
00208 
00209       shadow_joints::JointToMuscle joint_to_muscle;
00210 
00211       joint_to_muscle.muscle_driver_id[0] = mapping[i][0][0];
00212       joint_to_muscle.muscle_id[0] = mapping[i][0][1];
00213       joint_to_muscle.muscle_driver_id[1] = mapping[i][1][0];
00214       joint_to_muscle.muscle_id[1] = mapping[i][1][1];
00215 
00216       muscle_map.push_back(joint_to_muscle);
00217     }
00218 
00219     return muscle_map;
00220   } //end read_joint_to_muscle_mapping
00221 
00222 /*
00223 #ifdef DEBUG_PUBLISHER
00224   template <class StatusType, class CommandType>
00225   bool SrMuscleHandLib<StatusType, CommandType>::set_debug_data_to_publish(sr_robot_msgs::SetDebugData::Request& request,
00226                                             sr_robot_msgs::SetDebugData::Response& response)
00227   {
00228     //check if the publisher_index is correct
00229     if( request.publisher_index < this->nb_debug_publishers_const )
00230     {
00231       if( request.motor_index > NUM_MOTORS )
00232       {
00233         response.success = false;
00234         return false;
00235       }
00236       if( request.motor_data_type > 0 )
00237       {
00238         if( (request.motor_data_type < MOTOR_DATA_SGL) ||
00239             (request.motor_data_type > MOTOR_DATA_DTERM) )
00240         {
00241           response.success = false;
00242           return false;
00243         }
00244       }
00245       if(!this->debug_mutex.timed_lock(boost::posix_time::microseconds(this->debug_mutex_lock_wait_time)))
00246       {
00247         response.success = false;
00248         return false;
00249       }
00250 
00251       this->debug_motor_indexes_and_data[request.publisher_index] = boost::shared_ptr<std::pair<int, int> >(new std::pair<int, int>());
00252 
00253       this->debug_motor_indexes_and_data[request.publisher_index]->first = request.motor_index;
00254       this->debug_motor_indexes_and_data[request.publisher_index]->second = request.motor_data_type;
00255       this->debug_mutex.unlock();
00256     }
00257     else
00258     {
00259       response.success = false;
00260       return false;
00261     }
00262 
00263     response.success = true;
00264     return true;
00265   }
00266 #endif
00267 */
00268   //Only to ensure that the template class is compiled for the types we are interested in
00269   template class SrMuscleHandLib<ETHERCAT_DATA_STRUCTURE_0300_PALM_EDC_STATUS, ETHERCAT_DATA_STRUCTURE_0300_PALM_EDC_COMMAND>;
00270 }// end namespace
00271 
00272 /* For the emacs weenies in the crowd.
00273 Local Variables:
00274    c-basic-offset: 2
00275 End:
00276 */


sr_robot_lib
Author(s): Ugo Cupcic / ugo@shadowrobot.com , software@shadowrobot.com
autogenerated on Mon Oct 6 2014 07:37:50