Setting Up joystick to control Pioneer 3-DX in ROS

From Lofaro Lab Wiki
Jump to: navigation, search

Step 1: Connect and setup joystick in Ubuntu

Since a joystick is an HID, once connected to the computer, it should come up as a device under /dev/input . Run dmesg or lsusb to check that the device has been detected.
ls /dev/input shows that a new device file js0 has appeared. This can be jsX for you, where X is some number.
Allow read and execute permission to this device sudo chmod a+x /dev/input/jsX .


To test that the joystick is properly configured and working, we can use sudo jstest /dev/input/js0. This displays all the joystick and button data. Note: jstest is part of the joystick package and you may need to install joystick.

Step 2: Setup joy package in ROS and configure joy Node

    rosdep joy install
    rosmake joy

To run the joy Node, we need to specify the device file of the specific joystick that we are using.

    rosparam set joy_Node/dev "/dev/input/jsX"

Then run the joy_Node. It will publish a topic called /joy

    rosrun set joy joy_node

Now, we need a package that listens to the /joy topic and published velocity values to /RosAria/cmd_vel

Step 3: Create a listener package

Create a new package and have the following c++ code in the src directory.

    #include "ros/ros.h"
#include "geometry_msgs/Twist.h"
#include <sensor_msgs/Joy.h>
ros::Publisher pioneer_vel;
void joyCallback(const sensor_msgs::Joy::ConstPtr& joy)
	ROS_INFO("I heard: linear_:[%f]   angular_[%d]", joy->axes[1], joy->axes[0]);
  	geometry_msgs::Twist twist;
        twist.linear.x = joy->axes[1] > 0 ? (joy->axes[1] > 0.5 ? 0.5 : joy->axes[1]) : (joy->axes[1] < -0.5 ? -0.5 : joy->axes[1]) ;
        twist.linear.y = 0;
        twist.angular.z= joy->axes[0] > 0 ? (joy->axes[0] > 0.5 ? 0.5 : joy->axes[0]) : (joy->axes[0] < -0.5 ? -0.5 : joy->axes[0]);
int main(int argc, char **argv)
        // initialize ros
        ros::init(argc, argv, "Joystick_controller");
	ros::NodeHandle n;
        // create publisher
        ros::Publisher cmd_vel_topic = n.advertise<geometry_msgs::Twist>("/RosAria/cmd_vel", 1000);
        pioneer_vel = cmd_vel_topic;
        ros::Subscriber joy_command = n.subscribe<sensor_msgs::Joy>("joy", 10, joyCallback);
        // our loop will publish at 10Hz
        ros::Rate loop_rate(5);

Run catkin_make to build the package. Then rosrun package_name package_node .