Recently, I've been touching ROS little by little, and when implementing a node in Python, I'd like to introduce a problem with ** implementing publisher and subscriber in one node **. I was able to clean it up by using a Python class. It is assumed that you have enough ROS knowledge and a little object-oriented knowledge to make a package by yourself.
An implementation example is [here](https://qiita.com/drafts/d15d52856188120647f4/edit#publish-%E3%81%A8-subscribe%E3%82%92%E5%90%8C%E6%99%82% E3% 81% AB1% E3% 81% A4% E3% 81% AE% E3% 83% 8E% E3% 83% BC% E3% 83% 89% E3% 81% A7% E3% 82% 84% E3% 82% 8B)
I want to subscribe to the data on this topic, process it, and publish it to that topic
I think there is something like that. Did you find out wrong ** What should I do when using subscriber and publisher at the same time on one node **? I was quite addicted to it.
ROS.org and the popular Publisher and Subscriber tutorials often look like this: I think.
Specify the topic name and message type with rospy.Publisher
and publish withpub.publish ()
.
talker.py
#rospy and message import
import rospy
import std_msgs.msg import String
def talker():
pub = rospy.Publisher('chatter', String, queue_size=10)
rospy.init_node('talker', anonymous=True)
#Processing continues
pub.publish(data)
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException: pass
In rospy.Subscriber
, specify the topic name, data type, and specify the callback function, and perform the post-subscribe processing with the callback function.
listener.py
#rospy and message import
import rospy
from std_msgs.msg import String
def callback(data):
#subscribe callback function
def listener():
rospy.init_node('listener', anonymous=True)
rospy.Subscriber("chatter", String, callback)
rospy.spin()
if __name__ == '__main__':
listener()
After the tutorial, I understood the mechanism of Publish and Subscribe.
Then, let's create a new node and do Publish and Subscribe at the same time. I thought, and when I implemented it, should I publish it in the callback function of rospy.Subscriber
? I was worried because I couldn't clean the code.
At that time, I was looking at this article and I also saw the code /main.py#L10) By the way, it was very beautiful, so I used it as a reference. I didn't use Python classes in the tutorial above, but they can be nicely implemented.
Create Publisher and Subscriber with __init__ ()
, and process in the flow of callback function and publish function.
When you run it, create a class in the main part and you're done!
test.py
import rospy
from std_msgs.msg import String
class testNode():
def __init__(self):
#Creating a Subscriber
self.sub = rospy.Subscriber('topic name', String, self.callback)
#Creating Publisher
self.pub = rospy.Publisher('topic name', String, queue_size=1)
def callback(self, data):
#Write the processing of the callback function
Publisher(data)
def Publisher(self, data):
self.pub.publish(data)
def function(self, data):
#Write if there is any other processing
return data
if __name__ == '__main__':
rospy.init_node('test_node')
time.sleep(3.0)
node = testNode()
while not rospy.is_shutdown():
rospy.sleep(0.1)
I personally thought that object-oriented programming was difficult to get started with, but I was able to write it conveniently and neatly. Some people have made templates with like this!
I didn't have the information I wanted to search for "ros python publish subscribe same node". .. .. I'm too new to ROS, so am I doing something wrong? Please let me know if something is wrong.
For machine learning of ROS, I made a package to flow joint angles from OpenPose with Topic ROS course 31 python basics ROS program template Write a Simple Publisher and Subscriber (Python)
Recommended Posts