ROS2节点使用vscode debug

Ubuntn22.04 6 + ROS2 Humble

首先安装 gdbserver

sudo apt-get -y install gdbserver

创建工作空间及src文件夹 ws/src,用colcon编译(ros1 是 catkin,ros2是colcon)

创建功能包

ros2 pkg create your_package_name

source setup.bash文件,与ros1不同,ros2的setup.bash文件在工作空间的install文件夹下

source install/setup.bash

修改功能包的cmakelist.txt,加入

set(CMAKE_BUILD_TYPE Debug)

可以打断点暂停

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(pcl_ros REQUIRED)
find_package(pcl_conversions REQUIRED)
find_package(sensor_msgs REQUIRED)

加入相关的依赖项

# 生成可执行文件
add_executable (executable_file_name src/your_file_name.cpp)

# 配置可执行文件的依赖项
ament_target_dependencies(executable_file_name 
  rclcpp 
  pcl_ros 
  pcl_conversions 
  sensor_msgs)  

# 将可执行文件写入路径
install(TARGETS
  executable_file_name
  DESTINATION lib/${PROJECT_NAME})  

运行gdbserver

ros2 run --prefix 'gdbserver localhost:3000' package_name executable_name

用vscode打开工作空间

创建一个launch.json文件

写入如下内容

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C++ Debugger",
            "request": "launch",
            "type": "cppdbg",
            "miDebuggerServerAddress": "localhost:3000",
            "cwd": "/",
            "program": "你的可执行文件的路径"
        }
    ]
}

然后就可以打上断点debug了

你可能感兴趣的:(vscode,ide,编辑器,ros)