ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

How to publish Int16MultiArray from command line?

asked 2017-03-26 11:18:16 -0600

Cerin gravatar image

I have a C++ subscriber that listens for a message composed of a two integers, which I've implemented with a std_msgs::Int16MultiArray message type like:

void on_motor_speed(const std_msgs::Int16MultiArray& msg){
    ///do stuff
}
ros::Subscriber<std_msgs::Int16MultiArray> motor_speed_sub("motor/speed", &on_motor_speed);

Is there an easier way to do this?

How do I test this from the command line using rostopic pub? Based on this question, I think is should be something like:

rostopic pub motor/speed std_msgs/Int16MultiArray "{layout:{dim:[{label:'', size:0, stride:0}], data_offset:0}, data:[64, 64]}"

but I get the YAML parsing error:

Usage: rostopic pub /topic type [args...]

rostopic: error: Argument error: while scanning a plain scalar
  in "<string>", line 1, column 16:
    {layout:{dim:[{label:'', size:0, stride:0}], da ... 
                   ^
found unexpected ':'
  in "<string>", line 1, column 21:
    {layout:{dim:[{label:'', size:0, stride:0}], data_of ... 
                        ^
Please check http://pyyaml.org/wiki/YAMLColonInFlowContext for details.

As far as I know, YAML does support the inline syntax, and doesn't need to be multi-line. What am I doing wrong?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2017-03-26 12:03:51 -0600

jayess gravatar image

updated 2017-03-26 12:16:54 -0600

It says right in the error what went wrong.

rostopic: error: Argument error: while scanning a plain scalar
  in "<string>", line 1, column 16:
    {layout:{dim:[{label:'', size:0, stride:0}], da ... 
                   ^
found unexpected ':'
  in "<string>", line 1, column 21:
    {layout:{dim:[{label:'', size:0, stride:0}], data_of ...

This is saying that you need put a space between the colon : and the value. What you should be typing is this:

rostopic pub motor/speed std_msgs/Int16MultiArray "{layout: {dim: [{label: '', size: 0, stride: 0}], data_offset: 0}, data: [64, 64]}"

A useful tip is that you should be using the tab autocomplete. If you use that, it will fill in the message with a nicely formatted YAML string like so:

$ rostopipub motor/speed std_msgs/Int16MultiArray "layout:
  dim:
  - label: ''
    size: 0
    stride: 0
  data_offset: 0
data:
- 0"

Then you can go into the string itself and enter your data.

Edit: I originally stated that single quotes were needed around the keys, this is actually incorrect.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-03-26 11:18:16 -0600

Seen: 2,692 times

Last updated: Mar 26 '17