rq_sensor.cpp
Go to the documentation of this file.
1 /* Software License Agreement (BSD License)
2 *
3 * Copyright (c) 2014, Robotiq, Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 * * Neither the name of Robotiq, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *
33 * Copyright (c) 2014, Robotiq, Inc
34 */
35 
43 #include <string.h>
44 #include <stdio.h>
45 
46 #include "ros/ros.h"
47 #include "std_msgs/String.h"
48 #include "geometry_msgs/WrenchStamped.h"
50 #include "robotiq_ft_sensor/ft_sensor.h"
51 #include "robotiq_ft_sensor/sensor_accessor.h"
52 
54 static int max_retries_(100);
55 std::string ftdi_id;
56 
58 
59 
67  robotiq_ft_sensor::sensor_accessor::Response& res)
68 {
69  INT_8 buffer[100];
70  res.success = rq_state_get_command(req.command_id, buffer);
71  res.res = buffer;
72 
73  if (!res.success)
74  {
75  ROS_WARN("Unsupported command_id '%i', should be in [%i, %i, %i, %i]",
76  req.command_id,
77  Request::COMMAND_GET_SERIAL_NUMBER,
78  Request::COMMAND_GET_FIRMWARE_VERSION,
79  Request::COMMAND_GET_PRODUCTION_YEAR,
80  Request::COMMAND_SET_ZERO);
81  }
82 
83  return res.success;
84 }
85 
86 
92 static void decode_message_and_do(INT_8 const * const buff, INT_8 * const ret)
93 {
94  INT_8 get_or_set[3];
95  INT_8 nom_var[4];
96 
97  if(buff == NULL || strlen(buff) != 7)
98  {
99  return;
100  }
101 
102  strncpy(get_or_set, &buff[0], 3);
103  strncpy(nom_var, &buff[4], strlen(buff) -3);
104 
105  if(strstr(get_or_set, "GET"))
106  {
107  rq_state_get_command(nom_var, ret);
108  }
109  else if(strstr(get_or_set, "SET"))
110  {
111  if(strstr(nom_var, "ZRO"))
112  {
114  strcpy(ret,"Done");
115  }
116  }
117 }
118 
120  robotiq_ft_sensor::sensor_accessor::Response& res)
121 {
123  if (req.command.length())
124  {
125  ROS_WARN_ONCE("Usage of command-string is deprecated, please use the numeric command_id");
126  ROS_INFO("I heard: [%s]",req.command.c_str());
127  INT_8 buffer[512];
128  decode_message_and_do((char*)req.command.c_str(), buffer);
129  res.res = buffer;
130  ROS_INFO("I send: [%s]", res.res.c_str());
131  return true;
132  }
133 
135  decode_message_and_do(req, res);
136  return true;
137 }
138 
140 {
141  if (ftdi_id.empty())
142  {
144  }
145 
147 }
148 
154 {
155  INT_8 ret;
156 
157  while(ros::ok())
158  {
159  ROS_INFO("Waiting for sensor connection...");
160  usleep(1000000);//Attend 1 seconde.
161 
162  ret = sensor_state_machine();
163  if(ret == 0)
164  {
165  ROS_INFO("Sensor connected!");
166  break;
167  }
168 
169  ros::spinOnce();
170  }
171 }
172 
178 static robotiq_ft_sensor::ft_sensor get_data(void)
179 {
180  robotiq_ft_sensor::ft_sensor msgStream;
181 
182  msgStream.Fx = rq_state_get_received_data(0);
183  msgStream.Fy = rq_state_get_received_data(1);
184  msgStream.Fz = rq_state_get_received_data(2);
185  msgStream.Mx = rq_state_get_received_data(3);
186  msgStream.My = rq_state_get_received_data(4);
187  msgStream.Mz = rq_state_get_received_data(5);
188 
189  return msgStream;
190 }
191 
192 int main(int argc, char **argv)
193 {
194  ros::init(argc, argv, "robotiq_ft_sensor");
195  ros::NodeHandle n;
196  ros::param::param<int>("~max_retries", max_retries_, 100);
197  ros::param::get("~serial_id", ftdi_id);
198  if (!ftdi_id.empty())
199  {
200  ROS_INFO("Trying to connect to a sensor at /dev/%s", ftdi_id.c_str());
201  }
202  else
203  {
204  ROS_INFO("No device filename specified. Will attempt to discover Robotiq force torque sensor.");
205  }
206 
207  INT_8 bufStream[512];
208  robotiq_ft_sensor::ft_sensor msgStream;
209  INT_8 ret;
210 
211  //If we can't initialize, we return an error
212  ret = sensor_state_machine();
213  if(ret == -1)
214  {
216  }
217 
218  //Reads basic info on the sensor
219  ret = sensor_state_machine();
220  if(ret == -1)
221  {
223  }
224 
225  //Starts the stream
226  ret = sensor_state_machine();
227  if(ret == -1)
228  {
230  }
231 
232  ros::Publisher sensor_pub = n.advertise<robotiq_ft_sensor::ft_sensor>("robotiq_ft_sensor", 512);
233  ros::Publisher wrench_pub = n.advertise<geometry_msgs::WrenchStamped>("robotiq_ft_wrench", 512);
234  ros::ServiceServer service = n.advertiseService("robotiq_ft_sensor_acc", receiverCallback);
235 
236  //std_msgs::String msg;
237  geometry_msgs::WrenchStamped wrenchMsg;
238  ros::param::param<std::string>("~frame_id", wrenchMsg.header.frame_id, "robotiq_ft_frame_id");
239 
240  ROS_INFO("Starting Sensor");
241  while(ros::ok())
242  {
243  ret = sensor_state_machine();
244  if (ret == -1)
245  {
247  }
248 
250  {
251  strcpy(bufStream,"");
252  msgStream = get_data();
253 
255  {
256  sensor_pub.publish(msgStream);
257 
258  //compose WrenchStamped Msg
259  wrenchMsg.header.stamp = ros::Time::now();
260  wrenchMsg.wrench.force.x = msgStream.Fx;
261  wrenchMsg.wrench.force.y = msgStream.Fy;
262  wrenchMsg.wrench.force.z = msgStream.Fz;
263  wrenchMsg.wrench.torque.x = msgStream.Mx;
264  wrenchMsg.wrench.torque.y = msgStream.My;
265  wrenchMsg.wrench.torque.z = msgStream.Mz;
266  wrench_pub.publish(wrenchMsg);
267  }
268  }
269 
270  ros::spinOnce();
271  }
272  return 0;
273 }
bool receiverCallback(robotiq_ft_sensor::sensor_accessor::Request &req, robotiq_ft_sensor::sensor_accessor::Response &res)
Definition: rq_sensor.cpp:119
void publish(const boost::shared_ptr< M > &message) const
void rq_state_get_command(INT_8 const *const name, INT_8 *const value)
float rq_state_get_received_data(UINT_8 i)
robotiq_ft_sensor::sensor_accessor::Request Request
Definition: rq_sensor.cpp:53
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
int main(int argc, char **argv)
Definition: rq_sensor.cpp:192
#define ROS_WARN(...)
static bool decode_message_and_do(robotiq_ft_sensor::sensor_accessor::Request &req, robotiq_ft_sensor::sensor_accessor::Response &res)
decode_message_and_do Decode the message received and do the associated action
Definition: rq_sensor.cpp:66
enum rq_sensor_state_values rq_sensor_get_current_state(void)
Returns this module&#39;s state machine current state.
static void wait_for_other_connection()
Each second, checks for a sensor that has been connected.
Definition: rq_sensor.cpp:153
ROSCPP_DECL bool get(const std::string &key, std::string &s)
#define ROS_WARN_ONCE(...)
#define ROS_INFO(...)
ros::Publisher sensor_pub_acc
Definition: rq_sensor.cpp:57
ROSCPP_DECL bool ok()
static INT_8 sensor_state_machine()
Definition: rq_sensor.cpp:139
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
the sensor
static robotiq_ft_sensor::ft_sensor get_data(void)
Builds the message with the force/torque data.
Definition: rq_sensor.cpp:178
std::string ftdi_id
Definition: rq_sensor.cpp:55
void rq_state_do_zero_force_flag(void)
Command a zero on the sensor.
static Time now()
bool rq_state_got_new_message(void)
Returns true if a stream message is available.
char INT_8
Definition: rq_int.h:46
ROSCPP_DECL void spinOnce()
static int max_retries_(100)
INT_8 rq_sensor_state(unsigned int max_retries)


robotiq_ft_sensor
Author(s): Jonathan Savoie
autogenerated on Tue Jun 1 2021 02:30:04