|
| 1 | +""" |
| 2 | +The MIT License (MIT) |
| 3 | +
|
| 4 | +Copyright (c) 2013, 2014 Damien P. George |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in |
| 14 | +all copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +THE SOFTWARE. |
| 23 | +
|
| 24 | +
|
| 25 | +LSM9DS1 - 9DOF inertial sensor of STMicro driver for MicroPython. |
| 26 | +The sensor contains an accelerometer / gyroscope / magnetometer |
| 27 | +Uses the internal FIFO to store up to 16 gyro/accel data, use the iter_accel_gyro generator to access it. |
| 28 | +
|
| 29 | +Example usage: |
| 30 | +
|
| 31 | +import time |
| 32 | +from lsm9ds1 import LSM9DS1 |
| 33 | +from machine import Pin, I2C |
| 34 | +
|
| 35 | +lsm = LSM9DS1(I2C(1, scl=Pin(15), sda=Pin(14))) |
| 36 | +
|
| 37 | +while (True): |
| 38 | + #for g,a in lsm.iter_accel_gyro(): print(g,a) # using fifo |
| 39 | + print('Accelerometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.accel())) |
| 40 | + print('Magnetometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.magnet())) |
| 41 | + print('Gyroscope: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.gyro())) |
| 42 | + print("") |
| 43 | + time.sleep_ms(100) |
| 44 | +""" |
| 45 | +import array |
| 46 | + |
| 47 | + |
| 48 | +_WHO_AM_I = const(0xF) |
| 49 | +_CTRL_REG1_G = const(0x10) |
| 50 | +_INT_GEN_SRC_G = const(0x14) |
| 51 | +_OUT_TEMP = const(0x15) |
| 52 | +_OUT_G = const(0x18) |
| 53 | +_CTRL_REG4_G = const(0x1E) |
| 54 | +_STATUS_REG = const(0x27) |
| 55 | +_OUT_XL = const(0x28) |
| 56 | +_FIFO_CTRL_REG = const(0x2E) |
| 57 | +_FIFO_SRC = const(0x2F) |
| 58 | +_OFFSET_REG_X_M = const(0x05) |
| 59 | +_CTRL_REG1_M = const(0x20) |
| 60 | +_OUT_M = const(0x28) |
| 61 | +_SCALE_GYRO = const(((245, 0), (500, 1), (2000, 3))) |
| 62 | +_SCALE_ACCEL = const(((2, 0), (4, 2), (8, 3), (16, 1))) |
| 63 | + |
| 64 | + |
| 65 | +class LSM9DS1: |
| 66 | + def __init__(self, i2c, address_gyro=0x6B, address_magnet=0x1E): |
| 67 | + self.i2c = i2c |
| 68 | + self.address_gyro = address_gyro |
| 69 | + self.address_magnet = address_magnet |
| 70 | + # check id's of accelerometer/gyro and magnetometer |
| 71 | + if (self.magent_id() != b"=") or (self.gyro_id() != b"h"): |
| 72 | + raise OSError( |
| 73 | + "Invalid LSM9DS1 device, using address {}/{}".format(address_gyro, address_magnet) |
| 74 | + ) |
| 75 | + # allocate scratch buffer for efficient conversions and memread op's |
| 76 | + self.scratch = array.array("B", [0, 0, 0, 0, 0, 0]) |
| 77 | + self.scratch_int = array.array("h", [0, 0, 0]) |
| 78 | + self.init_gyro_accel() |
| 79 | + self.init_magnetometer() |
| 80 | + |
| 81 | + def init_gyro_accel(self, sample_rate=6, scale_gyro=0, scale_accel=0): |
| 82 | + """Initalizes Gyro and Accelerator. |
| 83 | + sample rate: 0-6 (off, 14.9Hz, 59.5Hz, 119Hz, 238Hz, 476Hz, 952Hz) |
| 84 | + scale_gyro: 0-2 (245dps, 500dps, 2000dps ) |
| 85 | + scale_accel: 0-3 (+/-2g, +/-4g, +/-8g, +-16g) |
| 86 | + """ |
| 87 | + assert sample_rate <= 6, "invalid sampling rate: %d" % sample_rate |
| 88 | + assert scale_gyro <= 2, "invalid gyro scaling: %d" % scale_gyro |
| 89 | + assert scale_accel <= 3, "invalid accelerometer scaling: %d" % scale_accel |
| 90 | + |
| 91 | + i2c = self.i2c |
| 92 | + addr = self.address_gyro |
| 93 | + mv = memoryview(self.scratch) |
| 94 | + # angular control registers 1-3 / Orientation |
| 95 | + mv[0] = ((sample_rate & 0x07) << 5) | ((_SCALE_GYRO[scale_gyro][1] & 0x3) << 3) |
| 96 | + mv[1:4] = b"\x00\x00\x00" |
| 97 | + i2c.writeto_mem(addr, _CTRL_REG1_G, mv[:5]) |
| 98 | + # ctrl4 - enable x,y,z, outputs, no irq latching, no 4D |
| 99 | + # ctrl5 - enable all axes, no decimation |
| 100 | + # ctrl6 - set scaling and sample rate of accel |
| 101 | + # ctrl7,8 - leave at default values |
| 102 | + # ctrl9 - FIFO enabled |
| 103 | + mv[0] = mv[1] = 0x38 |
| 104 | + mv[2] = ((sample_rate & 7) << 5) | ((_SCALE_ACCEL[scale_accel][1] & 0x3) << 3) |
| 105 | + mv[3] = 0x00 |
| 106 | + mv[4] = 0x4 |
| 107 | + mv[5] = 0x2 |
| 108 | + i2c.writeto_mem(addr, _CTRL_REG4_G, mv[:6]) |
| 109 | + |
| 110 | + # fifo: use continous mode (overwrite old data if overflow) |
| 111 | + i2c.writeto_mem(addr, _FIFO_CTRL_REG, b"\x00") |
| 112 | + i2c.writeto_mem(addr, _FIFO_CTRL_REG, b"\xc0") |
| 113 | + |
| 114 | + self.scale_gyro = 32768 / _SCALE_GYRO[scale_gyro][0] |
| 115 | + self.scale_accel = 32768 / _SCALE_ACCEL[scale_accel][0] |
| 116 | + |
| 117 | + def init_magnetometer(self, sample_rate=7, scale_magnet=0): |
| 118 | + """ |
| 119 | + sample rates = 0-7 (0.625, 1.25, 2.5, 5, 10, 20, 40, 80Hz) |
| 120 | + scaling = 0-3 (+/-4, +/-8, +/-12, +/-16 Gauss) |
| 121 | + """ |
| 122 | + assert sample_rate < 8, "invalid sample rate: %d (0-7)" % sample_rate |
| 123 | + assert scale_magnet < 4, "invalid scaling: %d (0-3)" % scale_magnet |
| 124 | + i2c = self.i2c |
| 125 | + addr = self.address_magnet |
| 126 | + mv = memoryview(self.scratch) |
| 127 | + mv[0] = 0x40 | (sample_rate << 2) # ctrl1: high performance mode |
| 128 | + mv[1] = scale_magnet << 5 # ctrl2: scale, normal mode, no reset |
| 129 | + mv[2] = 0x00 # ctrl3: continous conversion, no low power, I2C |
| 130 | + mv[3] = 0x08 # ctrl4: high performance z-axis |
| 131 | + mv[4] = 0x00 # ctr5: no fast read, no block update |
| 132 | + i2c.writeto_mem(addr, _CTRL_REG1_M, mv[:5]) |
| 133 | + self.scale_factor_magnet = 32768 / ((scale_magnet + 1) * 4) |
| 134 | + |
| 135 | + def calibrate_magnet(self, offset): |
| 136 | + """ |
| 137 | + offset is a magnet vecor that will be substracted by the magnetometer |
| 138 | + for each measurement. It is written to the magnetometer's offset register |
| 139 | + """ |
| 140 | + offset = [int(i * self.scale_factor_magnet) for i in offset] |
| 141 | + mv = memoryview(self.scratch) |
| 142 | + mv[0] = offset[0] & 0xFF |
| 143 | + mv[1] = offset[0] >> 8 |
| 144 | + mv[2] = offset[1] & 0xFF |
| 145 | + mv[3] = offset[1] >> 8 |
| 146 | + mv[4] = offset[2] & 0xFF |
| 147 | + mv[5] = offset[2] >> 8 |
| 148 | + self.i2c.writeto_mem(self.address_magnet, _OFFSET_REG_X_M, mv[:6]) |
| 149 | + |
| 150 | + def gyro_id(self): |
| 151 | + return self.i2c.readfrom_mem(self.address_gyro, _WHO_AM_I, 1) |
| 152 | + |
| 153 | + def magent_id(self): |
| 154 | + return self.i2c.readfrom_mem(self.address_magnet, _WHO_AM_I, 1) |
| 155 | + |
| 156 | + def magnet(self): |
| 157 | + """Returns magnetometer vector in gauss. |
| 158 | + raw_values: if True, the non-scaled adc values are returned |
| 159 | + """ |
| 160 | + mv = memoryview(self.scratch_int) |
| 161 | + f = self.scale_factor_magnet |
| 162 | + self.i2c.readfrom_mem_into(self.address_magnet, _OUT_M | 0x80, mv) |
| 163 | + return (mv[0] / f, mv[1] / f, mv[2] / f) |
| 164 | + |
| 165 | + def gyro(self): |
| 166 | + """Returns gyroscope vector in degrees/sec.""" |
| 167 | + mv = memoryview(self.scratch_int) |
| 168 | + f = self.scale_gyro |
| 169 | + self.i2c.readfrom_mem_into(self.address_gyro, _OUT_G | 0x80, mv) |
| 170 | + return (mv[0] / f, mv[1] / f, mv[2] / f) |
| 171 | + |
| 172 | + def accel(self): |
| 173 | + """Returns acceleration vector in gravity units (9.81m/s^2).""" |
| 174 | + mv = memoryview(self.scratch_int) |
| 175 | + f = self.scale_accel |
| 176 | + self.i2c.readfrom_mem_into(self.address_gyro, _OUT_XL | 0x80, mv) |
| 177 | + return (mv[0] / f, mv[1] / f, mv[2] / f) |
| 178 | + |
| 179 | + def iter_accel_gyro(self): |
| 180 | + """A generator that returns tuples of (gyro,accelerometer) data from the fifo.""" |
| 181 | + while True: |
| 182 | + fifo_state = int.from_bytes( |
| 183 | + self.i2c.readfrom_mem(self.address_gyro, _FIFO_SRC, 1), "big" |
| 184 | + ) |
| 185 | + if fifo_state & 0x3F: |
| 186 | + # print("Available samples=%d" % (fifo_state & 0x1f)) |
| 187 | + yield self.gyro(), self.accel() |
| 188 | + else: |
| 189 | + break |
0 commit comments