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

l3gd20: its actually the orientation delta

This commit is contained in:
Karan Misra 2014-01-07 20:53:41 +05:30
parent 236cd2fd33
commit 58a1eb5a56
2 changed files with 12 additions and 19 deletions

View File

@ -17,10 +17,10 @@ func main() {
defer gyro.Close() defer gyro.Close()
x, y, z := 0.0, 0.0, 0.0 x, y, z := 0.0, 0.0, 0.0
dt := 0.02 dt := 0.1 // Seconds
for { for {
dx, dy, dz, err := gyro.Orientation() dx, dy, dz, err := gyro.OrientationDelta()
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
@ -29,14 +29,7 @@ func main() {
y += dy * dt y += dy * dt
z += dz * dt z += dz * dt
log.Printf("Orientation is (%v, %v, %v)", x, y, z) log.Printf("%v", z)
temp, err := gyro.Temperature()
if err != nil {
log.Panic(err)
}
log.Printf("Temperature is %v", temp)
time.Sleep(time.Duration(dt*1000) * time.Millisecond) time.Sleep(time.Duration(dt*1000) * time.Millisecond)
} }

View File

@ -90,7 +90,7 @@ var (
// A L3GD20 implements access to the L3GD20 sensor. // A L3GD20 implements access to the L3GD20 sensor.
type L3GD20 interface { type L3GD20 interface {
// Orientation returns the current orientation reading. // Orientation returns the current orientation reading.
Orientation() (x, y, z float64, err error) OrientationDelta() (x, y, z float64, err error)
// Temperature returns the current temperature reading. // Temperature returns the current temperature reading.
Temperature() (temp int, err error) Temperature() (temp int, err error)
@ -114,7 +114,7 @@ func (ac axisCalibration) String() string {
} }
type data struct { type data struct {
x, y, z float64 dx, dy, dz float64
} }
type l3gd20 struct { type l3gd20 struct {
@ -290,28 +290,28 @@ func (d *l3gd20) calibratedOrientation(a *axis) (value float64, err error) {
return return
} }
func (d *l3gd20) measureOrientation() (x, y, z float64, err error) { func (d *l3gd20) measureOrientation() (dx, dy, dz float64, err error) {
if err = d.setup(); err != nil { if err = d.setup(); err != nil {
return return
} }
if x, err = d.calibratedOrientation(ax); err != nil { if dx, err = d.calibratedOrientation(ax); err != nil {
return return
} }
if y, err = d.calibratedOrientation(ay); err != nil { if dy, err = d.calibratedOrientation(ay); err != nil {
return return
} }
if z, err = d.calibratedOrientation(az); err != nil { if dz, err = d.calibratedOrientation(az); err != nil {
return return
} }
return return
} }
func (d *l3gd20) Orientation() (x, y, z float64, err error) { func (d *l3gd20) OrientationDelta() (dx, dy, dz float64, err error) {
select { select {
case data := <-d.orientations: case data := <-d.orientations:
x, y, z = data.x, data.y, data.z dx, dy, dz = data.dx, data.dy, data.dz
return return
default: default:
if d.debug { if d.debug {
@ -350,7 +350,7 @@ func (d *l3gd20) Run() (err error) {
select { select {
case <-timer: case <-timer:
var err error var err error
dt.x, dt.y, dt.z, err = d.measureOrientation() dt.dx, dt.dy, dt.dz, err = d.measureOrientation()
if err == nil && d.orientations == nil { if err == nil && d.orientations == nil {
d.orientations = make(chan data) d.orientations = make(chan data)
} }