当前位置:网站首页>ROS notes (06) - definition and use of topic messages
ROS notes (06) - definition and use of topic messages
2022-06-25 11:46:00 【wohu1104】
ROS Pass through std_msgs Encapsulates some native data types , such as : String、Int32、Int64、Char、Bool、Empty… however , These data generally contain only one data Field , The singleness of structure means the limitation of function , When transmitting some complex data , such as : Lidar information … std_msgs Because of the poor description, it seems to be inadequate , In this scenario, you can use custom message types .
msgs Just a simple text file , Each row has a field type and a field name , The field types that can be used are :
- int8, int16, int32, int64 ( Or unsigned type : uint*)
- float32, float64
- string
- time, duration
- other msg files
- variable-length array[] and fixed-length array[C]
ROS There is also a special type in :Header , The header contains a timestamp and ROS Coordinate frame information commonly used in . Will often see msg The first line of the file has Header header .
1. Topic model
The topic model we want to implement is as follows :
among Message Medium Person Message format customized for us , The message is passed between the subscriber and the publisher .
2. Custom message implementation
2.1 Definition msg file
In Feature Pack topic_demo Create one in the directory msg Folder , Create a... In this folder Person.msg file , As shown in the figure below :
Person.msg There is our custom message format in
string name
uint8 age
uint8 gender
uint8 unknown = 0
uint8 male = 1
uint8 female = 2
2.2 Add Feature Pack dependencies
stay package.xml Add the following exec_depend Operational dependency :
<exec_depend>message_generation</exec_depend>
<exec_depend>message_runtime</exec_depend>
2.3 Add compile options

find_package(catkin REQUIRED COMPONENTS
geometry_msgs
rospy
std_msgs
turtlesim
message_generation # The new content
add_message_files(
FILES
Person.msg
)
generate_messages(
DEPENDENCIES
std_msgs
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES topic_demo
CATKIN_DEPENDS geometry_msgs rospy std_msgs turtlesim message_runtime
# DEPENDS system_lib
)
2.4 Compile and generate language related files
To the root directory of the project catkin_make command
$ catkin_make
Base path: /home/wohu/project/ros/ros_demo
Source space: /home/wohu/project/ros/ros_demo/src
Build space: /home/wohu/project/ros/ros_demo/build
Devel space: /home/wohu/project/ros/ros_demo/devel
Install space: /home/wohu/project/ros/ros_demo/install
####
#### Running command: "make cmake_check_build_system" in "/home/wohu/project/ros/ros_demo/build"
####
####
#### Running command: "make -j12 -l12" in "/home/wohu/project/ros/ros_demo/build"
####
[ 0%] Built target std_msgs_generate_messages_py
[ 0%] Built target std_msgs_generate_messages_eus
[ 0%] Built target std_msgs_generate_messages_lisp
[ 0%] Built target std_msgs_generate_messages_nodejs
[ 0%] Built target std_msgs_generate_messages_cpp
[ 0%] Built target _topic_demo_generate_messages_check_deps_Person
[ 57%] Built target topic_demo_generate_messages_py
[ 57%] Built target topic_demo_generate_messages_eus
[ 71%] Built target topic_demo_generate_messages_cpp
[ 85%] Built target topic_demo_generate_messages_nodejs
[100%] Built target topic_demo_generate_messages_lisp
[100%] Built target topic_demo_generate_messages
You can see that code files in various languages will be generated . among Python The relevant code files are in
/devel/lib/python2.7/dist-packages
Under the table of contents
2.5 Run code
Publisher code person_publisher.py :
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# The routine will publish /person_info topic of conversation , Custom message type learning_topic::Person
import rospy
from topic_demo.msg import Person
def velocity_publisher():
# ROS Node initialization
rospy.init_node('person_publisher', anonymous=True)
# Create a Publisher, The release is called /person_info Of topic, The message type is learning_topic::Person, The queue length 10
person_info_pub = rospy.Publisher('/person_info', Person, queue_size=10)
# Set the frequency of the cycle
rate = rospy.Rate(10)
while not rospy.is_shutdown():
# initialization learning_topic::Person Type of message
person_msg = Person()
person_msg.name = "Tom";
person_msg.age = 18;
person_msg.gender = Person.male;
# Release the news
person_info_pub.publish(person_msg)
rospy.loginfo("Publsh person message[%s, %d, %d]",
person_msg.name, person_msg.age, person_msg.gender)
# Delay according to the cycle frequency
rate.sleep()
if __name__ == '__main__':
try:
velocity_publisher()
except rospy.ROSInterruptException:
pass
Subscriber code person_subscriber.py Content :
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This routine will subscribe to /person_info topic of conversation , Custom message type learning_topic::Person
import rospy
from topic_demo.msg import Person
def personInfoCallback(msg):
rospy.loginfo("Subcribe Person Info: name:%s age:%d gender:%d",
msg.name, msg.age, msg.gender)
def person_subscriber():
# ROS Node initialization
rospy.init_node('person_subscriber', anonymous=True)
# Create a Subscriber, The subscription is called /person_info Of topic, Register callback function personInfoCallback
rospy.Subscriber("/person_info", Person, personInfoCallback)
# Loop waiting for callback function
rospy.spin()
if __name__ == '__main__':
person_subscriber()
Run the code command :
$ cd ~/catkin_ws
$ catkin_make
$ source ./devel/setup.bash
$ rescore
$ rosrun topic_demo person_subscriber.py
$ rosrun topic_demo person_publisher.py
边栏推荐
- Sword finger offer II 091 Painting house: application of state machine DP
- 客从何处来
- Manually rollback abnormal data
- Redis6笔记02 配置文件,发布和订阅,新数据类型,Jedis操作
- Thirty lines of code prevent VFP forms from running repeatedly, and the function supports parameter transfer
- Big endian and little endian
- 9 cases where elements cannot be located
- Yisheng biological sprint scientific innovation board: 25% of the revenue comes from the sales of new crown products, and it is planned to raise 1.1 billion yuan
- Very important very important very important very important very important very important very important very important very important
- 揭秘GaussDB(for Redis):全面对比Codis
猜你喜欢

揭秘GaussDB(for Redis):全面对比Codis

Research on parallel computing architecture of meteorological early warning based on supercomputing platform

Spark history server and event log details

SQL注入漏洞(繞過篇)

寿命分布 4种

Ladder side tuning: the "wall ladder" of the pre training model

redis的dict的扩容机制(rehash)

How TCP handles exceptions during three handshakes and four waves

TCP如何處理三次握手和四次揮手期間的异常

Share 7 immortal wallpaper websites, let the new wallpaper give you a little joy, and don't fall into the repetition year after year.
随机推荐
为什么要分布式 id ?分布式 id 生成方案有哪些?
交易期货沪镍产品网上怎么开户
VFP a picture processing library, simple and easy to use, free of charge, worth recommending
Causes and solutions of over fitting
Countdownlatch source code analysis
The temporary table from XML to VFP is simple and easy to use and worth collecting
.Net Core 中使用工厂模式
Openfeign uses
Research on parallel computing architecture of meteorological early warning based on supercomputing platform
JS judge whether a number is in the set
Flink partition policy
Handler、Message、Looper、MessageQueue
牛客网:分糖果问题
WebRTC Native M96 基础Base模块介绍之网络相关的封装
ArcGIS services query filter by time field
ThingsPanel 發布物聯網手機客戶端(多圖)
Spark Tuning common configuration parameters
Explain websocket protocol in detail
客从何处来
VFP calls the command line image processing program, and adding watermark is also available