2014-11-10 07:37:12 +08:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2014-11-23 11:38:39 +08:00
|
|
|
"time"
|
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
2014-11-10 07:37:12 +08:00
|
|
|
)
|
|
|
|
|
2015-07-04 09:57:29 +08:00
|
|
|
const mpu6050Address = 0x68
|
|
|
|
|
2014-11-10 07:37:12 +08:00
|
|
|
const MPU6050_RA_ACCEL_XOUT_H = 0x3B
|
|
|
|
const MPU6050_RA_PWR_MGMT_1 = 0x6B
|
|
|
|
const MPU6050_PWR1_CLKSEL_BIT = 2
|
|
|
|
const MPU6050_PWR1_CLKSEL_LENGTH = 3
|
|
|
|
const MPU6050_CLOCK_PLL_XGYRO = 0x01
|
|
|
|
const MPU6050_GYRO_FS_250 = 0x00
|
|
|
|
const MPU6050_RA_GYRO_CONFIG = 0x1B
|
|
|
|
const MPU6050_GCONFIG_FS_SEL_LENGTH = 2
|
|
|
|
const MPU6050_GCONFIG_FS_SEL_BIT = 4
|
|
|
|
const MPU6050_RA_ACCEL_CONFIG = 0x1C
|
|
|
|
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
|
2016-02-17 09:55:07 +08:00
|
|
|
const MPU6050_PWR1_ENABLE_BIT = 0
|
2014-11-10 07:37:12 +08:00
|
|
|
|
|
|
|
type ThreeDData struct {
|
|
|
|
X int16
|
|
|
|
Y int16
|
|
|
|
Z int16
|
|
|
|
}
|
|
|
|
|
|
|
|
type MPU6050Driver struct {
|
2017-02-09 18:23:36 +08:00
|
|
|
name string
|
|
|
|
connector I2cConnector
|
|
|
|
connection I2cConnection
|
|
|
|
I2cBusser
|
2014-11-29 10:37:03 +08:00
|
|
|
interval time.Duration
|
2014-11-10 07:37:12 +08:00
|
|
|
Accelerometer ThreeDData
|
|
|
|
Gyroscope ThreeDData
|
|
|
|
Temperature int16
|
2014-11-29 10:37:03 +08:00
|
|
|
gobot.Eventer
|
2014-11-10 07:37:12 +08:00
|
|
|
}
|
|
|
|
|
2016-09-25 20:08:18 +08:00
|
|
|
// NewMPU6050Driver creates a new driver with specified i2c interface
|
2017-02-06 07:19:42 +08:00
|
|
|
func NewMPU6050Driver(a I2cConnector, v ...time.Duration) *MPU6050Driver {
|
2014-11-20 08:56:48 +08:00
|
|
|
m := &MPU6050Driver{
|
2017-02-06 07:19:42 +08:00
|
|
|
name: gobot.DefaultName("MPM6050"),
|
|
|
|
connector: a,
|
|
|
|
interval: 10 * time.Millisecond,
|
|
|
|
Eventer: gobot.NewEventer(),
|
2014-11-10 07:37:12 +08:00
|
|
|
}
|
2014-11-29 10:37:03 +08:00
|
|
|
|
|
|
|
if len(v) > 0 {
|
|
|
|
m.interval = v[0]
|
|
|
|
}
|
|
|
|
|
2014-11-30 04:10:23 +08:00
|
|
|
m.AddEvent(Error)
|
2014-11-20 08:56:48 +08:00
|
|
|
return m
|
2014-11-10 07:37:12 +08:00
|
|
|
}
|
|
|
|
|
2014-11-23 11:38:39 +08:00
|
|
|
func (h *MPU6050Driver) Name() string { return h.name }
|
2016-09-25 20:08:18 +08:00
|
|
|
func (h *MPU6050Driver) SetName(n string) { h.name = n }
|
2017-02-06 07:19:42 +08:00
|
|
|
func (h *MPU6050Driver) Connection() gobot.Connection { return h.connector.(gobot.Connection) }
|
2014-11-10 07:37:12 +08:00
|
|
|
|
|
|
|
// Start writes initialization bytes and reads from adaptor
|
|
|
|
// using specified interval to accelerometer andtemperature data
|
2016-11-07 21:55:21 +08:00
|
|
|
func (h *MPU6050Driver) Start() (err error) {
|
2014-11-20 15:21:19 +08:00
|
|
|
if err := h.initialize(); err != nil {
|
2016-11-07 21:55:21 +08:00
|
|
|
return err
|
2014-11-20 08:56:48 +08:00
|
|
|
}
|
2014-11-10 07:37:12 +08:00
|
|
|
|
2014-12-23 05:36:52 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
2017-02-06 07:19:42 +08:00
|
|
|
if _, err := h.connection.Write([]byte{MPU6050_RA_ACCEL_XOUT_H}); err != nil {
|
2016-08-30 20:22:09 +08:00
|
|
|
h.Publish(h.Event(Error), err)
|
2014-12-23 05:36:52 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
data := make([]byte, 14)
|
|
|
|
_, err := h.connection.Read(data)
|
2014-12-23 05:36:52 +08:00
|
|
|
if err != nil {
|
2016-08-30 20:22:09 +08:00
|
|
|
h.Publish(h.Event(Error), err)
|
2014-12-23 05:36:52 +08:00
|
|
|
continue
|
|
|
|
}
|
2017-02-06 07:19:42 +08:00
|
|
|
buf := bytes.NewBuffer(data)
|
2014-12-23 05:36:52 +08:00
|
|
|
binary.Read(buf, binary.BigEndian, &h.Accelerometer)
|
|
|
|
binary.Read(buf, binary.BigEndian, &h.Temperature)
|
2016-02-17 09:55:07 +08:00
|
|
|
binary.Read(buf, binary.BigEndian, &h.Gyroscope)
|
|
|
|
h.convertToCelsius()
|
2016-11-05 20:05:49 +08:00
|
|
|
time.Sleep(h.interval)
|
2014-11-20 08:56:48 +08:00
|
|
|
}
|
2014-12-23 05:36:52 +08:00
|
|
|
}()
|
2014-11-20 15:21:19 +08:00
|
|
|
return
|
2014-11-10 07:37:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Halt returns true if devices is halted successfully
|
2016-11-07 21:55:21 +08:00
|
|
|
func (h *MPU6050Driver) Halt() (err error) { return }
|
2014-11-10 07:37:12 +08:00
|
|
|
|
2014-11-20 08:56:48 +08:00
|
|
|
func (h *MPU6050Driver) initialize() (err error) {
|
2017-02-06 07:19:42 +08:00
|
|
|
bus := h.connector.I2cGetDefaultBus()
|
|
|
|
h.connection, err = h.connector.I2cGetConnection(mpu6050Address, bus)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-11-20 08:56:48 +08:00
|
|
|
}
|
2014-11-10 07:37:12 +08:00
|
|
|
|
|
|
|
// setClockSource
|
2017-02-06 07:19:42 +08:00
|
|
|
if _, err = h.connection.Write([]byte{MPU6050_RA_PWR_MGMT_1,
|
2014-11-10 07:37:12 +08:00
|
|
|
MPU6050_PWR1_CLKSEL_BIT,
|
|
|
|
MPU6050_PWR1_CLKSEL_LENGTH,
|
2014-11-20 08:56:48 +08:00
|
|
|
MPU6050_CLOCK_PLL_XGYRO}); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-11-10 07:37:12 +08:00
|
|
|
|
|
|
|
// setFullScaleGyroRange
|
2017-02-06 07:19:42 +08:00
|
|
|
if _, err = h.connection.Write([]byte{MPU6050_RA_GYRO_CONFIG,
|
2016-08-30 20:22:09 +08:00
|
|
|
MPU6050_GCONFIG_FS_SEL_BIT,
|
|
|
|
MPU6050_GCONFIG_FS_SEL_LENGTH,
|
|
|
|
MPU6050_GYRO_FS_250}); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-11-10 07:37:12 +08:00
|
|
|
|
|
|
|
// setFullScaleAccelRange
|
2017-02-06 07:19:42 +08:00
|
|
|
if _, err = h.connection.Write([]byte{MPU6050_RA_ACCEL_CONFIG,
|
2014-11-10 07:37:12 +08:00
|
|
|
MPU6050_ACONFIG_AFS_SEL_BIT,
|
|
|
|
MPU6050_ACONFIG_AFS_SEL_LENGTH,
|
2014-11-20 08:56:48 +08:00
|
|
|
MPU6050_ACCEL_FS_2}); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-11-10 07:37:12 +08:00
|
|
|
|
|
|
|
// setSleepEnabled
|
2017-02-06 07:19:42 +08:00
|
|
|
if _, err = h.connection.Write([]byte{MPU6050_RA_PWR_MGMT_1,
|
2016-02-17 09:55:07 +08:00
|
|
|
MPU6050_PWR1_ENABLE_BIT,
|
2014-11-20 08:56:48 +08:00
|
|
|
0}); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-11-10 07:37:12 +08:00
|
|
|
|
2014-11-20 08:56:48 +08:00
|
|
|
return nil
|
2014-11-10 07:37:12 +08:00
|
|
|
}
|
2016-02-17 09:55:07 +08:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|