ros project debugging: A detailed tutorial on configuring and developing ROS projects under vscode

  • 2020-11-18 06:23:30
  • OfStack

The introduction

Configure the environment for ROS project development under vscode

This includes the configuration of the header file directory, the catkin_make command, the GDB debug configuration, and the ROS plug-in.

The vscode header file directory is configured to install the "c/c++" plug-in

Go to EXTENSIONS in the left column of vscode, search for "C/C++" and install

Generate c_cpp_properties json

The vscode self configuration file is all in the./.vscode/ directory

However, the.vscode directory is not in the folder after the initial editing of the newly created directory and files

Also, #include in our cpp file code < > This sentence is underlined to warn that the document cannot be found

At this time, use the mouse hover function, click "red bulb", click edit c_ES46en_properties.json option, vscode will automatically create.vscode/ folder in the configuration folder, and at the same time initialize c_cpp_properties.json file

Output the compile command file

At this point, there may still be 1 header file not found, such as ros/ ros.h, and we need to configure 1 more.

Compile the c++ code we wrote with the command line, and output the compilation information file, taking ROS as an example


catkin_make -DCMAKE_EXPORT_COMPILE_COMMANDS=Yes

This command outputs an ES69en_ES70en.json file under the build folder in the ROS workspace

Then add the following paragraph to the c_cpp_properties.json file


"compileCommands": "${workspaceFolder}/build/compile_commands.json"

The modified c_ES84en_ES85en.json file is as follows:


{
 "configurations": [
 {
 "name": "Linux",
 "includePath": [
 "${workspaceFolder}/**"
 ],
 "defines": [],
 "compilerPath": "/usr/bin/gcc",
 "cStandard": "c11",
 "cppStandard": "c++17",
 "intelliSenseMode": "clang-x64",
 "compileCommands": "${workspaceFolder}/build/compile_commands.json"
 }
 ],

 "version": 4
}

In this way, you can basically find all the header files, and then you can use code tips to code.

catkin_make set

vscode has no built-in make functionality and needs to be configured with Task functionality

Ctrl+shift+P enter command mode and type tasks: Configure Task

The task.json file is automatically generated under the.vscode folder, as shown below:


{
 "version": "2.0.0",
 "tasks": [
 {
 "label": "catkin_make", // Descriptive information that represents a prompt 
 "type": "shell", // You can choose shell or process, If it is shell The code is in shell Run inside 1 A command, if yes process On behalf of a 1 Three processes to run 
 "command": "catkin_make",// This is the command we need to run 
 "args": [],// Add after the command if necessary 1 Some suffixes, you could put them here, for example -DCATKIN_WHITELIST_PACKAGES= " pac1;pac2 " 
 "group": {"kind":"build","isDefault":true},
 "presentation": {
 "reveal": "always"// optional always or silence , represents whether to output information or not 
 },
 "problemMatcher": "$msCompile"
 },
 ]
}

Where this row is set


"group": {"kind":"build","isDefault":true},

This means to add the task we defined to the build group, so that you can find the compile command by using the Ctrl+Shift+B shortcut. The command name is defined in label. If "isDefault":true then it means to execute command

One more thing to mention, we open vscode1 to open it in our ROS workspace directory:

[

code .

]

In this way, your vscode Base path is the position where you open vscode. When we execute catkin_make, we need to use this Base path, which must be our ROS workspace to work normally

Once configured, the ROS workspace will be ready for easy use when compiling

[

Ctrl+Shift+B

]

GDB debug configuration

The GDB debugger is an artifact for debugging C++ code. The ROS project is essentially an ROS project, so you can also debug with GDB

Having inherited the GDB debugger in vscode, all we need to do is configure the ES177en.json file

Click the left toolbar "Debug" and click the "Gear" button. At this time, the vscode folder will automatically generate the ES183en.json file, as shown below


{
 "version": "0.2.0",
 "configurations": [
 {
 "name": "(gdb) Launch", //  The configuration name will be displayed in the debug configuration drop-down list 
 "type": "cppdbg", //  Debugger type   The value is generated automatically 
 "request": "launch", //  Debug mode , You can also choose attach
 "program": "${workspaceRoot}/devel/lib/waypoint_follower/pure_persuit", // Program to be debugged (full path, relative path supported) 
 "args": [], //  Parameters passed to the above program, no parameters can be left blank 
 "stopAtEntry": false, //  Whether to stop at the program entry point (stop at main Function start) 
 "cwd": "${workspaceRoot}", //  Working directory when debugging a program 
 "environment": [], // Environment variables to add to the environment for the program being debugged .  For example, : [ { "name": "squid", "value": "clam" } ]
 "externalConsole": false, // If I set it to true , starts the external console for the application.   If it is false , the console will not be started and used VS Code The built-in debugging console. 
 "MIMode": "gdb", // VSCode Debugging tools to use 
 "setupCommands": [
 {
  "description": "Enable pretty-printing for gdb",
  "text": "-enable-pretty-printing",
  "ignoreFailures": true
 }
 ]
 }
 ]
}

It should be noted that the "program" parameter here needs to be given by myself. For example, If I want to debug an ROS node, I need to find the executable target generated by this node, which is the executable base 2 file, and then add it to the end of the "program" parameter. If I want to debug other nodes, I need to manually modify this parameter

In addition, since we are not the node started by roslaunch, we need one more terminal to run roscore, otherwise ROS MASTER will not be found

In the "request" parameter, gdb provides launch and attach configuration tasks in vscode. The difference between the two is that launch actually starts 1 node to execute the specified code, and can be debuggable in vscode at the same time. attach is the task that performs the listening.

The means of debugging using vscode mainly include single-step execution, observing and tracking variable values, etc

For more GDB debug commands, you can type the GDB command directly in the DEBUG_CONSOLE window below vscode, but note that you need to prefix the original GDB command with a prefix of "-exec", as shown below:

[

-exec b main

]

Summary of the common GDB command: the use of GDB

It is also important to note that when adding variables that require Watch to the Watch window, write the global name of the variable name, including the previous command space

Based on the above description, we can be happy with debug

Add ROS plug-in installation

Open vscode's shortcut input window (Ctrl+P)

Enter the following command to install the ROS plug-in


ext install ajshort.ros

usage

You can create 1 ROS package by right-clicking on a folder and selecting creat catkin package

You can also press (Ctrl+Shift+P) to enter

[

ros::showMasterStatus

]

This command displays details of the current ROS communication system, including all current topics, all publishers and subscribers of the topic

Basically, the ROS plug-in for vscode is useful for just these two things


Related articles: