0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

yamlファイルを使って一連のROSメッセージをpublishする

Posted at

ROSノードの自動テストなどで、一連のメッセージを、値を指定してpublishしたいときがあります。
たとえば、/cmd_velトピックに、Twist型で、1秒おきに
linear = {x:1.0, y:0,0, z:0.0}, {x:0.0, y:1.0, z:0.0}, {x:0.0, y:0.0, z:1.0}
の3つのメッセージをpublishしたいとしましょう。
そんなとき、通常は

  • トピックにメッセージをpublishするROSのプログラムをpythonなどで書いてrunする。
  • なんとかして一連のメッセージを入れたbagファイルを作ってplayする。

といった方法で実現するかと思います。
今回はこれをコマンドラインツールrostopicとyamlファイルを使って行います。

yamlファイルを作る

ROSメッセージの値が入ったyamlファイルを作ります。
まず目的のトピックに、目的の型のメッセージをpublishします。
中身の値はなんでもいいです。
今回は/cmd_velトピックにgeometry_msgs/Twist型のメッセージをpublishします。
ここからはpub側とsub(echo)側で、2つのターミナルを使います。

term_pub
$ rostopic pub /cmd_vel geometry_msgs/Twist "linear:
  x: 0.0
  y: 0.0
  z: 0.0
angular:
  x: 0.0
  y: 0.0
  z: 0.0"

途中までタイプすると、自動補完してくれると思います。
メッセージの値はyamlフォーマットになっています。
つぎにこのトピックをrostopic echoします。

term_sub
$ rostopic echo /cmd_vel 
linear: 
  x: 0.0
  y: 0.0
  z: 0.0
angular: 
  x: 0.0
  y: 0.0
  z: 0.0
---

yamlフォーマットで出力してくれます。
これをファイルに保存します

term_sub
$ rostopic echo -n 1 /cmd_vel > cmd_vel_val.yaml
$ cat cmd_vel_val.yaml 
linear: 
  x: 0.0
  y: 0.0
  z: 0.0
angular: 
  x: 0.0
  y: 0.0
  z: 0.0
---

Twist型のメッセージの値が1個入ったファイルができました。

yamlに入っているデータをpublishする

このデータをpublishしてみます。
sub側のターミナルでrostopic echoしておきます。

term_sub
$ rostopic echo /cmd_vel

もう一方のターミナルで以下のようにします。

term_pub
$ cat cmd_vel_val.yaml| rostopic pub /cmd_vel geometry_msgs/Twist

すると

term_sub
$ rostopic echo /cmd_vel
linear: 
  x: 0.0
  y: 0.0
  z: 0.0
angular: 
  x: 0.0
  y: 0.0
  z: 0.0
---

と、yamlファイルに入っていたデータが/cmd_velトピックでpub/subされます。

複数のメッセージをpublishする

複数のメッセージをpublishします。
cmd_vel_val.yamlを編集して、以下のようにします。

cmd_vel_val.yaml
linear: 
  x: 1.0
  y: 0.0
  z: 0.0
angular: 
  x: 0.0
  y: 0.0
  z: 0.0
---
linear: 
  x: 0.0
  y: 1.0
  z: 0.0
angular: 
  x: 0.0
  y: 0.0
  z: 0.0
---
linear: 
  x: 0.0
  y: 0.0
  z: 1.0
angular: 
  x: 0.0
  y: 0.0
  z: 0.0
---

3つのTwistの値が入ったファイルになりました。
これをpublishすると

term_pub
$ cat cmd_vel_val.yaml| rostopic pub /cmd_vel geometry_msgs/Twist
publishing and latching message for 3.0 seconds
publishing and latching message for 3.0 seconds
publishing and latching message for 3.0 seconds
term_sub
$ rostopic echo /cmd_vel
linear: 
  x: 1.0
  y: 0.0
  z: 0.0
angular: 
  x: 0.0
  y: 0.0
  z: 0.0
---
linear: 
  x: 0.0
  y: 1.0
  z: 0.0
angular: 
  x: 0.0
  y: 0.0
  z: 0.0
---
linear: 
  x: 0.0
  y: 0.0
  z: 1.0
angular: 
  x: 0.0
  y: 0.0
  z: 0.0
---

このように3つのメッセージが順にpublishされます。
term_subのrosotpic echo /cmd_velはterm_pubのrostopic pubより先に実行しておいてください。
この方法では、rostopic pubすると3つのデータが待ち時間無しでpublishされます。
一定の時間間隔でpublishしたい場合は

term_a
$ cat cmd_vel_val.yaml| rostopic pub -r 1 /cmd_vel geometry_msgs/Twist

のように、-rオプションを付けてください。
これで/cmd_velトピックに、1秒おきに
linear = {x:1.0, y:0,0, z:0.0}, {x:0.0, y:1.0, z:0.0}, {x:0.0, y:0.0, z:1.0}
の3つのメッセージをpublishすることができました。

所感

自動テストで一連のメッセージを、値を指定してpublishしたいときなどに、
テキストエディタで値をいじれて便利なやり方ではないでしょうか。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?