6.ROS中創建一個話題以及自建一個msg
ROS通信方式有三種,現在我們講解其中一種通信方式:話題(Topic);
話題(Topic)的傳遞實際上傳遞的是消息(msg),如果將消息(msg)比喻成需要運輸的物體,那麼話題是運輸工具;
ROS消息通信中使用的發佈者(Publisher)和訂閱者(Subscriber),分別是msg的發送者和接收者。
在ROS中,發送端稱爲發佈者,接收端稱爲訂閱者。兩者採用的是鬆耦合機制,可以隨時建立和斷開聯繫,可以一對多同時進行通信;
本節旨在創建一個簡單的msg文件,並創建和運行發佈者和訂閱者節點。
創建一個自檢msg的話題步驟:
- 建立基本的工作空間catkin_ws;
- 創建包pkg;
- 創建msg;
- 編輯源碼創建Publisher和Subscriber;
1.創建工作空間
工作環境未配置正確時,會導致提示非節點等,請設置好ros工作環境:
臨時設置ROS工作環境
source /home/dongdong/catkin_ws/devel/setup.bash
如果永久性配置
echo "source /home/dongdong/catkin_ws/devel/setup.bash" >> ~/.bashrc
source ~/.bashrc
查看是否配置成功
echo $ROS_PACKAGE_PATH
2.創建包(submap_switch)
cd ~/catkin_ws/src
catkin_create_pkg submap_switch std_msgs rospy roscpp
- 依賴包文件
std_msgs
rospy
roscpp
自動生成以下Cmake文件,可以根據實際需求在這裏創建包時添加,也可後續在Cmake文件中添加!
## Find catkin macros and libraries ## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
nav_msgs
roscpp
)
95 ###################################
96 ## catkin specific configuration ##
97 ###################################
98 ## The catkin_package macro generates cmake config files for your package
99 ## Declare things to be passed to dependent projects
100 ## INCLUDE_DIRS: uncomment this if your package contains header files
101 ## LIBRARIES: libraries you create in this project that dependent projects also need
102 ## CATKIN_DEPENDS: catkin_packages dependent projects also need
103 ## DEPENDS: system dependencies of this project that dependent projects also need
104 catkin_package(
105 # INCLUDE_DIRS include
106 LIBRARIES submap_switch
107 CATKIN_DEPENDS nav_msgs roscpp
108 # DEPENDS system_lib
109 )
3.創建msg
消息(msg): msg文件就是一個描述ROS中所使用消息類型的簡單文本。它們會被用來生成不同語言的源代碼。
msg文件存放在package的msg目錄下
msg文件實際上就是每行聲明一個數據類型和變量名。
可以使用的數據類型如下:
int8, int16, int32, int64 (plus uint*) float32, float64 string time, duration other msg files variable-length array[] and fixed-length array[C]
首先,創建.msg文件,在包的路徑下/submap_switch新建一個msg文件夾,並新建一個xxx.msg文件;
vim SingleSubmapState.msg
geometry_msgs/Pose2D position
int64 state
其中geometry_msgs/Pose2D position就是其他msg文件中創建的數據類型;同時SingleSubmapState生成後也可以作爲一種數據類型被其他新建的消息引用!
然後,在CMakeList.txt中添加生成msg的相關信息以及package.xml配置,這樣msg文件才能在編譯後生成C++/Python的源代碼;
在CMakeList.txt中添加:
1)message_generation依賴
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs message_generation)
2).msg文件的路徑
add_message_files(FILES SingleSubmapState.msg)
3)生成msg的相關依賴信息
#generate_messages(DEPENDENCIES std_msgs geometry_msgs submap_msgs)
generate_messages(DEPENDENCIES std_msgs geometry_msgs )
在package.xml中添加
<build_depend>message_generation</build_depend>
<run_depend>message_runtime</run_depend>
4.添加源代碼(C++/Python)
1)創建話題發佈者程序,關鍵點是添加msg頭文件和創建Publisher
cd ~/catkin_ws/src/submap_switch
vim submap_switch.cpp
代碼:
#include <submap_service/SingleSubmapState.h> //(Automatically created after build)
解釋:
- 導入submap_service/SingleSubmapState消息頭文件,這個是由submap_service包的SingleSubmapState.msg文件自動生成。
代碼:
//Declare topic publisher server 'submap_service'
ros::Publisher submap_state_pub = nh.advertise<submap_service::SingleSubmapState >("submap_state1",1000);
解釋:
- 發佈一個消息類型爲submap_service::SingleSubmapState,命名爲submap_state1的話題
- 定義消息隊列大小爲1000,即超過1000條消息之後,舊的消息就會丟棄。
2)創建話題訂閱者程序,關鍵點是添加msg頭文件和創建Subscriber
cd ~/catkin_ws/src/submap_switch
vim submap_switch_subscriber.cpp
代碼:
#include <submap_service/SingleSubmapState.h> //(Automatically created after build)
解釋:
- 導入submap_service/SingleSubmapState消息頭文件,這個是由submap_service包的SingleSubmapState.msg文件自動生成。
代碼:
//Declare topic subscribe server 'submap_state1'
ros::Subscriber sub = n.subscribe("submap_state1", 1000, submap_state1Callback);
解釋:
- 定義訂閱節點,名稱爲submap_state1,指定回調函數submap_state1Callback
- 但收到消息,則調用函數submap_state1Callback來處理。
- 參數1000,定義隊列大小,如果處理不夠快,超過1000,則丟棄舊的消息。
void submap_state1Callback(const submap_service::SingleSubmapState::ConstPtr& msg)
{
ROS_INFO("I heard: [%s]", msg->state);
}
5.編譯源代碼
a.修改CMakeList.txt文件,主要修改一下五項,將其開啓(自動生成的CmakeList.txt未自動開啓)!
b.編譯包
cmake_minimum_required(VERSION 2.8.3)
project(submap_switch)
包名即是CMakeList.txt中的project名!
catkin_make -DCATKIN_WHITELIST_PACKAGES="submap_switch"
這樣一個功能包創建完成!
c.如何驗證是否編譯成功?
啓動ros,執行節點submap_switch即可!(巧用teb自動補齊鍵,也可以做判斷是否編譯成功)
roscore
rosrun submap_switch submap_switch
6.運行節點
打開新終端執行
roscore
打開新終端執行
rosrun submap_switch submap_switch
打開新終端執行
rosrun submap_switch submap_switch_subcriber