本案例在之前的基礎上進行擴展,傳遞一個自定義的復數類訊息,
本系列用時7天,博主也是從零開始,盡力去寫的,如果發現了錯誤一定要私信告訴我呀,這么努力的博主,關注一下吧,
作者:楊絲兒
座右銘:始于興趣,源于熱愛,成于投入,
介紹:愛丁堡大學 人工智能專業,技術興趣點集中在機器人、人工智能可解釋性、數學、物理等等,
聊天吹水QQ群:兔嘰的魔術工房 (942848525)
個人博客:discover304.top
個人B站賬號:楊絲兒今天也在科學修仙(UP主跨站求個三連加關注)
?注意
- 本文只提供在Linux平臺上的代碼演示和最基本的注釋,不進行邏輯的解釋,
- 相關解釋以及需要用到的基礎知識參見:【機器人】ROS程式框架:架構部分
- 本案例的前置任務包括環境搭建,作業區創建,基本軟體(訊息傳遞communicate_bot)代碼,參見:【機器人】ROS1工程案例:基礎部分
?定義訊息
我們設計一個傳遞復數型別,復數類,
在包下新建msg檔案夾,并在檔案夾內新建Complex.msg檔案
mkdir msg
touch Complex.msg
編輯Complex.msg檔案
int32 real_part
int32 imaginary_part
?添加依賴
編輯包內的package.xml檔案添加依賴
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
小貼士:package.xml內已經有大量的待選依賴,直接去掉注釋就好
?添加配置資訊
在包內CMakeLists.txt中修改
find_package(catkin REQUIRED COMPONENTS
rospy
std_msgs # 新增行
message_generation # 新增行
)
catkin_package(
CATKIN_DEPENDS message_runtime # 新增行
)
將下面面代碼去掉注釋
add_message_files(
FILES
Complex.msg # 修改為型別檔案名
)
generate_messages(
DEPENDENCIES
std_msgs # Or other packages containing msgs
)
小貼士:
CMakeLists.txt也有大量的注釋以及說明內容,可以參考,
?重編譯
在作業區執行下面的命令,
catkin_make
如果出現紅色報錯,需要回溯,看看哪里出現了問題,
?使用新型別
修改包內topic_publisher.py代碼
#!/usr/bin/env python3
import rospy
# from std_msgs.msg import Int32
from communicate_bot.msg import Complex
class Publisher():
# count = 0
count = Complex()
def Publisher(self):
# pass
self.count.real_part = 0
self.count.imaginary_part = 0
def publish(self):
rospy.init_node('topic_publisher')
# pub = rospy.Publisher('counter', Int32, queue_size=10)
pub = rospy.Publisher('counter', Complex, queue_size=10)
rate = rospy.Rate(2)
while not rospy.is_shutdown():
pub.publish(self.count)
# self.count += 1
self.count.real_part += 1
self.count.imaginary_part += 1
# print(self.count)
print(f"{self.count.real_part}+{self.count.imaginary_part}i")
rate.sleep()
if __name__ == '__main__':
publisher = Publisher()
publisher.publish()
修改包內topic_subscriber.py代碼
#!/usr/bin/env python3
import rospy
# from std_msgs.msg import Int32
from communicate_bot.msg import Complex
class Subscriber():
def Subscriber():
pass
def listen(self):
rospy.init_node('topic_subscriber')
# sub = rospy.Subscriber('counter', Int32, lambda msg : print(msg.data))
sub = rospy.Subscriber('counter', Complex, lambda msg : print(f"{msg.real_part}+{msg.imaginary_part}i"))
rospy.spin()
if __name__ == "__main__":
subscriber = Subscriber()
subscriber.listen()
小貼士:自定義的型別有的時候會很大,所以我們可以采用鎖存話題(latched)的形式,上傳一個訊息后,會一直保留到下一個新的同類訊息上傳,
pub = rospy.Publisher(<訊息名>, <訊息型別>, latched=True)
?運行效果

?附加內容
CMakeLists.txt中的說明/教程:
################################################
## Declare ROS messages, services and actions ##
################################################
## To declare and build messages, services or actions from within this
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
## * add a build_depend tag for "message_generation"
## * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
## * If MSG_DEP_SET isn't empty the following dependency has been pulled in
## but can be declared for certainty nonetheless:
## * add a exec_depend tag for "message_runtime"
## * In this file (CMakeLists.txt):
## * add "message_generation" and every package in MSG_DEP_SET to
## find_package(catkin REQUIRED COMPONENTS ...)
## * add "message_runtime" and every package in MSG_DEP_SET to
## catkin_package(CATKIN_DEPENDS ...)
## * uncomment the add_*_files sections below as needed
## and list every .msg/.srv/.action file to be processed
## * uncomment the generate_messages entry below
## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)
下一篇會涉及運行階段的重命名,鏈接在這里:TODO正在施工中
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/398582.html
標籤:AI
上一篇:linux磁盤及檔案系統
下一篇:資料分析——資料清洗和準備

