lsm303agr

package
v0.15.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2021 License: BSD-3-Clause Imports: 2 Imported by: 4

Documentation

Overview

Package lsm303agr implements a driver for the LSM303AGR, a 3 axis accelerometer/magnetic sensor which is included on BBC micro:bits v1.5.

Datasheet: https://www.st.com/resource/en/datasheet/lsm303agr.pdf

Index

Constants

View Source
const (

	// Constants/addresses used for I2C.
	ACCEL_ADDRESS = 0x19
	MAG_ADDRESS   = 0x1E

	// accelerometer registers.
	ACCEL_WHO_AM_I    = 0x0F
	ACCEL_CTRL_REG1_A = 0x20
	ACCEL_CTRL_REG4_A = 0x23
	ACCEL_OUT_X_L_A   = 0x28
	ACCEL_OUT_X_H_A   = 0x29
	ACCEL_OUT_Y_L_A   = 0x2A
	ACCEL_OUT_Y_H_A   = 0x2B
	ACCEL_OUT_Z_L_A   = 0x2C
	ACCEL_OUT_Z_H_A   = 0x2D

	// magnetic sensor registers.
	MAG_WHO_AM_I  = 0x4F
	MAG_MR_REG_M  = 0x60
	MAG_OUT_X_L_M = 0x68
	MAG_OUT_X_H_M = 0x69
	MAG_OUT_Y_L_M = 0x6A
	MAG_OUT_Y_H_M = 0x6B
	MAG_OUT_Z_L_M = 0x6C
	MAG_OUT_Z_H_M = 0x6D

	// temperature sensor registers.
	TEMP_CFG_REG_A = 0x1F
	OUT_TEMP_L_A   = 0x0C
	OUT_TEMP_H_A   = 0x0D

	// accelerometer power mode.
	ACCEL_POWER_NORMAL = 0x00 // default
	ACCEL_POWER_LOW    = 0x08

	// accelerometer range.
	ACCEL_RANGE_2G  = 0x00 // default
	ACCEL_RANGE_4G  = 0x01
	ACCEL_RANGE_8G  = 0x02
	ACCEL_RANGE_16G = 0x03

	// accelerometer data rate.
	ACCEL_DATARATE_1HZ    = 0x01
	ACCEL_DATARATE_10HZ   = 0x02
	ACCEL_DATARATE_25HZ   = 0x03
	ACCEL_DATARATE_50HZ   = 0x04
	ACCEL_DATARATE_100HZ  = 0x05 // default
	ACCEL_DATARATE_200HZ  = 0x06
	ACCEL_DATARATE_400HZ  = 0x07
	ACCEL_DATARATE_1344HZ = 0x09 // 5376Hz in low-power mode

	// magnetic sensor power mode.
	MAG_POWER_NORMAL = 0x00 // default
	MAG_POWER_LOW    = 0x01

	// magnetic sensor operate mode.
	MAG_SYSTEM_CONTINUOUS = 0x00 // default
	MAG_SYSTEM_SINGLE     = 0x01

	// magnetic sensor data rate
	MAG_DATARATE_10HZ  = 0x00 // default
	MAG_DATARATE_20HZ  = 0x01
	MAG_DATARATE_50HZ  = 0x02
	MAG_DATARATE_100HZ = 0x03
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Configuration

type Configuration struct {
	AccelPowerMode uint8
	AccelRange     uint8
	AccelDataRate  uint8
	MagPowerMode   uint8
	MagSystemMode  uint8
	MagDataRate    uint8
}

Configuration for LSM303AGR device.

type Device

type Device struct {
	AccelAddress   uint8
	MagAddress     uint8
	AccelPowerMode uint8
	AccelRange     uint8
	AccelDataRate  uint8
	MagPowerMode   uint8
	MagSystemMode  uint8
	MagDataRate    uint8
	// contains filtered or unexported fields
}

Device wraps an I2C connection to a LSM303AGR device.

func New

func New(bus drivers.I2C) Device

New creates a new LSM303AGR connection. The I2C bus must already be configured.

This function only creates the Device object, it does not touch the device.

func (*Device) Configure

func (d *Device) Configure(cfg Configuration)

Configure sets up the LSM303AGR device for communication.

func (*Device) Connected

func (d *Device) Connected() bool

Connected returns whether both sensor on LSM303AGR has been found. It does two "who am I" requests and checks the responses.

func (*Device) ReadAcceleration

func (d *Device) ReadAcceleration() (x int32, y int32, z int32)

ReadAcceleration reads the current acceleration from the device and returns it in µg (micro-gravity). When one of the axes is pointing straight to Earth and the sensor is not moving the returned value will be around 1000000 or -1000000.

func (*Device) ReadCompass

func (d *Device) ReadCompass() (h int32)

ReadCompass reads the current compass heading from the device and returns it in micro-degrees. When the z axis is pointing straight to Earth and the y axis is pointing to North, the heading would be zero.

However, the heading may be off due to electronic compasses would be effected by strong magnetic fields and require constant calibration.

func (*Device) ReadMagneticField

func (d *Device) ReadMagneticField() (x int32, y int32, z int32)

ReadMagneticField reads the current magnetic field from the device and returns it in mG (milligauss). 1 mG = 0.1 µT (microtesla).

func (*Device) ReadPitchRoll

func (d *Device) ReadPitchRoll() (pitch int32, roll int32)

ReadPitchRoll reads the current pitch and roll angles from the device and returns it in micro-degrees. When the z axis is pointing straight to Earth the returned values of pitch and roll would be zero.

func (*Device) ReadTemperature

func (d *Device) ReadTemperature() (c int32, e error)

ReadTemperature returns the temperature in Celsius milli degrees (°C/1000)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL