ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
Hi @schizzz8,
I have created a simple publisher at 30hz and a subscriber that simulates a process that takes 0.2 seconds (works at 5hz)
I'm using two variables to control the flow: new_msg
and processing
. I think it can help you.
It is written in python, but you can use the same logic (basically because it does not rely on ROS Python topic queue, which could be or not different of ROScpp).
Publisher (Simulating your camera publisher)
#!/usr/bin/env python
import rospy
from std_msgs.msg import Int32
def publisher():
pub = rospy.Publisher('topic_name', Int32, queue_size=10)
rospy.init_node('publisher')
r = rospy.Rate(30) # 30hz
count = 1
msg = Int32()
while not rospy.is_shutdown():
msg.data = count
count = count + 1 if count < 30 else 1
pub.publish(msg)
rospy.loginfo(msg)
r.sleep()
if __name__ == "__main__":
publisher()
Subscriber (Simulating your script that processes a given image)
#!/usr/bin/env python
import rospy
from std_msgs.msg import Int32
processing = False
new_msg = False
msg = None
def callback(data):
global processing, new_msg, msg
if not processing:
new_msg = True
msg = data
def listener():
global processing, new_msg, msg
rospy.init_node('subscriber')
rospy.Subscriber("topic_name", Int32, callback)
# just to use the sleep method
r = rospy.Rate(5)
while not rospy.is_shutdown():
if new_msg:
# set processing to True
new_msg = False
processing = True
# simulate a process that takes 0.2 seconds
rospy.loginfo(msg)
r.sleep()
# set processing to False
processing = False
if __name__ == "__main__":
listener()
I hope it can help you.
Cheers
2 | No.2 Revision |
Hi @schizzz8,
I have created a simple publisher at 30hz and a subscriber that simulates a process that takes 0.2 seconds (works at 5hz)
I'm using two variables to control the flow: new_msg
and processing
. I think it can help you.
It is written in python, but you can use the same logic (basically because it does not rely on ROS Python topic queue, which could be or not different of ROScpp).
Publisher (Simulating your camera publisher)
#!/usr/bin/env python
import rospy
from std_msgs.msg import Int32
def publisher():
pub = rospy.Publisher('topic_name', Int32, queue_size=10)
rospy.init_node('publisher')
r = rospy.Rate(30) # 30hz
count = 1
msg = Int32()
while not rospy.is_shutdown():
msg.data = count
count = count + 1 if count < 30 else 1
pub.publish(msg)
rospy.loginfo(msg)
r.sleep()
if __name__ == "__main__":
publisher()
Subscriber (Simulating your script that processes a given image)
#!/usr/bin/env python
import rospy
from std_msgs.msg import Int32
processing = False
new_msg = False
msg = None
def callback(data):
global processing, new_msg, msg
if not processing:
new_msg = True
msg = data
def listener():
global processing, new_msg, msg
rospy.init_node('subscriber')
rospy.Subscriber("topic_name", Int32, callback)
# just to use the sleep method
r = rospy.Rate(5)
while not rospy.is_shutdown():
if new_msg:
# set processing to True
new_msg = False
processing = True
# simulate a process that takes 0.2 seconds
rospy.loginfo(msg)
r.sleep()
# set processing to False
processing = False
if __name__ == "__main__":
listener()
I hope it can help you.you.
You can see it being executed at this video (https://youtu.be/XoirEyK4J2A)
Cheers
3 | No.3 Revision |
Hi @schizzz8,
I have created a simple publisher at 30hz and a subscriber that simulates a process that takes 0.2 seconds (works at 5hz)
5hz).
This script demonstrates that you can receive an image and do not have its value updated while you are processing it.
I'm using two variables to control the flow: new_msg
and processing
. I think it can help you.
you.
It is written in python, but you can use the same logic (basically because it does not rely on ROS Python topic queue, which could be or not different of ROScpp).
Publisher (Simulating your camera publisher)
#!/usr/bin/env python
import rospy
from std_msgs.msg import Int32
def publisher():
pub = rospy.Publisher('topic_name', Int32, queue_size=10)
rospy.init_node('publisher')
r = rospy.Rate(30) # 30hz
count = 1
msg = Int32()
while not rospy.is_shutdown():
msg.data = count
count = count + 1 if count < 30 else 1
pub.publish(msg)
rospy.loginfo(msg)
r.sleep()
if __name__ == "__main__":
publisher()
Subscriber (Simulating your script that processes a given image)
#!/usr/bin/env python
import rospy
from std_msgs.msg import Int32
processing = False
new_msg = False
msg = None
def callback(data):
global processing, new_msg, msg
if not processing:
new_msg = True
msg = data
def listener():
global processing, new_msg, msg
rospy.init_node('subscriber')
rospy.Subscriber("topic_name", Int32, callback)
# just to use the sleep method
r = rospy.Rate(5)
while not rospy.is_shutdown():
if new_msg:
# set processing to True
new_msg = False
processing = True
# simulate a process that takes 0.2 seconds
rospy.loginfo(msg)
r.sleep()
# uncomment below to see the same value being "processed"
# rospy.loginfo(msg)
# r.sleep()
# set processing to False
processing = False
if __name__ == "__main__":
listener()
I hope it can help you. You can see it being executed at this video (https://youtu.be/XoirEyK4J2A)
Cheers