1. 概要
前ページで作成したソースをビルドして、実行します。
本ページは、下記のサイトを参考にさせていただきました。
「Writing a simple service and client (C++) — ROS 2 Documentation: Foxy documentation」
2. ビルド
初めて動かす呪文があります。
cd ~/ros2_ws
rosdep install -i --from-path src --rosdistro jazzy -y
下記の出力があります。
#All required rosdeps installed successfully
次に。
colcon build --packages-select cpp_srvcli
以降、この環境を使用するために。
echo "source ~/ros2_ws/install/setup.bash" >> ~/.bashrc
source ~/.bashrc
3. 実行
端末を開いて、まず、サーバ側を実行します。
ros2 run cpp_srvcli server
下記が表示されます。
[INFO] [1733710900.757127614] [rclcpp]: Ready to add two ints.
クライアント側として、端末を開いて。
ros2 run cpp_srvcli client 1 2
末尾の2つの数値は、適当な値をいれます。
サーバ側の表示。
[INFO] [1733711223.343656984] [rclcpp]: Incoming request
a: 1 b: 2
[INFO] [1733711223.343706025] [rclcpp]: sending back response: [3]
クライアント側の表示。
[INFO] [1733711223.346700939] [rclcpp]: Sum: 3
なるほどね。
思い通りに動きました。
4. まとめ
サンプルが、サンプル通りに動くというのは、当たり前なことで、わかっていることと、わかっていないことをまとめておきたいと思います。
サービスのサーバ・クライアント間のやりとりを定義するソースが
~/ros2_jazzy/src/ros2/example_interfaces/srv/AddTwoInts.srv
に用意してあって、中身がこんなの。
int64 a
int64 b
---
int64 sum
ただ、これを元に。
~/ros2_jazzy/build/example_interfaces/rosidl_generator_cpp/example_interfaces/srv/
|-- add_two_ints.hpp
|-- detail
| |-- add_two_ints__builder.hpp
| |-- add_two_ints__struct.hpp
| |-- add_two_ints__traits.hpp
| |-- add_two_ints__type_support.hpp
| |-- set_bool__builder.hpp
| |-- set_bool__struct.hpp
| |-- set_bool__traits.hpp
| |-- set_bool__type_support.hpp
| |-- trigger__builder.hpp
| |-- trigger__struct.hpp
| |-- trigger__traits.hpp
| `-- trigger__type_support.hpp
|-- set_bool.hpp
`-- trigger.hpp
ちゅうのが作成されていて。
~/ros2_jazzy/build/example_interfaces/rosidl_generator_cpp/example_interfaces/srv/detail/add_two_ints__struct.hpp
の。
namespace example_interfaces
{
namespace srv
{
// message struct
template<class ContainerAllocator>
struct AddTwoInts_Request_
{
using Type = AddTwoInts_Request_<ContainerAllocator>;
// field types and members
using _a_type =
int64_t;
_a_type a;
using _b_type =
int64_t;
_b_type b;
namespace example_interfaces
{
namespace srv
{
// message struct
template<class ContainerAllocator>
struct AddTwoInts_Response_
{
using Type = AddTwoInts_Response_<ContainerAllocator>;
// field types and members
using _sum_type =
int64_t;
_sum_type sum;
あたりのコードを利用できるのだな。
ということまでは、わかるのですが、「srv/」に書いてあるものが、ビルドされて「.hpp」になるのだか。
「.hpp」は、ごりごり書かねばならないのかが、わからなかったのですが・・・。
「Creating custom ROS 2 msg and srv files — ROS 2 Documentation: Crystal documentation」あたりに書いてあるようです。