程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

ROS writes subscribers and publishers in python (using custom MSG files stored in other packages)

編輯:Python

This paper records the use of python Write messages that use custom messages ros Some problems with subscribers and publishers .

Make a statement , The code of this article is intercepted from your own project , Not suitable for direct use , Only suitable as a reference template

1、 The first is how to create msg File and compile

The file structure is as shown in the figure :

catkin_ws
├── build
├── devel
└── src
├── robot_msgs
│ ├── CMakeLists.txt
│ ├── msg
│ │ └── Controldata.msg
│ └── package.xml
└── tcp_ros
└── script
└── test.py

catkin_ws There are two in the workspace package, One is only used to store messages ( You can add more messages by yourself ), One for storing code .

Controldata.msg The message file is as follows :

uint32 cmd
float32 param1
float32 param2

stay robot_msgs In the bag CMakerList.txt:

cmake_minimum_required(VERSION 2.8.3)
project(robot_msgs)
# Add dependency here message_generation Used to generate messages
find_package(catkin REQUIRED COMPONENTS
message_generation
std_msgs
geometry_msgs
)
# Add message file here Controldata.msg
add_message_files(
DIRECTORY msg
FILES
Controldata.msg
)
generate_messages(DEPENDENCIES std_msgs geometry_msgs)
# Message generation package is added here message_runtime
catkin_package(CATKIN_DEPENDS message_runtime std_msgs geometry_msgs)

stay robot_msgs In the bag package.xml Add the following :

<build_depend>message_generation</build_depend>
<run_depend>message_runtime</run_depend>

stay tcp_ros In the bag CMakerList:

find_package(catkin REQUIRED COMPONENTS # lookup robot_msgs package
roscpp
rospy
std_msgs
robot_msgs
)
catkin_package( # contain robot_msgs package
INCLUDE_DIRS include
LIBRARIES tjurobot
CATKIN_DEPENDS roscpp rospy std_msgs robot_msgs message_runtime
DEPENDS system_lib
)

2、python Write the publisher node

  • Publisher program : The key is how to python There are other... Imported in the node package Of msg file !!

    #!/usr/bin/env python
    # license removed for brevity
    import rospy
    from robot_msgs.msg import Controldata # Translated into : from robot_msgs In the bag msg Import a folder named Controldata Of msg file !
    def talker():
    pub = rospy.Publisher('chatter', Controldata, queue_size=10) # Set the publishing topic to be named “chatter”, The message type is Controldata.msg, The maximum message queue capacity is 10 The object of the frame pub
    rospy.init_node('talker', anonymous=True) # The initialization name is “talker” The node of 
    rate = rospy.Rate(10) # 10hz 
    while not rospy.is_shutdown():
    send_msg = Controldata() # Initialize a message structure : Directly with this msg File name as a function 
    send_msg.cmd =0X15 # to send_msg Parameter assignment in the structure ( That is the msg Parameters in the file )
    send_msg.param1 = 123
    send_msg.param2 = 0
    pub.publish(send_msg) # call pub Object's publish Function publishing topic 
    rate.sleep() # During the cycle, you can use just the right sleep time to maintain the desired rate , Applicable to topics that need to be published constantly 
    if __name__ == '__main__':
    try:
    talker()
    except rospy.ROSInterruptException:
    pass
    

3、python Write the subscriber node

  • Subscriber program : The key is how to use topic data in callback function

    #!/usr/bin/env python
    import rospy
    from robot_msgs.msg import Controldata
    def callback_fuc(data): # The data Is equivalent to the publisher's send_msg Structure 
    rospy.loginfo("I heard %d,%d,%d", data.cmd,data.param1,data.param2)
    def listener():
    rospy.init_node('listener', anonymous=True) # The initialization name is “listener” Subscriber node for :anonymous=True A unique name will be generated for the node to avoid duplicate node names 
    rospy.Subscriber("chatter", Controldata, callback_fuc) # Set subscriber properties : Subscribed topic name “chatter”, The topic corresponds to msg Message file name , Callback function name 
    rospy.spin() # There must be , Otherwise, we can't receive , But it will be equivalent to an endless loop, and the program will get stuck here 
    if __name__ == '__main__':
    listener()
    

    About how to solve rospy.spin() For the problem that the post program is stuck and cannot be executed, please refer to this blog :

(94 Bar message ) solve rospy.spin() Cycle all the time , Unable to execute remaining programs _DLUT_ Xiaodingdang's blog -CSDN Blog _rospy.spin


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved