Created
May 22, 2023 09:22
-
-
Save tutorgaming/b08d5d1c1cb4867c8750871788259cdf to your computer and use it in GitHub Desktop.
rospy ROS1 Connection Header Secret !
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<launch> | |
<node pkg="test_pkg" type="quick_publisher.py" name="my_talker_1" output="screen" /> | |
<node pkg="test_pkg" type="quick_publisher.py" name="my_talker_2" output="screen" /> | |
<node pkg="test_pkg" type="quick_publisher.py" name="my_talker_3" output="screen" /> | |
<node pkg="test_pkg" type="quick_publisher.py" name="my_talker_4" output="screen" /> | |
</launch> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import random | |
import rospy | |
from std_msgs.msg import String | |
class Talker(object): | |
def __init__(self): | |
self.publisher = rospy.Publisher("/chatter", String, queue_size=1) | |
self.rand = random.randint(1,4) | |
self.rate = rospy.Rate(self.rand) | |
self.node_name = rospy.get_name() | |
rospy.Timer(self.rate.sleep_dur, self.timer_cb) | |
def timer_cb(self, _): | |
msg = String() | |
msg.data = "Hello World from : {}".format(self.node_name) | |
rospy.loginfo("[%s][%d] Publishing: %s",self.node_name, self.rand, msg.data) | |
self.publisher.publish(msg) | |
if __name__ == "__main__": | |
rospy.init_node("talker") | |
TALKER = Talker() | |
rospy.spin() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import rospy | |
from std_msgs.msg import String | |
class Listener(object): | |
def __init__(self): | |
self.subscriber = rospy.Subscriber("/chatter", String, self.callback) | |
def callback(self, msg): | |
rospy.loginfo("From : [%s] I heard: %s", msg._connection_header["callerid"], msg.data) | |
if __name__ == "__main__": | |
rospy.init_node("listener") | |
LISTENER = Listener() | |
rospy.spin() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment