Guide to gyro and accelerometer with Arduino including Kalman filtering

Hi Laszus the mpu6050 is working fine with your sketch, is there any way to match the angle of the servo to the angle of the gyro, at the moment if the gyro tilts to say 45 degrees the servo only moves to approx 30 degrees.

@garwalt
Just multiply the angle by a constant, as all Servos are a little different.
If you want more control use the writeMicroseconds function: Servo - Arduino Reference.

For instance you could fine the maximum and minimum values and then use map (map() - Arduino Reference) to map the angle to these values.

The post is very informative. Thank you so much.
I am working on something similar to it.

I am working on a self-balancing bicycle which is my final year project.The main idea is to run the bicycle,powered by a motor, in balanced position by measuring the tilt when ever the cycle tend to fall and using steering control to balance it. In my project, I am using two sensors gyro meter

and accelerometer
http://robokits.co.in/shop/index.php?main_page=product_info&cPath=11&products_id=39

to measure the tilt angle.

roll angle of the gyro along the front wheel and 'x-axis' of the accelerometer perpendicular to roll angle,towards the right side of the cycle,parallel to the ground so the roll angle will give the degree/sec and accelerometer will give the displaced angle or tilt angle when the bicycle tends to fall. 'y-axis' of the accelero also comes along the roll angle(I m not using it).

so, firstly I want to know whether the program you used to measure the tilt angle (in your case front and back) is applicable to sensors to accurately measure the tilt angle? (I m not using a imu). It would be of great help if you can make me figure out working of my sensors properly.

nowhere i could get the information about how you placed the axis of gyro and accelero....can you please tell me

please help me

Hi
very interesting post, a bit offtopic but I d need to build a 3 axis angle sensor, which should pass me the pitch and yaw of the sensor in real time. I understand that part of this project iwll do exactly that but I am concerned about multi-turn sensing, so to say. I see the sensor will report easily the angles o a "floating" platform where angles go positive and negative starting for a zero/center, but how will it react when going over 360 degrees? Is i tpossible to sense this kind of rotations? Any hint is much appreciated.
Simone

@madalam
I'm not totally sure what you question is? Are you talking about the physical placement of the gyro and accelerometer or something else?
Btw an IMU is simply a device that reports a crafts velocity, angle etc. In this context consist of the accelerometer and gyro that is used to calculate the angle of the device. See wiki for more information: Inertial measurement unit - Wikipedia.

@io
I had the exact same problem with my Balancing robot. This is how I solved it: Balanduino/Balanduino.ino at 711ef4484173694815e27a78f7cbf0dec4911c90 · TKJElectronics/Balanduino · GitHub.
Also check out the Kickstarter for more information about the project: http://www.kickstarter.com/projects/tkjelectronics/balanduino-balancing-robot-kit/posts.

Hello guys. I have been following this post for quite a time. Now that i had the gears , i have begun to play with mpu6050 . And i have recently run the code successfully given in this post. Thanks for sharing.
Eventually what i am trying to do with mpu6050 is to measure sea wave data. An make a wave buoy. Since i am only a newbie in electronics and coding. I made some research on the web and found a scientific article about it. In this article they use an accelerometer to measure wave height and period. Main problem is that while buoy drifts with the wave , wave changes its position relative to the ground. So that acc data can not be used to measure wave accurately. They managed to find a way to compensate directional forces applied to buoy and measure z axis . But off course neither the code nor how to do info are given. But equations in this project are shared fortunately. I am adding the image to the post. I am now trying to integrate these equations to the mpu6050 's acc readings. First equation is interesting i think i can measure the acc data with it regardless of tilt or yaw. But i am getting 16xxx something number instead of g. According to mpu 's sensitivity level i am supposed to divide the number to get the g number. If you are interested. Your help would be much appreciated. Thanks in advance.

