Callback function never stop subscribing
Hello, dear community. I am facing a problem and I hope you guys can help me to figure it out.
First, I have one node that is the publisher, it has a conditional if an object is detected it will publish "object_found"
string data under the topic "/object_detection/data_found", if not then it'll publish "nothing"
string data under the topic "/object_detection/no_data"
. The publisher works fine, the problem comes in the subscriber. The first subscriber is subscribing from the topic "/object_detection/no_data"
. In the subscriber script inside the callback function, I wrote a while loop because there is a variable called angle
this variable must go from 0° to 360° and publish it to another topic. The problem comes when there is no object is subscribing to the topic "/object_detection/no_data"
but when the publisher stops publishing that while loop inside the callback function never stops publishing, even if the publisher is not publishing.
How can I stop the while loop when the publisher is not publishing anything?
import rospy
from std_msgs.msg import String
from geometry_msgs.msg import Point
angle = 0
def callback(data):
angle = data.z
while angle < 360:
print("Angle: ", angle)
x, y = 0, 0
angle += 1
coordinates = Point(x, y, z = angle)
pub.publish(coordinates )
def listener():
rospy.init_node('listener', anonymous=True)
rospy.Subscriber("/object_detection/no_data", Point, callback)
rospy.spin()
if __name__ == '__main__':
pub = rospy.Publisher('Coordinates', Point, queue_size=10)
listener()
The goal is when the publisher is not publishing anything, then the while loop inside the callback function must stop publishing the angle and keep the current value for later.
In the subscriber, if the value of data.z
is equal to 1 then the subscriber must publish the angle from 0 to 360 degrees and if the subscriber stops receiving data then the angle must be stopped and the subscriber must not publish anything.
Please help me.
do you want your callback to run until you get a new msg?
Yes. when the publisher stops publishing, then the while loop inside the callback function must stop. Right now I can see by using
rostopic echo
that the subscriber is not getting anything but the "while loop" inside the callback function is still publishing a new msg.why not use if? each msg will used once, the last value will stay published