current position:Home>Lesson 3 of ROS quick start - subscriber subscriber of ROS

Lesson 3 of ROS quick start - subscriber subscriber of ROS

2022-04-29 07:37:52Loading_ create

ROS Of Subscriber subscriber

What is a subscriber

Receiving information , adopt ROS Topic Receive from other Node Information about , And handle through the callback function

Usually used to monitor system status , For example, motion interruption is triggered when the robot joint reaches the limit position

ROS Topic Example

Topic The communication process is :

  1. Publisher Nodes and Subscriber The nodes are located at Master To register
  2. Publisher Release Topic
  3. Subscriber stay Master Subscription under command Topic, To establish Pub-Sub Communication between

Publisher Node Demo

#!/usr/bin/env python
#-- coding:UTF-8 --
import rospy
from std_msgs.msg import String

def push_msgs():
    rospy.init_node("push_node", anonymous=False)
    push_pub = rospy.Publisher("push", String, queue_size=10)
    rate = rospy.Rate(100)

    while not rospy.is_shutdown():
        msg = "hello world"
        push_pub.publish(msg)
        rospy.loginfo("send message %s", msg)
        rate.sleep()


if __name__ == "__main__":
    push_msgs()

Subscriber Node Demo

#!/usr/bin/env python
#-- coding:UTF-8 --
import rospy
from std_msgs.msg import String

def stringSubscriberCallback(data): #data The data type of is related to Subscriber Received Topic The corresponding message types are consistent 
    rospy.loginfo('The contents of simple_topic: %s', data.data)

def stringSubscriber():
    rospy.init_node('sub_node', anonymous = False) # initialization ROS node 
    rospy.Subscriber('push', String, stringSubscriberCallback) # Definition Subscriber object 

    rospy.spin()


if __name__ == "__main__":
    stringSubscriber()

Run it for the following effect
 Insert picture description here

copyright notice
author[Loading_ create],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204290527320497.html

Random recommended