Visual Studio Code 運行ROS
- 學習目標:
- 操作步驟:
- Visual Studio Code下載
- Visual Studio Code安裝與卸載
- 安裝
- 方式1:雙擊安裝即可(或右擊選擇安裝)
- 方式2:sudo dpkg -i xxxx.deb
- 卸載
- 集成 ROS 插件
- 使用步驟
- 創建 ROS 作業空間
- 啟動 vscode打開作業空間
- 使用vscode 編譯 ros
- 創建 ROS 功能包
- C++ 實作
- python 實作
- 配置 CMakeLists.txt
- C++ 配置格式:
- Python 配置格式:
- 執行ROS功能包
- 使用C++在控制臺輸出 Hello ROS !!!
- 使用Python在控制臺輸出 你好 Python
- 添加launch檔案啟動ROS節點
- 踩坑
- 總結
學習目標:
Visual Studio Code 運行ROS
環境:
- ubuntu:18.04.5 LTS
- VMware:15.5.0
- Visual Studio Code:1.59.0
- ROS:melodic
- Python:3.6.9
- gcc:7.5.0
Visual Studio Code下載:https://code.visualstudio.com/docs?start=true
Visual Studio Code 歷史版本下載鏈接: https://code.visualstudio.com/updates
操作步驟:
ROS中的程式即使使用不同的編程語言,實作流程大致如下:
- 創建作業空間
- 編譯作業空間
- 創建ROS功能包
- 進入ROS包的src目錄編輯源檔案
- 編輯功能包下的Cmakelist.txt檔案
- 進入作業空間目錄并編譯(ROS版本為melodic,Python無需編譯)
- 配置環境變數
- 啟動ROS Master 和 執行撰寫的源檔案
Visual Studio Code下載
進入Visual Studio Code官方找到 Download 的點擊進行下載,

由于使用的作業系統格式ubuntu,選擇 .deb 64bit 進行下載,

以下是下載完以后在電腦本地的安裝包,

Visual Studio Code安裝與卸載
安裝
方式1:雙擊安裝即可(或右擊選擇安裝)


方式2:sudo dpkg -i xxxx.deb
sudo dpkg -i code_1.59.0-1628120042_amd64.deb


把Visual Studio Code添加到收藏夾
Show Applications → 搜索 Visual Studio Code → 右擊選擇 Add to Favorites


以下為啟動 Visual Studio Code,看到的界面,

卸載
使用以下指令卸載 Visual Studio Code
sudo dpkg --purge code
集成 ROS 插件
使用 VScode 開發 ROS 程式,需要先安裝一些插件,常用插件如下:
- Python
- C/C++
- CMake Tools
- ROS
打開插件插件庫,安裝第三方插件,

通過搜索框搜索 CMake Tools和ROS插件,


安裝完成后,加載的插件有以下幾個,就算安裝成功了,

使用步驟
4.vscode 使用_基本配置
創建 ROS 作業空間
格式:
mkdir -p xxx_ws/src(必須得有 src)
例如:
mkdir -p day02_ws/src
創建成功后,會在Home目錄下生成 day02_ws 作業空間

進入剛創建的作業空間
cd ~/day02_ws
編譯作業空間
catkin_make

啟動 vscode打開作業空間
cd ~/day02_ws
創建完作業空間后,要使用vscode打開作業空間,使用以下命令,
code .
在 vscode 中可以看到,作業空間下有以下檔案
- .vscode
- build:編譯空間
- devel:開發空間
- src:代碼空間
- .catkin_workspace

使用vscode 編譯 ros
在vscode中設定快捷鍵編譯的方法如下:
先使用快捷鍵 ctrl + shift + B 呼叫編譯選項配置
選擇: catkin_make:build

可以點擊配置設定為默認,修改 .vscode/tasks.json 檔案
以下為vscode添加的默認配置,

如果需要設定快捷鍵編譯,把 .vscode/tasks.json 檔案的內容全部替換為以下內容:
{
// 有關 tasks.json 格式的檔案,請參見
// https://go.microsoft.com/fwlink/?LinkId=733558
"version": "2.0.0",
"tasks": [
{
"label": "catkin_make:debug", //代表提示的描述性資訊
"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"
}
]
}
創建 ROS 功能包
選定 src 右擊 —> create catkin package

設定包名
hello_world_c

設定完包名后,按下回車,需要繼續設定依賴
添加依賴
roscpp rospy std_msgs

依賴添加成功后,會自動生成 hello_world_c 功能包所需要的相關檔案,
注意:使用快捷鍵 ctrl + shift + B 呼叫編譯,可以驗證加入的依賴是否正確,

C++ 實作
在功能包的 src 下新建 cpp 檔案

以下為hello_ros_c.cpp檔案內容
/*
控制臺輸出 Hello ROS !!!
*/
#include "ros/ros.h"
int main(int argc, char *argv[])
{
setlocale(LC_ALL,""); // 防止輸出日志亂碼
//執行節點初始化
ros::init(argc,argv,"HelloVSCode");
//輸出日志
ROS_INFO("Hello ROS !!!");
return 0;
}
注:: main 函式的引數不可以被 const 修飾
python 實作
在 功能包 下新建 scripts 檔案夾,添加 python 檔案,并添加可執行權限


