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

Conversation

FoamyGuy
Copy link
Contributor

@FoamyGuy FoamyGuy commented Feb 5, 2020

This adds an inclinometer example based on the code in the older PR referenced in issue #2. It's been tested successfully with lsm303_accel and mpu6050 and should work with any sensor that uses the same .acceleration property to expose accelerometer data.

Let me know if there is a different / better place for this example to live for eventual inclusion in a learn guide and if I should go ahead create similar PRs for the other sensor libraries that this example will work with.

@evaherrada evaherrada requested a review from a team February 6, 2020 14:06
Copy link
Contributor

@caternuson caternuson left a comment

Choose a reason for hiding this comment

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

Code is functional. So comments are mainly code style suggestions.



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.

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.

Copy link
Contributor

@caternuson caternuson left a comment

Choose a reason for hiding this comment

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

Cool. Looks good!

@caternuson caternuson merged commit 5cba278 into adafruit:master Feb 7, 2020
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Feb 18, 2020
Updating https://github.com/adafruit/Adafruit_CircuitPython_ADS1x15 to 2.1.3 from 2.1.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_ADS1x15#52 from ares-est/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_CLUE to 2.0.3 from 2.0.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_CLUE#17 from kattni/slideshow-fix
  > Merge pull request adafruit/Adafruit_CircuitPython_CLUE#14 from dherrada/master
  > Merge pull request adafruit/Adafruit_CircuitPython_CLUE#16 from kattni/slideshow-example
  > Merge pull request adafruit/Adafruit_CircuitPython_CLUE#15 from kattni/add-display-object
  > Merge pull request adafruit/Adafruit_CircuitPython_CLUE#12 from kattni/add-space
  > Merge pull request adafruit/Adafruit_CircuitPython_CLUE#11 from kattni/variable-change
  > Merge pull request adafruit/Adafruit_CircuitPython_CLUE#10 from kattni/level-bubble-fix
  > Merge pull request adafruit/Adafruit_CircuitPython_CLUE#9 from kattni/color-fix
  > Merge pull request adafruit/Adafruit_CircuitPython_CLUE#8 from kattni/example-update

Updating https://github.com/adafruit/Adafruit_CircuitPython_Fingerprint to 1.1.6 from 1.1.5:
  > Merge pull request adafruit/Adafruit_CircuitPython_Fingerprint#11 from stitchesnburns/stitchesnburns-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_LIS2MDL to 2.0.2 from 2.0.1:
  > Update README.rst

Updating https://github.com/adafruit/Adafruit_CircuitPython_LSM303_Accel to 1.0.3 from 1.0.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_LSM303_Accel#5 from BiffoBear/rename-example-file
  > Merge pull request adafruit/Adafruit_CircuitPython_LSM303_Accel#4 from FoamyGuy/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_LSM303DLH_Mag to 1.0.4 from 1.0.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_LSM303DLH_Mag#5 from FoamyGuy/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx to 2.2.3 from 2.2.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_MCP230xx#21 from foozmeat/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_MPU6050 to 1.0.4 from 1.0.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_MPU6050#5 from FoamyGuy/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_PyPortal to 3.1.10 from 3.1.9:
  > Merge pull request adafruit/Adafruit_CircuitPython_PyPortal#65 from cogliano/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_RGB_Display to 3.8.9 from 3.8.8:
  > Merge pull request adafruit/Adafruit_CircuitPython_RGB_Display#70 from makermelissa/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_BluefruitConnect to 1.0.11 from 1.0.10:
  > Merge pull request adafruit/Adafruit_CircuitPython_BluefruitConnect#17 from caternuson/controller_example

Updating https://github.com/adafruit/Adafruit_CircuitPython_BusDevice to 4.2.0 from 4.1.4:
  > Merge pull request adafruit/Adafruit_CircuitPython_BusDevice#43 from dhalbert/avoid-alloc

Updating https://github.com/adafruit/Adafruit_CircuitPython_Gizmo to 1.1.3 from 1.1.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_Gizmo#10 from FoamyGuy/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Added the following libraries: Adafruit_CircuitPython_LPS2X
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants