Script to test the LSM6DSOX IMU (https://circuitpython.readthedocs.io/projects/lsm6dsox/en/latest/examples.html).
import time import board import busio from adafruit_lsm6ds.lsm6dsox import LSM6DSOX i2c = busio.I2C(board.SCL, board.SDA) sox = LSM6DSOX(i2c) while True: print("Acceleration: X:%.2f, Y: %.2f, Z: %.2f m/s^2"%(sox.acceleration)) print("Gyro X:%.2f, Y: %.2f, Z: %.2f radians/s"%(sox.gyro)) print("") time.sleep(0.5)
Script to test the LIS3MDL 3-axis magnetometer (https://adafruit-circuitpython-lis3mdl.readthedocs.io/en/latest/examples.html#)
import time import board import busio import adafruit_lis3mdl i2c = busio.I2C(board.SCL, board.SDA) sensor = adafruit_lis3mdl.LIS3MDL(i2c) while True: mag_x, mag_y, mag_z = sensor.magnetic print('X:{0:10.2f}, Y:{1:10.2f}, Z:{2:10.2f} uT'.format(mag_x, mag_y, mag_z)) print('') time.sleep(1.0)
Convert the Magnetometer readings to Compass data (https://adafruit-circuitpython-lis3mdl.readthedocs.io/en/latest/examples.html#compass-example):
import time from math import atan2, degrees import board import busio import adafruit_lis3mdl i2c = busio.I2C(board.SCL, board.SDA) sensor = adafruit_lis3mdl.LIS3MDL(i2c) def vector_2_degrees(x, y): angle = degrees(atan2(y, x)) if angle < 0: angle += 360 return angle def get_heading(_sensor): magnet_x, magnet_y, _ = _sensor.magnetic return vector_2_degrees(magnet_x, magnet_y) while True: print("heading: {:.2f} degrees".format(get_heading(sensor))) time.sleep(0.2)
Kommentarer