JoyStick.h
Go to the documentation of this file.
00001 /*********************************************************************
00002  *
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2012, Kevin J. Walchko.
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of Kevin  nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  * Author: Kevin J. Walchko on 9/9/2012
00036  *********************************************************************
00037  *
00038  * Change Log:
00039  *  9 Sep 2012 Created
00040  *
00041  **********************************************************************
00042  *
00043  *
00044  *
00045  */
00046 
00047 #ifndef __JOY_STICK_H__
00048 #define __JOY_STICK_H__
00049 
00050 //--- C++ ---------------------
00051 #include <math.h>
00052 #include <stdlib.h> // ?
00053 #include <stdio.h>  // ?
00054 #include <string.h>
00055 
00056 //--- GLFW --------------------
00057 #include <GL/glfw.h>
00058 
00059 //--- ROS ---------------------
00060 #include <ros/ros.h>
00061 #include <sensor_msgs/Joy.h> // joystick
00062 #include <geometry_msgs/Twist.h> // twist to command robot
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         //ros::NodeHandle n("~");
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         //ROS_INFO("joystick");
00104     }
00105     
00106     // grab the current axes and button values
00107     bool get(void){
00108         int err = 0;
00109         //ROS_INFO("loop");
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     // main loop
00127     void spin(float hz){
00128         ros::Rate r(hz);
00129         unsigned int err_cnt = 0;
00130 
00131         // Main Loop -- go until ^C terminates
00132         while (ros::ok()){
00133             bool ok = get();
00134             
00135             // if we get enough errors, then exit
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         // publish joystick message
00154         sensor_msgs::Joy joy_msg;
00155         joy_msg.header.stamp = ros::Time::now();
00156         
00157         // copy axes
00158         joy_msg.axes.resize(num_axes);
00159         for(int i=0;i<num_axes;++i) joy_msg.axes[i] = a[i];
00160         
00161         // copy buttons
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;    // what joystick
00170     int num_axes;   // how many axes
00171     int num_buttons; // how many buttons
00172     float a[32];    // tmp storage
00173     unsigned char b[32]; // tmp storage
00174     
00175     //sensor_msgs::Joy joy_msg; // should i keep this?
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         //ros::NodeHandle n("~");
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         //ROS_INFO("twist");
00200     }
00201     
00202     virtual void publishMessage(void){
00203         
00204         // publish joystick message
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]; // pitch
00213         msg.angular.z = a[2]; // yaw
00214         
00215         twist_pub.publish(msg);
00216     }
00217 
00218 };
00219 
00220 #endif
00221 


osx_joystick
Author(s): Kevin Walchko
autogenerated on Tue Jan 7 2014 11:27:05