0
$\begingroup$

Rosanswers logo

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

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()

Originally posted by Tufte on ROS Answers with karma: 3 on 2019-05-28

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by gvdhoorn with karma: 86574 on 2019-05-28

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by Tufte on 2019-05-28:
Thank you for the quick reply. I see, so where do i need that line? instead of "try" or the over the function call?

Comment by gvdhoorn on 2019-05-28:
Instead of your current while True.

Comment by Tufte on 2019-05-28:
Great. Just tried it out. Works perfectly. Thank you so much!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.