@tamayaytam
The MPU-6050 will output it's values as a 16-bit signed number (-32768 to 32767). Depending on the which scaling you use 1g is a certain value. The default is +/-2g. Then 1g would be equal to 16384. Simply divide your readings by 16384 if you want to convert the reading into g's and are using the default setting.
For more information see the datasheet: http://www.invensense.com/mems/gyro/documents/PS-MPU-6000A.pdf at page 13.

Hi!, I'm using IMUs for my final project, and I've been studying Kalman filters implementations for a while.

Studying Lauszus code, I see that Kalman filter gain (K0 and K1) is obtained from the error covariance matrix (P00, P01, P10 and P11) multiplied by the time between readings (dt) and added some constants.

If I see how Kalman gains evolve in time, I see that K0 approachs to 1 and, if time between readings remain constant, K1 approachs to -1/dt. If this is OK, where does Kalman filter improve the Complementary Filter?

Thanks and greetings from Argentina

@juanv
You are totally right, see this comment on my blog: TKJ Electronics » A practical approach to Kalman filter and how to implement it down to this one: TKJ Electronics » A practical approach to Kalman filter and how to implement it.

Hi, I'm a newbie, but i would like try with this IMU Pololu - AltIMU-10 Gyro, Accelerometer, Compass, and Altimeter (L3GD20, LSM303DLHC, and LPS331AP Carrier).
What's is your opinion?. Can I implement your algorithm with this IMU?

Albert

@AlbertG
You can use any IMU you like :slight_smile:

Hello, im doing my little Self Balancing robot project, got 4 days left to show it to the pubblic, ive calculated the PID for the motors (using DC motors) and this forum was the GOD for me, better said Lazarus is God! So i wondered if this forum is thread is still opened and you still answer newbie quesitons. Anways ill just ask away, ive obtained the data from my sensor, ill try to attach it, all the action is on the X axis, do i need the kalman Y calculus to be done? or how does it work? From what ive read, i only have to pass to my motors the kalman X value through PWM, thats analogWrite(kalmanX, pin number); ... wait... this is where i have the problem... where does my PID loop go? i should pass the kalman calculus value to the PID and then pass the PID calculus to the motors... no? Im confused, ive read so many to get this thing working, now my sensors are accurate have accurate readings thanks to you, ive calculated the pid loop, now i have to put it all together and imm a little confused. Ima try attaching that .txt sensor reading, i hope youre still here and still have some time to post a reply :slight_smile:
Thanks in advance for the answer!
Best regards,
Cata

Data Lazarus.txt (298 KB)

@LillSlick
You should check out my robot at Kickstarter: http://www.kickstarter.com/projects/tkjelectronics/balanduino-balancing-robot-kit.
The source code is available at Github: Balanduino/Firmware/Balanduino at master · TKJElectronics/Balanduino · GitHub. It will pretty much answer all your questions :wink:

WOW thanks for the reply on such a short notice! Your code is very elaborate, youre very thorough. I dont need complicated stuff, only want to balance the damn thing :slight_smile: no time for other stuff, i have to present my work in 4 days or else im screwed, it is my first robotics project, im an automation engineer and this project was proposed by my boss and was stupid enough to tell a deadline without even having the hardware, they gave me all the hardware 3-4 days ago and they said 1week ... so now after ive done the phisical part i would like to get the code done, ill use your data acquisition and kalman filtering and i have to get the motors to balance the robot. I had a look on your code and saw you have much control over the motors, im not using the encoders, i dont care if one of the wheels will have a different caracteristic, plus im using two PmodHB5 h-bridges, the pins: Dir/En/Vcc/GND ive merged them and i thoat ill send the same command for them all, if dir is 1, only 1 pin is high and bolth h-bridges should behave in the same manner, and the PWM is the same for both ill send 1 pwm to 1 pin which runs both h-bridges at the same time... thats what i thoat, and wanna get this sorted as soon as possible so i dont get buttfucked by my bosses :)). Its kinda hard putting up with 10 hours of work and after that, concentrate on the "other project" but they dont care. Can u have some pointers, how should i implement a simplified version of ur code so that the robot balances itsself based on the kalman calculus uve done in the mpu6050 file and thats about it... is it complicated? jasons done some cool stuff with the motor control, he has an if decision there which does two types of commands for the motors, one aggressive command and one conservative command, thats kinda nice but i wont have the time to experiment with it. So, i only need the basics working, after that, ill build on it as you built on your own project which frankly is a super-project! Im very interested in this project, i love it, i want to take it to a whole new level, but for my own, not for some fool who thinks hes gonna unemploy me. Sorry for misspelling ive done so far, hope you can give me some pointers :). Thanks! Best regards, Cata PS: ill post the code i was talking about :slight_smile:

