基于VS code调试ROS2功能包教程

该教程介绍了如何在VScode中安装ROS插件,创建并编译ROS2功能包,使用colcon或VScode任务进行构建,并设置GDB进行单节点调试。还提供了一个示例仓库链接和参考资料。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1. 安装ROS的vscode插件

image.png
额外可以安装插件:CMake C/C++ 等等

2. 创建功能包并用vscode插件方式编译

编译方式不仅有VScode插件这一种方式,可以通过命令行在终端中进行编译。

$ cd ~

$ mkdir -p learn_ros2_ws/src

$ cd learn_ros2_ws/src

$ ros2 pkg create cpp_primer_pubsub --build-type ament_cmake --dependencies rclcpp std_msgs

$ cd ~/learn_ros2_ws

$ code .
  • 方法1:

通过vscode配置 task.json,task.json将位于learn_ros2_ws/.vscode/,快捷键Ctrl+Shift+B编译功能包或者点击Terminal-> Run Build Task.
image.png
image.png

  • 方法二:

    按照方式一任意选择配置风格,然后做如下修改:

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "colcon",
			"args": [
				"build",
				"--symlink-install",
				"--event-handlers",
				"console_cohesion+",
				"--base-paths",
				"/home/linux/learn_ros2_ws",
				"--cmake-args",
				"-DCMAKE_BUILD_TYPE=RelWithDebInfo"
			],
			"problemMatcher": [
				"$catkin-gcc"
			],
			"group": "build",
			"label": "colcon: build"
		}
	]
}

3. 调试(基于VScode Debug)

  • learn_ros2_ws/.vscode/下创建launch.json

  • 单节点(进程) 调试 – 选择GDB launch方式

image.png

{
  // 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/${input:package_name}/lib/${input:package_name}/${input:package_executable}",
      "args": [],
      "stopAtEntry": true,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "setupCommands": [
          {
              "description": "Enable pretty-printing for gdb",
              "text": "-enable-pretty-printing",
              "ignoreFailures": true
          },
          // {
          //     "description": "Set Disassembly Flavor to Intel",
          //     "text": "-gdb-set disassembly-flavor intel",
          //     "ignoreFailures": true
          // }
      ]
    }
  ],
  "inputs": [
    {
      "id": "package_name",
      "type": "promptString",
      "description": "ros2 package name",
      "default": "cpp_primer_pubsub"
    },
    {
      "id": "package_executable",
      "type": "promptString",
      "description": "ros2 package executable",
      "default": "talker"
    }
  ]
}

4. 示例仓库:

https://gitee.com/mingdonghu/vscode-debugger-ros1_2

5.参考资料

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值