-
Notifications
You must be signed in to change notification settings - Fork 5
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
|
||
def get_inclination(_sensor): | ||
return get_inclination_respect_x(_sensor), get_inclination_respect_y(_sensor) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider switching to newer style 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))) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like it, will make this change tonight. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm getting this error when I use this code as is.
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.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 angle_xz, angle_yz = get_inclination(sensor)
print("XZ angle = {:6.2f}deg YZ angle = {:6.2f}deg".format(angle_xz, angle_yz)) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.