You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/writing_a_simple_publisher_and_subscriber.md
+22-20Lines changed: 22 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
# Writing a simple publisher and subscriber (RUST)
2
-
* Goal: Create and run a publisher and subscriber node using Python.
2
+
* Goal: Create and run a publisher and subscriber node using RUST.
3
3
* Tutorial level: Beginner
4
4
* Time: 20 minutes
5
5
<details><summary>Background</summary>
@@ -8,40 +8,42 @@ In this tutorial you will create a pair of
8
8
[nodes](https://docs.ros.org/en/humble/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Nodes/Understanding-ROS2-Nodes.html) that pass information to each other via a
9
9
[topic](https://docs.ros.org/en/humble/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Topics/Understanding-ROS2-Topics.html) in the form of string messages. The example used here is a simple "talker" and "listener" system; one node publishes data and the other subscribes to the topic to receive that data.
10
10
11
-
Since Rust doesn't have inheritance, it's not possible to inherit from `Node` as is common practice in [`rclcpp`](https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Cpp-Publisher-And-Subscriber.html) or [`rclpy`](https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Py-Publisher-And-Subscriber.html).
11
+
Since RUST doesn't have inheritance, it's not possible to inherit from `Node` as is common practice in [`rclcpp`](https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Cpp-Publisher-And-Subscriber.html) or [`rclpy`](https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Py-Publisher-And-Subscriber.html).
12
12
13
-
The code used in these examples can be found [here](https://gitlab.com/ros21923912/simple_ros2_node)
13
+
The code used in these examples can be found [here](https://gitlab.com/ROS21923912/simple_ROS2_node)
14
14
<divstyle="margin-left:20px;">
15
15
<details><summary>Side-note on dependencies</summary>
16
16
17
-
You may be wondering why you can't just add all your ros2-specific dependencies to `cargo.toml` with `cargo add ${dependencie}` and have to edit this file manually. Here is why:
18
-
Almost none of the ROS2 dependencies you'll need for your ros2 rust node development currently exist on [crates.io](https://crates.io/), the main source for rust depencies. So the add command simply can't find the dependency targets. What colcon does by compiling the ros2 rust dependencies and your ros2 rust project is redirect the cargo search for dependencies directly into your `workspace/install` folder, where it'll find locally generated rust projects to use as dependencies. In particular, almost all message types will be called as dependencies for your ros2 rust project this way.
17
+
You may be wondering why you can't just add all your ROS2-specific dependencies to `Cargo.toml` with `cargo add YOUR_DEPENDENCIES` and have to edit this file manually. Here is why:
18
+
Almost none of the ROS2 dependencies you'll need for your ROS2 RUST node development currently exist on [crates.io](https://crates.io/), the main source for RUST depencies. So the add command simply can't find the dependency targets. What colcon does by compiling the ROS2 RUST dependencies and your ROS2 RUST project is redirect the cargo search for dependencies directly into your `workspace/install` folder, where it'll find locally generated RUST projects to use as dependencies. In particular, almost all message types will be called as dependencies for your ROS2 RUST project this way.
19
19
20
20
</details></div>
21
21
22
22
</details>
23
23
24
24
<details><summary>Prerequisites </summary>
25
25
26
-
In previous tutorials, you learned how to create a [workspace](https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Creating-A-Workspace/Creating-A-Workspace.html) and [create a package](https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Creating-Your-First-ROS2-Package.html).
26
+
Basic concepts of development with ROS2 should be known:
A basic understanding of [RUST](https://www.rust-lang.org/) is recommended, but not entirely necessary.
29
-
Before developing ROS2 RUST nodes, you must follow the
30
-
[installation instructions](https://github.com/ros2-rust/ros2_rust/blob/main/README.md) for [`rclrs`](https://docs.rs/rclrs/latest/rclrs/).
30
+
A basic understanding of [RUST](https://doc.rust-lang.org/book/) is recommended, but not entirely necessary.
31
+
Before developing [ros2-rust](https://github.com/ros2-rust/ros2_rust) nodes, you must follow the
32
+
[installation instructions](https://github.com/ros2-rust/ros2-rust/blob/main/README.md) for [`rclrs`](https://docs.rs/rclrs/latest/rclrs/).
31
33
32
34
33
35
</details>
34
36
35
37
<details><summary>Tasks </summary>
36
38
<divstyle="margin-left:20px;"><details><summary>Create a Package</summary>
37
39
38
-
Currently, building a package for ROS2 RUST is different
39
-
from building packages for Python or C/C++.
40
+
Currently, building a package for ros2-rust is different
41
+
from building packages for [Python](https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Py-Publisher-And-Subscriber.html) or [C/C++](https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Cpp-Publisher-And-Subscriber.html).
40
42
41
43
First, you'll need to create and go into a standard [cargo](https://doc.rust-lang.org/cargo/)
42
44
project as follows:
43
45
```
44
-
cargo new your_project_name && cd your_project_name
46
+
cargo new your_package_name && cd your_package_name
45
47
```
46
48
In the [`Cargo.toml`](https://doc.rust-lang.org/book/ch01-03-hello-cargo.html) file, add a dependency on `rclrs = "*"` and `std_msgs = "*"` by editing this file. For a full Introduction into RUST, please read the very good [RUST book](https://doc.rust-lang.org/book/title-page.html). Your `Cargo.toml` could now look like this:
47
49
```
@@ -61,7 +63,7 @@ std_msgs = "*"
61
63
Additionally, create a new `package.xml` if you want your node to be buildable with [`colcon`](https://colcon.readthedocs.io/en/released/user/installation.html). Make sure to change the build type to `ament_cargo` and to include the two packages mentioned above in the dependencies, as such:
<details><summary>Examining the code in detail:</summary>
172
174
173
-
#### The first 3 lines of the Rust code imports tools for thread synchronization, time handling, iteration, threading, ROS 2 communication, and string message publishing.
175
+
#### The first 3 lines of the RUST code imports tools for thread synchronization, time handling, iteration, threading, ROS 2 communication, and string message publishing.
174
176
```
175
177
use std::{sync::Arc,time::Duration,iter,thread};
176
178
use rclrs::{RclrsError,QOS_PROFILE_DEFAULT,Context,create_node,Node,Publisher};
<details><summary>Having several Ros2 rust nodes in one Package</summary>
288
+
<details><summary>Having several ROS2 RUST nodes in one Package</summary>
287
289
288
290
Of course, you can write for each node you want to implement its own package, and that can have it's advantages. I implore you to use some cargo tricks and add some binary targets to your `cargo.toml`. That could look like this:
289
291
```
@@ -300,7 +302,7 @@ path="src/main.rs"
300
302
rclrs = "*"
301
303
std_msgs = "*"
302
304
```
303
-
You'll find the name of your executable and the corresponding file name under the `[[bin]]` tag. As you can see, the filename and the name you want to call your node don't have to match. Please remember to include your executable name with snake_cases. The rust compiler will be a bit grumpy if you don't.
305
+
You'll find the name of your executable and the corresponding file name under the `[[bin]]` tag. As you can see, the filename and the name you want to call your node don't have to match. Please remember to include your executable name with snake_cases. The RUST compiler will be a bit grumpy if you don't.
304
306
Now, by recompiling the package from the previous chapter and making it usable:
305
307
```
306
308
cd ${MainFolderOfWorkspace}
@@ -315,7 +317,7 @@ As you can see, you are now calling your node by the name declared in `[[bin]]`
315
317
</details>
316
318
<details><summary>Write the subscriber node</summary>
317
319
318
-
Of course, you can implement a new ros2 rust package for this node. You can find out how to do this in the section called 'Create a package'.
320
+
Of course, you can implement a new ROS2 RUST package for this node. You can find out how to do this in the section called 'Create a package'.
319
321
Or you can add a new binary target to your package. To do so, just add a new `<file>.rs` to your source directory - for simplicity I'll call this file `simple_subscriber.rs` - and add a corresponding binary target to your `Cargo.toml`:
320
322
```
321
323
[[bin]]
@@ -479,11 +481,11 @@ Once you have implemented the code, you are ready to make it runnable:
479
481
cd ${MainFolderOfWorkspace}
480
482
colcon build
481
483
```
482
-
Please note that you'll need to run your nodes in separate terminals. In each terminal, you'll need to source your ros2 installation separately. So for each of the two nodes you've built so far, open a terminal and type the following:
484
+
Please note that you'll need to run your nodes in separate terminals. In each terminal, you'll need to source your ROS2 installation separately. So for each of the two nodes you've built so far, open a terminal and type the following:
483
485
```
484
486
cd ${MainFolderOfWorkspace}
485
487
source install/setup.bash
486
-
ros2 run your_project_name your_node_name
488
+
ros2 run your_package_name your_node_name
487
489
```
488
490
In my case, the nodes are called `simple_publisher` and `simple_subscriber`. You can name your nodes whatever you like. It is important that the publisher and subscriber use the same topic type and name.
489
491
If you haven't had any errors so far and have successfully started the Publisher and Subscriber, you should see something similar in the Subscriber's Terminal window:
0 commit comments