Ask Your Question

Trouble hand-writing a sensor_msgs::Joy

asked 2011-12-13 02:59:57 -0500

erpa gravatar image

Hello everyone, I have a mobile wheeled robot that can be controlled through a joystick. I am trying to replace the joystick with another device but I also would like to leave the controller of the robot untouched. Since the controller read a sensor_msgs::Joy I'm trying to "hand-write" it and then publish it but I fail to set the dimension of the axes and button vector (and therefore their values). I tried both axes.resize() and set_axes_size() and they are not working but I suspect that, since i don't have the physical device, I'm failing to provide some needed parameters.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2011-12-13 03:14:14 -0500

DimitriProsser gravatar image

updated 2011-12-13 05:24:20 -0500

The main concern is the button/axis mapping in the subscriber. Each robot interprets joy messages differently. You need to know what fields you have to emulate. For example, the Xbox 360 controller works as such:

#define XBOX_AXIS_LHORZ     0
#define XBOX_AXIS_LVERT     1
#define XBOX_AXIS_LTRIG     2
#define XBOX_AXIS_RHORZ     3
#define XBOX_AXIS_RVERT     4
#define XBOX_AXIS_RTRIG     5

If you wanted to publish to the left stick vertical axis (up/down) to tell the robot to go forward, you could do the following in the publisher:

sensor_msgs::Joy joy_msg;
joy_msg.axes.push_back(0.0); // Left Horizontal
joy_msg.axes.push_back(1.0); // Left Vertical (-1.0 through 1.0)
joy_msg.axes.push_back(0.0); // Left Trigger
joy_msg.axes.push_back(0.0); // Right Horizontal
joy_msg.axes.push_back(0.0); // Right Vertical
joy_msg.axes.push_back(0.0); // Right Trigger


The .push_back() method appends a new value to the "axes" vector. If you don't add to the vector, it will be empty. If it's empty when you try to access it with axes[i], you'll get a segmentation fault. The above code will generate a new message for you to publish to your robot.

edit flag offensive delete link more


what does exactly axes.push_back() does? My concern was with the publisher because the joystick I'm trying to emulate has 8 axis but when I try to access to joy_msg.axes[i] and set the desired value I get a segmentation fault (and checking the dimension of the vecotr with joy_msg.ages.size() it's 0)
erpa gravatar image erpa  ( 2011-12-13 05:17:49 -0500 )edit
I quickly tried on my laptop and it seems to work! Tomorrow I'll check with the actual robot, thanks a lot!! By the way even if now I'm able to generate the correct vectors for joy_msg.axes and joy_msg.button axes.size() still return 0!
erpa gravatar image erpa  ( 2011-12-13 05:48:04 -0500 )edit
You have to do a buttons.push_back() for each button as well. I believe the value of buttons is either 0 or 1.
DimitriProsser gravatar image DimitriProsser  ( 2011-12-13 05:57:39 -0500 )edit
@erpa: Variable size arrays such as axes and buttons are realized with std::vector. If you are not familiar with the C++ standard containers, take a look at this reference:
roehling gravatar image roehling  ( 2011-12-13 20:51:23 -0500 )edit

Hey there, I know it's almost 6 years after you guys had this conversation.

While this appears to work, I'm having issues with my "joy_msg" filling up. Each new command from the controller adds to the vector array until it eventually begins to lag. Any idea why? @DimitriProsser

Jo_Han_Solo gravatar image Jo_Han_Solo  ( 2017-10-25 01:29:15 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2011-12-13 02:59:57 -0500

Seen: 1,164 times

Last updated: Dec 13 '11