1. 程式人生 > 其它 >VScode配置ROS開發環境

VScode配置ROS開發環境

技術標籤:移動機器人ROSc++ubuntu

VScode配置ROS開發環境

簡介

ROS(機器人作業系統)作為研究slam演算法的平臺,提供了開放式介面,然而,在寫自己的ros程式的時候,沒有一款好的IDE看程式碼會十分麻煩。VScode繼承了很多開發工具,因此我選擇用VScode新增一些外掛,再通過一個例項來演示程式碼除錯過程。

1.安裝外掛

到VScode左側工具欄找到擴充套件,安裝以下幾個外掛:
C/C++;
C/C++ Intellisense;
CMake Tools;
Code Runner;
ROS;
ROS(deprecated);
XML Tools。

2.建立ROS工作空間

在你想要完成測試目錄下新建一個工作空間,這裡我的是Code_test,終端中開啟該資料夾,通過以下命令快速生成:

 mkdir -r Code_test/src
 cd Code_test
 catkin_make

然後再次回到工作空間下通過VScode開啟:

code .

在Code_test目錄下可以看到4個資料夾:

—.vscode
—build
—devel
—src

右鍵單擊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.h,通過後面的步驟解決

3.配置.json檔案

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

  1. c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                //"/mnt/home/ubuntu/Code_test",
                "/usr/local/",
                "/usr/include/",
                "/opt/ros/melodic/include/"
                //"/opt/ros/melodic/include/ros/"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "compileCommands": "${workspaceFolder}/build/compile_commands.json",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-arm64",
            "browse": {
                "path": [
                    "/usr/local/*",
                    "/usr/include/*",
                    "/opt/ros/melodic/include/*",
                    "/opt/ros/melodic/include/ros/*"
                ]
            }
        }
    ],
    "version": 4
}

其中/opt/ros/melodic/include為ROS相關標頭檔案所在的路徑,此時可能仍然找不到ros/ros.h,繼續執行以下命令即可在build資料夾下生成compile_commands.json檔案:

catkin_make -DCMAKE_EXPORT_COMPILE_COMMANDS=1

然後就可以找到ros/ros.h了;

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

  1. launch.json
// 使用 IntelliSense 瞭解相關屬性。 
// 懸停以檢視現有屬性的描述。
// 欲瞭解更多資訊,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "(gdb) Launch", // 配置名稱,將會在除錯配置下拉列表中顯示
                "type": "cppdbg",  // 偵錯程式型別 該值自動生成
                "request": "launch",  // 除錯方式,還可以選擇attach
                //"program": "/mnt/home/ubuntu/Code_test/src/helloworld/src/helloworld.cpp", //要除錯的程式(完整路徑,支援相對路徑)
                "program": "${workspaceFolder}/devel/lib/helloworld/helloworld",// 表示可執行程式所在的路徑,其中,${workspaceRoot}表示VScode載入的資料夾的根目錄
                "args": [],  // 傳遞給上面程式的引數,沒有引數留空即可
                "stopAtEntry": false,  // 是否停在程式入口點(停在main函式開始)
                "cwd": "${workspaceRoot}",  // 除錯程式時的工作目錄
                "environment": [], //針對除錯的程式,要新增到環境中的環境變數. 例如: [ { "name": "squid", "value": "clam" } ]
                "externalConsole": false,   //如果設定為true,則為應用程式啟動外部控制檯。 如果為false,則不會啟動控制檯,並使用VS Code的內建除錯控制檯。
                "MIMode": "gdb",  // VSCode要使用的除錯工具
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ]
                //"preLaunchTask": "make build"//最好刪了,不然會影響除錯,每次除錯都直接執行make build
            }
        ]
    }
  1. 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"
        },
    ]
}

4.修改CmakeLists檔案

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

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

5.測試

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

> Executing task: catkin_make <

Base path: /mnt/home/ubuntu/Code_test
Source space: /mnt/home/ubuntu/Code_test/src
Build space: /mnt/home/ubuntu/Code_test/build
Devel space: /mnt/home/ubuntu/Code_test/devel
Install space: /mnt/home/ubuntu/Code_test/install
####
#### Running command: "cmake /mnt/home/ubuntu/Code_test/src -DCATKIN_DEVEL_PREFIX=/mnt/home/ubuntu/Code_test/devel -DCMAKE_INSTALL_PREFIX=/mnt/home/ubuntu/Code_test/install -G Unix Makefiles" in "/mnt/home/ubuntu/Code_test/build"
####
-- Using CATKIN_DEVEL_PREFIX: /mnt/home/ubuntu/Code_test/devel
-- Using CMAKE_PREFIX_PATH: /mnt/home/ubuntu/catkin_ws/devel_isolated/cartographer_turtlebot;/mnt/home/ubuntu/catkin_ws/install_isolated;/mnt/home/ubuntu/turtlebot_ws/devel;/opt/ros/melodic
-- This workspace overlays: /mnt/home/ubuntu/catkin_ws/devel_isolated/cartographer_turtlebot;/mnt/home/ubuntu/catkin_ws/install_isolated;/mnt/home/ubuntu/turtlebot_ws/devel;/opt/ros/melodic
-- Found PythonInterp: /usr/bin/python2 (found suitable version "2.7.17", minimum required is "2") 
-- Using PYTHON_EXECUTABLE: /usr/bin/python2
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /mnt/home/ubuntu/Code_test/build/test_results
-- Found gtest sources under '/usr/src/googletest': gtests will be built
-- Found gmock sources under '/usr/src/googletest': gmock will be built
-- Found PythonInterp: /usr/bin/python2 (found version "2.7.17") 
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.7.29
-- BUILD_SHARED_LIBS is on
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~  traversing 1 packages in topological order:
-- ~~  - helloworld
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'helloworld'
-- ==> add_subdirectory(helloworld)
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/home/ubuntu/Code_test/build
####
#### Running command: "make -j6 -l6" in "/mnt/home/ubuntu/Code_test/build"
####
Scanning dependencies of target helloworld
[ 50%] Building CXX object helloworld/CMakeFiles/helloworld.dir/src/helloworld.cpp.o
[100%] Linking CXX executable /mnt/home/ubuntu/Code_test/devel/lib/helloworld/helloworld
[100%] Built target helloworld

