rq_sensor.cpp
Go to the documentation of this file.
00001 /* Software License Agreement (BSD License)
00002 *
00003 * Copyright (c) 2014, Robotiq, Inc.
00004 * All rights reserved.
00005 *
00006 * Redistribution and use in source and binary forms, with or without
00007 * modification, are permitted provided that the following conditions
00008 * are met:
00009 *
00010 * * Redistributions of source code must retain the above copyright
00011 * notice, this list of conditions and the following disclaimer.
00012 * * Redistributions in binary form must reproduce the above
00013 * copyright notice, this list of conditions and the following
00014 * disclaimer in the documentation and/or other materials provided
00015 * with the distribution.
00016 * * Neither the name of Robotiq, Inc. nor the names of its
00017 * contributors may be used to endorse or promote products derived
00018 * from this software without specific prior written permission.
00019 *
00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 * POSSIBILITY OF SUCH DAMAGE.
00032 *
00033 * Copyright (c) 2014, Robotiq, Inc
00034 */
00035 
00043 #include <string.h>
00044 #include <stdio.h>
00045 
00046 #include "ros/ros.h"
00047 #include "std_msgs/String.h"
00048 #include "robotiq_force_torque_sensor/rq_sensor_state.h"
00049 #include "robotiq_force_torque_sensor/ft_sensor.h"
00050 #include "robotiq_force_torque_sensor/sensor_accessor.h"
00051 
00052 static void decode_message_and_do(INT_8 const  * const buff, INT_8 * const ret);
00053 static void wait_for_other_connection(void);
00054 
00055 ros::Publisher sensor_pub_acc;
00056 
00062 static void decode_message_and_do(INT_8 const  * const buff, INT_8 * const ret)
00063 {
00064         INT_8 get_or_set[3];
00065         INT_8 nom_var[4];
00066 
00067         if(buff == NULL || strlen(buff) != 7)
00068         {
00069                 return;
00070         }
00071 
00072         strncpy(get_or_set, &buff[0], 3);
00073         strncpy(nom_var, &buff[4], strlen(buff) -3);
00074         
00075         if(strstr(get_or_set, "GET"))
00076         {
00077                 rq_state_get_command(nom_var, ret);
00078         }
00079         else if(strstr(get_or_set, "SET"))
00080         {
00081                 if(strstr(nom_var, "ZRO"))
00082                 {
00083                         rq_state_do_zero_force_flag();
00084                         strcpy(ret,"Done");
00085                 }
00086         }
00087 }
00088 
00089 bool receiverCallback(robotiq_force_torque_sensor::sensor_accessor::Request& req,
00090         robotiq_force_torque_sensor::sensor_accessor::Response& res)
00091 {
00092         ROS_INFO("I heard: [%s]",req.command.c_str());
00093         INT_8 buffer[512];
00094         decode_message_and_do((char*)req.command.c_str(), buffer);
00095         res.res = buffer;
00096         ROS_INFO("I send: [%s]", res.res.c_str());
00097         return true;
00098 }
00099 
00104 static void wait_for_other_connection(void)
00105 {
00106         INT_8 ret;
00107 
00108         while(1)
00109         {
00110                 usleep(1000000);//Attend 1 seconde.
00111                 ret = rq_sensor_state();
00112 
00113                 if(ret == 0)
00114                 {
00115                         break;
00116                 }
00117                 ros::spinOnce();
00118         }
00119 }
00120 
00126 static robotiq_force_torque_sensor::ft_sensor get_data(void)
00127 {
00128         robotiq_force_torque_sensor::ft_sensor msgStream;
00129 
00130         msgStream.Fx = rq_state_get_received_data(0);
00131         msgStream.Fy = rq_state_get_received_data(1);
00132         msgStream.Fz = rq_state_get_received_data(2);
00133         msgStream.Mx = rq_state_get_received_data(3);
00134         msgStream.My = rq_state_get_received_data(4);
00135         msgStream.Mz = rq_state_get_received_data(5);
00136 
00137         return msgStream;
00138 }
00139 
00140 int main(int argc, char **argv)
00141 {
00142         ros::init(argc, argv, "robotiq_force_torque_sensor");
00143 
00144         INT_8 bufStream[512];
00145         robotiq_force_torque_sensor::ft_sensor msgStream;
00146         ros::NodeHandle n;
00147         INT_8 ret; 
00148 
00149 
00150         //If we can't initialize, we return an error
00151         ret = rq_sensor_state();
00152         if(ret == -1)
00153         {
00154                 wait_for_other_connection();
00155         }
00156 
00157         //Reads basic info on the sensor
00158         ret = rq_sensor_state();
00159         if(ret == -1)
00160         {
00161                 wait_for_other_connection();
00162         }
00163 
00164         //Starts the stream
00165         ret = rq_sensor_state();
00166         if(ret == -1)
00167         {
00168                 wait_for_other_connection();
00169         }
00170         
00171         ros::Publisher sensor_pub = n.advertise<robotiq_force_torque_sensor::ft_sensor>("robotiq_force_torque_sensor", 512);
00172         ros::ServiceServer service = n.advertiseService("robotiq_force_torque_sensor_acc", receiverCallback);
00173 
00174         //std_msgs::String msg;
00175 
00176         while(1)
00177         {
00178                 ret = rq_sensor_state();
00179 
00180                 if(ret == -1)
00181                 {
00182                         wait_for_other_connection();
00183                 }
00184 
00185                 if(rq_sensor_get_current_state() == RQ_STATE_RUN)
00186                 {
00187                         strcpy(bufStream,"");
00188                         msgStream = get_data();
00189 
00190                         if(rq_state_got_new_message())
00191                         {
00192                                 sensor_pub.publish(msgStream);
00193                         }
00194                 }
00195 
00196                 ros::spinOnce();
00197         }
00198         return 0;
00199 }


robotiq_force_torque_sensor
Author(s): Jonathan Savoie
autogenerated on Thu Aug 27 2015 14:44:30