Monday, March 17, 2014

51: Raspberry Pi Audio Out

I had a little battery powered Bluetooth speaker that also had a 3.5mm "Line In" jack. I foolishly expected that to work with the audio out jack on the Pi. But it didn't and I learned quickly that the details of Linux audio are arcane and nasty. So I just bought an earphone plug speaker from Radio Shack for $20. The Auvio brand rechargeable unit works fine for my purposes -- and has a builtin volume dial.

To go with it, I downloaded the "festival" text-to-speech software. It's crude but ok in a 1980s sort of way.

Saturday, March 15, 2014

50: Programming an Ultrasonic Rangefinder
-- revised --

I got the device below from mpja.com.

And I followed wiring and programming advice from--

http://www.raspberrypi-spy.co.uk/2012/12/ultrasonic-distance-measurement-using-python-part-1/

Running their sample program gave consistent results (within 5cm at 5m, within 1cm at 3m). But that was with no load on the Pi. When I ran my 10,000-place pi calculation (code listed in an early post) in the background, I got wildly longer readings -- 5m went to 7m.

Here's my mod of pi-spy code:
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

GPIO_TRIGGER = 8
GPIO_ECHO = 7

#print "Ultrasonic Measurement"
GPIO.setwarnings(False) #
# Set pins as output and input
GPIO.setup(GPIO_TRIGGER,GPIO.OUT)  # Trigger
GPIO.setup(GPIO_ECHO,GPIO.IN)      # Echo

# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)

# Allow module to settle
time.sleep(0.5)

# Send 10us pulse to trigger
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
start = time.time()
while GPIO.input(GPIO_ECHO)==0:
  start = time.time()

while GPIO.input(GPIO_ECHO)==1:
  stop = time.time()

# Calculate pulse length
elapsed = stop-start

# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distance = elapsed * 34000

# That was the distance there and back so halve the value
distance = distance / 2

print "Distance : %.1f cm" % distance

As you can see, the two while loops make this a cpu hog. I suppose I could make 3 readings and keep the shortest one.

Comments?

Added 12/16:

I have tried modifying the process priority of the rangefinder.py script. I tried the Unix/Linux nice(1) command. It was named "nice" because a non-super-user can only lower priority, not raise it. Anyway, didn't help.

I tried the code at--
http://www.raspberrypi-spy.co.uk/2012/12/ultrasonic-distance-measurement-using-python-part-2/

Also, inconsistent. So I finally changed the python script above to do 3 readings and only keep the lowest. That worked pretty well (within 2cm at 2m). But (reasonably) accurate back-to-back readings will be 2 seconds apart. Not much help for a vision-impaired person checking for the next curb.

What I really need is for the ECHO pin to cause an interrupt (instead of the code looping)!