終端將被任務重用,按任意鍵關閉。

> Executing task: catkin_make <

Base path: /mnt/home/ubuntu/Code_test
Source space: /mnt/home/ubuntu/Code_test/src
Build space: /mnt/home/ubuntu/Code_test/build
Devel space: /mnt/home/ubuntu/Code_test/devel
Install space: /mnt/home/ubuntu/Code_test/install
####
#### Running command: "make cmake_check_build_system" in "/mnt/home/ubuntu/Code_test/build"
####
####
#### Running command: "make -j6 -l6" in "/mnt/home/ubuntu/Code_test/build"
####
[100%] Built target helloworld

終端將被任務重用,按任意鍵關閉。

> Executing task: catkin_make <

Base path: /mnt/home/ubuntu/Code_test
Source space: /mnt/home/ubuntu/Code_test/src
Build space: /mnt/home/ubuntu/Code_test/build
Devel space: /mnt/home/ubuntu/Code_test/devel
Install space: /mnt/home/ubuntu/Code_test/install
####
#### Running command: "make cmake_check_build_system" in "/mnt/home/ubuntu/Code_test/build"
####
####
#### Running command: "make -j6 -l6" in "/mnt/home/ubuntu/Code_test/build"
####
Scanning dependencies of target helloworld
[ 50%] Building CXX object helloworld/CMakeFiles/helloworld.dir/src/helloworld.cpp.o
[100%] Linking CXX executable /mnt/home/ubuntu/Code_test/devel/lib/helloworld/helloworld
[100%] Built target helloworld

終端將被任務重用,按任意鍵關閉。

> Executing task: catkin_make <

Base path: /mnt/home/ubuntu/Code_test
Source space: /mnt/home/ubuntu/Code_test/src
Build space: /mnt/home/ubuntu/Code_test/build
Devel space: /mnt/home/ubuntu/Code_test/devel
Install space: /mnt/home/ubuntu/Code_test/install
####
#### Running command: "make cmake_check_build_system" in "/mnt/home/ubuntu/Code_test/build"
####
####
#### Running command: "make -j6 -l6" in "/mnt/home/ubuntu/Code_test/build"
####
[100%] Built target helloworld

終端將被任務重用,按任意鍵關閉。

> Executing task: catkin_make <

Base path: /mnt/home/ubuntu/Code_test
Source space: /mnt/home/ubuntu/Code_test/src
Build space: /mnt/home/ubuntu/Code_test/build
Devel space: /mnt/home/ubuntu/Code_test/devel
Install space: /mnt/home/ubuntu/Code_test/install
####
#### Running command: "make cmake_check_build_system" in "/mnt/home/ubuntu/Code_test/build"
####
####
#### Running command: "make -j6 -l6" in "/mnt/home/ubuntu/Code_test/build"
####
[100%] Built target helloworld

終端將被任務重用,按任意鍵關閉。

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


[ INFO] [1608535222.099141881]: hello world 0
[ INFO] [1608535222.199263812]: hello world 1
[ INFO] [1608535222.299218928]: hello world 2
[ INFO] [1608535222.399286523]: hello world 3
[ INFO] [1608535222.499294439]: hello world 4
[ INFO] [1608535222.599360882]: hello world 5
[ INFO] [1608535222.699323230]: hello world 6
[ INFO] [1608535222.799303370]: hello world 7
[ INFO] [1608535222.899336022]: hello world 8
[ INFO] [1608535222.999295714]: hello world 9
[ INFO] [1608535223.099223374]: hello world 10
[ INFO] [1608535223.199598039]: hello world 11
[ INFO] [1608535223.299278789]: hello world 12
[ INFO] [1608535223.399296081]: hello world 13
[ INFO] [1608535223.499288924]: hello world 14
[ INFO] [1608535223.599297256]: hello world 15
[ INFO] [1608535223.699288820]: hello world 16
[ INFO] [1608535223.799404127]: hello world 17
[ INFO] [1608535223.899241932]: hello world 18
[ INFO] [1608535223.999230008]: hello world 19
[ INFO] [1608535224.099249219]: hello world 20
[ INFO] [1608535224.199362318]: hello world 21
[ INFO] [1608535224.299336954]: hello world 22
[ INFO] [1608535224.399341222]: hello world 23

在另一個終端中輸入

rostopic echo /chatter

即可輸出該程式釋出的話題:

data: "hello world 653"
---
data: "hello world 654"
---
data: "hello world 655"
---
data: "hello world 656"
---
data: "hello world 657"
---
data: "hello world 658"
---
data: "hello world 659"
---
data: "hello world 660"
---
data: "hello world 661"
---
data: "hello world 662"
---

至此,ROS開發環境已經搭好了,要測試不同的程式仍需要重新更改配置檔案,方法同上,主要改launch.json和CmakeLists這兩個檔案。

參考

使用VScode搭建ROS開發環境