s_model_joint_states.cpp
Go to the documentation of this file.
00001 // Copyright (c) 2016, The University of Texas at Austin
00002 // All rights reserved.
00003 // 
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are met:
00006 //     * Redistributions of source code must retain the above copyright
00007 //       notice, this list of conditions and the following disclaimer.
00008 //     * Redistributions in binary form must reproduce the above copyright
00009 //       notice, this list of conditions and the following disclaimer in the
00010 //       documentation and/or other materials provided with the distribution.
00011 //     * Neither the name of the <organization> nor the
00012 //       names of its contributors may be used to endorse or promote products
00013 //       derived from this software without specific prior written permission.
00014 // 
00015 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00016 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00017 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00018 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
00019 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00020 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00021 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00022 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00023 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00024 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00025 
00026 
00037 #include <vector>
00038 #include <string>
00039 #include <csignal>
00040 #include <ros/ros.h>
00041 #include <sensor_msgs/JointState.h>
00042 #include <robotiq_s_model_control/SModel_robot_input.h>
00043 #include <robotiq_s_model_control/SModel_robot_output.h>
00044 
00045 
00046 const double DEG_TO_RAD = M_PI/180.0;
00047 
00051 class Finger {
00052  public:
00053   Finger() { position = 0; }                            
00054   Finger(int pos) { position = pos; }                   
00055   Finger(const Finger &f) { position = f.position; }    
00056   inline double joint1() const;                         
00057   inline double joint2() const;                         
00058   inline double joint3() const;                         
00059   int position;                                         
00060 };
00061 
00066 inline double Finger::joint1() const {
00067   if(0 <= position && position <= 140) return (70.0/148.0 * DEG_TO_RAD) * position;
00068   else return 70.0 * DEG_TO_RAD;
00069 }
00070 
00075 inline double Finger::joint2() const {
00076   if(0 <= position && position <= 140) return 0.0;
00077   else if(140 < position && position <= 240) return (90.0/100.0 * DEG_TO_RAD) * (position-140.0);
00078   else return 90.0 * DEG_TO_RAD;
00079 }
00080 
00085 inline double Finger::joint3() const {
00086   if(0 <= position && position <= 140) return (-70.0/140.0 * DEG_TO_RAD) * position;
00087   else return -55.0 * DEG_TO_RAD;
00088 }
00089 
00093 class Robotiq3 {
00094  public:
00096   Robotiq3() {
00097     scissor = 137;
00098     joint_positions.resize(11, 0.0);
00099     prefix = "";
00100   }
00101   
00103   Robotiq3(std::string gripper_prefix) {
00104     scissor = 137;
00105     joint_positions.resize(11, 0.0);
00106     prefix = gripper_prefix;    
00107   }
00108   
00109   inline double scissorJoint() const;                                                   
00110   void callback(const robotiq_s_model_control::SModel_robot_input::ConstPtr &msg);      
00111   Finger finger_left;                                                                   
00112   Finger finger_right;                                                                  
00113   Finger finger_middle;                                                                 
00114   int scissor;                                                                          
00115   std::string prefix;                                                                   
00116   std::vector<std::string> jointNames();                                                
00117   std::vector<double> joint_positions;                                                  
00118 };
00119 
00123 inline double Robotiq3::scissorJoint() const {
00124   // Max range for scissor mode should be 32 degrees [http://support.robotiq.com/display/IMB/6.1+Technical+dimensions].
00125   // That would mean that the limits of a single joint are -16 and +16.
00126   // By actually measuring the joint angles, the limits appear to be approximately at -11 and +11.
00127   if(0 <= scissor && scissor <= 15) return 11.0*DEG_TO_RAD;
00128   else if(15 < scissor && scissor <= 240) return (11.0-22.0*((scissor-15.0)/225.0))*DEG_TO_RAD;
00129   else return -11.0*DEG_TO_RAD;
00130 }
00131 
00135 void Robotiq3::callback(const robotiq_s_model_control::SModel_robot_input::ConstPtr &msg) {
00136   finger_left = Finger(msg->gPOA);              // set left finger position
00137   finger_right = Finger(msg->gPOB);             // set right finger position
00138   finger_middle = Finger(msg->gPOC);            // set middle finger position
00139   scissor = msg->gPOS;                          // set scissor position
00140 
00141   // Set all the joint values
00142   joint_positions.at(0)  =  scissorJoint();
00143   joint_positions.at(1)  =  finger_left.joint1();
00144   joint_positions.at(2)  =  finger_left.joint2();
00145   joint_positions.at(3)  =  finger_left.joint3();
00146   joint_positions.at(4)  = -joint_positions.at(0);
00147   joint_positions.at(5)  =  finger_right.joint1();
00148   joint_positions.at(6)  =  finger_right.joint2();
00149   joint_positions.at(7)  =  finger_right.joint3();
00150   joint_positions.at(8)  =  finger_middle.joint1();
00151   joint_positions.at(9)  =  finger_middle.joint2();
00152   joint_positions.at(10) =  finger_middle.joint3();
00153 }
00154 
00158 inline std::vector<std::string>  Robotiq3::jointNames() {
00159   // joint names for sensor_msgs::JointState message
00160   // order matters!
00161   std::vector<std::string> joint_names(11, "");
00162   joint_names.at(0).assign(prefix + "palm_finger_1_joint");
00163   joint_names.at(1).assign(prefix + "finger_1_joint_1");
00164   joint_names.at(2).assign(prefix + "finger_1_joint_2");
00165   joint_names.at(3).assign(prefix + "finger_1_joint_3");
00166   joint_names.at(4).assign(prefix + "palm_finger_2_joint");
00167   joint_names.at(5).assign(prefix + "finger_2_joint_1");
00168   joint_names.at(6).assign(prefix + "finger_2_joint_2");
00169   joint_names.at(7).assign(prefix + "finger_2_joint_3");
00170   joint_names.at(8).assign(prefix + "finger_middle_joint_1");
00171   joint_names.at(9).assign(prefix + "finger_middle_joint_2");
00172   joint_names.at(10).assign(prefix + "finger_middle_joint_3");
00173   return joint_names;
00174 }
00175 
00179 int main(int argc, char *argv[]) {
00180   
00181   // ROS init, nodehandle, and rate
00182   ros::init(argc, argv, "s_model_joint_states");
00183   ros::NodeHandle nh;
00184   ros::NodeHandle pnh("~");
00185   ros::Rate loop_rate(20);  // Hz
00186 
00187   // set user-specified prefix
00188   std::string gripper_prefix;
00189   pnh.param<std::string>("prefix", gripper_prefix, "");
00190 
00191   // Create Robotiq3
00192   Robotiq3 robotiq(gripper_prefix);
00193 
00194   // joint state publisher
00195   ros::Publisher joint_pub;
00196   joint_pub = nh.advertise<sensor_msgs::JointState>("joint_states", 10);
00197 
00198   // robotiq state message subscriber
00199   ros::Subscriber joint_sub;
00200   joint_sub = nh.subscribe("SModelRobotInput", 10, &Robotiq3::callback, &robotiq);
00201   
00202   // Output JointState message
00203   sensor_msgs::JointState joint_msg;
00204   
00205   // Joint names to JointState message
00206   joint_msg.name = robotiq.jointNames();
00207 
00208   while (ros::ok()) {
00209     joint_msg.position = robotiq.joint_positions;
00210     joint_msg.header.stamp = ros::Time::now();
00211     joint_pub.publish(joint_msg);
00212     ros::spinOnce();
00213     loop_rate.sleep();
00214   }
00215 
00216   return 0;
00217 }


robotiq_joint_state_publisher
Author(s): Jack Thompson
autogenerated on Thu Jun 6 2019 17:58:11