当前位置:网站首页>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注入漏洞(绕过篇)
- Multiple clicks of the button result in results
- 建造者模式
- Detailed explanation of Spark's support source code for Yan priority
- Sentinel integrated Nacos data source
- Thingpanel publie le client mobile IOT (Multi - images)
- Convergence by probability
- ThingsPanel 發布物聯網手機客戶端(多圖)
- 時創能源沖刺科創板:擬募資11億 年營收7億淨利反降36%
- ThingsPanel 发布物联网手机客户端(多图)
猜你喜欢

ThingsPanel 發布物聯網手機客戶端(多圖)

Xishan technology rushes to the scientific innovation board: it plans to raise 660million yuan. Guoyijun and his wife have 60% of the voting rights

Eureka accesses the console and reports an error: whitelabel error page

What is the development history, specific uses and structure of the chip

SQL注入漏洞(繞過篇)

Flink deeply understands the graph generation process (source code interpretation)

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

Record the process of submitting code to openharmony once

Idea local launch Flink task

Application of analytic hierarchy process in college teaching evaluation system (principle + example + tool)
随机推荐
Spark runs wordcount (case 1)
ThingsPanel 发布物联网手机客户端(多图)
记一次有趣的逻辑SRC挖掘
手机上股票开户安全吗?找谁可以开户啊?
VFP uses Kodak controls to control the scanner to solve the problem that the volume of exported files is too large
Golden sun education listed in the U.S.: a small cap medium cap stock with a market value of USD 360million
How to realize the rich text editor function of mobile terminal
What should I do to dynamically add a column and button to the gird of VFP?
Spark history server and event log details
云原生数据湖以存储、计算、数据管理等能力通过信通院评测认证
牛客网:旋转数组
4 life distributions
redis的dict的扩容机制(rehash)
Thirty lines of code prevent VFP forms from running repeatedly, and the function supports parameter transfer
SQL注入漏洞(繞過篇)
西山科技冲刺科创板:拟募资6.6亿 郭毅军夫妇有60%表决权
芯片的发展史和具体用途以及结构是什么样的
VFP serial port communication is difficult for 9527. Maomao just showed his skill and was defeated by kiss
Under what circumstances will Flink combine operator chains to form operator chains?
Deeply understand Flink SQL execution process based on flink1.12