///////////////////////////////////////////////////////////////////////////////////////////
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