|
| 1 | +# Writing a simple publisher and subscriber (RUST) |
| 2 | +* Goal: Create and run a publisher and subscriber node using Python. |
| 3 | +* Tutorial level: Beginner |
| 4 | +* Time: 20 minutes |
| 5 | +<details><summary>Background</summary> |
| 6 | + |
| 7 | +In this tutorial you will create |
| 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 | +[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 | + |
| 11 | +The code used in these examples can be found [here](https://gitlab.com/ros21923912/simple_ros2_node) |
| 12 | +<div style="margin-left:20px;"> |
| 13 | +<details><summary>Sidenode to dependencies</summary> |
| 14 | + |
| 15 | +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: |
| 16 | +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 | + |
| 18 | +</details></div> |
| 19 | + |
| 20 | +</details> |
| 21 | + |
| 22 | +<details><summary>Prerequisites </summary> |
| 23 | + |
| 24 | +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). |
| 25 | + |
| 26 | +A basic understanding of [RUST](https://www.rust-lang.org/) is recommended, but not entirely necessary. |
| 27 | +Before developing ROS2 RUST nodes, you must follow the |
| 28 | +[installation instructions](https://github.com/ros2-rust/ros2_rust/blob/main/README.md) for them. |
| 29 | + |
| 30 | + |
| 31 | +</details> |
| 32 | + |
| 33 | +<details><summary>Tasks </summary> |
| 34 | +<div style="margin-left:20px;"> |
| 35 | +<details><summary>Create a Package</summary> |
| 36 | + |
| 37 | +Currently, building a package for ROS2 RUST is different |
| 38 | +from building packages for Python or C/C++. |
| 39 | + |
| 40 | +First, you'll need to create and go into a standard [cargo](https://doc.rust-lang.org/cargo/) |
| 41 | +project as follows: |
| 42 | +``` |
| 43 | +cargo new your_project_name && your_project_name |
| 44 | +``` |
| 45 | +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) |
| 46 | + |
| 47 | + |
| 48 | +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: |
| 49 | +```xml |
| 50 | +<package format="3"> |
| 51 | + <name>your_project_name</name> |
| 52 | + <version>0.0.0</version> |
| 53 | + <description>TODO: Package description. Seriously. Please make a Package description. We all will thank for it.</description> |
| 54 | + <maintainer email="user@todo.todo">user</maintainer> |
| 55 | + <license>TODO: License declaration. Licenses are Great. Please add a Licence</license> |
| 56 | + |
| 57 | + <depend>rclrs</depend> |
| 58 | + <depend>std_msgs</depend> |
| 59 | + |
| 60 | + <export> |
| 61 | + <build_type>ament_cargo</build_type> |
| 62 | + </export> |
| 63 | +</package> |
| 64 | +``` |
| 65 | +<details><summary>Write the publisher node</summary><details> |
| 66 | + |
| 67 | + |
| 68 | +</details> |
| 69 | +</div> |
| 70 | +</details> |
0 commit comments