BalancingRobot.rar (4.11 KB)

@LillSlick
You should take a look at the old version of the code here: GitHub - TKJElectronics/BalancingRobotArduino: This is the Arduino version of the code for my balancing robot/segway, but basically you can just comment out these lines: Balanduino/Balanduino.ino at master · TKJElectronics/Balanduino · GitHub and then remove all the code related to the encoders. I would recommend that.

But you really need to work hard all weekend to make it in 4 days! It should be possible to make it balance if your hardware is alright.
This was my first attempt to make a balancing robot: TKJ Electronics » Sneak Peak: Segway guide + code. It didn't use the encoders - you might want to take a look at the source code below the video.

I just looked at Jason's robot: transistor.io, but he using a servo. I'm using a brushed DC motor.

How long are you? Have you build the entire hardware and checked that all hardware is working as intended? It's important that you get the basics like the IMU, motor and all the other stuff working before you start writing the software to make it balance.

I got all the hardware done, the H-Bridges and motors are working correctly, the mechanical part is balanced(almost standing on its own) wiring is done, the only thing i need to do is code. ill attach some pictures i took while building it, theyre not the final product as it looks now, but it should give you an idea :). Thanks for the pointers, i`ll get right on it!

The pictures are a little to big for the forum so i`ll share them like this:
https://dl.dropboxusercontent.com/u/40769383/20130623_190515.png
https://dl.dropboxusercontent.com/u/40769383/20130627_005027.png
https://dl.dropboxusercontent.com/u/40769383/20130702_120216.jpg

Looks alright. Maybe a little to wide and what's up with that big cable box on the middle level? Is there anything inside it? :slight_smile:

Any progress with the code?
I also noticed that you actually got encoders on those motors. If you have time I would recommend implementing them as well :slight_smile:

