My repository for ros2 code examples
- Create your ROS2 workspace
mkdir ~/<your_ros2_workspace_name>/src
- Change directory into src
cd ~/<your_ros2_workspace_name>/src
- Clone this repo
git clone [email protected]:rizkymille/ros2-examples
- Rename
ros2-examples
toros2_examples
(avoid confusing ros2 package system by using snake case)
mv ros2-examples ros2_examples
- Build package in workspace
cd ~/<your_ros2_workspace_name>
colcon build
- If the interface package only uses msg type, then the package name should be:
<package_name>_msgs
- If the interface package only uses srv type, then the package name should be:
<package_name>_srvs
- If the interface package only uses action type, then the package name should be:
<package_name>_acts
- If the interface package contains srv and msg type, or all the three types, then the package name should be:
<package_name>_infs
This package is language mixed, that's why I still use CMakeLists even there's python program.
module
folder are for module or library. Andscripts
are for executables.- In executables, don't forget to add shebang line
#!/usr/bin/env python3
. Otherwise your program can't be found - Refrain yourself from declaring variables in class. Use
self.variable = <initial_variable>
- Put variable declaration above method declaration in the class (Please don't follow ros2 examples).
- Use
const
and&
as possible in function parameters. You'll save memory. Example:void RunTest(const int& test_num)
- Contrary to headers, you can define method body directly inside the class
- Rather than traditional
.h
format, use.hpp
. Put that insideinclude/<project_name>
- In terms of header files, write the function body in
.cpp
file - The define name should be
<PACKAGE_NAME>_HPP_
in#ifndef
,#define
, and as comments in#endif
. Example:#endif //_<PACKAGE_NAME>_HPP_
Name should be hiearchical with slash as separator of hiearchy,contains package name as the top level of hierarchy, and represents the message.
Examples:
Service name for takeoff requests
<package_name>/motion/takeoff
Topic name for vision position
<package_name>/sensor/vision
Action name for position requests
<package_name>/motion/position
Parameter name for Proportional roll constant
<package_name>/control/PID/roll/P
Message variables
auto tkf_req_msg
Best practices:
- Always use single word whenever possible
- Create documentation in README.md for every message name and types
- Custom data type name should be one word and reusable
- Use available data type from ROS (like std_msgs and sensor_msgs) if possible
Variable for construction of message operation should be go like this:
<lowest_message_hierarchy>_<message_operation>
The message_operation should contains just 3 starting syllables
Example:
- Service name for takeoff requests
Server:
takeoff_ser
Client:
takeoff_cli
- Topic name for vision position
Publisher:
vision_pub
Subscriber:
vision_sub
Callback:
vision_cb
- Action name for position requests
Special case: use_act_
between message operation and lowest message hierarchy as designator
Client:
pos_act_cli
Server:
pos_act_ser
- Timer name for trajectory publishing task
Timer naming should be representing what the task it is doing
Example:
publish_trajectory_timer
- Callback group
Callback group naming is based on priority number
Example:
prio_1_cb_group
prio_2_cb_group