ubuntu22.04 vscode配置ros2调试环境,支持python,c++功能包

1. 安装VSCode扩展

在VSCode中安装以下扩展:

  • C/C++:用于C++代码的智能提示和调试。
  • Python:用于Python代码的代码补全和调试。
  • ROS:ROS官方扩展,提供ROS 2的完整开发支持。

2. 配置VSCode

打开VSCode,并打开ROS 2工作空间:

code ~/ros2_ws

2.1 配置tasks.json

在VSCode中,按Ctrl+Shift+P,输入Tasks: Configure Task,然后选择Create tasks.json file from template,选择Others

编辑生成的tasks.json文件,添加以下内容:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "colcon build",
            "type": "shell",
            "command": "colcon",
            "args": [
		            "build",
								"--symlink-install",
								"--event-handlers",
								"console_cohesion+",
								"--cmake-args",
								"-DCMAKE_BUILD_TYPE=RelWithDebInfo"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [],
            "detail": "Build the ROS 2 workspace"
        }
    ]
}

2.2 配置launch.json

Ctrl+Shift+P,输入Debug: Open launch.json,然后选择C++ (GDB/LLDB)

编辑生成的launch.json文件,添加以下内容:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "ROS2 Debug C++",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/install/${input:package_name}/lib/${input:package_name}/${input:package_executable}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "colcon build",
            "miDebuggerPath": "/usr/bin/gdb",
            "targetArchitecture": "x86_64",
            "logging": {
                "trace": true,
                "traceResponse": true,
                "engineLogging": true
            }
        },
        {
            "name": "ROS2 Debug Python",
            "type": "debugpy",
            "request": "launch",
            "program": "${workspaceFolder}/src/${input:package_name}/${input:package_name}/${input:package_executable}.py",
            "args": [],
            "console": "integratedTerminal",
            "justMyCode": false,
            "preLaunchTask": "colcon build",
    ],
    "inputs": [
    {
      "id": "package_name",
      "type": "promptString",
      "description": "ros2 package name",
      "default": "pub_test"
    },
    {
      "id": "package_executable",
      "type": "promptString",
      "description": "ros2 package executable",
      "default": "pub"
    }
  ]
}

注意:将package_namepackage_executable替换为要运行的ROS 2包名和节点名。

3. 调试

按F5选择定义好的调试器,输入包名和节点。

4. 参考资料

古月居 - ROS机器人知识分享社区

基于VS code调试ROS2功能包教程_ubuntu22.04 vscode 调试ros2-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值