@TOC
1.工作空間目錄

在這里插入圖片描述
WorkSpace --- 自定義的工作空間
|--- build:編譯空間,用于存放CMake和catkin的緩存信息、配置信息和其他中間文件。
|--- devel:開發(fā)空間,用于存放編譯后生成的目標(biāo)文件,包括頭文件、動態(tài)&靜態(tài)鏈接庫、可執(zhí)行文件等。
|--- src:源碼
|-- package:功能包(ROS基本單元)包含多個節(jié)點、庫與配置文件,包名所有字母小寫,只能由字母、數(shù)字與下劃線組成
|-- CMakeLists.txt:配置編譯規(guī)則,比如源文件、依賴項、目標(biāo)文件
|-- package.xml:包信息,比如:包名、版本、作者、依賴項...(以前版本是 manifest.xml)
|-- scripts:存儲python文件
|-- src:存儲C++源文件
|-- include:頭文件
|-- msg:消息通信格式文件
|-- srv:服務(wù)通信格式文件
|-- action:動作格式文件
|-- launch:可一次性運行多個節(jié)點
|-- config:配置信息
|-- CMakeLists.txt:編譯的基本配置
1.1 package.xml
- 固定格式
<?xml version="1.0"?>
<!-- 格式: 以前是 1,推薦使用格式 2 -->
<package format="2">
<!-- 包名 -->
<name><the name of package></name>
<!-- 版本 -->
<version>0.0.0</version>
<!-- 描述信息 -->
<description>The <the name of package> package</description>
<!-- One maintainer tag required, multiple allowed, one person per tag -->
<!-- Example: -->
<!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> -->
<!-- 維護(hù)人員 -->
<maintainer email="*">XXX</maintainer>
<!-- One license tag required, multiple allowed, one license per tag -->
<!-- Commonly used license strings: -->
<!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
<!-- 許可證信息,ROS核心組件默認(rèn) BSD -->
<license>TODO</license>
<!-- Url tags are optional, but multiple are allowed, one per tag -->
<!-- Optional attribute type can be: website, bugtracker, or repository -->
<!-- Example: -->
<!-- <url type="website">http://wiki.ros.org/demo01_hello_vscode</url> -->
<!-- Author tags are optional, multiple are allowed, one per tag -->
<!-- Authors do not have to be maintainers, but could be -->
<!-- Example: -->
<!-- <author email="jane.doe@example.com">Jane Doe</author> -->
<!-- The *depend tags are used to specify dependencies -->
<!-- Dependencies can be catkin packages or system dependencies -->
<!-- Examples: -->
<!-- Use depend as a shortcut for packages that are both build and exec dependencies -->
<!-- <depend>roscpp</depend> -->
<!-- Note that this is equivalent to the following: -->
<!-- <build_depend>roscpp</build_depend> -->
<!-- <exec_depend>roscpp</exec_depend> -->
<!-- Use build_depend for packages you need at compile time: -->
<!-- <build_depend>message_generation</build_depend> -->
<!-- Use build_export_depend for packages you need in order to build against this package: -->
<!-- <build_export_depend>message_generation</build_export_depend> -->
<!-- Use buildtool_depend for build tool packages: -->
<!-- <buildtool_depend>catkin</buildtool_depend> -->
<!-- Use exec_depend for packages you need at runtime: -->
<!-- <exec_depend>message_runtime</exec_depend> -->
<!-- Use test_depend for packages you need only for testing: -->
<!-- <test_depend>gtest</test_depend> -->
<!-- Use doc_depend for packages you need only for building documentation: -->
<!-- <doc_depend>doxygen</doc_depend> -->
<!-- 依賴的構(gòu)建工具,這是必須的 -->
<buildtool_depend>catkin</buildtool_depend>
<!-- 指定構(gòu)建此軟件包所需的軟件包 -->
<build_depend>roscpp</build_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<!-- 指定根據(jù)這個包構(gòu)建庫所需要的包 -->
<build_export_depend>roscpp</build_export_depend>
<build_export_depend>rospy</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>
<!-- 運行該程序包中的代碼所需的程序包 -->
<exec_depend>roscpp</exec_depend>
<exec_depend>rospy</exec_depend>
<exec_depend>std_msgs</exec_depend>
<!-- The export tag contains other, unspecified, tags -->
<export>
<!-- Other tools can request additional information be placed here -->
</export>
</package>
2.啟動節(jié)點的方式
2.1 一次啟動一個
rosrun <the name of package> <the name of executable file>
- executable file:對于C語言,這個文件就是CMakeLists.txt中用add_executable生成的可執(zhí)行文件;對于python,這個文件就是CMakeLists.txt中用catkin_install_python指向的文件
對應(yīng)語法:
add_executable(<the name of executable file>
src/<the name of the source>.cpp
)
catkin_install_python(PROGRAMS scripts/<the name of the source>.py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)
2.2 一次啟動多個
如果想要一次性啟動一個包里的多個節(jié)點,而不是一行行地輸入rosrun指令...
- 我們需要一個launch文件,它的固定格式是這樣的:
<launch>
<node pkg="helloworld" type="demo_hello" name="hello" output="screen" />
<node pkg="turtlesim" type="turtlesim_node" name="t1"/>
<node pkg="turtlesim" type="turtle_teleop_key" name="key1" />
</launch>
- node:包含的某個節(jié)點
- pkg:功能包
- type:被運行的節(jié)點文件
- name:為節(jié)點命名
- output:設(shè)置日志的輸出目標(biāo)
然后,在終端輸入:
roslaunch <the name of package> <the name of launch file>
3.ROS常用命令
3.1 增
創(chuàng)建新的ROS功能包:
catkin_create_pkg 自定義包名 依賴包
3.2 查
列出所有功能包:
rospack list
查找某個功能包是否存在,如果存在返回安裝路徑:
rospack find 包名
3.3 執(zhí)行
3.3.1 加載環(huán)境變量
先進(jìn)入工作目錄,然后:
source ./devel/setup.bash
3.3.2 運行節(jié)點
roscore:ROS的系統(tǒng)先決條件節(jié)點和程序的集合,必須運行 roscore 才能使 ROS 節(jié)點進(jìn)行通信。新開一個終端并輸入:
roscore #用法一
roscore -p xxxx #用法二,指定端口號
roscore 將啟動:
- ros master
- ros 參數(shù)服務(wù)器
- rosout 日志節(jié)點
3.4 查看計算圖
在節(jié)點正在運行的情況下,終端輸入:
rosrun rqt_graph rqt_graph

在這里插入圖片描述
4.創(chuàng)建功能包
4.1 選擇工作目錄
首先,選擇一個路徑作為工作目錄。這里我選擇了如下路徑作為工作目錄

在這里插入圖片描述
4.2 創(chuàng)建功能包目錄
mkdir src
然后,在工作空間目錄下:
catkin_make
src目錄下會多出一個鎖定的CMakeLists.txt文件,不用管它。
4.3 建立功能包
先進(jìn)入src目錄
cd src
然后使用ROS提供的指令直接創(chuàng)建功能包即可:
catkin_create_pkg <the name of your package> roscpp rospy std_msgs message_generatio
roscpp、rospy、std_msgs、message_generatio是你所需要的依賴,你可以自由決定想要哪些依賴
本文由博客一文多發(fā)平臺 OpenWrite 發(fā)布!