1
0
mirror of https://github.com/kidoman/embd synced 2024-12-22 12:50:19 +01:00

us020: decouple from bmp085 and default to 25 C when we cannot detect

the ambient temperature
This commit is contained in:
Karan Misra 2014-01-09 03:39:06 +05:30
parent 1b5a3a40c3
commit 1b6c346d40
2 changed files with 44 additions and 32 deletions

View File

@ -8,11 +8,11 @@ import (
) )
func main() { func main() {
rangeFinder := us020.New(10, 9) rf := us020.New(10, 9, nil)
defer rangeFinder.Close() defer rf.Close()
for { for {
distance, err := rangeFinder.Distance() distance, err := rf.Distance()
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }

View File

@ -6,24 +6,32 @@ import (
"sync" "sync"
"time" "time"
"github.com/kid0m4n/go-rpi/sensor/bmp085"
"github.com/stianeikeland/go-rpio" "github.com/stianeikeland/go-rpio"
) )
const ( const (
pulseDelay = 30000 * time.Nanosecond pulseDelay = 30000 * time.Nanosecond
defaultTemp = 25
) )
// A US020 implements access to a US020 ultrasonic range finder. type Thermometer interface {
type US020 interface { Temperature() (float64, error)
// Distance computes the distance of the bot from the closest obstruction.
Distance() (float64, error)
Close()
} }
type us020 struct { type nullThermometer struct {
echoPinNumber, triggerPinNumber int }
func (*nullThermometer) Temperature() (float64, error) {
return defaultTemp, nil
}
var NullThermometer = &nullThermometer{}
// US020 represents a US020 ultrasonic range finder.
type US020 struct {
EchoPinNumber, TriggerPinNumber int
Thermometer Thermometer
echoPin rpio.Pin echoPin rpio.Pin
triggerPin rpio.Pin triggerPin rpio.Pin
@ -31,18 +39,18 @@ type us020 struct {
speedSound float64 speedSound float64
initialized bool initialized bool
mu *sync.RWMutex mu sync.RWMutex
debug bool Debug bool
} }
// New creates a new US020 interface. The bus variable controls // New creates a new US020 interface. The bus variable controls
// the I2C bus used to communicate with the device. // the I2C bus used to communicate with the device.
func New(e, t int) US020 { func New(e, t int, thermometer Thermometer) *US020 {
return &us020{echoPinNumber: e, triggerPinNumber: t, mu: new(sync.RWMutex)} return &US020{EchoPinNumber: e, TriggerPinNumber: t, Thermometer: thermometer}
} }
func (d *us020) setup() (err error) { func (d *US020) setup() (err error) {
d.mu.RLock() d.mu.RLock()
if d.initialized { if d.initialized {
d.mu.RUnlock() d.mu.RUnlock()
@ -57,35 +65,38 @@ func (d *us020) setup() (err error) {
return return
} }
d.echoPin = rpio.Pin(d.echoPinNumber) // ECHO port on the US020 d.echoPin = rpio.Pin(d.EchoPinNumber) // ECHO port on the US020
d.triggerPin = rpio.Pin(d.triggerPinNumber) // TRIGGER port on the US020 d.triggerPin = rpio.Pin(d.TriggerPinNumber) // TRIGGER port on the US020
d.echoPin.Input() d.echoPin.Input()
d.triggerPin.Output() d.triggerPin.Output()
temp, err := bmp085.Temperature() if d.Thermometer == nil {
if err != nil { d.Thermometer = NullThermometer
d.speedSound = 340 }
} else {
d.speedSound = 331.4 + 0.606*temp
if d.debug { if temp, err := d.Thermometer.Temperature(); err == nil {
d.speedSound = 331.3 + 0.606*temp
if d.Debug {
log.Printf("read a temperature of %v, so speed of sound = %v", temp, d.speedSound) log.Printf("read a temperature of %v, so speed of sound = %v", temp, d.speedSound)
} }
} else {
d.speedSound = 340
} }
d.initialized = true d.initialized = true
return nil return
} }
// Distance computes the distance of the bot from the closest obstruction. // Distance computes the distance of the bot from the closest obstruction.
func (d *us020) Distance() (distance float64, err error) { func (d *US020) Distance() (distance float64, err error) {
if err = d.setup(); err != nil { if err = d.setup(); err != nil {
return return
} }
if d.debug { if d.Debug {
log.Print("us020: trigerring pulse") log.Print("us020: trigerring pulse")
} }
@ -94,7 +105,7 @@ func (d *us020) Distance() (distance float64, err error) {
time.Sleep(pulseDelay) time.Sleep(pulseDelay)
d.triggerPin.Low() d.triggerPin.Low()
if d.debug { if d.Debug {
log.Print("us020: waiting for echo to go high") log.Print("us020: waiting for echo to go high")
} }
@ -104,7 +115,7 @@ func (d *us020) Distance() (distance float64, err error) {
startTime := time.Now() // Record time when ECHO goes high startTime := time.Now() // Record time when ECHO goes high
if d.debug { if d.Debug {
log.Print("us020: waiting for echo to go low") log.Print("us020: waiting for echo to go low")
} }
@ -120,7 +131,8 @@ func (d *us020) Distance() (distance float64, err error) {
return return
} }
func (d *us020) Close() { // Close.
func (d *US020) Close() {
d.echoPin.Output() d.echoPin.Output()
rpio.Close() rpio.Close()
} }