yes thats my power box, i got a battery in there 9V one, i dont know if its going to be enough... i hope so! with the code, ive tried some stuff, but i think i messed it up when I choose to command both Enable pins and Direction pins through the same pin, for the Enable the pin number 3 and for the Direction - pin number 1 on the arduino UNO. I`m using PmodHB5 drivers, up to 2A. I was using this code, your example for collecting and filtering data and my stupid coding :)) :

 Kristian Lauszus, TKJ Electronics
 Web      :  http://www.tkjelectronics.com
 e-mail   :  kristianl@tkjelectronics.com
 */

#include <Wire.h>
#include "Kalman.h" // Source: https://github.com/TKJElectronics/KalmanFilter

#define APWM 3          // left motor PWM
//#define RPWM 10         // right motor PWM
#define ADIR 1         // left motor direction
//#define RDIR 12         // right motor direction 

#define KP 0.5          // proportional controller gain [LSB/deg/loop]
#define KD 0.5          // derivative controller gain [LSB/deg/loop]

Kalman kalmanX; // Create the Kalman instances
Kalman kalmanY;

/* IMU Data */
int16_t accX, accY, accZ;
int16_t tempRaw;
int16_t gyroX, gyroY, gyroZ;

double accXangle, accYangle; // Angle calculate using the accelerometer
double temp; // Temperature
double gyroXangle, gyroYangle; // Angle calculate using the gyro
double compAngleX, compAngleY; // Calculate the angle using a complementary filter
double kalAngleX, kalAngleY; // Calculate the angle using a Kalman filter

uint32_t timer;
uint8_t i2cData[14]; // Buffer for I2C data
float output = 0.0; 

void setup() {  
    // Make sure all motor controller pins start low.
  digitalWrite(APWM, LOW);
 // digitalWrite(RPWM, LOW);
  digitalWrite(ADIR, LOW);
 // digitalWrite(RDIR, LOW);
  
  // Set all motor control pins to outputs.
  pinMode(APWM, OUTPUT);
 // pinMode(RPWM, OUTPUT);
  pinMode(ADIR, OUTPUT);
 // pinMode(RDIR, OUTPUT);
  pinMode(13, OUTPUT);
  
  Serial.begin(115200);
  Wire.begin();
  i2cData[0] = 7; // Set the sample rate to 1000Hz - 8kHz/(7+1) = 1000Hz
  i2cData[1] = 0x00; // Disable FSYNC and set 260 Hz Acc filtering, 256 Hz Gyro filtering, 8 KHz sampling
  i2cData[2] = 0x00; // Set Gyro Full Scale Range to ±250deg/s
  i2cData[3] = 0x00; // Set Accelerometer Full Scale Range to ±2g
  while(i2cWrite(0x19,i2cData,4,false)); // Write to all four registers at once
  while(i2cWrite(0x6B,0x01,true)); // PLL with X axis gyroscope reference and disable sleep mode 
  
  while(i2cRead(0x75,i2cData,1));
  if(i2cData[0] != 0x68) { // Read "WHO_AM_I" register
    Serial.print(F("Error reading sensor"));
    while(1);
  }
  
  delay(100); // Wait for sensor to stabilize
  
  /* Set kalman and gyro starting angle */
  while(i2cRead(0x3B,i2cData,6));
  accX = ((i2cData[0] << 8) | i2cData[1]);
  accY = ((i2cData[2] << 8) | i2cData[3]);
  accZ = ((i2cData[4] << 8) | i2cData[5]);
  // atan2 outputs the value of -π to π (radians) - see http://en.wikipedia.org/wiki/Atan2
  // We then convert it to 0 to 2π and then from radians to degrees
  accYangle = (atan2(accX,accZ)+PI)*RAD_TO_DEG;
  accXangle = (atan2(accY,accZ)+PI)*RAD_TO_DEG;
  
  kalmanX.setAngle(accXangle); // Set starting angle
  kalmanY.setAngle(accYangle);
  gyroXangle = accXangle;
  gyroYangle = accYangle;
  compAngleX = accXangle;
  compAngleY = accYangle;
  
  timer = micros();
}

void loop() {
  /* Update all the values */  
  while(i2cRead(0x3B,i2cData,14));
  accX = ((i2cData[0] << 8) | i2cData[1]);
  accY = ((i2cData[2] << 8) | i2cData[3]);
  accZ = ((i2cData[4] << 8) | i2cData[5]);
  tempRaw = ((i2cData[6] << 8) | i2cData[7]);  
  gyroX = ((i2cData[8] << 8) | i2cData[9]);
  gyroY = ((i2cData[10] << 8) | i2cData[11]);
  gyroZ = ((i2cData[12] << 8) | i2cData[13]);
  
  // atan2 outputs the value of -π to π (radians) - see http://en.wikipedia.org/wiki/Atan2
  // We then convert it to 0 to 2π and then from radians to degrees
  accXangle = (atan2(accY,accZ)+PI)*RAD_TO_DEG;
  accYangle = (atan2(accX,accZ)+PI)*RAD_TO_DEG;
  
  double gyroXrate = (double)gyroX/131.0;
  double gyroYrate = -((double)gyroY/131.0);
  gyroXangle += gyroXrate*((double)(micros()-timer)/1000000); // Calculate gyro angle without any filter  
 // gyroYangle += gyroYrate*((double)(micros()-timer)/1000000);
  //gyroXangle += kalmanX.getRate()*((double)(micros()-timer)/1000000); // Calculate gyro angle using the unbiased rate
  //gyroYangle += kalmanY.getRate()*((double)(micros()-timer)/1000000);
  
  //compAngleX = (0.93*(compAngleX+(gyroXrate*(double)(micros()-timer)/1000000)))+(0.07*accXangle); // Calculate the angle using a Complimentary filter
  //compAngleY = (0.93*(compAngleY+(gyroYrate*(double)(micros()-timer)/1000000)))+(0.07*accYangle);
  
  kalAngleX = kalmanX.getAngle(accXangle, gyroXrate, (double)(micros()-timer)/1000000); // Calculate the angle using a Kalman filter
  //kalAngleY = kalmanY.getAngle(accYangle, gyroYrate, (double)(micros()-timer)/1000000);
  timer = micros();
   
   //temp = ((double)tempRaw + 12412.0) / 340.0;
   
    // PD controller.
  output += kalAngleX * KP + gyroXrate * KD;
   
   // Clip as float (to prevent wind-up).
  if(output < -255.0) { output = -255.0; } 
  if(output > 255.0) { output = 255.0; }
  if(kalAngleX > 3) { 
  digitalWrite(ADIR, HIGH);
  analogWrite(APWM,output);
  }
  if(kalAngleX > 300) {
    digitalWrite(ADIR, LOW);
    analogWrite(APWM,output);
  }
 
 
 
  /* Print Data */
  /*
  Serial.print(accX);Serial.print("\t");
  Serial.print(accY);Serial.print("\t");
  Serial.print(accZ);Serial.print("\t");
  
  Serial.print(gyroX);Serial.print("\t");
  Serial.print(gyroY); Serial.print("\t");
  Serial.print(gyroZ);Serial.print("\t");
  */
  Serial.print(accXangle);Serial.print("\t");
  Serial.print(gyroXangle);Serial.print("\t");
  //Serial.print(compAngleX);Serial.print("\t");
  Serial.print(kalAngleX);Serial.print("\t");
  Serial.print(output);Serial.print("\t");  
  Serial.print("\t");
  
 // Serial.print(accYangle);Serial.print("\t");
 // Serial.print(gyroYangle);Serial.print("\t");
 // Serial.print(compAngleY); Serial.print("\t");
 // Serial.print(kalAngleY);Serial.print("\t");
  
  //Serial.print(temp);Serial.print("\t");
   
  Serial.print("\r\n");
  delay(1);
}

Based on your mpu6050 example for data filtering and i added some lines to make something move :slight_smile:
The wheels go in separate directions... it doesnt balance its self. Im going through your code now, im commenting the parts with the encoders and EEPROM readings and all the stuff that stands for the BT, PS3, WII so on... In the baladuino library ive modified the ports, i`m using pin 3 for the pwm and pin 1 for the enable signal that means portD,

