Go Down

### Topic: Guide to gyro and accelerometer with Arduino including Kalman filtering (Read 689164 times)previous topic - next topic

#### Lauszus

#90
##### Jul 30, 2011, 12:07 pm
Try changing the calibration page to this:
Code: [Select]
`//This is pretty simple. It takes 100 readings and calculate the average.//gyroslong resultGyroX;//x-axislong resultGyroY;//y-axislong resultGyroZ;//z-axis//accelerometerslong resultAccX;//x-axislong resultAccY;//y-axislong resultAccZ;//z-axis//gyrosint 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;}//accelerometersint 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;}`

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.

#### lara

#91
##### Jul 30, 2011, 12:14 pm
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!

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?

#### Lauszus

#92
##### Jul 30, 2011, 12:25 pm
Glad I could help. I see your point. I am not home right now, but I will check it tonight

#### lara

#93
##### Jul 30, 2011, 12:49 pm
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.   compAngleX, compAngleY, AccX,Y,Z values are all I need.

Thanks!

#### Lauszus

#94
##### Jul 31, 2011, 03:48 pm
I have updated the code, so it is now ver3. Please try it out, and say if it works
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
Anyway the project sound really cool, please post a video when your are done

#### Rivello

#95
##### Aug 09, 2011, 09:32 am
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    Thank you

Here are the results:

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

#### Lauszus

#96
##### Aug 09, 2011, 10:14 am
#Rivello
Awesome job, glad I could help
- Lauszus

#### elwylly

#97
##### Aug 10, 2011, 03:55 pm
Hi!!

I was hoping you could help me as I'm getting nuts with this IMU

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

Thanks for the help!

Guillermo

#### Lauszus

#98
##### Aug 10, 2011, 04:00 pm
#Elwylly
Have you remembered to connect 3.3V to the VREF pin?

#### elwylly

#99
##### Aug 10, 2011, 04:28 pm
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  XD

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

Cheers,

#### Lauszus

#100
##### Aug 10, 2011, 04:34 pm
#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
?
It is pretty hard for me, to help when I do not know more
It should not be necessary to use the self-test.
- Lauszus

#### elwylly

#101
##### Aug 10, 2011, 07:13 pm
Yep. Everything connected as you mentioned. I've checked hundreds of times as it is a stupid mistake that I would love to avoid  XD

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,

#### elwylly

#102
##### Aug 10, 2011, 07:58 pm
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,

#### Lauszus

#103
##### Aug 10, 2011, 10:12 pm
Okay, it is simple then.
In line 97, is says:
Code: [Select]
`  accZval++;//1g in horizontal position`
That should just be changed to:
Code: [Select]
`  accZval--;//-1g when facing down`

#### elwylly

#104
##### Aug 11, 2011, 08:34 pm
I had to adjust the 360 degrees part as well but finally it is working as expected  ]