00001 00030 #include <sr_hand/hand_commander.hpp> 00031 #include <pr2_mechanism_msgs/ListControllers.h> 00032 #include <sr_robot_msgs/sendupdate.h> 00033 #include <std_msgs/Float64.h> 00034 00035 00036 namespace shadowrobot 00037 { 00038 00039 const double HandCommander::TIMEOUT_TO_DETECT_CONTROLLER_MANAGER = 3.0; 00040 00041 HandCommander::HandCommander(): 00042 hand_type(shadowhandRosLib::UNKNOWN), 00043 ethercat_controllers_found(false) 00044 { 00045 //We use the presence of the pr2_controller_manager/list_controllers service to detect that the hand is ethercat 00046 if(ros::service::waitForService("pr2_controller_manager/list_controllers", ros::Duration(TIMEOUT_TO_DETECT_CONTROLLER_MANAGER))) 00047 { 00048 hand_type = shadowhandRosLib::ETHERCAT; 00049 initializeEthercatHand(); 00050 ROS_INFO("HandCommander library: ETHERCAT hand detected"); 00051 } 00052 else 00053 { 00054 hand_type = shadowhandRosLib::CAN; 00055 sr_hand_target_pub = node_.advertise<sr_robot_msgs::sendupdate>("/srh/sendupdate", 2); 00056 ROS_INFO("HandCommander library: CAN hand detected"); 00057 } 00058 } 00059 00060 HandCommander::~HandCommander() 00061 { 00062 } 00063 00064 void HandCommander::initializeEthercatHand() 00065 { 00066 sr_hand_target_pub_map.clear(); 00067 00068 ros::ServiceClient controller_list_client = node_.serviceClient<pr2_mechanism_msgs::ListControllers>("pr2_controller_manager/list_controllers"); 00069 00070 pr2_mechanism_msgs::ListControllers controller_list; 00071 std::string controlled_joint_name; 00072 00073 controller_list_client.call(controller_list); 00074 00075 for (size_t i=0;i<controller_list.response.controllers.size() ;i++ ) 00076 { 00077 if(controller_list.response.state[i]=="running") 00078 { 00079 std::string controller = controller_list.response.controllers[i]; 00080 if (node_.getParam("/"+controller+"/joint", controlled_joint_name)) 00081 { 00082 ROS_DEBUG("controller %d:%s controls joint %s\n", 00083 (int)i,controller.c_str(),controlled_joint_name.c_str()); 00084 sr_hand_target_pub_map[controlled_joint_name] 00085 = node_.advertise<std_msgs::Float64>(controller+"/command", 2); 00086 ethercat_controllers_found = true; 00087 } 00088 } 00089 } 00090 00091 } 00092 00093 void HandCommander::sendCommands(std::vector<sr_robot_msgs::joint> joint_vector) 00094 { 00095 if(hand_type == shadowhandRosLib::ETHERCAT) 00096 { 00097 if(!ethercat_controllers_found) 00098 { 00099 initializeEthercatHand(); 00100 // No controllers we found so bail out 00101 if (!ethercat_controllers_found) 00102 return; 00103 } 00104 for(size_t i = 0; i < joint_vector.size(); ++i) 00105 { 00106 std_msgs::Float64 target; 00107 target.data = joint_vector.at(i).joint_target * M_PI/180.0; 00108 sr_hand_target_pub_map[joint_vector.at(i).joint_name].publish(target); 00109 } 00110 } 00111 else 00112 { 00113 sr_robot_msgs::sendupdate sendupdate_msg; 00114 sendupdate_msg.sendupdate_length = joint_vector.size(); 00115 sendupdate_msg.sendupdate_list = joint_vector; 00116 00117 sr_hand_target_pub.publish(sendupdate_msg); 00118 } 00119 } 00120 00121 }