俗話說"工欲善其事必先利其器",之前在Ubuntu上運行的ROS項目都是用vim或者gedit編寫和修改代碼,然后在終端編譯運行,很不方便,函數跳轉查看都沒辦法實現。所以今天我決定找一個方便的開發工具,也就是找一個像Windows上的VS那樣的集成開發工具(IDE),ROS官網上有一個不同IDE的對比文章,網址在這里
我選擇使用VScode.下載安裝好VScode后,在擴展欄安裝C/C++,CMake,CMake Tools,Code Runner,ROS(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.h
和std_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.h
和std_msgs/String.h
,繼續運行以下命令即可在build
文件夾下生成compile_commands.json
文件
- catkin_make -DCMAKE_EXPORT_COMPILE_COMMANDS=1
然后就可以找到ros/ros.h
和std_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
一樣的編譯過程
最后測試生成的可執行文件.新開一個終端,運行ROS的master節點,然后按住Fn+F5
運行生成的可執行文件,結果如下;
在另一個終端中輸出該程序發布的話題:
這樣,VScode的ROS開發環境就搭建好了
參考
到此這篇關于使用VScode搭建ROS開發環境的教程詳解的文章就介紹到這了,更多相關VScode搭建ROS開發環境內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/qq_42688495/article/details/107750466