当前位置:网站首页>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
边栏推荐
- SQL server saves binary fields to disk file
- 杭州/北京内推 | 阿里达摩院招聘视觉生成方向学术实习生(人才计划)
- The service layer reports an error. The XXX method invalid bound statement (not found) cannot be found
- Two ways of redis persistence -- detailed explanation of RDB and AOF
- Simple use of stream
- Builder pattern
- Ladder side tuning: the "wall ladder" of the pre training model
- Shichuang energy rushes to the scientific innovation board: it plans to raise 1.1 billion yuan, with an annual revenue of 700million yuan and a 36% decrease in net profit
- PHP如何提取字符串中的图片地址
- Old ou, a fox friend, has had a headache all day. The VFP format is always wrong when it is converted to JSON format. It is actually caused by disordered code
猜你喜欢

Develop two modes of BS mode verification code with VFP to make your website more secure

CFCA Anxin sign access

SQL server saves binary fields to disk file

西山科技冲刺科创板:拟募资6.6亿 郭毅军夫妇有60%表决权

TCP如何处理三次握手和四次挥手期间的异常

Use of Presto visualization client-yanagishima20.0

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

Solution to the timeout scenario of Flink streaming computing (official live broadcast)

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

按钮多次点击造成结果
随机推荐
Deeply understand Flink SQL execution process based on flink1.12
How PHP extracts image addresses from strings
JS judge whether a number is in the set
Detailed explanation of Spark's support source code for Yan priority
杭州/北京内推 | 阿里达摩院招聘视觉生成方向学术实习生(人才计划)
Cesium editing faces
try-catch-finally
Using DBF of VFP to web salary query system
Real software developers will use this method to predict the future
Presto Web UI introduction
Use of JSP sessionscope domain
Comparison between relu and SIGMOD
Simple use of stream (II)
Semaphore source code analysis
Handler、Message、Looper、MessageQueue
TCP如何處理三次握手和四次揮手期間的异常
Use of Presto visualization client-yanagishima20.0
现在网上炒股开户身份证信息安全吗?
GC
2022 mathematical modeling competition time and registration fee