目錄
一、創建一個工作空間
二、再創建一個功能包
三、編輯源文件
四、編輯配置文件
五、編譯並執行
First create a workspace and a src子目錄,然後再進入工作空間調用catkin_make命令編譯
mkdir -p demo02_ws/src
cd demo02_ws/
catkin_make
啟動VScode
code .
按Ctrl+Shift+B,點擊catkin_make:build
Replace all the text inside
{
"version": "2.0.0",
"tasks": [
{
"type": "catkin_make",
"problemMatcher": [
"$catkin-gcc"
],
"group": "build",
"label": "catkin_make: build"
}
]
}
Replace all the above code with the following code
{
"version": "2.0.0",
"tasks": [
{
"label":"catkin_make:debug",
"type": "shell",
"command":"catkin_make",
"args":[],
"group": {"kind":"build","isDefault":true},
"presentation":{
"reveal":"always"
},
"problemMatcher":"$msCompile"
}
]
}
The purpose of this is to facilitate compilation,以後在VScode中按Ctrl+Shift+B就可以實現編譯
在srcto generate a function package,該功能包依賴於roscpp,rospy,std_msgs,其中roscpp是使用C++實現的庫,rospy是使用python實現的庫,std_msgs是標准消息庫,創建ROS功能包時,一般都會依賴這三個庫實現.
It is recommended that writing requires high performance,Algorithmically complex codeC++,Write some functionally simple toolkit code to usepython.
用鼠標右鍵點擊src,選擇create catkin package,Fill in a name in the input boxhello_vscode
Enter into the input box roscpp rospy std_msgs
在hello_vscode目錄下面新建目錄scripts
然後在scripts新建hello_vscode_p.py
然後修改scripts文件權限,右鍵點擊scriptsSelect Open in Terminal,輸入
chmod +x *.py
再到hello_vscode_p.py中寫代碼
#! /usr/bin/env python
#導入包
import rospy
#入口
if __name__=="__main__":
#初始化ros節點
rospy.init_node("hello_p")
#輸出日志
rospy.loginfo("hello vscode! 這是python!")
At this point open with insidesrc同目錄的CmakeLists.txt文件
找到
# catkin_install_python(PROGRAMS
# scripts/my_python_script
# DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )
去掉 #
#
#
#
將scripts/my_python_script改成scripts/hello_vscode_p.py
最後按Ctrl+Shift+B進行編譯
#當不配置CmakeLists.txt 執行pythonfile throws an exception
#/usr/bin/env :"python":沒有那個文件或目錄
#原因是:當前ros版本是 noetic ,它使用的是python3
#解決方案
#1、直接聲明解釋器為python3:#! /usr/bin/env python3(不建議,If you call someone else's code, you may not be able to modify it directlypy文件)
#2、By way of soft linkpython鏈接到python3(建議)
sudo ln -s /usr/bin/python3 /usr/bin/python
Click in the terminal+,新建終端
進入工作環境
cd ~/demo02_ws/
roscore
再新開一個終端, Then create a new terminal input
source ./devel/setup.bash
rosrun hello_vscode hello_vscode_p.py
成功後如圖所示