|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2017 Tony DiCola for Adafruit Industries |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +# THE SOFTWARE. |
| 22 | +""" |
| 23 | +`adafruit_fxas21002c` |
| 24 | +==================================================== |
| 25 | +
|
| 26 | +CircuitPython module for the NXP FXAS21002C gyroscope. Based on the driver |
| 27 | +from: |
| 28 | + https://github.com/adafruit/Adafruit_FXAS21002C |
| 29 | +
|
| 30 | +See examples/simpletest.py for a demo of the usage. |
| 31 | +
|
| 32 | +* Author(s): Tony DiCola |
| 33 | +""" |
| 34 | +import time |
| 35 | + |
| 36 | +import adafruit_bus_device.i2c_device as i2c_device |
| 37 | + |
| 38 | + |
| 39 | +# Internal constants and register values: |
| 40 | +_FXAS21002C_ADDRESS = const(0x21) # 0100001 |
| 41 | +_FXAS21002C_ID = const(0xD7) # 1101 0111 |
| 42 | +_GYRO_REGISTER_STATUS = const(0x00) |
| 43 | +_GYRO_REGISTER_OUT_X_MSB = const(0x01) |
| 44 | +_GYRO_REGISTER_OUT_X_LSB = const(0x02) |
| 45 | +_GYRO_REGISTER_OUT_Y_MSB = const(0x03) |
| 46 | +_GYRO_REGISTER_OUT_Y_LSB = const(0x04) |
| 47 | +_GYRO_REGISTER_OUT_Z_MSB = const(0x05) |
| 48 | +_GYRO_REGISTER_OUT_Z_LSB = const(0x06) |
| 49 | +_GYRO_REGISTER_WHO_AM_I = const(0x0C) # 11010111 r |
| 50 | +_GYRO_REGISTER_CTRL_REG0 = const(0x0D) # 00000000 r/w |
| 51 | +_GYRO_REGISTER_CTRL_REG1 = const(0x13) # 00000000 r/w |
| 52 | +_GYRO_REGISTER_CTRL_REG2 = const(0x14) # 00000000 r/w |
| 53 | +_GYRO_SENSITIVITY_250DPS = 0.0078125 # Table 35 of datasheet |
| 54 | +_GYRO_SENSITIVITY_500DPS = 0.015625 # .. |
| 55 | +_GYRO_SENSITIVITY_1000DPS = 0.03125 # .. |
| 56 | +_GYRO_SENSITIVITY_2000DPS = 0.0625 # .. |
| 57 | + |
| 58 | +# User facing constants/module globals: |
| 59 | +GYRO_RANGE_250DPS = 250 |
| 60 | +GYRO_RANGE_500DPS = 500 |
| 61 | +GYRO_RANGE_1000DPS = 1000 |
| 62 | +GYRO_RANGE_2000DPS = 2000 |
| 63 | + |
| 64 | +class FXAS21002C: |
| 65 | + |
| 66 | + # Class-level buffer for reading and writing data with the sensor. |
| 67 | + # This reduces memory allocations but means the code is not re-entrant or |
| 68 | + # thread safe! |
| 69 | + _BUFFER = bytearray(7) |
| 70 | + |
| 71 | + def __init__(self, i2c, address=_FXAS21002C_ADDRESS, |
| 72 | + gyro_range=GYRO_RANGE_250DPS): |
| 73 | + assert gyro_range in (GYRO_RANGE_250DPS, GYRO_RANGE_500DPS, |
| 74 | + GYRO_RANGE_100DPS, GYRO_RANGE_2000DPS) |
| 75 | + self._gyro_range = gyro_range |
| 76 | + self._device = i2c_device.I2CDevice(i2c, address) |
| 77 | + # Check for chip ID value. |
| 78 | + if self._read_u8(_GYRO_REGISTER_WHO_AM_I) != _FXAS21002C_ID: |
| 79 | + raise RuntimeError('Failed to find FXAS21002C, check wiring!') |
| 80 | + ctrlReg0 = 0x00 |
| 81 | + if gyro_range == GYRO_RANGE_250DPS: |
| 82 | + ctrlReg0 = 0x03 |
| 83 | + elif gyro_range == GYRO_RANGE_500DPS: |
| 84 | + ctrlReg0 = 0x02 |
| 85 | + elif gyro_range == GYRO_RANGE_1000DPS: |
| 86 | + ctrlReg0 = 0x01 |
| 87 | + elif gyro_range == GYRO_RANGE_2000DPS: |
| 88 | + ctrlReg0 = 0x00 |
| 89 | + # Reset then switch to active mode with 100Hz output |
| 90 | + self._write_u8(_GYRO_REGISTER_CTRL_REG1, 0x00) # Standby |
| 91 | + self._write_u8(_GYRO_REGISTER_CTRL_REG1, (1<<6)) # Reset |
| 92 | + self._write_u8(_GYRO_REGISTER_CTRL_REG0, ctrlReg0) # Set sensitivity |
| 93 | + self._write_u8(_GYRO_REGISTER_CTRL_REG1, 0x0E) # Active |
| 94 | + time.sleep(0.1) # 60 ms + 1/ODR |
| 95 | + |
| 96 | + def _read_u8(self, address): |
| 97 | + # Read an 8-bit unsigned value from the specified 8-bit address. |
| 98 | + with self._device: |
| 99 | + self._BUFFER[0] = address & 0xFF |
| 100 | + self._device.write(self._BUFFER, end=1) |
| 101 | + self._device.readinto(self._BUFFER, end=1) |
| 102 | + return self._BUFFER[0] |
| 103 | + |
| 104 | + def _write_u8(self, address, val): |
| 105 | + # Write an 8-bit unsigned value to the specified 8-bit address. |
| 106 | + with self._device: |
| 107 | + self._BUFFER[0] = address & 0xFF |
| 108 | + self._BUFFER[1] = val & 0xFF |
| 109 | + self._device.write(self._BUFFER, end=2) |
| 110 | + |
| 111 | + def read_raw(self): |
| 112 | + """Read the raw gyroscope readings. Returns a 3-tuple of X, Y, Z axis |
| 113 | + 16-bit unsigned values. If you want the gyroscope values in friendly |
| 114 | + units consider using the gyroscope property! |
| 115 | + """ |
| 116 | + # Read 7 bytes from the sensor. |
| 117 | + with self._device: |
| 118 | + self._BUFFER[0] = GYRO_REGISTER_STATUS | 0x80 |
| 119 | + self._device.write(self._BUFFER, end=1) |
| 120 | + self._device.readinto(self._BUFFER) |
| 121 | + # Parse out the gyroscope data. |
| 122 | + status = self._BUFFER[0] |
| 123 | + xhi = self._BUFFER[1] |
| 124 | + xlo = self._BUFFER[2] |
| 125 | + yhi = self._BUFFER[3] |
| 126 | + ylo = self._BUFFER[4] |
| 127 | + zhi = self._BUFFER[5] |
| 128 | + zlo = self._BUFFER[6] |
| 129 | + # Shift values to create properly formed integers |
| 130 | + raw_x = ((xhi << 8) | xlo) & 0xFFFF |
| 131 | + raw_y = ((yhi << 8) | ylo) & 0xFFFF |
| 132 | + raw_z = ((zhi << 8) | zlo) & 0xFFFF |
| 133 | + return (raw_x, raw_y, raw_z) |
| 134 | + |
| 135 | + @property |
| 136 | + def gyroscope(self): |
| 137 | + """Read the gyroscope value and return its X, Y, Z axis values as a |
| 138 | + 3-tuple in radians/second. |
| 139 | + """ |
| 140 | + raw = self.read_raw() |
| 141 | + # Compensate values depending on the resolution |
| 142 | + if self._gyro_range == GYRO_RANGE_250DPS: |
| 143 | + return map(lambda x: x * _GYRO_SENSITIVITY_250DPS, raw) |
| 144 | + elif self._gyro_range == GYRO_RANGE_500DPS: |
| 145 | + return map(lambda x: x * _GYRO_SENSITIVITY_500DPS, raw) |
| 146 | + elif self._gyro_range == GYRO_RANGE_1000DPS: |
| 147 | + return map(lambda x: x * _GYRO_SENSITIVITY_1000DPS, raw) |
| 148 | + elif self._gyro_range == GYRO_RANGE_2000DPS: |
| 149 | + return map(lambda x: x * _GYRO_SENSITIVITY_2000DPS, raw) |
0 commit comments