robotiq_3f_gripper_joint_states.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016, The University of Texas at Austin
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of the <organization> nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 
37 #include <vector>
38 #include <string>
39 #include <csignal>
40 #include <ros/ros.h>
41 #include <sensor_msgs/JointState.h>
42 #include <robotiq_3f_gripper_articulated_msgs/Robotiq3FGripperRobotInput.h>
43 #include <robotiq_3f_gripper_articulated_msgs/Robotiq3FGripperRobotOutput.h>
44 
45 
46 const double DEG_TO_RAD = M_PI/180.0;
47 
51 class Finger {
52  public:
53  Finger() { position = 0; }
54  Finger(int pos) { position = pos; }
55  Finger(const Finger &f) { position = f.position; }
56  inline double joint1() const;
57  inline double joint2() const;
58  inline double joint3() const;
59  int position;
60 };
61 
66 inline double Finger::joint1() const {
67  if(0 <= position && position <= 140) return (70.0/148.0 * DEG_TO_RAD) * position;
68  else return 70.0 * DEG_TO_RAD;
69 }
70 
75 inline double Finger::joint2() const {
76  if(0 <= position && position <= 140) return 0.0;
77  else if(140 < position && position <= 240) return (90.0/100.0 * DEG_TO_RAD) * (position-140.0);
78  else return 90.0 * DEG_TO_RAD;
79 }
80 
85 inline double Finger::joint3() const {
86  if(0 <= position && position <= 140) return (-70.0/140.0 * DEG_TO_RAD) * position;
87  else return -55.0 * DEG_TO_RAD;
88 }
89 
93 class Robotiq3 {
94  public:
97  scissor = 137;
98  joint_positions.resize(11, 0.0);
99  prefix = "";
100  }
101 
103  Robotiq3(std::string gripper_prefix) {
104  scissor = 137;
105  joint_positions.resize(11, 0.0);
106  prefix = gripper_prefix;
107  }
108 
109  inline double scissorJoint() const;
110  void callback(const robotiq_3f_gripper_articulated_msgs::Robotiq3FGripperRobotInput::ConstPtr &msg);
114  int scissor;
115  std::string prefix;
116  std::vector<std::string> jointNames();
117  std::vector<double> joint_positions;
118 };
119 
123 inline double Robotiq3::scissorJoint() const {
124  // Max range for scissor mode should be 32 degrees [http://support.robotiq.com/display/IMB/6.1+Technical+dimensions].
125  // That would mean that the limits of a single joint are -16 and +16.
126  // By actually measuring the joint angles, the limits appear to be approximately at -11 and +11.
127  if(0 <= scissor && scissor <= 15) return 11.0*DEG_TO_RAD;
128  else if(15 < scissor && scissor <= 240) return (11.0-22.0*((scissor-15.0)/225.0))*DEG_TO_RAD;
129  else return -11.0*DEG_TO_RAD;
130 }
131 
135 void Robotiq3::callback(const robotiq_3f_gripper_articulated_msgs::Robotiq3FGripperRobotInput::ConstPtr &msg) {
136  finger_left = Finger(msg->gPOA); // set left finger position
137  finger_right = Finger(msg->gPOB); // set right finger position
138  finger_middle = Finger(msg->gPOC); // set middle finger position
139  scissor = msg->gPOS; // set scissor position
140 
141  // Set all the joint values
142  joint_positions.at(0) = scissorJoint();
143  joint_positions.at(1) = finger_left.joint1();
144  joint_positions.at(2) = finger_left.joint2();
145  joint_positions.at(3) = finger_left.joint3();
146  joint_positions.at(4) = -joint_positions.at(0);
147  joint_positions.at(5) = finger_right.joint1();
148  joint_positions.at(6) = finger_right.joint2();
149  joint_positions.at(7) = finger_right.joint3();
150  joint_positions.at(8) = finger_middle.joint1();
151  joint_positions.at(9) = finger_middle.joint2();
152  joint_positions.at(10) = finger_middle.joint3();
153 }
154 
158 inline std::vector<std::string> Robotiq3::jointNames() {
159  // joint names for sensor_msgs::JointState message
160  // order matters!
161  std::vector<std::string> joint_names(11, "");
162  joint_names.at(0).assign(prefix + "palm_finger_1_joint");
163  joint_names.at(1).assign(prefix + "finger_1_joint_1");
164  joint_names.at(2).assign(prefix + "finger_1_joint_2");
165  joint_names.at(3).assign(prefix + "finger_1_joint_3");
166  joint_names.at(4).assign(prefix + "palm_finger_2_joint");
167  joint_names.at(5).assign(prefix + "finger_2_joint_1");
168  joint_names.at(6).assign(prefix + "finger_2_joint_2");
169  joint_names.at(7).assign(prefix + "finger_2_joint_3");
170  joint_names.at(8).assign(prefix + "finger_middle_joint_1");
171  joint_names.at(9).assign(prefix + "finger_middle_joint_2");
172  joint_names.at(10).assign(prefix + "finger_middle_joint_3");
173  return joint_names;
174 }
175 
179 int main(int argc, char *argv[]) {
180 
181  // ROS init, nodehandle, and rate
182  ros::init(argc, argv, "robotiq_3f_gripper_joint_states");
183  ros::NodeHandle nh;
184  ros::NodeHandle pnh("~");
185  ros::Rate loop_rate(20); // Hz
186 
187  // set user-specified prefix
188  std::string gripper_prefix;
189  pnh.param<std::string>("prefix", gripper_prefix, "");
190 
191  // Create Robotiq3
192  Robotiq3 robotiq(gripper_prefix);
193 
194  // joint state publisher
195  ros::Publisher joint_pub;
196  joint_pub = nh.advertise<sensor_msgs::JointState>("joint_states", 10);
197 
198  // robotiq state message subscriber
199  ros::Subscriber joint_sub;
200  joint_sub = nh.subscribe("Robotiq3FGripperRobotInput", 10, &Robotiq3::callback, &robotiq);
201 
202  // Output JointState message
203  sensor_msgs::JointState joint_msg;
204 
205  // Joint names to JointState message
206  joint_msg.name = robotiq.jointNames();
207 
208  while (ros::ok()) {
209  joint_msg.position = robotiq.joint_positions;
210  joint_msg.header.stamp = ros::Time::now();
211  joint_pub.publish(joint_msg);
212  ros::spinOnce();
213  loop_rate.sleep();
214  }
215 
216  return 0;
217 }
msg
int main(int argc, char *argv[])
Finger()
Default constructor for creating Finger, position is set 0.
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void callback(const robotiq_3f_gripper_articulated_msgs::Robotiq3FGripperRobotInput::ConstPtr &msg)
Callback function for "Robotiq3FGripperRobotInput" topic.
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int position
Position of the Finger.
const double DEG_TO_RAD
std::string prefix
Gripper prefix.
double joint3() const
joint_3 value for Finger
double joint2() const
joint_2 value for Finger
Finger(const Finger &f)
Create Finger, position is taken form.
bool param(const std::string &param_name, T &param_val, const T &default_val) const
double scissorJoint() const
Joint value for so-called scissor joint.
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
int scissor
Scissor position.
bool sleep()
Finger finger_middle
Robotiq FINGER C.
Finger(int pos)
Create Finger, position is.
static Time now()
Finger finger_right
Robotiq FINGER B.
ROSCPP_DECL void spinOnce()
std::vector< double > joint_positions
Joint values.
Robotiq3(std::string gripper_prefix)
double joint1() const
joint_1 value for Finger
std::vector< std::string > jointNames()
Joint names.
Finger finger_left
Robotiq FINGER A.


robotiq_3f_gripper_joint_state_publisher
Author(s): Jack Thompson
autogenerated on Tue Jun 1 2021 02:30:01