这里的源码,是指的ros2 humble的官方源码。如果是自己手撸的节点或相关源码,请参考本人以前的贴子,
Ubuntu20.04+vscode快速调试ROS通用程序_ubuntu20.04vscode那个版本和ros 兼容_高精度计算机视觉的博客-CSDN博客
Ubuntu20.04+gdb/vscode调试ROS(VINS-Mono)程序_ros gdb用terminal_高精度计算机视觉的博客-CSDN博客
======================================
这一步,只编译debug模式,但不要安装到/usr/bin下,
本来这里不需要贴出来,但由于官方老是更新,所以为了直接反映当前的情况,我直接贴到下面,
https://docs.ros.org/en/humble/Installation/Alternatives/Ubuntu-Development-Setup.html
Table of Contents
The current Debian-based target platforms for Humble Hawksbill are:
Tier 1: Ubuntu Linux - Jammy (22.04) 64-bit
Tier 3: Ubuntu Linux - Focal (20.04) 64-bit
Tier 3: Debian Linux - Bullseye (11) 64-bit
Other Linux platforms with varying support levels include:
Arch Linux, see alternate instructions
Fedora Linux, see alternate instructions
OpenEmbedded / webOS OSE, see alternate instructions
As defined in REP 2000.
Make sure you have a locale which supports UTF-8
.
If you are in a minimal environment (such as a docker container), the locale may be something minimal like POSIX
.
We test with the following settings. However, it should be fine if you’re using a different UTF-8 supported locale.
locale # check for UTF-8
sudo apt update && sudo apt install locales
sudo locale-gen en_US en_US.UTF-8
sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
export LANG=en_US.UTF-8
locale # verify settings
You will need to add the ROS 2 apt repository to your system.
First ensure that the Ubuntu Universe repository is enabled.
sudo apt install software-properties-common
sudo add-apt-repository universe
Now add the ROS 2 GPG key with apt.
sudo apt update && sudo apt install curl -y
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg
Then add the repository to your sources list.
echo “deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(. /etc/os-release && echo $UBUNTU_CODENAME) main” | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null
Install common packages.
sudo apt update && sudo apt install -y
python3-flake8-docstrings
python3-pip
python3-pytest-cov
ros-dev-tools
Install packages according to your Ubuntu version.
sudo apt install -y
python3-flake8-blind-except
python3-flake8-builtins
python3-flake8-class-newline
python3-flake8-comprehensions
python3-flake8-deprecated
python3-flake8-import-order
python3-flake8-quotes
python3-pytest-repeat
python3-pytest-rerunfailures
Create a workspace and clone all repos:
mkdir -p ~/ros2_humble/src
cd ~/ros2_humble
vcs import --input https://raw.githubusercontent.com/ros2/ros2/humble/ros2.repos src
ROS 2 packages are built on frequently updated Ubuntu systems.
It is always recommended that you ensure your system is up to date before installing new packages.
sudo apt upgrade
sudo rosdep init
rosdep update
rosdep install --from-paths src --ignore-src -y --skip-keys “fastcdr rti-connext-dds-6.0.1 urdfdom_headers”
Note:
If you’re using a distribution that is based on Ubuntu (like Linux
Mint) but does not identify itself as such, you’ll get an error message
like Unsupported OS [mint]
. In this case append --os=ubuntu:jammy
to the above command.
If you would like to use another DDS or RTPS vendor besides the default, you can find instructions here.
If you have already installed ROS 2 another way (either via Debians
or the binary distribution), make sure that you run the below commands
in a fresh environment that does not have those other installations
sourced.
Also ensure that you do not have source /opt/ros/${ROS_DISTRO}/setup.bash
in your .bashrc
.
You can make sure that ROS 2 is not sourced with the command printenv | grep -i ROS
.
The output should be empty.
More info on working with a ROS workspace can be found in this tutorial.
cd ~/ros2_humble/
colcon build --symlink-install
Note:
if you are having trouble compiling all examples and this is preventing
you from completing a successful build, you can use COLCON_IGNORE
in the same manner as CATKIN_IGNORE to ignore the subtree or remove the folder from the workspace.
Take for instance: you would like to avoid installing the large OpenCV library.
Well then simply run touch COLCON_IGNORE
in the cam2image
demo directory to leave it out of the build process.
Set up your environment by sourcing the following file.
Replace “.bash” with your shell if you’re not using bash
Possible values are: setup.bash, setup.sh, setup.zsh
. ~/ros2_humble/install/local_setup.bash
In one terminal, source the setup file and then run a C++ talker
:
. ~/ros2_humble/install/local_setup.bash
ros2 run demo_nodes_cpp talker
In another terminal source the setup file and then run a Python listener
:
. ~/ros2_humble/install/local_setup.bash
ros2 run demo_nodes_py listener
You should see the talker
saying that it’s Publishing
messages and the listener
saying I heard
those messages.
This verifies both the C++ and Python APIs are working properly.
Hooray!
Continue with the tutorials and demos to configure your environment, create your own workspace and packages, and learn ROS 2 core concepts.
The ROS 1 bridge can connect topics from ROS 1 to ROS 2 and vice-versa. See the dedicated documentation on how to build and use the ROS 1 bridge.
The default middleware that ROS 2 uses is Fast DDS
, but the middleware (RMW) can be replaced at runtime.
See the guide on how to work with multiple RMWs.
Using a different compiler besides gcc to compile ROS 2 is easy. If you set the environment variables CC
and CXX
to executables for a working C and C++ compiler, respectively, and retrigger CMake configuration (by using --force-cmake-config
or by deleting the packages you want to be affected), CMake will reconfigure and use the different compiler.
To configure CMake to detect and use Clang:
sudo apt install clang
export CC=clang
export CXX=clang++
colcon build --cmake-force-configure
See Maintain source checkout to periodically refresh your source installation.
Troubleshooting techniques can be found here.
If you installed your workspace with colcon as instructed above,
“uninstalling” could be just a matter of opening a new terminal and not
sourcing the workspace’s setup
file.
This way, your environment will behave as though there is no Humble install on your system.
If you’re also trying to free up space, you can delete the entire workspace directory with:
rm -rf ~/ros2_humble
======================================
《colcon-build-ros2-humble》
cd ~/ros2_humble/
当然,你可以使用官方的指令,不过编译出来默认是Release,不能进行源码调试,
colcon build --symlink-install
https://autowarefoundation.github.io/autoware-documentation/pr-278/how-to-guides/advanced-usage-of-colcon/
实际这里要使用下面的Debug模式,因为要调试,
colcon build --cmake-args -DCMAKE_BUILD_TYPE=Debug
colcon build --cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo
colcon build --cmake-args -DCMAKE_BUILD_TYPE=Release
然后,如果下面的命令可以成功执行,就说明编译成功了,
. ~/ros2_humble/install/local_setup.bash
ros2 run demo_nodes_cpp talker
. ~/ros2_humble/install/local_setup.bash
ros2 run demo_nodes_py listener
因为ROS2在ubuntu下面的编译几乎没什么难度,所以这里不展开了。
======================================
为了避免修改源码,这里我把
/home/matthew/ros2_humble/src/ros2/demos/demo_nodes_cpp,
/home/matthew/ros2_humble/src/ros2/examples,
等测试文件夹直接拷贝到这里,
/home/matthew/test/src/demo_nodes_cpp
然后test就相当于workspace,教程上一般会用ros2_ws这样的目录,本质上是一样的,其下一定要有src目录,方便管理。
cd ~/test
colcon build --cmake-args -DCMAKE_BUILD_TYPE=Debug
一切正常的话就会生成debug版本的talker等一系列可执行文件,其中talker的位置在这里,
/home/matthew/test/install/demo_nodes_cpp/lib/demo_nodes_cpp
其他应用程序都是一样的原理,因为实际生成的可执行文件比较多,这里就不列出来了,后面我会调试talker和publisher_member_function进行调试。
======================================
用VSCODE打开文件夹,
/home/matthew/test/
然后通过vscode的运行调试按钮添加launch.json(这里只调试talker),内容如下,
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/install/demo_nodes_cpp/lib/demo_nodes_cpp/talker",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
快捷键ctrl+shift+p,找到Tasks:Configure Task,添加tasks.json文件;
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang-14 生成活动文件",
"command": "/usr/bin/clang-14",
"args": [
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "编译器: /usr/bin/clang-14"
}
]
}
快捷键ctrl+shift+p,找到C/C++ :Edit configurations (JSON),添加c_cppproperties.json文件,
{
"configurations": [
{
"name": "testconfig",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/src/demo_nodes_cpp_native/include",
"${workspaceFolder}/src/demo_nodes_cpp/include",
"/usr/include/**",
"/home/matthew/ros2_humble/installed/include/**",
"/home/matthew/ros2_humble/installed/include/rclcpp/**",
"/home/matthew/ros2_humble/installed/include/rclcpp/**",
"/home/matthew/ros2_humble/installed/include/rclcpp/**",
"/home/matthew/ros2_humble/installed/include/actionlib_msgs/**",
"/home/matthew/ros2_humble/installed/include/action_msgs/**",
"/home/matthew/ros2_humble/installed/include/action_tutorials_interfaces/**",
"/home/matthew/ros2_humble/installed/include/ament_index_cpp/**",
"/home/matthew/ros2_humble/installed/include/benchmark/**",
"/home/matthew/ros2_humble/installed/include/builtin_interfaces/**",
"/home/matthew/ros2_humble/installed/include/camera_calibration_parsers/**",
"/home/matthew/ros2_humble/installed/include/camera_info_manager/**",
"/home/matthew/ros2_humble/installed/include/class_loader/**",
"/home/matthew/ros2_humble/installed/include/composition_interfaces/**",
"/home/matthew/ros2_humble/installed/include/dds/**",
"/home/matthew/ros2_humble/installed/include/ddsc/**",
"/home/matthew/ros2_humble/installed/include/diagnostic_msgs/**",
"/home/matthew/ros2_humble/installed/include/example_interfaces/**",
"/home/matthew/ros2_humble/installed/include/examples_rclcpp_cbg_executor/**",
"/home/matthew/ros2_humble/installed/include/fastcdr/**",
"/home/matthew/ros2_humble/installed/include/fastdds/**",
"/home/matthew/ros2_humble/installed/include/fastrtps/**",
"/home/matthew/ros2_humble/installed/include/foonathan_memory/**",
"/home/matthew/ros2_humble/installed/include/geometry_msgs/**",
"/home/matthew/ros2_humble/installed/include/gmock/**",
"/home/matthew/ros2_humble/installed/include/gtest/**",
"/home/matthew/ros2_humble/installed/include/iceoryx/**",
"/home/matthew/ros2_humble/installed/include/idl/**",
"/home/matthew/ros2_humble/installed/include/idlc/**",
"/home/matthew/ros2_humble/installed/include/image_tools/**",
"/home/matthew/ros2_humble/installed/include/image_transport/**",
"/home/matthew/ros2_humble/installed/include/interactive_markers/**",
"/home/matthew/ros2_humble/installed/include/intra_process_demo/**",
"/home/matthew/ros2_humble/installed/include/kdl_parser/**",
"/home/matthew/ros2_humble/installed/include/keyboard_handler/**",
"/home/matthew/ros2_humble/installed/include/laser_geometry/**",
"/home/matthew/ros2_humble/installed/include/libstatistics_collector/**",
"/home/matthew/ros2_humble/installed/include/libyaml_vendor/**",
"/home/matthew/ros2_humble/installed/include/lifecycle_msgs/**",
"/home/matthew/ros2_humble/installed/include/logging_demo/**",
"/home/matthew/ros2_humble/installed/include/map_msgs/**",
"/home/matthew/ros2_humble/installed/include/mcap_vendor/**",
"/home/matthew/ros2_humble/installed/include/message_filters/**",
"/home/matthew/ros2_humble/installed/include/mimick/**",
"/home/matthew/ros2_humble/installed/include/moodycamel/**",
"/home/matthew/ros2_humble/installed/include/nav_msgs/**",
"/home/matthew/ros2_humble/installed/include/OGRE/**",
"/home/matthew/ros2_humble/installed/include/osrf_testing_tools_cpp/**",
"/home/matthew/ros2_humble/installed/include/pendulum_msgs/**",
"/home/matthew/ros2_humble/installed/include/performance_test_fixture/**",
"/home/matthew/ros2_humble/installed/include/pluginlib/**",
"/home/matthew/ros2_humble/installed/include/qt_gui_cpp/**",
"/home/matthew/ros2_humble/installed/include/rcl/**",
"/home/matthew/ros2_humble/installed/include/rcl_action/**",
"/home/matthew/ros2_humble/installed/include/rclcpp/**",
"/home/matthew/ros2_humble/installed/include/rclcpp_action/**",
"/home/matthew/ros2_humble/installed/include/rclcpp_components/**",
"/home/matthew/ros2_humble/installed/include/rclcpp_lifecycle/**",
"/home/matthew/ros2_humble/installed/include/rcl_interfaces/**",
"/home/matthew/ros2_humble/installed/include/rcl_lifecycle/**",
"/home/matthew/ros2_humble/installed/include/rcl_logging_interface/**",
"/home/matthew/ros2_humble/installed/include/rcl_yaml_param_parser/**",
"/home/matthew/ros2_humble/installed/include/rcpputils/**",
"/home/matthew/ros2_humble/installed/include/rcutils/**",
"/home/matthew/ros2_humble/installed/include/resource_retriever/**",
"/home/matthew/ros2_humble/installed/include/rmw/**",
"/home/matthew/ros2_humble/installed/include/rmw_connextdds/**",
"/home/matthew/ros2_humble/installed/include/rmw_dds_common/**",
"/home/matthew/ros2_humble/installed/include/rmw_fastrtps_cpp/**",
"/home/matthew/ros2_humble/installed/include/rmw_fastrtps_dynamic_cpp/**",
"/home/matthew/ros2_humble/installed/include/rmw_fastrtps_shared_cpp/**",
"/home/matthew/ros2_humble/installed/include/robot_state_publisher/**",
"/home/matthew/ros2_humble/installed/include/ros2cli_test_interfaces/**",
"/home/matthew/ros2_humble/installed/include/rosbag2_compression/**",
"/home/matthew/ros2_humble/installed/include/rosbag2_compression_zstd/**",
"/home/matthew/ros2_humble/installed/include/rosbag2_cpp/**",
"/home/matthew/ros2_humble/installed/include/rosbag2_interfaces/**",
"/home/matthew/ros2_humble/installed/include/rosbag2_storage/**",
"/home/matthew/ros2_humble/installed/include/rosbag2_storage_default_plugins/**",
"/home/matthew/ros2_humble/installed/include/rosbag2_storage_mcap_testdata/**",
"/home/matthew/ros2_humble/installed/include/rosbag2_test_common/**",
"/home/matthew/ros2_humble/installed/include/rosbag2_transport/**",
"/home/matthew/ros2_humble/installed/include/rosgraph_msgs/**",
"/home/matthew/ros2_humble/installed/include/rosidl_runtime_c/**",
"/home/matthew/ros2_humble/installed/include/rosidl_runtime_cpp/**",
"/home/matthew/ros2_humble/installed/include/rosidl_typesupport_c/**",
"/home/matthew/ros2_humble/installed/include/rosidl_typesupport_cpp/**",
"/home/matthew/ros2_humble/installed/include/rosidl_typesupport_fastrtps_c/**",
"/home/matthew/ros2_humble/installed/include/rosidl_typesupport_fastrtps_cpp/**",
"/home/matthew/ros2_humble/installed/include/rosidl_typesupport_interface/**",
"/home/matthew/ros2_humble/installed/include/rosidl_typesupport_introspection_c/**",
"/home/matthew/ros2_humble/installed/include/rosidl_typesupport_introspection_cpp/**",
"/home/matthew/ros2_humble/installed/include/rqt_gui_cpp/**",
"/home/matthew/ros2_humble/installed/include/rttest/**",
"/home/matthew/ros2_humble/installed/include/rviz_common/**",
"/home/matthew/ros2_humble/installed/include/rviz_default_plugins/**",
"/home/matthew/ros2_humble/installed/include/rviz_rendering/**",
"/home/matthew/ros2_humble/installed/include/rviz_visual_testing_framework/**",
"/home/matthew/ros2_humble/installed/include/sensor_msgs/**",
"/home/matthew/ros2_humble/installed/include/shape_msgs/**",
"/home/matthew/ros2_humble/installed/include/statistics_msgs/**",
"/home/matthew/ros2_humble/installed/include/std_msgs/**",
"/home/matthew/ros2_humble/installed/include/std_srvs/**",
"/home/matthew/ros2_humble/installed/include/stereo_msgs/**",
"/home/matthew/ros2_humble/installed/include/test_msgs/**",
"/home/matthew/ros2_humble/installed/include/tf2/**",
"/home/matthew/ros2_humble/installed/include/tf2_bullet/**",
"/home/matthew/ros2_humble/installed/include/tf2_eigen/**",
"/home/matthew/ros2_humble/installed/include/tf2_eigen_kdl/**",
"/home/matthew/ros2_humble/installed/include/tf2_geometry_msgs/**",
"/home/matthew/ros2_humble/installed/include/tf2_kdl/**",
"/home/matthew/ros2_humble/installed/include/tf2_msgs/**",
"/home/matthew/ros2_humble/installed/include/tf2_ros/**",
"/home/matthew/ros2_humble/installed/include/tf2_sensor_msgs/**",
"/home/matthew/ros2_humble/installed/include/tlsf/**",
"/home/matthew/ros2_humble/installed/include/tlsf_cpp/**",
"/home/matthew/ros2_humble/installed/include/tracetools/**",
"/home/matthew/ros2_humble/installed/include/trajectory_msgs/**",
"/home/matthew/ros2_humble/installed/include/turtlesim/**",
"/home/matthew/ros2_humble/installed/include/unique_identifier_msgs/**",
"/home/matthew/ros2_humble/installed/include/urdf/**",
"/home/matthew/ros2_humble/installed/include/urdfdom/**",
"/home/matthew/ros2_humble/installed/include/urdfdom_headers/**",
"/home/matthew/ros2_humble/installed/include/urdf_parser_plugin/**",
"/home/matthew/ros2_humble/installed/include/visualization_msgs/**"
],
"defines": [],
"compilerPath": "/usr/bin/clang-14",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "linux-clang-x64"
}
],
"version": 4
}
这里面,为什么include path会这么多,因为官方的源码是把所有的include合并到一个文件夹中的,我这里就不合并了,只是把所有的install下面的include文件夹拷贝到/home/matthew/test/installed下面去了;子目录非常多,我是通过脚本自动添加的这些路径。
脚本的办法五花八门,我自己喜欢用NotePad++结合Linux下几个常用的指令,参考如下,
find . -type d > output.txt
or tree
tree -d > output.txt
or,
ls > list.txt
然后通过文本处理器Notepad++替换添加一顿操作弄好之后,直接贴到include path里就OK了。
======================================
到这里,一切准备就绪,vscode里打开一个terminal,输入,
$ cd ~/test
$ . ~/ros2_humble/install/setup.bash
$ . ./install/setup.bash
这里要注意 ,一定是要在左边已经选择(gdb) Launch的情况 下,在右侧的cppdbg:publisher_member_function窗口中输入setup.bash指令 ,不是bash窗口,如下图的右下角所示,否则会找不到librclcpp.so等库文件 而无法调试。
然后,就可以在member_function里随便打断点,进入ros2 humble的源码了,截图如下所示,
可以看到,成功进入了ros2 humble的源码了,
本文结束