Accelerometers and measurement of orientation
-
- Posts: 149
- Joined: Sun Mar 01, 2015 12:10 pm
Accelerometers and measurement of orientation
Hi,
I have searched high and low and done experiments with the pyb.Accel but just can't seem to get it.
My goal is to detect changes in orientation in the pyboard:
- rotation about the x-axis
- rotation about the y-axis
- rotation about the z-axis
Ideally I would like to take a reference value for each, then regularly measure changes.
Could anyone suggest a way of doing that ?
Also, could anyone tell me the exact specs for the Accel class methods?
- accel.filtered_xyz()
- accel.tilt()
Thanks,
Bob
I have searched high and low and done experiments with the pyb.Accel but just can't seem to get it.
My goal is to detect changes in orientation in the pyboard:
- rotation about the x-axis
- rotation about the y-axis
- rotation about the z-axis
Ideally I would like to take a reference value for each, then regularly measure changes.
Could anyone suggest a way of doing that ?
Also, could anyone tell me the exact specs for the Accel class methods?
- accel.filtered_xyz()
- accel.tilt()
Thanks,
Bob
Re: Accelerometers and measurement of orientation
If the pyboard is stationary and level, then what you're going to see in the X/Y/Z values is gravity, which should work out to 9.8 m/sec squared pointing down (so X and Y should be close to zero and Z should be measuring gravity). Think of X, Y, and Z as a vector in 3D space. When the pyboard is stationary, then the vector will point towards the center of the earth. If you're in your car accelerating, then the vector will swing towards the back of the seat (just like you feel yourself being pushed into the seat).
If you rotate the pyboard about the Z axis (so just spinning in the table) when it comes to rest again, you'll still see Z going down. With just an accelerometer, you can't actually tell which direction your pointed in (as in N/W/E/S), just which direction the center of the earth is.
If you were to take your pyboard and power it by a batery and throw it in the air, as your hand is speeding up, you should see the acceleration increase, and when it leaves your hand, it should more or less drop to zero (while in the air it is essentially in free fall). WHen you catch it again (or it falls to the ground) then the acceleration should increase again.
To properly determine and track orientation, you really want a 9-axis IMU, like this one: https://www.sparkfun.com/products/10736 which combines an accelerometer, a gyroscope and a magnetometer.
If you rotate the pyboard about the Z axis (so just spinning in the table) when it comes to rest again, you'll still see Z going down. With just an accelerometer, you can't actually tell which direction your pointed in (as in N/W/E/S), just which direction the center of the earth is.
If you were to take your pyboard and power it by a batery and throw it in the air, as your hand is speeding up, you should see the acceleration increase, and when it leaves your hand, it should more or less drop to zero (while in the air it is essentially in free fall). WHen you catch it again (or it falls to the ground) then the acceleration should increase again.
To properly determine and track orientation, you really want a 9-axis IMU, like this one: https://www.sparkfun.com/products/10736 which combines an accelerometer, a gyroscope and a magnetometer.
-
- Posts: 149
- Joined: Sun Mar 01, 2015 12:10 pm
Re: Accelerometers and measurement of orientation
Hi!
Thanks for your reply.
I understand the physics of acceleration, but the values I see from the device do not seem to coincide with any that I can guess, or maybe the units need conversion? (In the meantime, I found the data sheet and now see how it works, with the bits for x,y,z, tilt, but not 'filtered')
Sitting horizontal and still:
----------------
filtered: (4, 0, 88)
raw: (1,0,22)
tilt: 1
----------------
filtered: (4, -1, 88)
raw: (1,-1,22)
tilt: 1
----------------
It seems that some simple trig should be enough to determine the angle, or am I naive?
If we say that the board is tilted by an angle 'alphaZ', then Za = G*cos(alphaZ) ?
Similarly or the X,Y right?
So, it seems that there should be a way of figuring out angles?
Is that the best that can be done?
Thanks
B
Thanks for your reply.
I understand the physics of acceleration, but the values I see from the device do not seem to coincide with any that I can guess, or maybe the units need conversion? (In the meantime, I found the data sheet and now see how it works, with the bits for x,y,z, tilt, but not 'filtered')
Sitting horizontal and still:
----------------
filtered: (4, 0, 88)
raw: (1,0,22)
tilt: 1
----------------
filtered: (4, -1, 88)
raw: (1,-1,22)
tilt: 1
----------------
It seems that some simple trig should be enough to determine the angle, or am I naive?
If we say that the board is tilted by an angle 'alphaZ', then Za = G*cos(alphaZ) ?
Similarly or the X,Y right?
So, it seems that there should be a way of figuring out angles?
Is that the best that can be done?
Thanks
B
Re: Accelerometers and measurement of orientation
I don't get it too. Is there any real-life example with code? )
-
- Posts: 149
- Joined: Sun Mar 01, 2015 12:10 pm
Re: Accelerometers and measurement of orientation
Hey, I got this to work long ago..
First, note that any value returned by the Accel instance > 31 or < -32 needs to be rejected because there was a read error.
To calculate the pitch, we need to measure Z axis acceleration, i.e. gravity to get our base value G, then use the formula
cos(angle) = a.z()/G
or angle = cos-1(a.z()/G)
I measured G as 23, then put the pyboard at approx 45° and got a reading of 16, which means the angle was 46°, not bad!
Here's me progressively slanting the board from flat to vertical, i.e. 0° to 90°. As you can see, I may have gone a little beyond vertical...
I hope this helps!
Ciao,
B
First, note that any value returned by the Accel instance > 31 or < -32 needs to be rejected because there was a read error.
To calculate the pitch, we need to measure Z axis acceleration, i.e. gravity to get our base value G, then use the formula
cos(angle) = a.z()/G
or angle = cos-1(a.z()/G)
I measured G as 23, then put the pyboard at approx 45° and got a reading of 16, which means the angle was 46°, not bad!
Here's me progressively slanting the board from flat to vertical, i.e. 0° to 90°. As you can see, I may have gone a little beyond vertical...
Code: Select all
>>> a=Accel()
>>> a.z()
23
>>> a.z()
22
>>> a.z()
23
>>> a.z()
20
>>> a.z()
19
>>> a.z()
17
>>> a.z()
13
>>> a.z()
9
>>> a.z()
7
>>> a.z()
5
>>> a.z()
5
>>> a.z()
3
>>> a.z()
2
>>> a.z()
1
>>> a.z()
-1
>>>
Ciao,
B
Re: Accelerometers and measurement of orientation
There is a very convenient math.atan2(a, b) function that takes two sides of the triangle, and gives you the angle. Together with the Pitagoras theorem and the Law of Cosines you should be able to do pretty much anything with triangles
Re: Accelerometers and measurement of orientation
My cousine is not really good at math!deshipu wrote:... and the Law of Cosines ...
Re: Accelerometers and measurement of orientation
In that case, you will have to fall back on the actual law: https://en.wikipedia.org/wiki/Law_of_cosines
Re: Accelerometers and measurement of orientation
Take a look on this document:
https://cache.freescale.com/files/senso ... AN3461.pdf
it explains the math, but atan2() function will help you.
https://cache.freescale.com/files/senso ... AN3461.pdf
it explains the math, but atan2() function will help you.