俗話說(shuō)"工欲善其事必先利其器",之前在Ubuntu上運(yùn)行的ROS項(xiàng)目都是用vim或者gedit編寫(xiě)和修改代碼,然后在終端編譯運(yùn)行,很不方便,函數(shù)跳轉(zhuǎn)查看都沒(méi)辦法實(shí)現(xiàn)。所以今天我決定找一個(gè)方便的開(kāi)發(fā)工具,也就是找一個(gè)像Windows上的VS那樣的集成開(kāi)發(fā)工具(IDE),ROS官網(wǎng)上有一個(gè)不同IDE的對(duì)比文章,網(wǎng)址在這里
我選擇使用VScode.下載安裝好VScode后,在擴(kuò)展欄安裝C/C++,CMake,CMake Tools,Code Runner,ROS(deprecated),Chinese 這些插件.接下來(lái)用一個(gè)簡(jiǎn)單的話題發(fā)布栗子來(lái)演示操作過(guò)程
創(chuàng)建ROS工作環(huán)境
首先新建一個(gè)文件夾,我命名為test_ros
,在該文件夾中打開(kāi)終端,執(zhí)行以下命令來(lái)創(chuàng)建ROS工作環(huán)境:
- mkdir src && cd src
- catkin_init_workspace
- cd ../
- catkin_make
然后在VScode中打開(kāi)test_ros
文件夾,此時(shí)的文件目錄如下
右鍵單擊src
,選擇Create Catkin Package
,Package命名為helloworld
添加roscpp, rospy作為依賴(lài)項(xiàng)
之后src
目錄下會(huì)出現(xiàn)以下文件:
繼續(xù)在src/helloworld/src
目錄下添加一個(gè)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;
- }
此時(shí)會(huì)提示找不到ros/ros.h
和std_msgs/String.h
,我們繼續(xù)通過(guò)后面的步驟來(lái)解決.
配置.json文件
接下來(lái)配置c_cpp_properties.json
,launch.json
,tasks.json
分別如下:
c_cpp_properties.json,用于指定C/C++類(lèi)庫(kù)和包含路徑以及配置
按住Fn+F1
,找到C/C++:編輯配置(JSON)
之后就會(huì)生產(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)頭文件所在的路徑,此時(shí)可能仍然找不到ros/ros.h
和std_msgs/String.h
,繼續(xù)運(yùn)行以下命令即可在build
文件夾下生成compile_commands.json
文件
- catkin_make -DCMAKE_EXPORT_COMPILE_COMMANDS=1
然后就可以找到ros/ros.h
和std_msgs/String.h
了
launch.json,用于調(diào)試
按住Fn+F5
啟動(dòng)調(diào)試,就會(huì)生成launch.json
,修改launch.json
文件內(nèi)容如下:
- {
- // 使用 IntelliSense 了解相關(guān)屬性。
- // 懸停以查看現(xiàn)有屬性的描述。
- // 欲了解更多信息,請(qǐng)?jiān)L問(wè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"//最好刪了,不然會(huì)影響調(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里面運(yùn)行一個(gè)命令,如果是process代表作為一個(gè)進(jìn)程來(lái)運(yùn)行
- "command": "catkin_make",//這個(gè)是我們需要運(yùn)行的命令
- "args": [],//如果需要在命令后面加一些后綴,可以寫(xiě)在這里,比如-DCATKIN_WHITELIST_PACKAGES=“pac1;pac2”
- "group": {"kind":"build","isDefault":true},
- "presentation": {
- "reveal": "always"//可選always或者silence,代表是否輸出信息
- },
- "problemMatcher": "$msCompile"
- },
- ]
- }
修改CMakeLists.txt
繼續(xù)修改src/helloworld/CMakeLists.txt
文件,在其中添加以下程序:
- # 頭文件路徑
- include_directories(
- include
- ${catkin_INCLUDE_DIRS}
- )
- # 生成可執(zhí)行文件
- add_executable( helloworld src/helloworld.cpp )
- # 鏈接庫(kù)
- target_link_libraries(helloworld ${catkin_LIBRARIES})
結(jié)果測(cè)試
按住Ctrl+Shift+B
編譯該程序,就可以看到與catkin_make
一樣的編譯過(guò)程
最后測(cè)試生成的可執(zhí)行文件.新開(kāi)一個(gè)終端,運(yùn)行ROS的master節(jié)點(diǎn),然后按住Fn+F5
運(yùn)行生成的可執(zhí)行文件,結(jié)果如下;
在另一個(gè)終端中輸出該程序發(fā)布的話題:
這樣,VScode的ROS開(kāi)發(fā)環(huán)境就搭建好了
參考
ros項(xiàng)目調(diào)試:vscode下配置開(kāi)發(fā)ROS項(xiàng)目
到此這篇關(guān)于使用VScode搭建ROS開(kāi)發(fā)環(huán)境的教程詳解的文章就介紹到這了,更多相關(guān)VScode搭建ROS開(kāi)發(fā)環(huán)境內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/qq_42688495/article/details/107750466