ROS的安装较为麻烦,需要的@我,我单独出一份教程
在vscode的左边中的拓展(快捷键Ctrl+Shift+X)中搜索并添加以下拓展
· C/C++
· CMake
· ROS
2.1 快捷键打开终端 ctrl+alt+t
2.2 新建文件夹 mkdir -p test_ros/src
2.3 进入新建的文件夹 cd test_ros
2.4 编译catkin_make
如果有多个版本 python, 输入:
catkin_make -DPYTHON_EXECUTABLE=/usr/bin/python3
2.5 输入code
打开vscode,点击文件
-打开文件夹
,选择刚刚新建的test_ros文件夹
3.1 右键点击src文件夹,选择Create Catkin Package
3.2 Package name
是自定义的,在运行时用到,这里命名为ros_test
3.3 Dependecies
中输入std_msgs rospy roscpp
以上两步等同于在命令行中输入
catkin_create_pkg ros_test std_msgs rospy roscpp
快捷键Ctrl+Shift+p
, 找到C/C++: 编辑配置 或者 C/C++: Edit configurations(JSON)
修改如下
// 指定C/C++类库和包含路径以及配置
{
"configurations": [
{
"browse": {
"databaseFilename": "${default}",
"limitSymbolsToIncludedHeaders": false
},
"includePath": [
"/opt/ros/noetic/include/**", //注意要跟所安装的ROS路径一致
"/usr/include/**"
],
"name": "ROS",
"intelliSenseMode": "gcc-x64",
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu11",
"cppStandard": "c++14",
"compileCommands": "${workspaceFolder}/build/compile_commands.json"
}
],
"version": 4
}
快捷键Ctrl+Shift+p
, 找到任务:配置任务 或者 Tasks: Configure task,选哪项都可以,因为要修改
// 指定catkin_make编译参数
{
"tasks": [
{
"label": "catkin_make: build",
"type": "shell",
"command": "catkin_make",
"args": [
"--directory",
"/home/jawenos/project/test_ros",// 注意文件路径
"-DCMAKE_BUILD_TYPE=Debug" // 注意这里是debug
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher":"$catkin-gcc"
}
],
"version": "2.0.0"
}
在vscode的左边栏中运行与调试
(快捷键Ctrl+Shift+D)创建launch.json,
修改如下:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "ros_test",
"type": "cppdbg",
"request": "launch",
//表示可执行文件所在路径。workspaceFolder表示vscode所加载文件根目录
"program": "${workspaceFolder}/devel/lib/ros_test/ros_test",
"args": [],
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
}
]
}
5.1 首先在ros_test
文件夹中的src文件夹中新建一个C++文件 ros_test.cpp
,在其中写入
#include
#include"ros/ros.h"
using namespace std;
int main(int argc, char** argv){
ROS_INFO("hello");
return 0;
}
5.2 修改ros_test中的CMakeList文件
cmake_minimum_required(VERSION 3.0.2)
project(ros_test)
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
)
catkin_package(
CATKIN_DEPENDS roscpp rospy std_msgs
)
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
add_executable(ros_test src/ros_test.cpp)
target_link_libraries(ros_test
${catkin_LIBRARIES}
)
5.3 点击vscode上方终端
,新建终端,catkin_make
重新编译一次文件,再输入roscore
启动ROS,最后到ros_test.cpp中F5
开始调试即可