Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #ifndef __JOY_STICK_H__
00048 #define __JOY_STICK_H__
00049
00050
00051 #include <math.h>
00052 #include <stdlib.h>
00053 #include <stdio.h>
00054 #include <string.h>
00055
00056
00057 #include <GL/glfw.h>
00058
00059
00060 #include <ros/ros.h>
00061 #include <sensor_msgs/Joy.h>
00062 #include <geometry_msgs/Twist.h>
00063
00069 class JoyStick {
00070 public:
00071 JoyStick(int i=GLFW_JOYSTICK_1){
00072 joy_num = i;
00073 int err = glfwGetJoystickParam(joy_num,GLFW_PRESENT);
00074
00075 if(err == GL_FALSE){
00076 ROS_ERROR("Couldn't connect to joystick[%i]",joy_num);
00077 exit(1);
00078 }
00079
00080 num_axes = glfwGetJoystickParam(joy_num,GLFW_AXES);
00081 num_buttons = glfwGetJoystickParam(joy_num,GLFW_BUTTONS);
00082
00083 if(num_axes == 0 || num_buttons == 0){
00084 ROS_ERROR("axes[%i] buttons[%i]",num_axes,num_buttons);
00085 ROS_ERROR("Couldn't connect to joystick[%i]",joy_num);
00086 exit(1);
00087 }
00088
00089 ROS_INFO("==================================");
00090 ROS_INFO("Joystick%i: axes[%i] buttons[%i]",joy_num,num_axes,num_buttons);
00091 ROS_INFO("==================================");
00092
00093 }
00094
00095 virtual void setUpPublisher(void){
00096
00097 ros::NodeHandle n;
00098 char joy_name[32];
00099 sprintf(joy_name, "joy%d",joy_num);
00100 joy_pub = n.advertise<sensor_msgs::Joy>(joy_name, 50);
00101
00102
00103
00104 }
00105
00106
00107 bool get(void){
00108 int err = 0;
00109
00110
00111 err = glfwGetJoystickPos(joy_num, a, num_axes );
00112 if(err == 0 || err < num_axes){
00113 ROS_ERROR_THROTTLE(1.0,"Couldn't read axes");
00114 return false;
00115 }
00116
00117 err = glfwGetJoystickButtons(joy_num, b, num_buttons);
00118 if(err == 0 || err < num_buttons){
00119 ROS_ERROR_THROTTLE(1.0,"Couldn't read buttons");
00120 return false;
00121 }
00122
00123 return true;
00124 }
00125
00126
00127 void spin(float hz){
00128 ros::Rate r(hz);
00129 unsigned int err_cnt = 0;
00130
00131
00132 while (ros::ok()){
00133 bool ok = get();
00134
00135
00136 if(!ok){
00137 ++err_cnt;
00138 if(err_cnt > hz*5){
00139 ROS_ERROR("Can't connect to joystick[%i] ... exiting",joy_num);
00140 return;
00141 }
00142 }
00143
00144 publishMessage();
00145
00146 ros::spinOnce();
00147 r.sleep();
00148 }
00149 }
00150
00151 virtual void publishMessage(void){
00152
00153
00154 sensor_msgs::Joy joy_msg;
00155 joy_msg.header.stamp = ros::Time::now();
00156
00157
00158 joy_msg.axes.resize(num_axes);
00159 for(int i=0;i<num_axes;++i) joy_msg.axes[i] = a[i];
00160
00161
00162 joy_msg.buttons.resize(num_buttons);
00163 for(int i=0;i<num_buttons;++i) joy_msg.buttons[i] = b[i];
00164
00165 joy_pub.publish(joy_msg);
00166 }
00167
00168 protected:
00169 int joy_num;
00170 int num_axes;
00171 int num_buttons;
00172 float a[32];
00173 unsigned char b[32];
00174
00175
00176 ros::Publisher joy_pub;
00177 ros::Publisher twist_pub;
00178 };
00179
00180
00185 class TwistJoyStick : public JoyStick {
00186 public:
00187
00188 TwistJoyStick(int i=GLFW_JOYSTICK_1) : JoyStick(i){
00189 ;
00190 }
00191
00192 virtual void setUpPublisher(void){
00193
00194 ros::NodeHandle n;
00195 char joy_name[32];
00196 sprintf(joy_name, "cmd%d",joy_num);
00197 twist_pub = n.advertise<geometry_msgs::Twist>(joy_name, 50);
00198
00199
00200 }
00201
00202 virtual void publishMessage(void){
00203
00204
00205 geometry_msgs::Twist msg;
00206
00207 msg.linear.x = a[1];
00208 msg.linear.y = a[0];
00209 msg.linear.z = 0.0;
00210
00211 msg.angular.x = 0.0;
00212 msg.angular.y = a[3];
00213 msg.angular.z = a[2];
00214
00215 twist_pub.publish(msg);
00216 }
00217
00218 };
00219
00220 #endif
00221