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
|
|
|
|
}
|
|
|
|
|
2017-02-11 19:34:51 +08:00
|
|
|
// MPU6050Driver is a new Gobot Driver for an MPU6050 I2C Accelerometer/Gyroscope.
|
2014-11-10 07:37:12 +08:00
|
|
|
type MPU6050Driver struct {
|
2017-02-09 18:23:36 +08:00
|
|
|
name string
|
2017-02-10 18:08:32 +08:00
|
|
|
connector Connector
|
|
|
|
connection Connection
|
|
|
|
Config
|
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
|
|
|
}
|
|
|
|
|
2017-02-11 17:37:34 +08:00
|
|
|
// NewMPU6050Driver creates a new Gobot Driver for an MPU6050 I2C Accelerometer/Gyroscope.
|
|
|
|
//
|
2017-02-09 23:47:11 +08:00
|
|
|
// Params:
|
2017-02-10 18:08:32 +08:00
|
|
|
// conn Connector - the Adaptor to use with this Driver
|
2017-02-09 23:47:11 +08:00
|
|
|
//
|
|
|
|
// Optional params:
|
2017-02-10 18:08:32 +08:00
|
|
|
// i2c.WithBus(int): bus to use with this driver
|
|
|
|
// i2c.WithAddress(int): address to use with this driver
|
2017-02-09 23:47:11 +08:00
|
|
|
//
|
2017-02-10 18:08:32 +08:00
|
|
|
func NewMPU6050Driver(a Connector, options ...func(Config)) *MPU6050Driver {
|
2014-11-20 08:56:48 +08:00
|
|
|
m := &MPU6050Driver{
|
2017-02-09 20:26:53 +08:00
|
|
|
name: gobot.DefaultName("MPU6050"),
|
2017-02-06 07:19:42 +08:00
|
|
|
connector: a,
|
2017-02-10 18:08:32 +08:00
|
|
|
Config: NewConfig(),
|
2017-02-06 07:19:42 +08:00
|
|
|
Eventer: gobot.NewEventer(),
|
2014-11-10 07:37:12 +08:00
|
|
|
}
|
2014-11-29 10:37:03 +08:00
|
|
|
|
2017-02-09 20:26:53 +08:00
|
|
|
for _, option := range options {
|
|
|
|
option(m)
|
2014-11-29 10:37:03 +08:00
|
|
|
}
|
|
|
|
|
2017-02-09 20:26:53 +08:00
|
|
|
// TODO: add commands to API
|
2014-11-20 08:56:48 +08:00
|
|
|
return m
|
2014-11-10 07:37:12 +08:00
|
|
|
}
|
|
|
|
|
2017-02-10 18:08:32 +08:00
|
|
|
// Name returns the name of the device.
|
|
|
|
func (h *MPU6050Driver) Name() string { return h.name }
|
|
|
|
|
|
|
|
// SetName sets the name of the device.
|
|
|
|
func (h *MPU6050Driver) SetName(n string) { h.name = n }
|
|
|
|
|
|
|
|
// Connection returns the connection for the device.
|
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
|
|
|
|
2017-02-11 17:37:34 +08:00
|
|
|
// Start writes initialization bytes to sensor
|
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-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
|
|
|
|
2017-02-11 17:37:34 +08:00
|
|
|
// GetData fetches the latest data from the MPU6050
|
|
|
|
func (h *MPU6050Driver) GetData() (err error) {
|
|
|
|
if _, err = h.connection.Write([]byte{MPU6050_RA_ACCEL_XOUT_H}); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data := make([]byte, 14)
|
|
|
|
_, err = h.connection.Read(data)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(data)
|
|
|
|
binary.Read(buf, binary.BigEndian, &h.Accelerometer)
|
|
|
|
binary.Read(buf, binary.BigEndian, &h.Temperature)
|
|
|
|
binary.Read(buf, binary.BigEndian, &h.Gyroscope)
|
|
|
|
h.convertToCelsius()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-11-20 08:56:48 +08:00
|
|
|
func (h *MPU6050Driver) initialize() (err error) {
|
2017-02-10 18:08:32 +08:00
|
|
|
bus := h.GetBusOrDefault(h.connector.GetDefaultBus())
|
|
|
|
address := h.GetAddressOrDefault(mpu6050Address)
|
2017-02-09 20:26:53 +08:00
|
|
|
|
2017-02-10 18:08:32 +08:00
|
|
|
h.connection, err = h.connector.GetConnection(address, bus)
|
2017-02-06 07:19:42 +08:00
|
|
|
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
|
|
|
|
}
|