/ examples / lsm303_accel_inclinometer.py
lsm303_accel_inclinometer.py
 1  """ Display inclination data five times per second """
 2  
 3  import time
 4  from math import atan2, degrees
 5  import board
 6  import busio
 7  import adafruit_lsm303_accel
 8  
 9  
10  i2c = busio.I2C(board.SCL, board.SDA)
11  sensor = adafruit_lsm303_accel.LSM303_Accel(i2c)
12  
13  
14  def vector_2_degrees(x, y):
15      angle = degrees(atan2(y, x))
16      if angle < 0:
17          angle += 360
18      return angle
19  
20  
21  def get_inclination(_sensor):
22      x, y, z = _sensor.acceleration
23      return vector_2_degrees(x, z), vector_2_degrees(y, z)
24  
25  
26  while True:
27      angle_xz, angle_yz = get_inclination(sensor)
28      print("XZ angle = {:6.2f}deg   YZ angle = {:6.2f}deg".format(angle_xz, angle_yz))
29      time.sleep(0.2)