Pages: 1 ... 9 10 [11] 12 13 ... 37   Go Down
Author Topic: Guide to gyro and accelerometer with Arduino including Kalman filtering  (Read 294177 times)
0 Members and 2 Guests are viewing this topic.
Denmark
Offline Offline
Sr. Member
****
Karma: 10
Posts: 287
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It is a bit wrong. Your code should look something like this:
Code:
gyroYadc = readGyroX; //Read with I2C
gyroYrate = (gyroYadc-gyroZeroY)/14.375;

accYadc = readAccX; //Read with I2C
accYval = (accYadc-accZeroY)/256;

Then just calculate the angles as usual smiley
Logged

Singapore
Offline Offline
Newbie
*
Karma: 0
Posts: 39
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you so much Lauszuz,

Finally my angles have started looking like "angles" ;-)

Though I noticed one thing, it takes it a while to get to the correct angle.
E.g. if I tilt the device slowly, all stays good but if i do it slightly faster, the change in values takes place gradually? (roughly 2+ seconds for getting down to 0 degree from 90 degree or vice versa) ... is that normal?
Logged

Denmark
Offline Offline
Sr. Member
****
Karma: 10
Posts: 287
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is that both with the Kalman- and Complementary filter? Mine does that as well, but definitely not that slow.
Logged

Manchester, UK
Offline Offline
Sr. Member
****
Karma: 0
Posts: 293
Rodrigo Constanzo is a performer and composer living in Manchester, England. He is an avid improviser and performs regularly using home made electro acoustic, and modified electronic instruments. He is currently working towards a PhD in Composition at th
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, so I've read nearly all of this thread and have a couple of questions.

In my understanding, you need 6dof (3accel, 3gyro) to be able to produce two absolute angles. The third angle is not possible to calculate at all? Or reliably due to gyro drift?

So if you add a magnetometer you can get 3 absolute angles?

