I want to publish via terminal a particular message format - sensor_msgs/Joy. However, every format I use seems to be wrong and I cannot understand how it is meant to be written on the terminal. On the internet all I can find is the generic structure and examples of similar messages but modifying my message based on that does not seem to produce any result. Say I want to publish that the object moves forward along a single axis while no motion in others. How do I achieve that with the proper syntax.

Also if you can let me know a place where I can find examples of the exact way I have to publish certain messages via terminal using rostopic pub.


To answer your question, you can use the following syntax to publish something to the joy node:

rostopic pub /joy sensor_msgs/Joy '{ header: {seq: 10, stamp: {secs: 1431222430, nsecs: 345678}, frame_id: "3"}, axes: [1.0, 0.0, 0.0, 0.0, 0.0, 0.0], buttons: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}'

Feel free to modify the axis and button values as you need, as well as the header. Axis values go from -1.0 to 1.0 and buttons are either 0 or 1.

For future reference, I would strongly recommend actually writing a publisher script, either in Python or C++, because the rostopic pub interface, while it works okay for some simple std_msgs messages, can get quite tedious for more complicated messages. You might be able to find better tutorials online, but the simplest interface to publish to a joystick topic from C++ would look like this.

sensor_msgs::Joy joy_msg;
joy_msg.axes.push_back(1.0); // Left Horizontal (-1.0 through 1.0)
joy_msg.axes.push_back(0.0); // Left Vertical 
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

// Repeat for all buttons

  • $\begingroup$ You write 'joy_msg.axes,=.push_back()' so many times, how do you know which axes it corresponds to...can you please explain if possible? $\endgroup$ – Zero Aug 9 '18 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.