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
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
00064
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
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
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
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
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
00116
00117
00118
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
00150 this->joints_vector.push_back( new shadow_joints::Joint() );
00151
00152
00153 boost::ptr_vector<shadow_joints::Joint>::reverse_iterator joint = this->joints_vector.rbegin();
00154
00155
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)
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 }
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
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 }
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 template class SrMuscleHandLib<ETHERCAT_DATA_STRUCTURE_0300_PALM_EDC_STATUS, ETHERCAT_DATA_STRUCTURE_0300_PALM_EDC_COMMAND>;
00270 }
00271
00272
00273
00274
00275
00276