|
| 1 | +import threading |
| 2 | +import time |
| 3 | +import board |
| 4 | +import busio |
| 5 | +from adafruit_lsm6ds import LSM6DSOX |
| 6 | +from adafruit_lis3mdl import LIS3MDL |
| 7 | + |
| 8 | +SAMPLE_SIZE = 500 |
| 9 | + |
| 10 | + |
| 11 | +class KeyListener: |
| 12 | + """Object for listening for input in a separate thread""" |
| 13 | + |
| 14 | + def __init__(self): |
| 15 | + self._input_key = None |
| 16 | + self._listener_thread = None |
| 17 | + |
| 18 | + def _key_listener(self): |
| 19 | + while True: |
| 20 | + self._input_key = input() |
| 21 | + |
| 22 | + def start(self): |
| 23 | + """Start Listening""" |
| 24 | + if self._listener_thread is None: |
| 25 | + self._listener_thread = threading.Thread( |
| 26 | + target=self._key_listener, daemon=True |
| 27 | + ) |
| 28 | + if not self._listener_thread.is_alive(): |
| 29 | + self._listener_thread.start() |
| 30 | + |
| 31 | + def stop(self): |
| 32 | + """Stop Listening""" |
| 33 | + if self._listener_thread is not None and self._listener_thread.is_alive(): |
| 34 | + self._listener_thread.join() |
| 35 | + |
| 36 | + @property |
| 37 | + def pressed(self): |
| 38 | + "Return whether enter was pressed since last checked" "" |
| 39 | + result = False |
| 40 | + if self._input_key is not None: |
| 41 | + self._input_key = None |
| 42 | + result = True |
| 43 | + return result |
| 44 | + |
| 45 | + |
| 46 | +def main(): |
| 47 | + # pylint: disable=too-many-locals, too-many-statements |
| 48 | + i2c = busio.I2C(board.SCL, board.SDA) |
| 49 | + |
| 50 | + gyro_accel = LSM6DSOX(i2c) |
| 51 | + magnetometer = LIS3MDL(i2c) |
| 52 | + key_listener = KeyListener() |
| 53 | + key_listener.start() |
| 54 | + |
| 55 | + ############################ |
| 56 | + # Magnetometer Calibration # |
| 57 | + ############################ |
| 58 | + |
| 59 | + print("Magnetometer Calibration") |
| 60 | + print("Start moving the board in all directions") |
| 61 | + print("When the magnetic Hard Offset values stop") |
| 62 | + print("changing, press ENTER to go to the next step") |
| 63 | + print("Press ENTER to continue...") |
| 64 | + while not key_listener.pressed: |
| 65 | + pass |
| 66 | + |
| 67 | + mag_x, mag_y, mag_z = magnetometer.magnetic |
| 68 | + min_x = max_x = mag_x |
| 69 | + min_y = max_y = mag_y |
| 70 | + min_z = max_z = mag_z |
| 71 | + |
| 72 | + while not key_listener.pressed: |
| 73 | + mag_x, mag_y, mag_z = magnetometer.magnetic |
| 74 | + |
| 75 | + print( |
| 76 | + "Magnetometer: X: {0:8.2f}, Y:{1:8.2f}, Z:{2:8.2f} uT".format( |
| 77 | + mag_x, mag_y, mag_z |
| 78 | + ) |
| 79 | + ) |
| 80 | + |
| 81 | + min_x = min(min_x, mag_x) |
| 82 | + min_y = min(min_y, mag_y) |
| 83 | + min_z = min(min_z, mag_z) |
| 84 | + |
| 85 | + max_x = max(max_x, mag_x) |
| 86 | + max_y = max(max_y, mag_y) |
| 87 | + max_z = max(max_z, mag_z) |
| 88 | + |
| 89 | + offset_x = (max_x + min_x) / 2 |
| 90 | + offset_y = (max_y + min_y) / 2 |
| 91 | + offset_z = (max_z + min_z) / 2 |
| 92 | + |
| 93 | + field_x = (max_x - min_x) / 2 |
| 94 | + field_y = (max_y - min_y) / 2 |
| 95 | + field_z = (max_z - min_z) / 2 |
| 96 | + |
| 97 | + print( |
| 98 | + "Hard Offset: X: {0:8.2f}, Y:{1:8.2f}, Z:{2:8.2f} uT".format( |
| 99 | + offset_x, offset_y, offset_z |
| 100 | + ) |
| 101 | + ) |
| 102 | + print( |
| 103 | + "Field: X: {0:8.2f}, Y:{1:8.2f}, Z:{2:8.2f} uT".format( |
| 104 | + field_x, field_y, field_z |
| 105 | + ) |
| 106 | + ) |
| 107 | + print("") |
| 108 | + time.sleep(0.01) |
| 109 | + |
| 110 | + mag_calibration = (offset_x, offset_y, offset_z) |
| 111 | + print( |
| 112 | + "Final Magnetometer Calibration: X: {0:8.2f}, Y:{1:8.2f}, Z:{2:8.2f} uT".format( |
| 113 | + offset_x, offset_y, offset_z |
| 114 | + ) |
| 115 | + ) |
| 116 | + |
| 117 | + ######################### |
| 118 | + # Gyroscope Calibration # |
| 119 | + ######################### |
| 120 | + |
| 121 | + gyro_x, gyro_y, gyro_z = gyro_accel.gyro |
| 122 | + min_x = max_x = gyro_x |
| 123 | + min_y = max_y = gyro_y |
| 124 | + min_z = max_z = gyro_z |
| 125 | + |
| 126 | + print("") |
| 127 | + print("") |
| 128 | + print("Gyro Calibration") |
| 129 | + print("Place your gyro on a FLAT stable surface.") |
| 130 | + print("Press ENTER to continue...") |
| 131 | + while not key_listener.pressed: |
| 132 | + pass |
| 133 | + |
| 134 | + for _ in range(SAMPLE_SIZE): |
| 135 | + gyro_x, gyro_y, gyro_z = gyro_accel.gyro |
| 136 | + |
| 137 | + print( |
| 138 | + "Gyroscope: X: {0:8.2f}, Y:{1:8.2f}, Z:{2:8.2f} rad/s".format( |
| 139 | + gyro_x, gyro_y, gyro_z |
| 140 | + ) |
| 141 | + ) |
| 142 | + |
| 143 | + min_x = min(min_x, gyro_x) |
| 144 | + min_y = min(min_y, gyro_y) |
| 145 | + min_z = min(min_z, gyro_z) |
| 146 | + |
| 147 | + max_x = max(max_x, gyro_x) |
| 148 | + max_y = max(max_y, gyro_y) |
| 149 | + max_z = max(max_z, gyro_z) |
| 150 | + |
| 151 | + offset_x = (max_x + min_x) / 2 |
| 152 | + offset_y = (max_y + min_y) / 2 |
| 153 | + offset_z = (max_z + min_z) / 2 |
| 154 | + |
| 155 | + noise_x = max_x - min_x |
| 156 | + noise_y = max_y - min_y |
| 157 | + noise_z = max_z - min_z |
| 158 | + |
| 159 | + print( |
| 160 | + "Zero Rate Offset: X: {0:8.2f}, Y:{1:8.2f}, Z:{2:8.2f} rad/s".format( |
| 161 | + offset_x, offset_y, offset_z |
| 162 | + ) |
| 163 | + ) |
| 164 | + print( |
| 165 | + "Rad/s Noise: X: {0:8.2f}, Y:{1:8.2f}, Z:{2:8.2f} rad/s".format( |
| 166 | + noise_x, noise_y, noise_z |
| 167 | + ) |
| 168 | + ) |
| 169 | + print("") |
| 170 | + |
| 171 | + gyro_calibration = (offset_x, offset_y, offset_z) |
| 172 | + print( |
| 173 | + "Final Zero Rate Offset: X: {0:8.2f}, Y:{1:8.2f}, Z:{2:8.2f} rad/s".format( |
| 174 | + offset_x, offset_y, offset_z |
| 175 | + ) |
| 176 | + ) |
| 177 | + print("") |
| 178 | + print("------------------------------------------------------------------------") |
| 179 | + print("Final Magnetometer Calibration Values: ", mag_calibration) |
| 180 | + print("Final Gyro Calibration Values: ", gyro_calibration) |
| 181 | + |
| 182 | + |
| 183 | +if __name__ == "__main__": |
| 184 | + main() |
0 commit comments