2013-12-13 01:21:22 +01:00
|
|
|
// Package us020 allows interfacing with the US020 ultrasonic range finder.
|
|
|
|
package us020
|
|
|
|
|
|
|
|
import (
|
2013-12-13 03:10:25 +01:00
|
|
|
"log"
|
2013-12-13 01:21:22 +01:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2014-03-02 20:21:23 +01:00
|
|
|
"github.com/kidoman/embd"
|
2013-12-13 01:21:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-01-08 23:09:06 +01:00
|
|
|
pulseDelay = 30000 * time.Nanosecond
|
|
|
|
defaultTemp = 25
|
2013-12-13 01:21:22 +01:00
|
|
|
)
|
|
|
|
|
2014-01-08 23:09:06 +01:00
|
|
|
type Thermometer interface {
|
|
|
|
Temperature() (float64, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type nullThermometer struct {
|
|
|
|
}
|
2014-01-08 15:48:40 +01:00
|
|
|
|
2014-01-08 23:09:06 +01:00
|
|
|
func (*nullThermometer) Temperature() (float64, error) {
|
|
|
|
return defaultTemp, nil
|
2013-12-13 01:21:22 +01:00
|
|
|
}
|
|
|
|
|
2014-01-08 23:09:06 +01:00
|
|
|
var NullThermometer = &nullThermometer{}
|
|
|
|
|
|
|
|
// US020 represents a US020 ultrasonic range finder.
|
|
|
|
type US020 struct {
|
2014-03-02 20:21:23 +01:00
|
|
|
EchoPin, TriggerPin embd.DigitalPin
|
2014-01-08 23:09:06 +01:00
|
|
|
|
|
|
|
Thermometer Thermometer
|
2013-12-13 01:21:22 +01:00
|
|
|
|
|
|
|
speedSound float64
|
|
|
|
|
|
|
|
initialized bool
|
2014-01-08 23:09:06 +01:00
|
|
|
mu sync.RWMutex
|
2013-12-13 03:10:25 +01:00
|
|
|
|
2014-01-08 23:09:06 +01:00
|
|
|
Debug bool
|
2013-12-13 01:21:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new US020 interface. The bus variable controls
|
|
|
|
// the I2C bus used to communicate with the device.
|
2014-03-02 20:21:23 +01:00
|
|
|
func New(echoPin, triggerPin embd.DigitalPin, thermometer Thermometer) *US020 {
|
2014-02-26 23:54:53 +01:00
|
|
|
return &US020{EchoPin: echoPin, TriggerPin: triggerPin, Thermometer: thermometer}
|
2013-12-13 01:21:22 +01:00
|
|
|
}
|
|
|
|
|
2014-01-08 23:09:06 +01:00
|
|
|
func (d *US020) setup() (err error) {
|
2013-12-13 01:21:22 +01:00
|
|
|
d.mu.RLock()
|
|
|
|
if d.initialized {
|
|
|
|
d.mu.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
d.mu.RUnlock()
|
|
|
|
|
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
|
2014-03-02 20:21:23 +01:00
|
|
|
d.TriggerPin.SetDirection(embd.Out)
|
|
|
|
d.EchoPin.SetDirection(embd.In)
|
2013-12-13 03:10:25 +01:00
|
|
|
|
2014-01-08 23:09:06 +01:00
|
|
|
if d.Thermometer == nil {
|
|
|
|
d.Thermometer = NullThermometer
|
|
|
|
}
|
|
|
|
|
|
|
|
if temp, err := d.Thermometer.Temperature(); err == nil {
|
|
|
|
d.speedSound = 331.3 + 0.606*temp
|
2013-12-13 03:10:25 +01:00
|
|
|
|
2014-01-08 23:09:06 +01:00
|
|
|
if d.Debug {
|
2013-12-13 03:10:25 +01:00
|
|
|
log.Printf("read a temperature of %v, so speed of sound = %v", temp, d.speedSound)
|
|
|
|
}
|
2014-01-08 23:09:06 +01:00
|
|
|
} else {
|
|
|
|
d.speedSound = 340
|
2013-12-13 01:21:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d.initialized = true
|
|
|
|
|
2014-01-08 23:09:06 +01:00
|
|
|
return
|
2013-12-13 01:21:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Distance computes the distance of the bot from the closest obstruction.
|
2014-01-08 23:09:06 +01:00
|
|
|
func (d *US020) Distance() (distance float64, err error) {
|
2013-12-13 01:21:22 +01:00
|
|
|
if err = d.setup(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-01-08 23:09:06 +01:00
|
|
|
if d.Debug {
|
2014-01-08 22:36:57 +01:00
|
|
|
log.Print("us020: trigerring pulse")
|
2013-12-13 03:10:25 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 01:21:22 +01:00
|
|
|
// Generate a TRIGGER pulse
|
2014-03-02 20:21:23 +01:00
|
|
|
d.TriggerPin.Write(embd.High)
|
2013-12-13 01:21:22 +01:00
|
|
|
time.Sleep(pulseDelay)
|
2014-03-02 20:21:23 +01:00
|
|
|
d.TriggerPin.Write(embd.Low)
|
2013-12-13 01:21:22 +01:00
|
|
|
|
2014-01-08 23:09:06 +01:00
|
|
|
if d.Debug {
|
2014-01-08 22:36:57 +01:00
|
|
|
log.Print("us020: waiting for echo to go high")
|
2013-12-13 03:10:25 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 01:21:22 +01:00
|
|
|
// Wait until ECHO goes high
|
2014-02-26 23:54:53 +01:00
|
|
|
for {
|
|
|
|
v, err := d.EchoPin.Read()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2014-03-02 20:21:23 +01:00
|
|
|
if v != embd.Low {
|
2014-02-26 23:54:53 +01:00
|
|
|
break
|
|
|
|
}
|
2013-12-13 01:21:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
startTime := time.Now() // Record time when ECHO goes high
|
|
|
|
|
2014-01-08 23:09:06 +01:00
|
|
|
if d.Debug {
|
2014-01-08 22:36:57 +01:00
|
|
|
log.Print("us020: waiting for echo to go low")
|
2013-12-13 03:10:25 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 01:21:22 +01:00
|
|
|
// Wait until ECHO goes low
|
2014-02-26 23:54:53 +01:00
|
|
|
for {
|
|
|
|
v, err := d.EchoPin.Read()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2014-03-02 20:21:23 +01:00
|
|
|
if v != embd.High {
|
2014-02-26 23:54:53 +01:00
|
|
|
break
|
|
|
|
}
|
2013-12-13 01:21:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
duration := time.Since(startTime) // Calculate time lapsed for ECHO to transition from high to low
|
|
|
|
|
|
|
|
// Calculate the distance based on the time computed
|
|
|
|
distance = float64(duration.Nanoseconds()) / 10000000 * (d.speedSound / 2)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2014-01-08 15:48:40 +01:00
|
|
|
|
2014-01-08 23:09:06 +01:00
|
|
|
// Close.
|
|
|
|
func (d *US020) Close() {
|
2014-03-02 20:21:23 +01:00
|
|
|
d.EchoPin.SetDirection(embd.Out)
|
2014-01-08 15:48:40 +01:00
|
|
|
}
|