Multiple Publishers and subscribers in ROS Noetic
Already we have tried publisher subscriber in Python script and multiple sub/pub also we have tried.
Here is an experiment that does the following.
1. A packet publisher publishes the name of the packet (string format)
2. A security publisher (string) converts the above packet into an encrypted packet (usually the ceaser cipher, VITCC is equivalent to YLWFF).
3. A third publisher is an Int32 publisher, which publishes an integer number with the number of packets.
4. There is only one subscriber, that tells the "Number of packets encrypted" after all the publishers publishes.
i.e P1 AND P2 AND P3 => Subscribers
SOURCE CODE: Publisher1.py
#!/usr/bin/env python3
import rospy
from std_msgs.msg import String
def talker():
rospy.init_node('publisher_node1', anonymous=True)
pub = rospy.Publisher('P1', String, queue_size=10)
rate = rospy.Rate(1)
while not rospy.is_shutdown():
_string = "ABCDEF"
rospy.loginfo(_string)
pub.publish(_string)
rate.sleep()
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException:
pass
Publisher2.py
#!/usr/bin/env python3
from cryptography.fernet import Fernet
import rospy
from std_msgs.msg import String
def cipher(s):
s1 = ""
for x in s:
s1 += s + 'X'
return s1
def talker():
rospy.init_node('publisher_node2', anonymous=True)
pub = rospy.Publisher('P2', String, queue_size=10)
rate = rospy.Rate(1)
while not rospy.is_shutdown():
_string = cipher("ABCDEF")
rospy.loginfo(_string)
pub.publish(_string)
rate.sleep()
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException:
pass
Publisher3.py
#!/usr/bin/env python3
import rospy
from std_msgs.msg import String
def talker():
rospy.init_node('publisher_node3', anonymous=True)
pub = rospy.Publisher('P3', String, queue_size=10)
rate = rospy.Rate(1)
while not rospy.is_shutdown():
_string = str(len("ABCDEF"))
rospy.loginfo(_string)
pub.publish(_string)
rate.sleep()
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException:
pass
Subscriber1.py
#!/usr/bin/env python3
import rospy
from std_msgs.msg import String
def course(data: str):
rospy.loginfo("I took %s", data.data)
def listener():
rospy.init_node('subscriber_node', anonymous=True)
rospy.Subscriber("P1", String, course)
rospy.Subscriber("P2", String, course)
rospy.Subscriber("P3", String, course)
rospy.spin()
if __name__ == '__main__':
listener()
OUTPUTS:
ROS Publisher 1 |
ROS Publisher 2 |
Comments
Post a Comment