rospy.wait_for_message is unable to subscribe to a message continuosly
Hi everyone. I have a question about rospy.wait_for_message. In my program, I use rospy.wait_for_message(topic, data type) to wait for a message published by other node. The first program will wait for the "forward" message from the second program in order to restart the program to check the distance again. However, the first program did subscribe to the "forward" message for the first time, but after that it is unable to subscribe the "forward" message anymore. Is there any method to solve this problem so that the rospy.wait_for_message can subscribe to a message continuously in a program. Thanks.
First program:
class servo_motor:
def __init__(self):
# publish topic to compare_distance
self.dist_0 = rospy.Publisher("dist_detect_right", Int16, queue_size=10)
self.dist_180 = rospy.Publisher("dist_detect_left", Int16, queue_size=10)
#publish topic to dc_motor
self.dist_detect = rospy.Publisher("dist_detect", String, queue_size=10)
self.forward = rospy.Subscriber("move" , String, self.halt)
def halt(self, data):
if data.data == "forward":
self.forward.unregister()
main()
def check_distance(self):
distance = self.read_sensor()
if distance < 15:
self.dist_detect.publish("stop")
PWM.set_duty_cycle(pwm_pin,96.04)
time.sleep(1)
distance = self.read_sensor()
self.dist_0.publish(distance)
PWM.set_duty_cycle(pwm_pin,86.45)
time.sleep(1.5)
distance = self.read_sensor()
self.dist_180.publish(distance)
PWM.set_duty_cycle(pwm_pin, 91.25)
time.sleep(1)
rospy.wait_for_message("forward", String)
Second program:
class dc_motor:
def __init__(self):
#publish msg move to servo_node
self.move = rospy.Publisher("move", Int16, queue_size=10)
#subscribe topic from compare_distance
self.navi_direction = rospy.Subscriber("direction", String, self.turn_direction)
def turn_direction(self,data):
if data.data == "turn left":
self.turn_left()
time.sleep(2)
elif data.data == "turn right":
self.turn_right()
time.sleep(2)
elif data.data == "move backward":
self.move_backward()
time.sleep(2)
self.move.publish("forward")
self.navi_direction.unregister()
main()