When using ROS, I wanted to communicate measurement data as a topic from the node functioning as a Subscriber, but I was in trouble because I could not find a way to write Subscriber and Publisher in one program. (There is a high possibility that Google search power is low), I will summarize it to deepen the understanding of ROS. I've just started using it recently, and I think it may be wrong, so if you are familiar with it, please point it out. informative.
Middleware that provides libraries and tools to support the creation of robot applications. Specifically, it will be possible to easily communicate between robots.
In ROS, the executable ones connected to the ROS network are called "nodes", and messages called "topics" are exchanged between the nodes. The node that delivers the message is called the Publisher, and the node that receives the message is called the Subscriber.
talker.py
#!/usr/bin/env python
# license removed for brevity
import rospy
from std_msgs.msg import String #Import the data type to use
def talker():
#Create Publisher('Topic name',Mold,size)
pub = rospy.Publisher('chatter', String, queue_size=10)
#Declare node name
rospy.init_node('talker', anonymous=True)
#Declare the cycle of the loop
rate = rospy.Rate(10) # 10hz
while not rospy.is_shutdown():
#Fill in the data to publish
hello_str = "hello world %s" % rospy.get_time()
#Display the data to publish in the terminal
rospy.loginfo(hello_str)
#Publish data
pub.publish(hello_str)
rate.sleep()
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException:
pass
Quoted from Writing a Simple Publisher and Subscriber (Python), supplementary explanation
listener.py
#!/usr/bin/env python
import rospy
from std_msgs.msg import String #Import the data type to use
def callback(data):
#Display the received data in the terminal
#Data is data.Received in data
rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
def listener():
#Declare node name
rospy.init_node('listener', anonymous=True)
#Create Subscriber('Topic name',Mold,callback function)
rospy.Subscriber("chatter", String, callback)
#Keep calling the callback function
rospy.spin()
if __name__ == '__main__':
listener()
Quoted from Writing a Simple Publisher and Subscriber (Python), supplementary explanation
Just because I don't use rospy.spin () (because it stops in the callback function standby state), it feels like it's just stuck together, but I was worried about this for about two days. Lol.
controller.py
#!/usr/bin/env python
# coding: utf-8
import rospy
from std_msgs.msg import String
def callback(data):
#Display the received data in the terminal
#Data is data.Received in data
rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
def controller():
#Declare node name
rospy.init_node('controller', anonymous=True)
#Create a Subscriber. Load the topic.
sub = rospy.Subscriber('listener', String, callback)
#Create Publisher('Topic name',Mold,size)
pub = rospy.Publisher('talker', String, queue_size=1)
#Loop period.
rate = rospy.Rate(10)
while not rospy.is_shutdown():
#Fill in the data to publish
hello_str = "hello world %s" % rospy.get_time()
#Display the data to publish in the terminal
rospy.loginfo(hello_str)
#Publish data
pub.publish(hello_str)
rate.sleep()
if __name__ == '__main__':
try:
controller()
except rospy.ROSInitException:
pass
Recommended Posts