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 2 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
38 changes: 38 additions & 0 deletions examples/lsm303_accel_inclinometer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
""" 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):
radians = atan2(y, x)
degrees_calc = degrees(radians)
if degrees_calc < 0:
degrees_calc = 360 + degrees_calc
return degrees_calc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be shortened to something like

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, will make this change tonight.



def get_inclination(_sensor):
return get_inclination_respect_x(_sensor), get_inclination_respect_y(_sensor)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this approach you read the sensor twice. It probably happens so fast it's no big deal. But you could get rid of the two get_inclination_respect functions and have this one be something like:

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

and then both inclination computations would be done from the same sensor reading.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing this out. Like you say it's so fast it usually doesn't matter but I have had the pleasure of debugging weirdness caused by making multiple reads like this getting different results on with other sensors, definitely should be storing it to a variable to reference from there instead. I will get this changed tonight as well.



def get_inclination_respect_x(_sensor):
accel_axis_data = _sensor.acceleration
return vector_2_degrees(accel_axis_data[0], accel_axis_data[2])


def get_inclination_respect_y(_sensor):
accel_axis_data = _sensor.acceleration
return vector_2_degrees(accel_axis_data[1], accel_axis_data[2])


while True:
print("inclination: (%s, %s)" % (get_inclination(sensor)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider switching to newer style .format string formatting instead of %.

Also maybe add more description of what the two values are? Something like this?

print("XZ angle = {:6.2}deg   YZ angle = {:6.2}deg".format(get_inclination(sensor)))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it, will make this change tonight.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm getting this error when I use this code as is.

TypeError: can't convert 'tuple' object to str implicitly

I've changed it to this which solves this tuple conversion error by making a variable and accessing the indexes individually. If there is some other syntax that allows it to work as a one liner I can use that but I'm not sure of how to do it. I also used float formatting because it showed exponential notation sometimes.

inclination = get_inclination(sensor)
print("XZ angle = {:6.2f}deg   YZ angle = {:6.2f}deg".format(inclination[0],inclination[1]))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yah. My bad. I was just coding from the hip. The actual formatter syntax may be wrong too.

Try this:

print("XZ angle = {:6.2f}deg   YZ angle = {:6.2f}deg".format(*get_inclination(sensor)))

Added fs to the formatter and used the * trick to unpack the tuple. What do you think about splitting it into two lines?

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah the asterisk does work. I like your two line version best. I've just pushed a change to that one.

time.sleep(0.2)