teleop_joy.cpp
Go to the documentation of this file.
00001 #include "ros/ros.h"
00002 
00003 #include <geometry_msgs/Twist.h>
00004 #include <sensor_msgs/Joy.h>
00005 #include <std_msgs/Bool.h>
00006 
00007 class TeleopJoy {
00008     public:
00009         TeleopJoy();
00010 
00011     private:
00012         void joyCallback(const sensor_msgs::Joy::ConstPtr& joy);
00013 
00014         ros::NodeHandle _nh;
00015 
00016         int axis_analog_linear, axis_analog_angular;
00017         int axis_digital_linear, axis_digital_angular;
00018         double scale_linear, scale_angular;
00019 
00020         // base publisher
00021         ros::Publisher vel_pub_;
00022 
00023         // arms publishers
00024         ros::Publisher right_arm_vel_pub_;
00025         ros::Publisher left_arm_vel_pub_;
00026 
00027         // neck publishers
00028         ros::Publisher neck_pan_vel_pub_; //movement around the z axis
00029         ros::Publisher neck_tilt_vel_pub_; // movement around the y axis
00030 
00031         ros::Subscriber joy_sub_;
00032 };
00033 
00035 
00036 TeleopJoy::TeleopJoy() {
00037     _nh.param("axis_analog_linear", axis_analog_linear, 3);
00038     _nh.param("axis_analog_angular", axis_analog_angular, 2);
00039     _nh.param("axis_digital_linear", axis_digital_linear, 1);
00040     _nh.param("axis_digital_angular", axis_digital_angular, 0);
00041     _nh.param("scale_angular", scale_angular, (double) 1);
00042     _nh.param("scale_linear", scale_linear, (double) 1);
00043 
00044     vel_pub_ = _nh.advertise<geometry_msgs::Twist>("cmd_vel", 1);
00045 
00046     // This topics must be checked to match the subscribers (if already existing)
00047 
00048     right_arm_vel_pub_ = _nh.advertise<geometry_msgs::Twist>("body/right_arm_cmd_vel", 1);
00049     left_arm_vel_pub_ = _nh.advertise<geometry_msgs::Twist>("body/left_arm_cmd_vel", 1);
00050 
00051     neck_pan_vel_pub_ = _nh.advertise<geometry_msgs::Twist>("body/neck_pan_cmd_vel", 1);
00052     neck_tilt_vel_pub_ = _nh.advertise<geometry_msgs::Twist>("body/neck_tilt_cmd_vel", 1);
00053 
00054     joy_sub_ = _nh.subscribe<sensor_msgs::Joy>("joy", 10, &TeleopJoy::joyCallback, this);
00055 }
00056 
00058 
00059 void TeleopJoy::joyCallback(const sensor_msgs::Joy::ConstPtr& joy) {
00060     // A super nice drawing for the code of the buttons on the Logitech RumblePad 2:
00061     //   ---------------
00062     //  |  [6]     [7]  |
00063     //  |  [4]     [5]  |
00064     //   ---------------
00065     //  |   |      (3)  |
00066     //  | --+--  (0) (2)|
00067     //  |   |      (1)  |
00068     //  / /-----------\ \
00069     // / /             \ \
00070 
00071     geometry_msgs::Twist vel;
00072     vel.linear.x = scale_linear * (joy->axes[axis_analog_linear] + joy->axes[axis_digital_linear]);
00073     vel.angular.z = scale_angular * (joy->axes[axis_analog_angular] + joy->axes[axis_digital_angular]);
00074 
00075     /* Buttons 0, 1, 2, 3 corrsponds to 1, 2, 3, 4 in the joypad
00076 
00077         Button 0 + joystick = move right arm
00078         Button 1 + joystick = move left arm
00079         Button 2 + joystick = pan head
00080         Button 3 + joystick = tilt head
00081     */
00082 
00083     if(joy->buttons[0]){
00084         ROS_INFO_THROTTLE(1, "Button 0 pushed. Moving right arm. \n Emitting linear: %g, angular: %g",
00085         vel.linear.x, vel.angular.z);
00086         right_arm_vel_pub_.publish(vel);
00087     }
00088     else if(joy->buttons[1]){
00089         ROS_INFO_THROTTLE(1, "Button 1 pushed. Moving left arm. \n Emitting linear: %g, angular: %g",
00090         vel.linear.x, vel.angular.z);
00091         left_arm_vel_pub_.publish(vel);
00092     }
00093     else if(joy->buttons[2]){
00094         ROS_INFO_THROTTLE(1, "Button 2 pushed. Panning head. \n Emitting linear: %g, angular: %g",
00095         vel.linear.x, vel.angular.z);
00096         neck_pan_vel_pub_.publish(vel);
00097     }
00098     else if(joy->buttons[3]){
00099         ROS_INFO_THROTTLE(1, "Button 3 pushed. Tilting head. \n Emitting linear: %g, angular: %g",
00100         vel.linear.x, vel.angular.z);
00101         neck_tilt_vel_pub_.publish(vel);
00102     }
00103     else{
00104         ROS_INFO_THROTTLE(1, "Emitting linear: %g, angular: %g", vel.linear.x, vel.angular.z);
00105         vel_pub_.publish(vel);
00106     }
00107 }
00108 
00110 
00111 int main(int argc, char** argv) {
00112     ros::init(argc, argv, "teleop_joy");
00113 
00114     TeleopJoy teleop_joy;
00115 
00116     ros::spin();
00117 }


teleop
Author(s): Raul Perula-Martinez , Jose Carlos Castillo Montoya
autogenerated on Thu Apr 23 2015 22:20:09