ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Callback function never stop subscribing

asked 2021-08-22 11:12:45 -0500

subarashi gravatar image

updated 2021-08-24 03:46:29 -0500

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.

edit retag flag offensive close merge delete

Comments

do you want your callback to run until you get a new msg?

Nachum gravatar image Nachum  ( 2021-08-23 04:09:07 -0500 )edit

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.

subarashi gravatar image subarashi  ( 2021-08-23 04:24:26 -0500 )edit

why not use if? each msg will used once, the last value will stay published

Nachum gravatar image Nachum  ( 2021-08-23 05:19:08 -0500 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2021-08-23 06:10:16 -0500

Nachum gravatar image

You should use "if" command.

Until you publish something new you will see the same data published. To check if the data is new you need to use a time stamp.

edit flag offensive delete link more

Comments

Thank you so much. I have created a class and I added an "If" statement according to this https://stackoverflow.com/questions/6...

subarashi gravatar image subarashi  ( 2021-08-24 03:48:46 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2021-08-22 11:12:45 -0500

Seen: 170 times

Last updated: Aug 24 '21