Ask Your Question
1

rospy.wait_for_message is unable to subscribe to a message continuosly

asked 2016-04-26 13:17:28 -0600

franciscs gravatar image

updated 2016-05-03 12:23:17 -0600

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()
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2016-04-27 00:33:03 -0600

NEngelhard gravatar image

wait_for_message is created for the purpose of getting exactly one message on a topic. If you want to subscribe contiuously, you should use a normal subscriber.

" The message is subscribed successfully for the first time, but it is unable to subscribe to the message anymore after that" Could you be a bit more specific (or show your code)?

edit flag offensive delete link more

Comments

Hi there, thanks for answered my question. I have already attached the code in the question above.

franciscs gravatar imagefranciscs ( 2016-05-03 08:37:01 -0600 )edit
0

answered 2018-08-15 06:21:08 -0600

"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." So here is the discrepancy. You are waiting for a message on the topic "forward", rather than waiting for the message "forward". Also, from your second program I can see that you are publishing on "move" topic a value of the type Int16 (assuming you have included the std_msgs library in your program) but later in the code, you are giving a String argument to move.publish. This will not work. Solution:

  1. Change:

    self.move = rospy.Publisher("move", Int16, queue_size=10)

to:

self.move = rospy.Publisher("move", std_msgs.msg.String, queue_size=10)

2.Change:

rospy.wait_for_message("forward", String)

to:

rospy.wait_for_message("move", String)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-04-26 13:17:28 -0600

Seen: 7,249 times

Last updated: Aug 15 '18