/* Left motor */
#define leftPort PORTD // a fost #define leftPort PORTC
#define leftPortDirection DDRD

#define leftA PINC6 // PC6 - M1A - pin 23
#define leftB PINC7 // PC7 - M1B - pin 24
#define leftPWM PIND2  // a fost #define leftPWM PIND5 // PD5 - PWM1A (OC1A) - pin 18

/* Right motor */
#define rightPort PORTD
#define rightPortDirection DDRD

#define rightA PINB0 // PB0 - M2A - pin 25
#define rightB PINB1 // PB1 - M2B - pin 26
#define rightPWM PIND4 // same as my pin 5 --> PD4 - PWM1B (OC1B) - pin 17

/* Pins connected to the motor drivers enable pins */
const uint8_t leftEnable = 1; // was  const uint8_t leftEnable = 21;
//const uint8_t leftEnable = 4; // was const uint8_t rightEnable = 22;

One question until now : targetAngle - you said " the resting angle of the robot" <=== where do i find that? gyro reading? and when the robot is balancing or?

So now i`m going through your code and making something much more simpler, only to balance the robot.
I agree with you on the Encoders, I will implement them, but not now... first i want to show them a simple thing, then i can work on it to get the complicated stuff :slight_smile: make a REAL balancing robot.

I still need to figure out how this works

  /* Set PWM frequency to 20kHz - see the datasheet http://www.atmel.com/Images/doc8272.pdf page 128-135 */
  // Set up PWM, Phase and Frequency Correct on pin 18 (OC1A) & pin 17 (OC1B) with ICR1 as TOP using Timer1
  TCCR1B = _BV(WGM13) | _BV(CS10); // Set PWM Phase and Frequency Correct with ICR1 as TOP and no prescaling
  ICR1H = (PWMVALUE >> 8); // ICR1 is the TOP value - this is set so the frequency is equal to 20kHz
  ICR1L = (PWMVALUE & 0xFF);

  /* Enable PWM on pin 18 (OC1A) & pin 17 (OC1B) */
  // Clear OC1A/OC1B on compare match when up-counting
  // Set OC1A/OC1B on compare match when downcounting
  TCCR1A = _BV(COM1A1) | _BV(COM1B1);
  setPWM(leftPWM,0); // Turn off pwm on both pins
  setPWM(rightPWM,0);

and how to do it for arduino uno and for my pins :slight_smile:

getting this strange error.. dunno wtf to do about it :frowning:

In file included from Schita001.ino:2:
Balanduino.h:59: error: redefinition of 'const uint8_t leftEnable'
Balanduino.h:58: error: 'const uint8_t leftEnable' previously defined here
Schita001:11: error: 'Kalman' does not name a type
Schita001.ino: In function 'void setup()':
Schita001:18: error: 'rightEnable' was not declared in this scope
Schita001:75: error: 'kalman' was not declared in this scope
Schita001.ino: In function 'void loop()':
Schita001:118: error: 'kalman' was not declared in this scope
Schita001:125: error: 'kalman' was not declared in this scope
Motor.ino: In function 'void moveMotor(Command, Command, double)':
Motor:9: error: 'PINC7' was not declared in this scope
Motor:13: error: 'PINC7' was not declared in this scope
Motor.ino: In function 'void stopMotor(Command)':
Motor:32: error: 'PINC7' was not declared in this scope

with the code based on the data acquisition and something to get the motors moving i get some results but the Output of the PD controller is not very good... i`ll attach a file so you can take a look.

