- Set locale https://docs.ros.org/en/humble/Installation/Ubuntu-Install-Debians.html#set-locale
- Setup sources https://docs.ros.org/en/humble/Installation/Ubuntu-Install-Debians.html#setup-sources
- sudo apt update; sudo apt upgrade
- sudo apt install ros-humble-desktop
- source the setup script adding this line to the end of your ~/.bashrc
source /opt/ros/humble/setup.bash
- Try the installation running default examples:
ros2 run demo_nodes_cpp talker
ros2 run demo_nodes_cpp listener
sudo apt install python3-colcon-common-extension
- source the setup script to autocomplete, adding this line to the end of your ~/.bashrc
source /usr/share/colcon_argcomplete/hook/colcon-argcomplete.bash
- Create the project directory. The convention is the following:
mkdir ros2_ws
- Create the src directory in the project dir.
cd ros2_ws; mkdir src
- Build the project
colcon build
- source the setup script of the project adding this line to the end of your ~/.bashrc
source /media/sf_github-sync/ROS/ros2_ws/install/setup.bash
- Go to src of your project
cd src
- Create the package using ros command
ros2 pkg create my_py_pkg --build-type ament_python --dependencies rclpy
- To compile step in the project root directory and execute:
cd ../
colcon build
or you can build an specific package:colcon build --packages-select my_py_pkg
- Go to src of your project
cd src
- Create the package using ros command
ros2 pkg create my_cpp_pkg --build-type ament_cmake --dependencies rclcpp
- To compile step in the project root directory and execute:
cd ../
colcon build
or you can build an specific package:colcon build --packages-select my_cpp_pkg
In order to use Clangd as language server instead of VScode Intellisense, do the following:
-
Install the Clangd extension in VSCode. If you don't have clangd installed in your OS it will suggest the installation.
-
Open your settings.json:
ctrl + shift + P
> Preferences: Open User Settings (JSON) Or if you are using ssh-server for remote IDEctrl + shift + P
> Preferences: Open Remote Settings (JSON) -
Add the following configuration:
"clangd.arguments": [
"-log=verbose",
"-pretty",
"--background-index",
"--query-driver=/**/*",
"--compile-commands-dir=${workspaceFolder}/build"
],
- Add the flag
CMAKE_EXPORT_COMPILE_COMMANDS
to your CMakeLists.txt in order to generate the compile_commands.json file required by clangd
set (CMAKE_EXPORT_COMPILE_COMMANDS ON)
NOTE: Here you must have the executables added (add_executable and install) to your CMakeLists.
-
Build the package:
colcon build --packages-select my_cpp_pkg
-
Restart you language server:
ctrl + shift + P
> clangd: Restart language server
- Open the setup.py file of the package
- Add to the 'console-scripts' the name of the executable and the entry point.
NAME_OF_EXEC = NAME_OF_PKG.NAME_OF_NODE_FILE:ENTRY_FUNCTION
Ex.py_node = my_py_pkg.my_first_node:main
This will create the output file in the path specified in the setup.cfg file. In this case it will be: ros2_ws/install/my_py_pkg/lib/my_py_pkg/py_node
- Now you can build the pkg with:
colcon build --packages-select my_py_pkg
NOTE: It is possible to symlink the executable in order to avoid recompilation after changes. The python file must have execution permits.
3.1 chmod +x NODE_FILE_NAME.py
3.2 colcon build --packages-select my_py_pkg --symlink-install
Check the CMakeLists.txt file of my_cpp_pkg. It includes the add_executables
and install
macros.
ros2 run PACKAGE_NAME NODE_EXECUTABLE_NAME
Ex.
ros2 run my_py_pkg py_node
ros2 node list
and ros2 node info NODE_NAME
NOTE: This only give information for running nodes.
ros2 run my_py_pkg py_node --ros-args -r __node:=node2
This will create a timer where a callback will be called every given period.
This will create a timer where a callback will be called every given period.
For available built-in-types check the documentation
-
List all installed interfaces:
ros2 interface list
-
Get information about specific interface:
ros2 interface show example_interfaces/msg/String
For using already existing interfaces it is useful to use the example_interfaces provided by the lib.
In this case we want to use a msg type for a new publisher.
ros2 interface show example_interfaces/msg/String
In the case of python you need to add the dependency to package.xml
<depend>example_interfaces</depend>
- It's a good practice to create the custom interfaces in a dedicated package
ros2 pkg create my_robot_interfaces
- Remove src and include directories from created package.
- Create an msg folder in the package
mkdir msg
- Add this 3 lines to the package.xml
<build_depend>rosidl_default_generators</build_depend>
<exec_depend>rosidl_default_runtime</exec_depend>
<member_of_group>rosidl_interface_packages</member_of_group>
- Add the find package to CMakeLists
find_package(rosidl_default_generators REQUIRED)
- The file name needs to be in PascalCase and have msg extension. Ex.
HardwareStatus.msg
- Add the types to your msg.
- Add functions to generate the interfaces and to export the dependencies in the CMakeLists
rosidl_generate_interfaces(${PROJECT_NAME}
"msg/HardwareStatus.msg"
)
ament_export_dependencies(rosidl_default_runtime)
- Built the interfaces package and you will get the interfaces for python and C++.
List the topics:
ros2 topic list
Print what the topic is receiving like a subscriber:
ros2 topic echo \node-name
Get info about the topic:
ros2 topic info /topic_name
Get running topic frequency:
ros2 topic hz /topic_name
Get running topic band width:
ros2 topic bw /topic_name
Publish directly to a topic:
ros2 topic pub -r 100 /topic_name example_interfaces/msg/String "{data: 'Hello'}"
Example:
ros2 topic pub -r 2 /robot_news example_interfaces/msg/String "{data: 'Hello'}"
Rename a topic in runtime:
ros2 run my_py_pkg robot_news_station --ros-args -r __node:=my_station -r robot_news:=my_news
or
ros2 run my_py_pkg robot_news_station --ros-args -r robot_news:=my_news
List the services:
ros2 service list
call service like a client:
ros2 service call <service_name> <srv_type> '<request_args>'
Example:
ros2 service call /add_two_ints example_interfaces/srv/AddTwoInts "{a: 2, b: 3}"
get service interface type:
ros2 service type <service_name>
The service can be called using the rqt plugin "service caller". It allows you to call the service trough an interactive GUI.
Remap the service in runtime:
ros2 run my_cpp_package <node_name> --ros-args -r <service_name>:=<new_service_name>
- Configuration value for a node, usefull for any kind of setting needed.
- These are setted when starting the node or run-time.
- A parmeter is specific to a node.
- Has a name and a data type.
List each node with its parameters:
ros2 param list
Get the current value of an specific parameter:
ros2 param get </node_name> <param_name>
Start a node setting a parameter value:
ros2 run <package_name> <node_name> --ros-args -p <param_name>:=<value>
For multiple parameters you only have to add more -p:
ros2 run <package_name> <node_name> --ros-args -p <param1_name>:=<value> -p <param2_name>:=<value>
This is GUI framework tool that is used to debug and understand better your graph, node, services, etc Is a collection of pluggins that can be connected.
rqt
rqt_graph
It is a simulator package that allows you to interact with a graphic interface.