Accelerometers and measurement of orientation

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
gratefulfrog
Posts: 149
Joined: Sun Mar 01, 2015 12:10 pm

Accelerometers and measurement of orientation

Post by gratefulfrog » Fri Jul 03, 2015 5:11 pm

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

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Accelerometers and measurement of orientation

Post by dhylands » Fri Jul 03, 2015 5:47 pm

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.

gratefulfrog
Posts: 149
Joined: Sun Mar 01, 2015 12:10 pm

Re: Accelerometers and measurement of orientation

Post by gratefulfrog » Fri Jul 03, 2015 7:19 pm

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

User avatar
kamikaze
Posts: 154
Joined: Tue Aug 16, 2016 10:10 am
Location: Latvia
Contact:

Re: Accelerometers and measurement of orientation

Post by kamikaze » Wed Sep 14, 2016 1:42 pm

I don't get it too. Is there any real-life example with code? )

gratefulfrog
Posts: 149
Joined: Sun Mar 01, 2015 12:10 pm

Re: Accelerometers and measurement of orientation

Post by gratefulfrog » Wed Sep 14, 2016 6:21 pm

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...

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
>>> 
I hope this helps!
Ciao,
B

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Accelerometers and measurement of orientation

Post by deshipu » Wed Sep 14, 2016 6:53 pm

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 :-)

User avatar
kfricke
Posts: 342
Joined: Mon May 05, 2014 9:13 am
Location: Germany

Re: Accelerometers and measurement of orientation

Post by kfricke » Thu Sep 15, 2016 8:27 am

deshipu wrote:... and the Law of Cosines ...
My cousine is not really good at math! :roll:

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Accelerometers and measurement of orientation

Post by deshipu » Thu Sep 15, 2016 10:14 am

In that case, you will have to fall back on the actual law: https://en.wikipedia.org/wiki/Law_of_cosines

User avatar
saulo
Posts: 16
Joined: Thu May 26, 2016 9:05 am
Location: Brasil

Re: Accelerometers and measurement of orientation

Post by saulo » Thu Sep 15, 2016 1:17 pm

Take a look on this document:

https://cache.freescale.com/files/senso ... AN3461.pdf

it explains the math, but atan2() function will help you.

Post Reply