1
0
mirror of https://github.com/kidoman/embd synced 2024-06-08 03:57:48 +02:00

l3gd20: clean up

This commit is contained in:
Karan Misra 2014-01-09 05:26:21 +05:30
parent 1b6c346d40
commit 95b06553e5
2 changed files with 23 additions and 27 deletions

View File

@ -4,6 +4,7 @@ import (
"log" "log"
"os" "os"
"os/signal" "os/signal"
"time"
"github.com/kid0m4n/go-rpi/i2c" "github.com/kid0m4n/go-rpi/i2c"
"github.com/kid0m4n/go-rpi/sensor/l3gd20" "github.com/kid0m4n/go-rpi/sensor/l3gd20"
@ -15,7 +16,7 @@ func main() {
log.Panic(err) log.Panic(err)
} }
gyro := l3gd20.New(bus, l3gd20.R250DPS) gyro := l3gd20.New(bus, l3gd20.R250DPS)
gyro.Poll = 50 gyro.Debug = true
defer gyro.Close() defer gyro.Close()
gyro.Start() gyro.Start()
@ -28,9 +29,12 @@ func main() {
log.Panic(err) log.Panic(err)
} }
timer := time.Tick(250 * time.Millisecond)
for { for {
select { select {
case orientation := <-orientations: case <-timer:
orientation := <-orientations
log.Printf("x: %v, y: %v, z: %v", orientation.X, orientation.Y, orientation.Z) log.Printf("x: %v, y: %v, z: %v", orientation.X, orientation.Y, orientation.Z)
case <-quit: case <-quit:
return return

View File

@ -33,22 +33,26 @@ const (
zlReg = 0x2C zlReg = 0x2C
zhReg = 0x2D zhReg = 0x2D
dr95 = 0x00
dr190 = 0x40
dr380 = 0x80
dr760 = 0xC0
xEnabled = 0x01 xEnabled = 0x01
xDisabled = 0x00
yEnabled = 0x02 yEnabled = 0x02
yDisabled = 0x00
zEnabled = 0x04 zEnabled = 0x04
zDisabled = 0x00
powerOn = 0x08 powerOn = 0x08
powerDown = 0x00 powerOff = 0x00
ctrlReg1Default = xEnabled | yEnabled | zEnabled | powerOn ctrlReg1Default = powerOn | xEnabled | yEnabled | zEnabled
ctrlReg1Finished = xDisabled | yDisabled | zDisabled | powerDown ctrlReg1Finished = powerOff | xEnabled | yEnabled | zEnabled
zyxAvailable = 0x08 zyxAvailable = 0x08
pollDelay = 100 odr = 95
mult = 1.0 / odr
pollDelay = mult * 1000 * 1000
) )
// Range represents a L3GD20 range setting. // Range represents a L3GD20 range setting.
@ -111,8 +115,6 @@ type L3GD20 struct {
Bus i2c.Bus Bus i2c.Bus
Range *Range Range *Range
Poll int
initialized bool initialized bool
mu sync.RWMutex mu sync.RWMutex
@ -130,7 +132,6 @@ func New(bus i2c.Bus, Range *Range) *L3GD20 {
return &L3GD20{ return &L3GD20{
Bus: bus, Bus: bus,
Range: Range, Range: Range,
Poll: pollDelay,
Debug: false, Debug: false,
} }
} }
@ -336,30 +337,21 @@ func (d *L3GD20) Start() (err error) {
go func() { go func() {
var x, y, z float64 var x, y, z float64
var orientations chan Orientation var orientations chan Orientation
oldTime := time.Now()
var timer <-chan time.Time timer := time.Tick(time.Duration(math.Floor(pollDelay)) * time.Microsecond)
resetTimer := func() {
timer = time.After(time.Duration(d.Poll) * time.Millisecond)
}
resetTimer()
for { for {
select { select {
case currTime := <-timer: case <-timer:
dx, dy, dz, err := d.measureOrientationDelta() dx, dy, dz, err := d.measureOrientationDelta()
if err != nil { if err != nil {
log.Printf("l3gd20: %v", err) log.Printf("l3gd20: %v", err)
} else { } else {
timeElapsed := currTime.Sub(oldTime)
mult := timeElapsed.Seconds()
x += dx * mult x += dx * mult
y += dy * mult y += dy * mult
z += dz * mult z += dz * mult
orientations = d.orientations orientations = d.orientations
} }
oldTime = currTime
resetTimer()
case orientations <- Orientation{x, y, z}: case orientations <- Orientation{x, y, z}:
orientations = nil orientations = nil
case waitc := <-d.closing: case waitc := <-d.closing: