Library

ROS 2 nodes with rclsuru

rclsuru is a ROS 2 client library for する. Nodes written with it use topics, services, actions, parameters, logging and ROS time, and talk to rclcpp and rclpy nodes and the ros2 tools as they are. This page walks through a first node in about ten minutes. Every function is described in the API documentation.

topics + QoSservicesactionsparameters/rosout logginguse_sim_timeros2 run + launchabout 200 message types

01Install

You need:

Run source /opt/ros/jazzy/setup.bash in every terminal you use, before building. rclsuru finds the ROS 2 headers and libraries under /opt/ros/${ROS_DISTRO}. The repository also has a docker/Dockerfile with ROS 2, する and clang in place.

02Your first node

Make a project and add rclsuru to it:

terminal
$ source /opt/ros/jazzy/setup.bash
$ kumu 新規 hello
$ cd hello
$ kumu 追加 github.com/suru-lang/rclsuru

Write 本体.suru. This node says 「こんにちは」 on chatter every second:

本体.suru
「rclsuru」 を 読み込んで 使う

始めする とは 以下の手順です           # 始めする = start
  n とは 「hello」 を ノード作成した 結果
  p とは 文字列型 を 「chatter」 へ n で 発行者作成した 結果
  (「こんにちは」 を p へ 発行する) を 1.0 で n に 周期登録する
  「始めました」 を n で 情報記録する  # 情報記録する = log info
  n を スピンする
  以上です

始めする

ノード作成する makes the node (its name uses letters, digits and _ only, as ROS requires), 発行者作成する makes a publisher, 周期登録する runs an action every 1.0 seconds of ROS time, and スピンする keeps handling timers and messages until Ctrl-C. Run it, and listen from another terminal:

terminal
$ kumu 実行                 # builds and runs the node

$ ros2 topic echo chatter std_msgs/msg/String
data: こんにちは

kumu 作る makes a plain executable 出力/hello instead. The node also runs in the interpreter with suru 本体.suru.

03Listening

A listener subscribes to chatter and calls a function for every message. Start it next to the talker above, or next to ros2 run demo_nodes_cpp talker:

listener/本体.suru
「rclsuru」 を 読み込んで 使う

始めする とは 以下の手順です
  n とは 「listener」 を ノード作成した 結果
  文字列型 を 「chatter」 から n で 聞いたこと表示すること に 購読する
  n を スピンする
  以上です

# 聞いたこと表示する = show what we heard, 文 = sentence
文 を 聞いたこと表示する とは (「聞きました: 」 + 文) を 表示する

始めする
terminal
$ kumu 実行
聞きました: こんにちは
聞きました: こんにちは

文字列型 is std_msgs/msg/String, and its value in する is the string itself.

04Message types

Types of common packages come bundled (std_msgs, geometry_msgs, sensor_msgs, nav_msgs, example_interfaces and more, about 200 in all). Load a package with 「rclsuru/型/パッケージ」. This node drives the turtle of turtlesim in a circle:

亀/本体.suru
「rclsuru」 を 読み込んで 使う
「rclsuru/型/geometry_msgs」 を 読み込んで 使う

始めする とは 以下の手順です
  n とは 「turtle_driver」 を ノード作成した 結果
  p とは Twist型 を 「turtle1/cmd_vel」 へ n で 発行者作成した 結果
  t とは linear は (x は 2.0 の Vector3既定) で angular は (z は 1.0 の Vector3既定) の Twist既定
  (t を p へ 発行する) を 0.5 で n に 周期登録する
  n を スピンする
  以上です

始めする
terminal
$ ros2 run turtlesim turtlesim_node   # in another terminal
$ kumu 実行

Each message gives a record type (Twist), a default value (Twist既定, changed in part with x は 2.0 の …) and the message type for publishers and subscriptions (Twist型). For your own .msg and .srv files, 生成器.suru writes the types.

05Services

A service server calls a function for each request and returns its result as the response:

足し算サーバー/本体.suru
「rclsuru」 を 読み込んで 使う
「rclsuru/型/example_interfaces」 を 読み込んで 使う

始めする とは 以下の手順です
  n とは 「add_two_ints_server」 を ノード作成した 結果
  AddTwoInts型 を 「add_two_ints」 で n に 足し算すること へ サービス作成する
  n を スピンする
  以上です

# 足し算する = add, 要求 = request
要求 を 足し算する とは (sum は (要求 の a) + (要求 の b) の AddTwoInts_Response既定) を 結果にする

始めする
terminal
$ kumu 実行 &
$ ros2 service call /add_two_ints example_interfaces/srv/AddTwoInts "{a: 2, b: 3}"

Call it from another terminal (the answer is sum: 5). A client uses クライアント作成する, サービス待機する and 呼出する; see examples/足し算クライアント. Actions work the same way with アクションサーバー作成する; see examples/フィボナッチサーバー.

06Parameters and logs

Declare parameters before using them. The value is the one given at start (--ros-args -p rate:=5.0 or a YAML file), or else the default. Logs go to the console, /rosout and the log file, like rclcpp:

本体.suru
rate とは n から 「rate」 を 1.0 で 小数パラメータ宣言した 結果
名 とは n から 「name」 を 「suru」 で 文字列パラメータ宣言した 結果
(「こんにちは、」 + 名) を n で 情報記録する

ros2 param list, get, set and dump work on a する node as they are. The five log levels are 詳細記録する, 情報記録する, 警告記録する, エラー記録する and 致命記録する.

07ros2 run and launch

A built node is a plain executable, so an ament package can place it in lib/<package>/ and ros2 run and ros2 launch start it. Call kumu 作る from CMake:

CMakeLists.txt
find_program(KUMU kumu REQUIRED)
add_custom_target(hello ALL
  COMMAND ${KUMU} 作る
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/hello
  VERBATIM)
install(PROGRAMS ${CMAKE_CURRENT_SOURCE_DIR}/hello/出力/hello DESTINATION lib/${PROJECT_NAME})

The package ros/rclsuru_examples in the repository does this for the examples:

terminal
$ mkdir -p ws/src && cd ws/src && git clone https://github.com/suru-lang/rclsuru && cd ..
$ colcon build
$ source install/setup.bash
$ ros2 launch rclsuru_examples talker_listener.launch.py

08Going further

Coming from rclcpp or rclpy, these are the same calls:

rclcpprclpyする (rclsuru)
Node::make_shared("talker")Node("talker")「talker」 を ノード作成する
create_publisher<String>("chatter", 10)create_publisher(String, "chatter", 10)文字列型 を 「chatter」 へ n で 発行者作成する
pub->publish(msg)pub.publish(msg)値 を p へ 発行する
create_subscription<T>("t", 10, cb)create_subscription(T, "t", cb, 10)T型 を 「t」 から n で 関数 に 購読する
create_wall_timer(1s, cb)create_timer(1.0, cb)動作 を 1.0 で n に 周期登録する
rclcpp::spin(node)rclpy.spin(node)n を スピンする
create_service<T>("s", cb)create_service(T, "s", cb)T型 を 「s」 で n に 関数 へ サービス作成する
declare_parameter("rate", 1.0)declare_parameter("rate", 1.0)n から 「rate」 を 1.0 で 小数パラメータ宣言する
RCLCPP_INFO(get_logger(), "…")get_logger().info("…")「…」 を n で 情報記録する