Skip to content

adding inclinometer example #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions examples/lsm303_accel_inclinometer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
""" Display inclination data five times per second """

import time
from math import atan2, degrees
import board
import busio
import adafruit_lsm303_accel


i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_lsm303_accel.LSM303_Accel(i2c)


def vector_2_degrees(x, y):
angle = degrees(atan2(y, x))
if angle < 0:
angle += 360
return angle


def get_inclination(_sensor):
x, y, z = _sensor.acceleration
return vector_2_degrees(x, z), vector_2_degrees(y, z)


while True:
angle_xz, angle_yz = get_inclination(sensor)
print("XZ angle = {:6.2f}deg YZ angle = {:6.2f}deg".format(angle_xz, angle_yz))
time.sleep(0.2)