ふるお〜と!- FullAuto

AI・ロボットが普及しBI(ベーシックインカム)が早急に実現されることを願う元ニートのブログ

ふるお〜と!-FullAuto

GazeboからROSプラグインを呼ぶサンプルを試す

ROS2 Dashingの場合:

/opt/ros/dashing/share/gazebo_plugins/worlds/ 配下にgazebo pluginのサンプルがあるので試してみた。

実行

単純に

$ gazebo --verbose /opt/ros/dashing/share/gazebo_plugins/worlds/gazebo_ros_camera_demo.world 

と実行すると

[Err] [Plugin.hh:180] Failed to load plugin libgazebo_ros_camera.so: libCameraPlugin.so: cannot open shared object file: No such file or directory

libCameraPlugin.soが見つからないよと怒られます。

とりあえず、探してみる。

$ locate libCameraPlugin.so
/usr/lib/x86_64-linux-gnu/gazebo-9/plugins/libCameraPlugin.so

ある。

/usr/share/gazebo-9/setup.shに上記の場所へのPATHを通すスクリプトがあるので、それを実行するとエラーが解消します。

再実行

$ . /usr/share/gazebo-9/setup.sh
$ gazebo --verbose /opt/ros/dashing/share/gazebo_plugins/worlds/gazebo_ros_camera_demo.world 

Publish & Subscribe

上のデモを実行すると ノードがトピックに対しデータの送信をし続けます(Publish)。
トピックにあるデータを購読(Subscribe)してみます。

上のデモのtopicのlistは下記コマンドで取得できます。

$ ros2 topic list
/clock
/demo_cam/camera1/camera_info
/demo_cam/camera1/image_raw
/demo_cam/parameter_events
/demo_cam/rosout
/parameter_events
/rosout

Subscriver例1:RViz2

トピックにあるデータは画像なのでビジュアライザを使用します。

$ /opt/ros/dashing/bin/rviz2

(※ Parallels等の仮想環境で実行するとOpenGLエラーとなる)

左下にある「Add」ボタン→「By topic」タブ→ツリーの「demo_cam/camera1/image_raw/Image」を選択肢しOK ボタン
をクリックすると左下にカメラの画像が表示されます。

f:id:nullpo24:20190622145653p:plain
RViz2

Subscriver例2:OpenCV

ros2/demosのソースを変えてビルドします

ワークスペースソースコード配置

$ mkdir -p ~/ros2_ws/src
$ cd ~/ros2_ws/src
# 依存関係
$ git clone https://github.com/ros2/launch
# ros/demos
$ git clone https://github.com/ros2/demos

(※demosは枯れていないので不要なソースはビルドエラーになるため削除した方がいいかもしれない)
demos/image_tools/src/showimage.cppの記述されているtopic名をpublisherが送信しているtopic名に合わせます。
上のデモは("/demo_cam/camera1/image_raw")

88: // std::string topic("image");
89: std::string topic("/demo_cam/camera1/image_raw");

ビルド

$ cd ~/ros2_ws
$ colcon build --symlink-install

※1

実行

$ ./build/image_tools/showimage 

f:id:nullpo24:20190622204757p:plain
OpenCV

備考

※1:公式のインストールガイドのみだとcolconコマンドが使えなかったので下記ページを参考に追加インストールしました。(-H オプションは必須)

ROS2インストール・初期設定 for Dashing Diademata - Qiita

トピックの画像データを文字列で取得

ros2 topic echoでトピックの場所を指定すれば画像のヘッダとデータを取得できます。

$ ros2 topic echo /demo_cam/camera1/image_raw
header:
  stamp:
    sec: 627
    nanosec: 34000000
  frame_id: camera_link
height: 480
width: 640
encoding: rgb8
is_bigendian: 0
step: 1920
data: [180, 180, 180, 177, 177, 177, 176, 176, 176, 180, 180, 180, 175, 175, 175, 178, 178, 178, 179, 179, 177, 177, 177, 179, 179, 179, 181, 181, '...']