Silly newbie question...
When I use math.sqrt() function I seem to get a type error. What I'm trying to do is take the XYZ accelerometer, square the values and then square root it. Further down the app will be an "if total acc>2 then" type call.
I feel stupid. Where am I going wrong. TIA
Newbie - floating point
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Newbie - floating point
Code: Select all
import pyb
import math
a = pyb.Accel()
print(math.sqrt(a.x()**2 + a.y()**2 + a.z()**2))
For serious measurement it's best to use an external module such as the InvenSys MPU9150/MPU9250. Drivers are available.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Newbie - floating point
By the way, it's much faster to compare the squared values with squared distance, rather than take the square root and compare it with normal distance. Just a small trick.