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

Try changing the calibration page to this:
Code:
//This is pretty simple. It takes 100 readings and calculate the average.

//gyros
long resultGyroX;//x-axis
long resultGyroY;//y-axis
long resultGyroZ;//z-axis

//accelerometers
long resultAccX;//x-axis
long resultAccY;//y-axis
long resultAccZ;//z-axis

//gyros
int calibrateGyroX()
{
  for(int i=0;i<100;i++)
  {
    resultGyroX += analogRead(gX);
    delay(1);
  }
  resultGyroX = resultGyroX/100;
  return resultGyroX;
}

int calibrateGyroY()
{
  for(int i=0;i<100;i++)
  {
    resultGyroY += analogRead(gY);
    delay(1);
  }
  resultGyroY = resultGyroY/100;
  return resultGyroY;
}

int calibrateGyroZ()
{
  for(int i=0;i<100;i++)
  {
    resultGyroZ += analogRead(gZ);
    delay(1);
  }
  resultGyroZ = resultGyroZ/100;
  return resultGyroZ;
}

//accelerometers
int calibrateAccX()
{
  for(int i=0;i<100;i++)
  {
    resultAccX += analogRead(aX);
    delay(1);
  }
  resultAccX = resultAccX/100;
  return resultAccX;
}
 
int calibrateAccY()
{
  for(int i=0;i<100;i++)
  {
    resultAccY += analogRead(aY);
    delay(1);
  }
  resultAccY = resultAccY/100;
  return resultAccY;
}

int calibrateAccZ()
{
  for(int i=0;i<100;i++)
  {
    resultAccZ += analogRead(aZ);
    delay(1);
  }
  resultAccZ = resultAccZ/100;
  return resultAccZ;
}
And double check your connections:

Acc_Gyro          Arduino
3.3V        <—>   3.3V     
GND        <—>   GND
Gx4 X      <—>   AN0
Gx4 Y      <—>   AN1
Gx4 Z      <—>   AN2
Acc X      <—>    AN3 
Acc Y      <—>    AN4 
Acc Z      <—>    AN5 

Also connect 3.3V to the AREF pin on the Arduino for more accuracy.
Logged

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

Changing the Calibration as you detailed is looking much more promising!

The data is starting to look reasonable, and it's not going away and never coming back anymore.

That's serious progress!  Thanks!

Please check:

accXval = (accXadc-accZeroX)/102,3;
accYval = (accYadc-accZeroY)/102,3;
  accZval = (accZadc-accZeroZ)/102,3;

I suspect you may have a problem there.  For me, 102,3 is not the same as 102.3...  We can check this by just writing a sketch that multiplies two decimal numbers together, once with a decimal point and once with a comma.  The results are not the same for me.  Are they for you?

Logged

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

Glad I could help. I see your point. I am not home right now, but I will check it tonight smiley
Logged

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

Once again, thanks!

That change to Calibration did the trick.

After changing 102,3 to 102.3, I'm getting results from just compAngle plenty repeatable enough for my use.  (Part of a nearspace-craft sensor array)  All we're really doing is syncing the rotation and vibration readouts to the video cameras from the balloon and rocket.  The ground control UI is taking data down from the craft over a radio telemetry link.  Now I just need to integrate it. smiley-wink  compAngleX, compAngleY, AccX,Y,Z values are all I need.

Thanks!
Logged

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

I have updated the code, so it is now ver3. Please try it out, and say if it works smiley
The accelerometer can not measure the rotation (yaw), look at some of the previous post for help. The accelerometer can only measure pitch and roll:

You need a magnetometer to measure yaw. Here is a link for one at Sparkfun: http://www.sparkfun.com/products/9371.
Why do you want to use the complimentary filter? The kalman, is more precise smiley
Anyway the project sound really cool, please post a video when your are done smiley-wink
Logged

Rio de Janeiro
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Lauszus, I made use of your already functional Kalman Filter algorithm in my project: A self stabilizing platform.

You code worked great and you saved me a lot of time smiley-grin   Thank you

Here are the results:


I also posted the project in the Exhibition forum, in this post.

Logged

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

#Rivello
Awesome job, glad I could help smiley-wink
- Lauszus
Logged

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

Hi!!

I was hoping you could help me as I'm getting nuts with this IMU  smiley-roll-sweat

I've tested the IMU just as in your post, same pin connections and the original code (version 2 & 3) but I'm getting some weird data. Or at least that's what it looks to me!

The xAngle and yAngle printed at the end of the processing function doesn't go further than 40 degrees. I've tested it by rotating over one of the axes until I reach 90º. I've checked the voltage output directly from the accelerometers to see if something is wrong but the readings I believe make sense. At calibration voltage for accelerometer is around 1.57v. At 90º it is around 1.9v. If I have understood correctly sensitivity is 330mV/g, so just rotating it by 90º should vary +- 0.33v; exactly as I can read. The gyros look "good". Obviously it is hard for me to make a precise reading of it, but when stable it shows around 1.22v which vary while being rotated. If done quite fast I can see values under 1v and around 2v.

I would really appreciate any help as I'm really lost on why I can only measure angles up to 40º. I've seen other replies commenting the same but no root cause. I'm quite sure I'm doing something wrong but I can't trace it  smiley-sad-blue

Thanks for the help!

Guillermo
Logged

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

#Elwylly
Have you remembered to connect 3.3V to the VREF pin? smiley
Logged

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

Hi Lauszus,

Yes, I've connected AREF to 3.3v. Also the rest of the pins are connected as you described to make sure I was not messing up with the values  smiley-lol

I'm wondering whether the gyros are working correctly. I think the acc is working as expected due to the voltage readings, but I'm trying to figure out the self-test option the gyros have. To be honest, that's the only explanation I seem to find. smiley-confuse

Cheers,
Logged

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

#Elwylly
Are you sure that you have connected the it to the right way:
Gx4 X      <—>   AN0
Gx4 Y      <—>   AN1
Gx4 Z      <—>   AN2
Acc X      <—>    AN3 
Acc Y      <—>    AN4 
Acc Z      <—>    AN5
? smiley
It is pretty hard for me, to help when I do not know more smiley
It should not be necessary to use the self-test.
- Lauszus
Logged

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

Yep. Everything connected as you mentioned. I've checked hundreds of times as it is a stupid mistake that I would love to avoid  smiley-lol

I've been checking just the accelerometer value, accYangle, and it is not even close to be correct. It is showing a value of 30º when rotated 90º. At 90º, considering that Gx4 sensitivity is 0.33v/g, the voltage reading is 1.9v (1.57v when flat). That seems to be correct as if I have understood correctly accelerometer in that position should be reflecting gravitational force only for that axis. I think the problem may be there. I'll try to see what's going on when calculating the accelerometer values as they seem to be the source of error. Maybe as somebody mention is a environment config problem that is messing with calculations.

Cheers,
Logged

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

Hi Lauszus,

I think I found what's disturbing my readings. The problem seems to be the Z axis. I've got the IMU with the ICs facing down, and I think the problem is there. I need to calculate correctly the Z axis...

Cheers,
Logged

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

Okay, it is simple then.
In line 97, is says:
Code:
 accZval++;//1g in horizontal position
That should just be changed to:
Code:
 accZval--;//-1g when facing down
Logged

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

I had to adjust the 360 degrees part as well but finally it is working as expected  smiley-twist

Thanks for your post Lauszus, it was really helpful.
Logged

Pages: 1 ... 5 6 [7] 8 9 ... 37   Go Up
Jump to: