Ask Your Question
0

Node stuck on endless loop

asked 2019-05-28 12:57:58 -0600

Tufte gravatar image

updated 2019-05-28 13:47:18 -0600

gvdhoorn gravatar image

///////////////////////////////////////////////////////////////////////////////////////////

Hi. I'm running a Raspberry 3+ with ROS and a CamJam Edu kit 3. This has a Audio/Echo locator (HC-SR04). The program below is the python file for calculating and publishing the distance in front of the robot. When I run this trough the launch file. And then try to stop it (Ctrl+C, Ctrl+Z). The rest of my program, pubs/subs/etc stops, but this file keeps on going on an endless loop, printing out the distance. And it does not show up in rosnode list, so I can't kill it. The only thing I find to work is to reboot my R-pi, by pulling the plug. Any good ideas of what I might be doing wrong?

#!/usr/bin/env python
#Libraries
import RPi.GPIO as GPIO
import time
import rospy
from sensor_msgs.msg import Range
#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BCM)

#set GPIO Pins
GPIO_TRIGGER = 17
GPIO_ECHO = 18

#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)

def distance():
    # set Trigger to HIGH
    GPIO.output(GPIO_TRIGGER, True)

    # set Trigger after 0.01ms to LOW
    time.sleep(0.00001)
    GPIO.output(GPIO_TRIGGER, False)

    StartTime = time.time()
    StopTime = time.time()

    # save StartTime
    while GPIO.input(GPIO_ECHO) == 0:
        StartTime = time.time()

    # save time of arrival
    while GPIO.input(GPIO_ECHO) == 1:
        StopTime = time.time()

    # time difference between start and arrival
    TimeElapsed = StopTime - StartTime
    # multiply with the sonic speed (34300 cm/s)
    # and divide by 2, because there and back
    distance = (TimeElapsed * 34300) / 2

    return distance

if __name__ == '__main__':
    rospy.init_node('distance_node')
    soundPub = rospy.Publisher('/distance', Range, queue_size=1)
    pub_range = Range()
    try:
        while True:
            dist = distance()
            pub_range.range = dist
            soundPub.publish(pub_range)
            print("Distance: %.1f cm" % pub_range.range)
            #print ("Measured Distance = %.1f cm" % dist)
            time.sleep(1)

        # Reset by pressing CTRL + C
    except KeyboardInterrupt:
        print("Measurement stopped by User")
        GPIO.cleanup()
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2019-05-28 13:49:52 -0600

gvdhoorn gravatar image

updated 2019-05-28 13:54:26 -0600

if __name__ == '__main__':
    rospy.init_node('distance_node')
    soundPub = rospy.Publisher('/distance', Range, queue_size=1)
    pub_range = Range()
    try:
        while True:
            [..]
            time.sleep(1)

Well, this is an endless loop. That is why it won't shut down.

You'll want to check for an exit condition and break the loop.

A good exit condition when using rospy would be to see whether ROS has been requested to shutdown, so something like this would work:

while not rospy.is_shutdown():

For reference, see the Writing a Simple Publisher and Subscriber (Python) tutorial on the wiki.

edit flag offensive delete link more

Comments

Thank you for the quick reply. I see, so where do i need that line? instead of "try" or the over the function call?

Tufte gravatar imageTufte ( 2019-05-28 13:53:05 -0600 )edit
2

Instead of your current while True.

gvdhoorn gravatar imagegvdhoorn ( 2019-05-28 13:54:02 -0600 )edit

Great. Just tried it out. Works perfectly. Thank you so much!

Tufte gravatar imageTufte ( 2019-05-28 13:59:29 -0600 )edit

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: 2019-05-28 12:57:58 -0600

Seen: 39 times

Last updated: May 28