2 wheel self balancing robot using arduino and IMU6050

IMG_2693_Medium.JPG (116699Bytes)
IMG_2694_Medium.JPG (109863Bytes)

I want to build a two wheel self balancing robot using Arduino and the IMU6050. After a few weeks looking around robot and arduino forums, without much luck, I thought perhaps users might share their experiences, ideas and code on this forum.

For starters, I built the platform out of 5mm plywood, using the following componnents

2x Tamiya 70168 Double Gearbox Kits
2x  Pololu Wheel 90x10mm
1x L298N Dual H Bridge motor driver
1x Arduino Uno
1x IMU6050 board
1x bluetooth module

I also built a 'cube' testing device so I can accurately measure the IMU6050 outputs for every 90 degrees of rotation.

So far the best results for accurate and driftless angle measurements have been using the Lauszus Kalman 6050 sketch below.

References:

Kalman filter for the IMU6050 by Lauszus
http://blog.tkjelectronics.dk/2012/09/a-practical-approach-to-kalman-filter-and-how-to-implement-it

2 and 4 wheel robot using an IMU6050 by Arkadi
http://forum.arduino.cc/index.php/topic,143261.msg1075848.html

Dorweiler 2 wheel robot
http://www.transistor.io/balancing-robot.html

Kerry Wong self balancing robot
http://www.kerrywong.com/2012/03/08/a-self-balancing-robot-i

Balancing robot for dummies by KAS
http://www.x-firm.com/?page_id=145
http://forum.arduino.cc/index.php/topic,60170.0.html

I found that having the

I found that having the right parts is important. I started with analog sensors and gave up as the measurements were very erratic (might have been bad sensors and or powersupply issues). Anyway having found inexpensive sources for the IMU6050 and having bought two of them I found that they do give consistent and accurate measurements.

Time will tell if the Tamiya gearmotors are OK for selfbalance, but just driving the motors directly through arduino and the bridge works fine.

I also use two LiPo cells for power, when not connected to the PC USB hub.

I started with the arduino miniPro and switched to the UNO as it has the USB port built in, making it easier for sketch uploading and testing.

one step at a time

Thanks for the comments on the motors and tips, and yes, it is more challenging than I thought. So I am taking this one step at a time. Understanding the orientation of the axis, offsets, filters, PIDs, timing loops etc. Actually I almost put in an advance order for the Balanduino kit (which use encoders and they sure have a stable 2 wheeled bot). So, having gone for a DIY mix of parts I am slowly moving ahead. For the moment I am concentrating on getting accurate angle measurements from the IMU.

update IMU6050 2 wheel robot

I am using the following for the test

-MiniPro instead of UNO as UNO seems to have bootloader bug

-IMU6050 mounted w xy plane perpendicular to wheel axis and Y axis points up, IMU centered on wheel axis

-MPU6050.ino modded sketch from Lauszus

 

https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/blob/master/IMU6DOF/MPU6050/MPU6050.ino

 

-added offset=272 to show 0 degrees when bot stands up vertically

-added below code so LED lights up when bot is within 1 degree of vertical

if (abs ( kalAngleX1 ) < 0.5 ) { digitalWrite(13, HIGH); }

else { digitalWrite(13, LOW); }

 

Below is serial line capture of degrees and time t in micros, every 100 ms when bot is near vertical:

0.08 t 23131148

0.05 t 23235356

-0.00 t 23339560

-0.03 t 23443788

-0.01 t 23548028

0.03 t 23652252

 

Is this looking good enough to move to next step?

 

These are the IMU6050 settings

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