1
1

備忘録 ラズパイ5 ROS2 ⑥ Ubuntu Desktop 24.04LTSでROS2環境 C++ その2 セットアップ pub.cpp rqt_graph

Last updated at Posted at 2024-07-29

 前回、C++で初めてのパッケージ・プログラムを作りました。
 パッケージ名はworksです。

yoshi@yoshi:~/ros2_ws$ tree
.
├── build
├── install
├── log
└── works
   ├── CMakeLists.txt
   ├── include
   │   └── works
   ├── package.xml
   └── src
       └── exec_sample
           └── main.c

環境

  • Raspberry Pi 5 8GB
  • 追加ボード;NVMe Base for Raspberry Pi 5 (NVMe Base by Pimoroni)
  • Crucial クルーシャル P2シリーズ 500GB 3D NAND NVMe PCIe M.2 SSD CT500P2SSD8
  • 初期;RaspberryPi OS Desktop 64bit (Debian version: 12 (bookworm) Release date: March 15th 2024)
  • 現在;Ubuntu Desktop 24.04LTS(64-bit)
  • ROS2 HumbleではなくJazzy

Windows10で、検索窓にcmdと入れ、コマンドプロンプトを起動します。
 sshでログインします(第1回参照)。必要ならupdateします。

C:\Users\yoshi>ssh yoshi.local

ros2が実行できるようにします。

$ source /opt/ros/jazzy/setup.bash
$ cd ros2_ws

publishするプログラムを作る

 works/srcの下に、pubという新しいソースのディレクトリを作り、プログラムpub.cppを保存します。

yoshi@yoshi:~/ros2_ws$ mkdir works/src/pub
yoshi@yoshi:~/ros2_ws$ nano works/src/pub/pub.cpp
pub.cpp
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include <stdio.h>

int main(int argc, char **argv)
{
 printf("start publish test\n");
 rclcpp::init(argc, argv);
 auto node = rclcpp::Node::make_shared("publisher_yoshi");
 auto publisher = node->create_publisher<std_msgs::msg::String>("greeting", 1);

 rclcpp::WallRate loop(1);
 int count = 0;
 while (rclcpp::ok()) {
   auto msg = std_msgs::msg::String();
   msg.data = "Hello, world " + std::to_string(count++);
   publisher->publish(msg);
   loop.sleep();
 }

 rclcpp::shutdown();
 return 0;
}
└── works
    ├── CMakeLists.txt
    ├── include
    │   └── works
    ├── package.xml
    └── src
        ├── exec_sample
        │   └── main.c
        └── pub
            └── pub.cpp

 CMakeLists.txtは、前回のままです。
 ビルドします。

yoshi@yoshi:~/ros2_ws$ colcon build
Starting >>> works
Finished <<< works [0.24s]

Summary: 1 package finished [0.38s]

 CMakeLists.txtを変更していないので、前回作ったmain.cがビルドされただけですね。

ヘッダー類を追加したときのエラーの問題を解決する

 CMakeLists.txtを変更します。次のように、pub.cpp部分を追加します。

add_executable(pub  src/pub/pub.cpp)

install(
 TARGETS pub
 DESTINATION lib/${PROJECT_NAME}
)

 ビルドするとエラーが出ました。rclcpp/rclcpp.hppが取り込めなかったようです。

yoshi@yoshi:~/ros2_ws$ colcon build
Starting >>> works
--- stderr: works
/home/yoshi/ros2_ws/works/src/pub/pub.cpp:1:10: fatal error: rclcpp/rclcpp.hpp: No such file or directory
    1 | #include "rclcpp/rclcpp.hpp"
      |          ^~~~~~~~~~~~~~~~~~~

 では、パッケージのクリエイトで追加しようとすると、

yoshi@yoshi:~/ros2_ws$ ros2 pkg create --build-type ament_cmake works --dependencies rclcpp rclcpp_components std_msgs
Aborted!
The directory already exists: ./works
Either remove the directory or choose a different destination directory or package name

worksには追加?できなさそうです。まっさらな状態で、上記を実行すると、CMakeLists.txtのなかに、find_package(..が追加されているのがわかりました。ros2をインストールした時点で、それらのヘッダー類はすでに全部が含まれているということのようです。1日費やしてなんとなくわかってきました。
 CMakeLists.txtを修正します。ライブラリを使うので探してきてねということを記述します。もともと、find_package(ament_cmake REQUIRED)はありました。そのあとに、今回利用する"rclcpp/rclcpp.hpp"と"std_msgs/msg/string.hpp"のために、なぜか3行追加しています。
 なお、 find_package(..した後に、add_executable(..の記述を追加します。順番が逆だとエラーになるようです。

CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
project(works)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_components REQUIRED)
find_package(std_msgs REQUIRED)

add_executable(exec_sample
  src/exec_sample/main.c)

install(
  TARGETS exec_sample
  DESTINATION lib/${PROJECT_NAME}
)

add_executable(pub src/pub/pub.cpp)

ament_target_dependencies(pub rclcpp std_msgs)

install(
  TARGETS pub
  DESTINATION lib/${PROJECT_NAME}
)


if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # comment the line when a copyright and license is added to all source files
  set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # comment the line when this package is in a git repo and when
  # a copyright and license is added to all source files
  set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()

 ラズパイのターミナルで、ビルドします(必要ないかも)。

 yoshi@yoshi:~/ros2_ws$ colcon build

 可視化ツールrqt_graphを動かします。

$ source /opt/ros/jazzy/setup.bash
$ rqt_graph

 今回作ったプログラムを登録し、起動します。

yoshi@yoshi:~/ros2_ws$ source ~/ros2_ws/install/setup.bash
(省略)
yoshi@yoshi:~/ros2_ws$ ros2 pkg list | grep works
ros_workspace
works
yoshi@yoshi:~/ros2_ws$ ros2 run works pub
start publish test

 rqt_graphの左上にあるリフレッシュ・アイコンをクリックしたのが次の画面です。nodeの名称が表示されています。たぶん、publisherが動いているようです。

Screenshot from 2024-07-26 03-31-59.png


備忘録 ラズパイ5 ROS2

① ハードの用意とUbuntu Desktop 24.04LTS

② Ubuntu Desktop 24.04LTSでROS2環境 rqt_graphとturtlesim

③ Ubuntu Desktop 24.04LTSでROS2環境 Python その1 responder(セットアップ)

④ Ubuntu Desktop 24.04LTSでROS2環境 Pythonその2responder(コーディングと実行;失敗)

⑤ Ubuntu Desktop 24.04LTSでROS2環境 C++ その1 セットアップ main.c

⑥ Ubuntu Desktop 24.04LTSでROS2環境 C++ その2 セットアップ pub.cpp rqt_graph

⑦ Ubuntu Desktop 24.04LTSでROS2環境 C++ その3 セットアップ sub.cpp rqt_graph

⑧ Ubuntu Desktop 24.04LTSでROS2環境 C++ その4 bme280.cpp 温度


1
1
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
1
1