以下為hello_ros_p.py檔案內容
#-*- coding: UTF-8 -*-
#! /usr/bin/env python
##指定解釋器
# 1.導包
import rospy
#2.撰寫主入口
if __name__ == "__main__":
# 3.初始化 ROS 節點
rospy.init_node("hello_world")
# 4.輸出日志
rospy.loginfo("你好 Python ....")
選擇Open in Integrated Terminal (在集成終端中打開),

給所有的Python檔案加上可執行權限
chmod +x *.py
通過以下命令查看Python檔案的權限
ls -l

配置 CMakeLists.txt
C++ 配置格式:
add_executable(節點名稱
src/C++源檔案名.cpp
)
target_link_libraries(節點名稱
${catkin_LIBRARIES}
)
配置如下:
add_executable(hello_ros_c
src/hello_ros_c.cpp
)
target_link_libraries(hello_ros_c
${catkin_LIBRARIES}
)

Python 配置格式:
Python 配置:
catkin_install_python(PROGRAMS scripts/自定義檔案名.py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)
配置如下:
catkin_install_python(PROGRAMS
scripts/hello_ros_p.py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

配置完成后,使用快捷鍵 ctrl + shift + B 編譯

執行ROS功能包
打開vscode自帶的終端啟動撰寫好的ros專案,可以在vscode中打開多個終端,執行以下命令,
roscore

配置環境變數
source ./devel/setup.bash
使用C++在控制臺輸出 Hello ROS !!!
rosrun hello_world_c hello_ros_c

使用Python在控制臺輸出 你好 Python
rosrun hello_world_c hello_ros_p.py

添加launch檔案啟動ROS節點
創建launch檔案

在launch檔案配置以下資訊
<launch>
<node pkg="hello_world_c" type="hello_ros_c" name="hello" output="screen" />
<node pkg="hello_world_c" type="hello_ros_c" name="hello" output="screen" />
</launch>
設定環境變數
source ~/作業空間名/devel/setup.bash
使用以下命令啟動launch檔案
roslaunch hello_world_c start_turtle.launch
roslaunch 功能包名 自定義launch檔案名.launch
launch檔案的使用:https://blog.csdn.net/qq_44989881/article/details/118607995
踩坑
注意1.:當使用C++撰寫的ROS__INFO 終端輸出有中文時,會出現亂碼
INFO: ???

解決辦法:在函式開頭加入下面代碼的任意一句
setlocale(LC_CTYPE, "zh_CN.utf8");
setlocale(LC_ALL, "");

當使用Python撰寫的loginfo終端輸出有中文時,會出現以下錯誤
SyntaxError: Non-ASCII character ‘\xe7’ in file /home/vvcat/day02_ws/src/hello_world_c/scripts /hello_ros_p.py on line 4, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
解決辦法:在Python檔案的開頭加上以下代碼
#-*- coding: UTF-8 -*-

注意2.:如果在撰寫c++代碼沒有代碼提示
需要修改 .vscode/c_cpp_properties.json 檔案,
我的.vscode/c_cpp_properties.json檔案如下
{
"configurations": [
{
"browse": {
"databaseFilename": "",
"limitSymbolsToIncludedHeaders": true
},
"includePath": [
"/home/vvcat/catkin_ws/devel/include/**",
"/opt/ros/melodic/include/**",
"/home/vvcat/catkin_ws/src/test_pkg/include/**",
"/usr/include/**"
],
"name": "ROS",
}
],
"version": 4
}
在 “name”: “ROS”,后面添加了
"intelliSenseMode": "gcc-x64",
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "c++17"
添加后成功解決代碼提示問題,
注:除了 “name”: “ROS” 后面添加幾條內容,其它地方無需修改,
修改完后的.vscode/c_cpp_properties.json檔案如下:
{
"configurations": [
{
"browse": {
"databaseFilename": "",
"limitSymbolsToIncludedHeaders": true
},
"includePath": [
"/home/vvcat/catkin_ws/devel/include/**",
"/opt/ros/melodic/include/**",
"/home/vvcat/catkin_ws/src/test_pkg/include/**",
"/usr/include/**"
],
"name": "ROS",
"intelliSenseMode": "gcc-x64",
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "c++17"
}
],
"version": 4
}
注意3.:
如果拋出例外: /usr/bin/env: “python”: 沒有那個檔案或目錄
解決1:Linux中在代碼的第一行解釋器宣告,可以使用絕對路徑定位到 python3 的安裝路徑
#! /usr/bin/python3
或
# !/usr/bin/python
解決2: 創建一個鏈接符號到 python 命令:
sudo ln -s /usr/bin/python3 /usr/bin/python
解決3:
在 CMakeLists.txt 檔案下 添加以下內容
catkin_install_python(PROGRAMS
scripts/自定義檔案名.py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)
總結
編譯: ctrl + shift + B
執行: 和之前一致,只是可以在 VScode 中添加終端,首先執行:
source ./devel/setup.bash
使用其它開發工具集成ROS插件:http://wiki.ros.org/IDEs
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/293975.html
標籤:python
