1. 程式人生 > 程式設計 >使用VScode搭建ROS開發環境的教程詳解

使用VScode搭建ROS開發環境的教程詳解

俗話說"工欲善其事必先利其器",之前在Ubuntu上執行的ROS專案都是用vim或者gedit編寫和修改程式碼,然後在終端編譯執行,很不方便,函式跳轉檢視都沒辦法實現。所以今天我決定找一個方便的開發工具,也就是找一個像Windows上的VS那樣的整合開發工具(IDE),ROS官網上有一個不同IDE的對比文章,網址在這裡

我選擇使用VScode.下載安裝好VScode後,在擴充套件欄安裝C/C++CMakeCMake ToolsCode RunnerROS(deprecated)Chinese 這些外掛.接下來用一個簡單的話題釋出栗子來演示操作過程

建立ROS工作環境

首先新建一個資料夾,我命名為test_ros

,在該資料夾中開啟終端,執行以下命令來建立ROS工作環境:

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

然後在VScode中開啟test_ros資料夾,此時的檔案目錄如下

在這裡插入圖片描述

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

在這裡插入圖片描述

新增roscpp,rospy作為依賴項

在這裡插入圖片描述

之後src目錄下會出現以下檔案:

在這裡插入圖片描述

繼續在src/helloworld/src目錄下新增一個cpp檔案,命名為helloworld.cpp,內容如下:

#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,我們繼續通過後面的步驟來解決.

配置.json檔案

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

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

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

在這裡插入圖片描述

之後就會生產c_cpp_properties.json檔案,修改檔案內容如下:

{
  "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相關標頭檔案所在的路徑,此時可能仍然找不到ros/ros.hstd_msgs/String.h,繼續執行以下命令即可在build資料夾下生成compile_commands.json檔案

catkin_make -DCMAKE_EXPORT_COMPILE_COMMANDS=1

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

launch.json,用於除錯

按住Fn+F5啟動除錯,就會生成launch.json,修改launch.json檔案內容如下:

{
  // 使用 IntelliSense 瞭解相關屬性。 
  // 懸停以檢視現有屬性的描述。
  // 欲瞭解更多資訊,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0","configurations": [
      {
      "name": "(gdb) Launch","type": "cppdbg","request": "launch","program": "${workspaceFolder}/build/helloworld/helloworld",// 表示可執行程式所在的路徑,其中,${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"//最好刪了,不然會影響除錯,每次除錯都直接執行make build
    }
  ]
}

tasks.json,用於編譯

按住Fn+F1,找到任務:配置任務,建立tasks.json檔案,修改tasks.json檔案內容如下:

{
  "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"//可選always或者silence,代表是否輸出資訊
      },"problemMatcher": "$msCompile"
    },]
}

修改CMakeLists.txt

繼續修改src/helloworld/CMakeLists.txt檔案,在其中新增以下程式:

# 標頭檔案路徑
include_directories(
include
 ${catkin_INCLUDE_DIRS}
)
# 生成可執行檔案
add_executable( helloworld src/helloworld.cpp )
# 連結庫
target_link_libraries(helloworld ${catkin_LIBRARIES})

結果測試

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

004

最後測試生成的可執行檔案.新開一個終端,執行ROS的master節點,然後按住Fn+F5執行生成的可執行檔案,結果如下;

在這裡插入圖片描述

在另一個終端中輸出該程式釋出的話題:

在這裡插入圖片描述

這樣,VScode的ROS開發環境就搭建好了

參考

ros專案除錯:vscode下配置開發ROS專案

到此這篇關於使用VScode搭建ROS開發環境的教程詳解的文章就介紹到這了,更多相關VScode搭建ROS開發環境內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!