If I have an ArduiMU (http://www.sparkfun.com/products/9956) (arduino + 3axis gyro + 3axis accel) can I add a manegometer that uses i2c and be able to spit out all the data to a computer? (without needing more analog ins).

Does it matter how many axis' the magnetometer has? (in order to calculate a third absolute angle)


Lastly, if the data is going to end up in a computer (running Max/MSP) is it better to do the math/calculations there rather than on the Arduino? Any benefit to doing the calculations locally on the Arduino?
Logged

Singapore
Offline Offline
Newbie
*
Karma: 0
Posts: 39
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is that both with the Kalman- and Complementary filter?

yup Kalman filter is giving much better results, so I have switched to that now.
One more thing I noticed, the moment X-axis angle starts to approach 90 degree and onwards, Y-axis angle suddenly goes crazy. And same is the case with X-axis angle when Y-axis starts to get near to 90 degree.

Is that due to my IMU or you are experiencing the simillar results with yours?
Logged

Denmark
Offline Offline
Sr. Member
****
Karma: 10
Posts: 287
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

#kriista
You actually only need a 5DOF to calculate pitch and roll. You can even calculate it with a 4DOF, as you can just calculate the angle using sinus. No the third angle (yaw) is not precise due to gyro drift - but it can be calculate with some kind of filter. You need a magnetometer to calculate the last angle (yaw). Yes you can just use a I2C magnetomer with your board (http://www.sparkfun.com/products/9956). I am pretty sure that you need a 3-axis magnetometer to calculate the last angle - haven't tried it yet, as I do not own a magnetometer.
I your need is very time-critical, then it is better to do the calculation on the computer, as it got a lot more processing power than the Arduino. The good thing about doing it on the Arduino, is that you can easily hook it up to any program, with supports serial communication.

#Kashif
It is because you use 360 resolution. Just uncomment it if you need both angles smiley
Logged

Manchester, UK
Offline Offline
Sr. Member
****
Karma: 0
Posts: 293
Rodrigo Constanzo is a performer and composer living in Manchester, England. He is an avid improviser and performs regularly using home made electro acoustic, and modified electronic instruments. He is currently working towards a PhD in Composition at th
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It'd be for a musical performance setting, so latency is definitely undesirable.
I'm gonna try going with just the 6DOF and approximating the 3rd angle to see if that works, and if it doesn't I can add the magnetometer with i2c. I was initially worried that I had used up all my analog ins, so I didn't know this was a way around that.
Logged

Denmark
Offline Offline
Sr. Member
****
Karma: 10
Posts: 287
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay, please post your work when you are finished smiley It always nice to see what people come up with!
Logged

Manchester, UK
Offline Offline
Sr. Member
****
Karma: 0
Posts: 293
Rodrigo Constanzo is a performer and composer living in Manchester, England. He is an avid improviser and performs regularly using home made electro acoustic, and modified electronic instruments. He is currently working towards a PhD in Composition at th
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Will do, though it will be a while as I've not even sorted out the wireless and hardware assembly part....not to mention figuring out the math on the computer side, and then applying it to something meaningful etc...

But yeah, I'll post it.
Logged

Singapore
Offline Offline
Newbie
*
Karma: 0
Posts: 39
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

#Kashif
It is because you use 360 resolution. Just uncomment it if you need both angles smiley

uncomment what? I don't have anything commented out ;-) using ur code as-is, the only change i made was to read the I2C
Logged

Denmark
Offline Offline
Sr. Member
****
Karma: 10
Posts: 287
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This part: smiley-grin
Code:
 if(accZval < 0)//360 degrees
  {
    if(accXangle < 0)
    {
      accXangle = -180-accXangle;
    }
    else
    {
      accXangle = 180-accXangle;
    }
    
    if(accYangle < 0)
    {
      accYangle = -180-accYangle;
    }
    else
    {
      accYangle = 180-accYangle;
    }    
  }
Logged

Singapore
Offline Offline
Newbie
*
Karma: 0
Posts: 39
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's what I meant, this part is already uncommented. smiley-wink
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@Kashif  You have to comment (or delete) that part in order to use both axis without any problems, but it will only measure correctly from 90 to -90
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If your filter is laggy, it might be because your gyro and accel signs do not match. Try inverting the gyro value passed to the filter.  The filter should be fast - I use a 10ms update rate for my quadcopter and the filter is fast and smooth for pitch and roll.

With a mag, you will have to calculate the tilt compensated angle using either the raw pitch, roll data from the accel or pass in the filtered data to the mag filter with the gyro Z angle.  However, depending on your mag, the update rate will be quite slow (10hz maybe).  I haven't had much success Kalman filtering mag data.  You might try using FreeIMU which uses a MARG algorithm to combine 9DOF data.  It's faster than the kalman, but far more suseptable to vibration.  Also, external magnetic influences may cause it to drift.

Make sure you calibrate your mag correctly so that you apply offsets to the mag outputs - otherwise you will never get reliable heading angles.

Ok, so I've read nearly all of this thread and have a couple of questions.

In my understanding, you need 6dof (3accel, 3gyro) to be able to produce two absolute angles. The third angle is not possible to calculate at all? Or reliably due to gyro drift?

So if you add a magnetometer you can get 3 absolute angles?

If I have an ArduiMU (http://www.sparkfun.com/products/9956) (arduino + 3axis gyro + 3axis accel) can I add a manegometer that uses i2c and be able to spit out all the data to a computer? (without needing more analog ins).

Does it matter how many axis' the magnetometer has? (in order to calculate a third absolute angle)


Lastly, if the data is going to end up in a computer (running Max/MSP) is it better to do the math/calculations there rather than on the Arduino? Any benefit to doing the calculations locally on the Arduino?
Logged

Singapore
Offline Offline
Newbie
*
Karma: 0
Posts: 39
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@Kashif  You have to comment (or delete) that part in order to use both axis without any problems, but it will only measure correctly from 90 to -90

Thanx Cristo829, I think Lauszus by mistake said "comment out", while he meant to "uncomment" ;-)
ya the angles seem ok now but only measuring +90 to -90

So, there is no way I can get a full 360 swing without compromising the other angle?

Lauszus, your code has helped me a lot reaching here, thats why I am sticking to this thread. Please be patient while I fire up some silly questions ;-) Once done, I will pass you my code which you can place along with the thread. It's all your code anyways just with the variation of I2C IMU, hope it will add more flavor to your article ;-)

OK guys, I am now starting my trial-n-error method once again to acheive the following
1. be able to calculate 360 degree (or at least 180) accurately on both axis without making the other one go crazy.
2. Remove (or at least minimize) the lag.


Logged

Pages: 1 ... 9 10 [11] 12 13 ... 37   Go Up
Jump to: