亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用VScode搭建ROS開發(fā)環(huán)境的教程詳解

 更新時間:2020年08月08日 11:24:33   作者:白鳥無言  
這篇文章主要介紹了使用VScode搭建ROS開發(fā)環(huán)境,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

俗話說"工欲善其事必先利其器",之前在Ubuntu上運行的ROS項目都是用vim或者gedit編寫和修改代碼,然后在終端編譯運行,很不方便,函數(shù)跳轉(zhuǎn)查看都沒辦法實現(xiàn)。所以今天我決定找一個方便的開發(fā)工具,也就是找一個像Windows上的VS那樣的集成開發(fā)工具(IDE),ROS官網(wǎng)上有一個不同IDE的對比文章,網(wǎng)址在這里

我選擇使用VScode.下載安裝好VScode后,在擴展欄安裝C/C++,CMake,CMake Tools,Code Runner,ROS(deprecated),Chinese 這些插件.接下來用一個簡單的話題發(fā)布栗子來演示操作過程

創(chuàng)建ROS工作環(huán)境

首先新建一個文件夾,我命名為test_ros,在該文件夾中打開終端,執(zhí)行以下命令來創(chuàng)建ROS工作環(huán)境:

mkdir src && cd src
catkin_init_workspace
cd ../
catkin_make

然后在VScode中打開test_ros文件夾,此時的文件目錄如下

在這里插入圖片描述

右鍵單擊src,選擇Create Catkin Package,Package命名為helloworld

在這里插入圖片描述

添加roscpp, rospy作為依賴項

在這里插入圖片描述

之后src目錄下會出現(xiàn)以下文件:

在這里插入圖片描述

繼續(xù)在src/helloworld/src目錄下添加一個cpp文件,命名為helloworld.cpp,內(nèi)容如下:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

#include "ros/ros.h"
#include "std_msgs/String.h"

int main(int argc, char** argv)
{
	ros::init(argc, argv, "talker");
	ros::NodeHandle n;
	ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
	ros::Rate loop_rate(10);
	int count = 0;
	while(ros::ok())
	{
		std_msgs::String msg;
		std::stringstream ss;
		ss << "hello world " << count;
		msg.data = ss.str();
		ROS_INFO("%s", msg.data.c_str());
		chatter_pub.publish(msg);
		ros::spinOnce();
		loop_rate.sleep();
		count++;
	}
	return 0;
}

此時會提示找不到ros/ros.hstd_msgs/String.h,我們繼續(xù)通過后面的步驟來解決.

配置.json文件

接下來配置c_cpp_properties.json,launch.json,tasks.json分別如下:

c_cpp_properties.json,用于指定C/C++類庫和包含路徑以及配置

按住Fn+F1,找到C/C++:編輯配置(JSON)

在這里插入圖片描述

之后就會生產(chǎn)c_cpp_properties.json文件,修改文件內(nèi)容如下:

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

其中/opt/ros/melodic/include為ROS相關(guān)頭文件所在的路徑,此時可能仍然找不到ros/ros.hstd_msgs/String.h,繼續(xù)運行以下命令即可在build文件夾下生成compile_commands.json文件

catkin_make -DCMAKE_EXPORT_COMPILE_COMMANDS=1

然后就可以找到ros/ros.hstd_msgs/String.h

launch.json,用于調(diào)試

按住Fn+F5啟動調(diào)試,就會生成launch.json,修改launch.json文件內(nèi)容如下:

{
 // 使用 IntelliSense 了解相關(guān)屬性。 
 // 懸停以查看現(xiàn)有屬性的描述。
 // 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
 "version": "0.2.0",
 "configurations": [
 {
 "name": "(gdb) Launch",
 "type": "cppdbg",
 "request": "launch",
 "program": "${workspaceFolder}/build/helloworld/helloworld",// 表示可執(zhí)行程序所在的路徑,其中,${workspaceRoot}表示VScode加載的文件夾的根目錄
 "args": [],
 "stopAtEntry": false,
 "cwd": "${workspaceFolder}",
 "environment": [],
 "externalConsole": false,
 "MIMode": "gdb",
 "setupCommands": [
 {
  "description": "Enable pretty-printing for gdb",
  "text": "-enable-pretty-printing",
  "ignoreFailures": true
 }
 ],
 //"preLaunchTask": "make build"http://最好刪了,不然會影響調(diào)試,每次調(diào)試都直接執(zhí)行make build
 }
 ]
}

tasks.json,用于編譯

按住Fn+F1,找到任務(wù):配置任務(wù),創(chuàng)建tasks.json文件,修改tasks.json文件內(nèi)容如下:

{
 "version": "2.0.0",
 "tasks": [
 {
 "label": "catkin_make", //代表提示的描述性信息
 "type": "shell", //可以選擇shell或者process,如果是shell代碼是在shell里面運行一個命令,如果是process代表作為一個進程來運行
 "command": "catkin_make",//這個是我們需要運行的命令
 "args": [],//如果需要在命令后面加一些后綴,可以寫在這里,比如-DCATKIN_WHITELIST_PACKAGES=“pac1;pac2”
 "group": {"kind":"build","isDefault":true},
 "presentation": {
 "reveal": "always"http://可選always或者silence,代表是否輸出信息
 },
 "problemMatcher": "$msCompile"
 },
 ]
}

修改CMakeLists.txt

繼續(xù)修改src/helloworld/CMakeLists.txt文件,在其中添加以下程序:

# 頭文件路徑
include_directories(
include
 ${catkin_INCLUDE_DIRS}
)
# 生成可執(zhí)行文件
add_executable( helloworld src/helloworld.cpp )
# 鏈接庫
target_link_libraries(helloworld ${catkin_LIBRARIES})

結(jié)果測試

按住Ctrl+Shift+B編譯該程序,就可以看到與catkin_make一樣的編譯過程

004

最后測試生成的可執(zhí)行文件.新開一個終端,運行ROS的master節(jié)點,然后按住Fn+F5運行生成的可執(zhí)行文件,結(jié)果如下;

在這里插入圖片描述

在另一個終端中輸出該程序發(fā)布的話題:

在這里插入圖片描述

這樣,VScode的ROS開發(fā)環(huán)境就搭建好了

參考

ros項目調(diào)試:vscode下配置開發(fā)ROS項目

到此這篇關(guān)于使用VScode搭建ROS開發(fā)環(huán)境的教程詳解的文章就介紹到這了,更多相關(guān)VScode搭建ROS開發(fā)環(huán)境內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論