1
1

備忘録 ラズパイ5 ROS2 ⑤ Ubuntu Desktop 24.04LTSでROS2環境 C++ その1 セットアップ main.c

Last updated at Posted at 2024-07-28

 Python環境の開発がうまくいかないので、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

挫折しないように

 こちらのWebページを参照させていただき、進めていきます。

 ここで解説されているC++によるROS2開発の手順をトレースしていきます。

 すでに、ros2とcolonはインストールしてあります。
 Pythonで使ったresponder関連は削除しました。

そもそも

 ROSのプログラミングのスタイルは、複数のパッケージがあって、それぞれのパッケージには複数のソース・プログラムが存在しているという形のようです。そうです、BLEのServiceとCharacteristicとそっくりです。

 前回まで、Pythonでプログラムを作ろうとしていました。挫折しましたが。Pythonはインタプリタなので、ソースを書けばOKです。でも、C++は、コンパイルをして実行ファイルを作っておかないといけません。コンパイルとリンクにはmakeが使われますが、今は、Cmakeを利用するのが一般的なので、CMakeLists.txtを記述しないといけません。
今まで読んだ入門記事では、colcon buildで、最低限必要なスケルトンを作って、ディレクトリや必要なフィルは自分で作るという事例が大半です。初心者の私には全容がつかめません。
 Pythonのときは、

$ ros2 pkg create responder --node-name responder --build-type ament_python

という、ディレクトリの作成と、必要なpackage.xml、setup.cfg、setup.pyのスケルトンが作られる便利なコマンドがありました。

 ros2コマンドを使えるようにし、作業用ros2_wsに移動します。

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

 C++のパッケージの引数は、下記のサイトを参考にしました。

 ここで、rclcppという知らない用語が出てきました。

もっとそもそも

 rclcppは、ROS2のC++ライブラリのようです。どこにあるのでしょうか。探しても、インストールする記事が見つかりません。

C++用パッケージを作る

 パッケージ名はworksにしました。

yoshi@yoshi:~/ros2_ws$ ros2 pkg create --build-type ament_cmake works
going to create a new package
package name: works
destination directory: /home/yoshi/ros2_ws
package format: 3
version: 0.0.0
description: TODO: Package description
maintainer: ['yoshi <yoshi@todo.todo>']
licenses: ['TODO: License declaration']
build type: ament_cmake
dependencies: []
creating folder ./works
creating ./works/package.xml
creating source and include folder
creating folder ./works/src
creating folder ./works/include/works
creating ./works/CMakeLists.txt

[WARNING]: Unknown license 'TODO: License declaration'.  This has been set in the package.xml, but no LICENSE file has been created.
It is recommended to use one of the ament license identifiers:
Apache-2.0
BSL-1.0
BSD-2.0
BSD-2-Clause
BSD-3-Clause
GPL-3.0-only
LGPL-3.0-only
MIT
MIT-0
yoshi@yoshi:~/ros2_ws$ tree
.
└── works
    ├── CMakeLists.txt
    ├── include
    │   └── works
    ├── package.xml
    └── src

5 directories, 2 files
yoshi@yoshi:~/ros2_ws$ cd works
yoshi@yoshi:~/ros2_ws/works$ cd src
yoshi@yoshi:~/ros2_ws/works/src$ mkdir exec_sample

 パッケージの下にソースを入れるsrcがあり、複数のソースを書けるので、ここでは、exec.sampleというディレクトリを作りました。
 ここに、main.cを入れます。

#include <stdio.h>
int main()
{
  printf("hello\n");
  return 0;
}

 worksの下に作られたCMakeLists.txtに、次の部分を追加します。


add_executable(exec_sample
  src/exec_sample/main.c)

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

 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)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)


add_executable(exec_sample
  src/exec_sample/main.c)

install(
  TARGETS exec_sample
  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()

 ros2_wsディレクトリに移り、ビルドします。

yoshi@yoshi:~/ros2_ws$ colcon build

 出来上がった実行プログラムをROS2システムに登録します。確認し、実行します。

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 exec_sample
hello

 ソースの部分だけのtree。ここまで3日かかりました。手探り状態が続きます。

.
├── build
├── install
├── log
└── works
    ├── CMakeLists.txt
    ├── include
    │   └── works
    ├── package.xml
    └── src
        └── exec_sample
            └── main.c

63 directories, 141 files

備忘録 ラズパイ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


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