Sensors.txt (74.2 KB)

You are using an Uno (ATmega328P), right?

Then pin 3 is located on PIND3 and not PIND2.
See this page for more information: http://arduino.cc/en/Hacking/PinMapping168.

Target angle is the balancing point of the robot - this is usually 180, but you might have to change it if you CG is offset to either side. I used that when I mounted a GoPro on mine: http://www.kickstarter.com/projects/tkjelectronics/balanduino-balancing-robot-kit/posts/450808.

Regarding the PWM registers. You should read the datasheet: http://www.atmel.com/Images/Atmel-8271-8-bit-AVR-Microcontroller-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet.pdf at page 128 to 138.

This error is caused because you are defining the same varaible twice:

Balanduino.h:59: error: redefinition of 'const uint8_t leftEnable'
Balanduino.h:58: error: 'const uint8_t leftEnable' previously defined here

Try renaming one of them two "rightEnable":

Schita001:18: error: 'rightEnable' was not declared in this scope

You need to download the Kalman library I wrote: GitHub - TKJElectronics/KalmanFilter: This is a Kalman filter used to calculate the angle, rate and bias from from the input of an accelerometer/magnetometer and a gyroscope..

Schita001:11: error: 'Kalman' does not name a type

PINC7 is not available is on the ATmega328P:

Motor:9: error: 'PINC7' was not declared in this scope
Motor:13: error: 'PINC7' was not declared in this scope
Motor:32: error: 'PINC7' was not declared in this scope

Simply uncomment it for now.

Regarding the "Sensors.txt". Please tell me what is the output from each column or I will not be able to help you :slight_smile: