- September 3, 2019 at 5:32 am #9959
Firstly, just want to say I love the Berry GPS-IMU. It's an awesome little package and I'll hope to order a few more if I can line up a few more ducks.
I found some code on the BerryIMU github that calculates the X and Y angles from the accelerometer (from the inclinometer code), and wanted to also calculate the Z. Seems like the code is calculating each axis as the normal vector to the plane created by the other axes, which seems right. I have added the same logic to the Z-Axis calc: see below.
if not IMU_UPSIDE_DOWN:
# If the IMU is up the correct way (Skull logo facing down), use these calculations
ACCXangle = (math.atan2(ACCy,ACCz)*RAD_TO_DEG)
ACCYangle = (math.atan2(ACCz,ACCx)+M_PI)*RAD_TO_DEG
ACCZangle = (math.atan2(ACCx,ACCy)*RAD_TO_DEG)
#Use these four lines when the IMU is upside down. Skull logo is facing up
ACCXangle = (math.atan2(-ACCy,-ACCz)*RAD_TO_DEG)
ACCYangle = (math.atan2(-ACCz,-ACCx)+M_PI)*RAD_TO_DEG
ACCZangle = (math.atan2(-ACCx,-ACCy)*RAD_TO_DEG)
I have two questions for you all:
1) Why would we have "+ pi" in the the Y angle calc but not the X? '+ pi' doesn't seem right to me... but my old high school trigonometry learning was a long time ago! 🙂
2) I think I'm on the right track with the Z angle; what do you think?September 3, 2019 at 2:55 pm #9969Mark WilliamsKeymaster
Without "+P", the values returned are from -180 to +180. 0 being horizontal (or level)
With "+P", the values returned are from 0 to 360. 180 being horizontal (or level)
We wanted to use -180 to +180 for both X and Y. This is easy for the X axis, but the Y axis is orientated differently in the IMU, so we have to use Pi to get values between 0 and 360 and then we use other code just below the atan2 lines convert it to -180 to +180
You cant calculate Z angle (or yaw) using accelerometer as the Z value never changes when spinning the IMU. To get this value, you would need to use the compass (magnetometer)
Mark --OzzMaker.com --September 3, 2019 at 8:15 pm #9976
1) Ah ha! Thanks Mark, that is a great explanation. Very succinct about Y and X. Perfect, thank you.
2) Sorry, I should have explained a little more about my application and what I'm trying to find: the IMU stays static and tilts to try to find level. I'm trying to find the tilt angle between the Z axis and the gravity vector, regardless of how much it has rotated around the Z angle. I've attached a diagram (from digikey website) and angle shown in figure (d) is what I'm trying to achieve. My thinking is this should always show the compound X and Y tilt value as a single angle value.
What do you think?
Attachments:September 19, 2019 at 10:33 am #10430
I'm going to post this on stack overflow to see what we get. Once I get confirmation I'll update.
- You must be logged in to reply to this topic.