当前位置: 首页 > news >正文

Ubuntu20.04+vscode快速调试ROS通用程序

本文旨在引导大家使用vscode快速建立一个调试环境,并真正学会用vscode调试ROS-c/c++程序。碰到任何问题,欢迎在下面留言,我会随时补充。

如果英文好的话,可以去这里看英文原版的调试入门介绍,作者写得很细。我这里就不重复造轮子了,为了让大家以最快的速度上手,只把关键几步的信息记录下来,供大家参考。

这里假设你已经安装好了ROS noetic和git。

(1) 安装vscode和extensions

ubuntu上如何安装vscode可以参考官网,

Running Visual Studio Code on Linux

需要安装的vscode扩展如下,

  • C/C++ (c++ intellisense and configuration help) -> Mandatory
  • Clangd (Alternative intellisense provider. C/C++ intellisense needs to be disabled for this one to work) -> Optional
  • CMake (Intellisense support in CMakeLists.txt files) -> Optional
  • GitLens (Git support and additional git tab) -> Optional
  • Python (If you're using rospy) -> Mandatory
  • vscode-icons (Optional, but helps with all the different file types used by ROS) -> Optional
  • ROS (Adds helper actions for starting the roscore, for creating ROS packages and provides syntax highlighting for .msg, .urdf and other ROS files) -> Mandatory (Needed for the catkin_make task type)

你可以单独安装,也可以在下载完下面的项目后,加载时会问你是否要添加这些依赖时安装;我主要安装了ROS,c/c++, CMake这3个。

(2) 创建文件夹,下载文件

创建文件夹,比如我的路径为

​$cd ~/studyslam/ws/src
$git clone https://github.com/RoboGnome/VS_Code_ROS.git

(3) 运行vscode打开文件夹

这时候你可以用你的vscode打开程序文件夹了

~/studyslam/ws

 注意这个是你的主工程目录文件,当然如果你想更直接点打开下面这个目录也是可以的,设置大同小异,

~/studyslam/ws/src/VS_Code_ROS/hello_vs_code

但我们这里都以打开“~/studyslam/ws”这个目录为准进行讲解,目录结构如下图所示,

(4) 创建配置文件

接下来你要配置几个文件,你可以使用Ctrl+Shift+P输入task:config task等这种类型的方式,也可以直接手动添加,准备好下面这个几文件,

c_cpp_properties.json

快捷键ctrl+shift+p,找到C/C++ :Edit configurations (JSON),添加c_cppproperties.json文件,这个文件应该是指定一些路径和语言标准

{
  "configurations": [
    {
      "browse": {
        "databaseFilename": "${workspaceFolder}/.vscode/browse.vc.db",
        "limitSymbolsToIncludedHeaders": false
      },
      "includePath": [
        "/opt/ros/noetic/include/**",
        "/home/matthew/studyslam/ws/src/beginner_tutorials/include/**",
        "/home/matthew/projects/vinsmono/src/VINS-Mono/camera_model/include/**",
        "/usr/include/**"
      ],
      "name": "ROS",
      "intelliSenseMode": "gcc-x64",
      "compilerPath": "/usr/bin/gcc",
      "cStandard": "gnu11",
      "cppStandard": "c++14",
      "compileCommands": "${workspaceFolder}/build/compile_commands.json"
    }
  ],
  "version": 4
}

tasks.json

快捷键ctrl+shift+p,找到Tasks:Configure Task,添加tasks.json文件,这个文件指定一些catkin_make的编译参数。

注意这里的定义"-DCMAKE_BUILD_TYPE=Debug",

{
	"version": "2.0.0",
	"tasks": [		
		{
			"type": "catkin_make",
			"args": [
				"--directory",
				"/home/matthew/studyslam/ws",
				"-DCMAKE_BUILD_TYPE=Debug"
			],
			"problemMatcher": [
				"$catkin-gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"label": "catkin_make: build"
		}
	]
}

launch.json

{
    // 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": "Talker_Node",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/devel/lib/hello_vs_code/vs_talker",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        },
        {
            "name": "Listener_Node",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/devel/lib/hello_vs_code/vs_listener",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        },
        {
            "name": "Listener2_Node",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/devel/lib/hello_vs_code/vs_listener2",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ],
     "compounds": [
        {
            "name": "Talker/Listener",
            "configurations": ["Talker_Node", "Listener_Node"]
        }
    ],
}

注意这里launch.json里启动了三个节点,同时还有一个compound,写完这个之后,你可以在你的vscode下拉框中看到这几个选项,如下图所示,

 比如我要同时调试vs_talker和vs_listener,就选了talker/listener那个选项,对应的就是launch.json中的compounds那个。

然后,就可以在talker.cpp和listener.cpp中打断点进行单步调试了。

launch.json中的节点个数主要取决于你想调试哪些节点,比如当我只想调试talker的时候,我的launch.json是这个样子的,

{
    // 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}/devel/lib/hello_vs_code/vs_talker",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ],
}

这时候的vscode下拉菜单中的名称就只有一个“(gdb) Launch”了。

(5)启动调试

启动调试后,会有两个terminal窗口出现,对应这两个线程;此时会出现一个报错,原因是roscore没有启动。在vscode中打开第三个terminal,输入"roscore"启动之后,这两个线程就可以正常工作了。

vscode调试的结果相当于运动下面的程序

终端1:
$ source devel/setup.bash
$ roscore

终端2:
$ source devel/setup.bash
$ rosrun hello_vs_code vs_talker

终端3:
$ source devel/setup.bash
$ rosrun hello_vs_code vs_listener

当然,这些生成的程序本身都是可执行程序,你也可以直接这样运行

终端1: 
source devel/setup.bash
~/studyslam/ws$ ./vs_talker

终端2:
source devel/setup.bash
cd devel/lib/hello_vs_code
~/studyslam/ws/devel/lib/hello_vs_code$ ./vs_talker

终端3:
source devel/setup.bash
cd devel/lib/hello_vs_code
~/studyslam/ws/devel/lib/hello_vs_code$ ./vs_listener

Ubuntu下没有找到好的gif录屏软件(如果哪位知道,请告诉我),所以只好录制了一小段视频,但因为这里没法直接发视频,所以又只能另行上传,下载地址参考:

Ubuntu20.04+vscode快速调试ROS通用程序-其它文档类资源-CSDN下载

本文结束

参考资料:

GitHub - RoboGnome/VS_Code_ROS: Step by Step Guide to Automate Your ROS Workflow in the VS Code IDE

https://github.com/ms-iot/vscode-ros/blob/master/doc/debug-support.md

相关文章:

  • ROS-Tutorials:rviz之Markers: Sending Basic Shapes (C++,附vscode调试说明)
  • Qt Creator 的下载与安装
  • Ubuntu20.04突然丢失网络时恢复的办法
  • Linux设置访问权限
  • ROS noetic view_frames TypeError: cannot use a string pattern on a bytes-like object
  • ROS noetic [turtle1_tf_broadcaster-4] process has died
  • Ubuntu下使用unzip或p7zip解压带密码的zip文件
  • libcurl: (51) SSL: no alternative certificate subject name
  • SSL和SSH和OpenSSH,OpenSSL有什么区别
  • Ubuntu18.04网络连接图标上显示问号
  • Ubuntu调用USB摄像头
  • ERROR: libcudnn_adv_infer.so.8 is not a symbolic link
  • ROS error: Could not find the GUI, install the ‘joint_state_publisher_gui‘ package
  • ROS error: cannot launch node of type [arbotix_python/arbotix_driver]: arbotix_python
  • ROS error: robot_voice/iat_publish/usr/bin/ld: 找不到 -lmsc
  • 【347天】每日项目总结系列085(2018.01.18)
  • 77. Combinations
  • ES6语法详解(一)
  • ES学习笔记(10)--ES6中的函数和数组补漏
  • Hexo+码云+git快速搭建免费的静态Blog
  • Java知识点总结(JDBC-连接步骤及CRUD)
  • js ES6 求数组的交集,并集,还有差集
  • js对象的深浅拷贝
  • js如何打印object对象
  • mongo索引构建
  • orm2 中文文档 3.1 模型属性
  • PaddlePaddle-GitHub的正确打开姿势
  • 产品三维模型在线预览
  • 湖南卫视:中国白领因网络偷菜成当代最寂寞的人?
  • 使用agvtool更改app version/build
  • 树莓派 - 使用须知
  • 网页视频流m3u8/ts视频下载
  • 我的面试准备过程--容器(更新中)
  • Unity3D - 异步加载游戏场景与异步加载游戏资源进度条 ...
  • ​ 全球云科技基础设施:亚马逊云科技的海外服务器网络如何演进
  • #vue3 实现前端下载excel文件模板功能
  • #我与Java虚拟机的故事#连载09:面试大厂逃不过的JVM
  • (1)Android开发优化---------UI优化
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (4)(4.6) Triducer
  • (板子)A* astar算法,AcWing第k短路+八数码 带注释
  • (定时器/计数器)中断系统(详解与使用)
  • (附源码)spring boot球鞋文化交流论坛 毕业设计 141436
  • (官网安装) 基于CentOS 7安装MangoDB和MangoDB Shell
  • (接口自动化)Python3操作MySQL数据库
  • (转)setTimeout 和 setInterval 的区别
  • .jks文件(JAVA KeyStore)
  • .libPaths()设置包加载目录
  • .NET delegate 委托 、 Event 事件
  • .Net Memory Profiler的使用举例
  • .NET4.0并行计算技术基础(1)
  • .Net中的设计模式——Factory Method模式
  • /bin、/sbin、/usr/bin、/usr/sbin
  • @Mapper作用
  • @property括号内属性讲解