Skip to content

Commit ea764b0

Browse files
pablogs9jamoralpFranFin
authored
Add creating new message tutorial (#191)
* Initial tutorial * Update * Update _docs/tutorials/core/create_new_type/index.md Co-authored-by: Jose Antonio Moral <joseantoniomoralparras@gmail.com> * Update _docs/tutorials/core/create_new_type/index.md Co-authored-by: Jose Antonio Moral <joseantoniomoralparras@gmail.com> * Update _docs/tutorials/core/create_new_type/index.md Co-authored-by: Jose Antonio Moral <joseantoniomoralparras@gmail.com> * Add type composition * Update _docs/tutorials/core/create_new_type/index.md Co-authored-by: FranFin <58737168+FranFin@users.noreply.github.com> * Update _docs/tutorials/core/create_new_type/index.md Co-authored-by: FranFin <58737168+FranFin@users.noreply.github.com> * Update _docs/tutorials/core/create_new_type/index.md Co-authored-by: FranFin <58737168+FranFin@users.noreply.github.com> * Update _docs/tutorials/core/create_new_type/index.md Co-authored-by: FranFin <58737168+FranFin@users.noreply.github.com> * Update index.md * Update _docs/tutorials/core/create_new_type/index.md Co-authored-by: FranFin <58737168+FranFin@users.noreply.github.com> Co-authored-by: Jose Antonio Moral <joseantoniomoralparras@gmail.com> Co-authored-by: FranFin <58737168+FranFin@users.noreply.github.com>
1 parent e440bec commit ea764b0

2 files changed

Lines changed: 139 additions & 0 deletions

File tree

_data/docs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
- tutorials/core/first_application_rtos
4747
- tutorials/core/programming_rcl_rclc
4848
- tutorials/core/microxrcedds_rmw_configuration
49+
- tutorials/core/create_new_type
4950

5051
- title: Advanced Tutorials
5152
docs:
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
---
2+
title: How to create a new micro-ROS type
3+
permalink: /docs/tutorials/core/create_new_type/
4+
---
5+
6+
This tutorial starts in a previously created micro-ROS environment. Check [**First micro-ROS application on an RTOS**](../first_application_rtos/) for instructions about how to create a micro-ROS environment for embedded platforms.
7+
8+
Once your micro-ROS workspace is created, go to `firmware/mcu_ws` and run the package creating command:
9+
10+
```bash
11+
cd firmware/mcu_ws
12+
ros2 pkg create --build-type ament_cmake my_custom_message
13+
cd my_custom_message
14+
mkdir msg
15+
touch msg/MyCustomMessage.msg
16+
```
17+
18+
In the autogenerated `CMakeLists.txt` file you should add the following lines just before `ament_package()`:
19+
20+
```cmake
21+
...
22+
find_package(rosidl_default_generators REQUIRED)
23+
24+
rosidl_generate_interfaces(${PROJECT_NAME}
25+
"msg/MyCustomMessage.msg"
26+
)
27+
...
28+
```
29+
30+
31+
In the autogenerated `package.xml` file you should add the following lines:
32+
33+
```xml
34+
...
35+
<build_depend>rosidl_default_generators</build_depend>
36+
<exec_depend>rosidl_default_runtime</exec_depend>
37+
<member_of_group>rosidl_interface_packages</member_of_group>
38+
...
39+
```
40+
41+
The content of the `msg/MyCustomMessage.msg` file contains your message defintion. For example, let's include these fields:
42+
43+
```
44+
bool bool_test
45+
byte byte_test
46+
char char_test
47+
float32 float32_test
48+
float64 double_test
49+
int8 int8_test
50+
uint8 uint8_test
51+
int16 int16_test
52+
uint16 uint16_test
53+
int32 int32_test
54+
uint32 uint32_test
55+
int64 int64_test
56+
uint64 uint64_test
57+
```
58+
59+
Now, you can build your micro-ROS workspace as usual. As explained in [**First micro-ROS application on an RTOS**](../first_application_rtos/), the `ros2 run micro_ros_setup build_firmware.sh` command will build all packages located inside `mcu_ws`.
60+
61+
In your micro-ROS application code, you can use your new message type as usual:
62+
63+
```c
64+
#include <my_custom_message/msg/my_custom_message.h>
65+
66+
...
67+
68+
my_custom_message__msg__MyCustomMessage msg;
69+
70+
msg.byte_test = 3;
71+
msg.uint32_test = 42;
72+
73+
...
74+
75+
rclc_publisher_init_default(&publisher, &node, ROSIDL_GET_MSG_TYPE_SUPPORT(my_custom_message, msg, MyCustomMessage), "my_custom_publisher");
76+
rcl_publish(&publisher, &msg, NULL);
77+
78+
...
79+
```
80+
81+
You can find further information in the [ROS 2 Create custom ROS 2 msg and srv files](https://index.ros.org/doc/ros2/Tutorials/Custom-ROS2-Interfaces).
82+
83+
## Using type composition
84+
85+
It is possible to create custom types that include members from another ROS 2 message types packages. For example let's add a member with type `Point32` from the ROS 2 package `geometry_msgs`.
86+
87+
First of all, you have to include the dependency in the `CMakeLists.txt`:
88+
89+
```cmake
90+
...
91+
find_package(rosidl_default_generators REQUIRED)
92+
find_package(geometry_msgs REQUIRED)
93+
94+
rosidl_generate_interfaces(${PROJECT_NAME}
95+
"msg/MyCustomMessage.msg"
96+
)
97+
...
98+
```
99+
100+
Also, include the dependency in `package.xml`:
101+
102+
```xml
103+
...
104+
<build_depend>rosidl_default_generators</build_depend>
105+
<exec_depend>rosidl_default_runtime</exec_depend>
106+
<member_of_group>rosidl_interface_packages</member_of_group>
107+
<depend>geometry_msgs</depend>
108+
...
109+
```
110+
111+
The message definition in `msg/MyCustomMessage.msg` can now include types from the `geometry_msgs` package:
112+
113+
```
114+
...
115+
int64 int64_test
116+
uint64 uint64_test
117+
geometry_msgs/Point32 point32_test
118+
```
119+
120+
And finally, in your code you can access this new member of your custom type:
121+
122+
123+
```c
124+
#include <my_custom_message/msg/my_custom_message.h>
125+
126+
...
127+
128+
my_custom_message__msg__MyCustomMessage msg;
129+
130+
msg.byte_test = 3;
131+
msg.uint32_test = 42;
132+
133+
msg.point32_test.x = 1.23;
134+
msg.point32_test.y = 2.31;
135+
msg.point32_test.z = 3.12;
136+
137+
...
138+
```

0 commit comments

Comments
 (0)