First, install the necessary package:
1 |
sudo apt-get install ros-indigo-joy |
make sure your joystick is working:
1 |
sudo jstest /dev/input/js0 |
also, make sure your controller has correct permissions:
1 |
ls -l /dev/input/js0 |
The permission on the last column must be rw, if it “–” like this:
1 |
crw-rw-r--+ 1 root root 13, 0 May 18 12:12 /dev/input/js0 |
then correct the permission:
1 |
sudo chmod a+rw /dev/input/js0 |
run the Joy node and roscore:
1 2 3 |
roscore rosparam set joy_node/dev "/dev/input/js0" rosrun joy joy_node |
The joystick data are being published under /joy topic. To see the data:
1 |
rostopic echo joy |
to use the joystick in your code follow this guideline:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <ros/ros.h> #include <sensor_msgs/Joy.h> void joyCallback(const sensor_msgs::Joy::ConstPtr& joy) { std::cout<<"axes: ["<< joy->axes[0] <<","<<joy->axes[1] <<","<<joy->axes[2] <<","<<joy->axes[3]<<","<< joy->axes[4]<<","<< joy->axes[5] <<","<<joy->axes[6]<<","<< joy->axes[7] <<"]"<<std::endl; std::cout<<"buttons: ["<< joy->buttons[0] <<","<< joy->buttons[1] <<","<<joy->buttons[2] <<","<<joy->buttons[3]<<","<< joy->buttons[4] <<","<<joy->buttons[5] <<","<<joy->buttons[6] <<","<<joy->buttons[7] <<","<<joy->buttons[8] <<","<<joy->buttons[9]<<","<<joy->buttons[10] <<"]"<<std::endl; } int main(int argc, char** argv) { ros::init(argc, argv, "joystcik_reader"); ros::NodeHandle nh; ros::Subscriber joy_sub = nh.subscribe<sensor_msgs::Joy>("joy", 10, &joyCallback); ros::spin(); } |
full code at my Github.