Skip to content

Commit

Permalink
Fix #236 & fix #239 by correcting initialization and temperature conv…
Browse files Browse the repository at this point in the history
…ersion for MPU-6050
  • Loading branch information
deadprogram committed Feb 17, 2016
1 parent 2a80610 commit e570750
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
35 changes: 35 additions & 0 deletions examples/chip_mpu6050.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"fmt"
"time"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/i2c"
"github.com/hybridgroup/gobot/platforms/chip"
)

func main() {
gbot := gobot.NewGobot()

board := chip.NewChipAdaptor("chip")
mpu6050 := i2c.NewMPU6050Driver(board, "mpu6050")

work := func() {
gobot.Every(100*time.Millisecond, func() {
fmt.Println("Accelerometer", mpu6050.Accelerometer)
fmt.Println("Gyroscope", mpu6050.Gyroscope)
fmt.Println("Temperature", mpu6050.Temperature)
})
}

robot := gobot.NewRobot("mpu6050Bot",
[]gobot.Connection{board},
[]gobot.Device{mpu6050},
work,
)

gbot.AddRobot(robot)

gbot.Start()
}
27 changes: 19 additions & 8 deletions platforms/i2c/mpu6050_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const MPU6050_ACONFIG_AFS_SEL_BIT = 4
const MPU6050_ACONFIG_AFS_SEL_LENGTH = 2
const MPU6050_ACCEL_FS_2 = 0x00
const MPU6050_PWR1_SLEEP_BIT = 6
const MPU6050_PWR1_ENABLE_BIT = 0

type ThreeDData struct {
X int16
Expand Down Expand Up @@ -84,8 +85,9 @@ func (h *MPU6050Driver) Start() (errs []error) {
}
buf := bytes.NewBuffer(ret)
binary.Read(buf, binary.BigEndian, &h.Accelerometer)
binary.Read(buf, binary.BigEndian, &h.Gyroscope)
binary.Read(buf, binary.BigEndian, &h.Temperature)
binary.Read(buf, binary.BigEndian, &h.Gyroscope)
h.convertToCelsius()
<-time.After(h.interval)
}
}()
Expand All @@ -109,12 +111,12 @@ func (h *MPU6050Driver) initialize() (err error) {
}

// setFullScaleGyroRange
if err = h.connection.I2cWrite(mpu6050Address, []byte{MPU6050_GYRO_FS_250,
MPU6050_RA_GYRO_CONFIG,
MPU6050_GCONFIG_FS_SEL_LENGTH,
MPU6050_GCONFIG_FS_SEL_BIT}); err != nil {
return
}
if err = h.connection.I2cWrite(mpu6050Address, []byte{MPU6050_RA_GYRO_CONFIG,
MPU6050_GCONFIG_FS_SEL_BIT,
MPU6050_GCONFIG_FS_SEL_LENGTH,
MPU6050_GYRO_FS_250}); err != nil {
return
}

// setFullScaleAccelRange
if err = h.connection.I2cWrite(mpu6050Address, []byte{MPU6050_RA_ACCEL_CONFIG,
Expand All @@ -126,10 +128,19 @@ func (h *MPU6050Driver) initialize() (err error) {

// setSleepEnabled
if err = h.connection.I2cWrite(mpu6050Address, []byte{MPU6050_RA_PWR_MGMT_1,
MPU6050_PWR1_SLEEP_BIT,
MPU6050_PWR1_ENABLE_BIT,
0}); err != nil {
return
}

return nil
}

// The temperature sensor is -40 to +85 degrees Celsius.
// It is a signed integer.
// According to the datasheet:
// 340 per degrees Celsius, -512 at 35 degrees.
// At 0 degrees: -512 - (340 * 35) = -12412
func (h *MPU6050Driver) convertToCelsius() {
h.Temperature = (h.Temperature + 12412) / 340
}

0 comments on commit e570750

Please sign in to comment.