使用Vscode搭建ROS开发环境
工作环境
Ubuntu 18 04
VSCODE
扩展栏安装C/C++,CMake,CMake Tools,Code Runner,Ros等插件;
创建ROS工作环境
新建一个文件夹,创建工作空间,执行以下命令来创建ROS工作环境:
mkdir -p test_ws/src
cd test_ws
catkin_make
创建功能包
cd src/
catkin_create_pkg hellociao roscpp rospy
在hellociao功能包目录下启动vscode
PS.务必在功能包目录下启动vscode
cd hellociao
code .
在该目录下创建src目录,并添加一个cpp文件,命名为hellociao.cpp,内容如下:
mkdir src
cd src/
touch hellociao.cpp
#include <iostream>
#include <string>
#include <sstream>
#include "ros/ros.h"
#include "std_msgs/String.h"
using namespace std;
int main(int argc, char** argv)
{
ros::init(argc, argv, "talker");
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter1", 1000);
ros::Rate loop_rate(10);
int count = 0;
while(ros::ok())
{
std_msgs::String msg;
std::stringstream ss;
ss << "hello ciao " << count;
msg.data = ss.str();
ROS_INFO("%s", msg.data.c_str());
chatter_pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
count++;
}
return 0;
}
配置.json文件
c_cpp_properties.json
c_cpp_properties.json:引用库文件配置(ctrl + shift + P ->C/C++ Edit configuration)
{
"configurations": [
{
"browse": {
"databaseFilename": "",
"limitSymbolsToIncludedHeaders": true
},
"includePath": [
"/opt/ros/melodic/include/**",
"${workspaceFolder}/**",
"/usr/include/**"
],
"name": "ROS",
"intelliSenseMode": "gcc-x64",
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu11",
"cppStandard": "gnu++14"
}
],
"version": 4
}
tasks.json
tasks.json(添加构建(编译、链接等)任务)[[ctrl][shift]+p -> Task,configure default build task] -> catkin_make:build
{
"version": "2.0.0",
"tasks": [
{
"label":"123",
"type": "shell",
"command": "cd ${workspaceFolder}/../../; catkin_make -j3",
"problemMatcher": [
"$catkin-gcc"
],
"group": "build",
}
]
}
launch.json
launch.json( 任务配置 F5),创建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": "hellociao",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/../../devel/lib/hellociao/hellociao",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask":"123"
}
]
}
修改CMakeLists.txt
cmake_minimum_required(VERSION 3.0.2)
project(hellociao)
# 设置编译类型
set(CMAKE_BUILD_TYPE "Debug")
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES helloworld
# CATKIN_DEPENDS roscpp rospy
# DEPENDS system_lib
)
## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
include
${catkin_INCLUDE_DIRS}
)
# 生成可执行文件
add_executable(hellociao src/hellociao.cpp)
# 链接库
target_link_libraries(hellociao ${catkin_LIBRARIES})
PS: 第5行,30行,33行必加。
结果测试
终端回到test_ws目录下,编译
catkin_make
新开一个终端,启动master节点
roscore
回到vscode界面,打断点,按F5测试
结果如下: