2013-12-13 05:51:22 +05:30
|
|
|
// Package us020 allows interfacing with the US020 ultrasonic range finder.
|
|
|
|
package us020
|
|
|
|
|
|
|
|
import (
|
2013-12-13 07:40:25 +05:30
|
|
|
"log"
|
2013-12-13 05:51:22 +05:30
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stianeikeland/go-rpio"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-01-09 03:39:06 +05:30
|
|
|
pulseDelay = 30000 * time.Nanosecond
|
|
|
|
defaultTemp = 25
|
2013-12-13 05:51:22 +05:30
|
|
|
)
|
|
|
|
|
2014-01-09 03:39:06 +05:30
|
|
|
type Thermometer interface {
|
|
|
|
Temperature() (float64, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type nullThermometer struct {
|
|
|
|
}
|
2014-01-08 20:18:40 +05:30
|
|
|
|
2014-01-09 03:39:06 +05:30
|
|
|
func (*nullThermometer) Temperature() (float64, error) {
|
|
|
|
return defaultTemp, nil
|
2013-12-13 05:51:22 +05:30
|
|
|
}
|
|
|
|
|
2014-01-09 03:39:06 +05:30
|
|
|
var NullThermometer = &nullThermometer{}
|
|
|
|
|
|
|
|
// US020 represents a US020 ultrasonic range finder.
|
|
|
|
type US020 struct {
|
|
|
|
EchoPinNumber, TriggerPinNumber int
|
|
|
|
|
|
|
|
Thermometer Thermometer
|
2013-12-13 05:51:22 +05:30
|
|
|
|
|
|
|
echoPin rpio.Pin
|
|
|
|
triggerPin rpio.Pin
|
|
|
|
|
|
|
|
speedSound float64
|
|
|
|
|
|
|
|
initialized bool
|
2014-01-09 03:39:06 +05:30
|
|
|
mu sync.RWMutex
|
2013-12-13 07:40:25 +05:30
|
|
|
|
2014-01-09 03:39:06 +05:30
|
|
|
Debug bool
|
2013-12-13 05:51:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new US020 interface. The bus variable controls
|
|
|
|
// the I2C bus used to communicate with the device.
|
2014-01-09 03:39:06 +05:30
|
|
|
func New(e, t int, thermometer Thermometer) *US020 {
|
|
|
|
return &US020{EchoPinNumber: e, TriggerPinNumber: t, Thermometer: thermometer}
|
2013-12-13 05:51:22 +05:30
|
|
|
}
|
|
|
|
|
2014-01-09 03:39:06 +05:30
|
|
|
func (d *US020) setup() (err error) {
|
2013-12-13 05:51:22 +05:30
|
|
|
d.mu.RLock()
|
|
|
|
if d.initialized {
|
|
|
|
d.mu.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
d.mu.RUnlock()
|
|
|
|
|
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
|
|
|
|
if err = rpio.Open(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-01-09 03:39:06 +05:30
|
|
|
d.echoPin = rpio.Pin(d.EchoPinNumber) // ECHO port on the US020
|
|
|
|
d.triggerPin = rpio.Pin(d.TriggerPinNumber) // TRIGGER port on the US020
|
2013-12-13 05:51:22 +05:30
|
|
|
|
2013-12-13 07:40:25 +05:30
|
|
|
d.echoPin.Input()
|
|
|
|
d.triggerPin.Output()
|
|
|
|
|
2014-01-09 03:39:06 +05:30
|
|
|
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 07:40:25 +05:30
|
|
|
|
2014-01-09 03:39:06 +05:30
|
|
|
if d.Debug {
|
2013-12-13 07:40:25 +05:30
|
|
|
log.Printf("read a temperature of %v, so speed of sound = %v", temp, d.speedSound)
|
|
|
|
}
|
2014-01-09 03:39:06 +05:30
|
|
|
} else {
|
|
|
|
d.speedSound = 340
|
2013-12-13 05:51:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
d.initialized = true
|
|
|
|
|
2014-01-09 03:39:06 +05:30
|
|
|
return
|
2013-12-13 05:51:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Distance computes the distance of the bot from the closest obstruction.
|
2014-01-09 03:39:06 +05:30
|
|
|
func (d *US020) Distance() (distance float64, err error) {
|
2013-12-13 05:51:22 +05:30
|
|
|
if err = d.setup(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-01-09 03:39:06 +05:30
|
|
|
if d.Debug {
|
2014-01-09 03:06:57 +05:30
|
|
|
log.Print("us020: trigerring pulse")
|
2013-12-13 07:40:25 +05:30
|
|
|
}
|
|
|
|
|
2013-12-13 05:51:22 +05:30
|
|
|
// Generate a TRIGGER pulse
|
|
|
|
d.triggerPin.High()
|
|
|
|
time.Sleep(pulseDelay)
|
|
|
|
d.triggerPin.Low()
|
|
|
|
|
2014-01-09 03:39:06 +05:30
|
|
|
if d.Debug {
|
2014-01-09 03:06:57 +05:30
|
|
|
log.Print("us020: waiting for echo to go high")
|
2013-12-13 07:40:25 +05:30
|
|
|
}
|
|
|
|
|
2013-12-13 05:51:22 +05:30
|
|
|
// Wait until ECHO goes high
|
|
|
|
for d.echoPin.Read() == rpio.Low {
|
|
|
|
}
|
|
|
|
|
|
|
|
startTime := time.Now() // Record time when ECHO goes high
|
|
|
|
|
2014-01-09 03:39:06 +05:30
|
|
|
if d.Debug {
|
2014-01-09 03:06:57 +05:30
|
|
|
log.Print("us020: waiting for echo to go low")
|
2013-12-13 07:40:25 +05:30
|
|
|
}
|
|
|
|
|
2013-12-13 05:51:22 +05:30
|
|
|
// Wait until ECHO goes low
|
|
|
|
for d.echoPin.Read() == rpio.High {
|
|
|
|
}
|
|
|
|
|
|
|
|
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 20:18:40 +05:30
|
|
|
|
2014-01-09 03:39:06 +05:30
|
|
|
// Close.
|
|
|
|
func (d *US020) Close() {
|
2014-01-08 20:18:40 +05:30
|
|
|
d.echoPin.Output()
|
|
|
|
rpio.Close()
|
|
|
|
}
|