Introduction: Making a Mini Self-balancing Car in Two Steps

Picture of Making a Mini Self-balancing Car in Two Steps

A self-stabilizing motor that will never fall sounds great. Many geeks playing arduino has come out of many self-stabilizing projects like self-stabilizing platform, motorbike etc. In this instructable we will introduce an amazing arduino shield that can help you make a self-stabilizing project easily. The stabilizer shield use L298P chip that is a high voltage, high current dual full-bridge driver designed to accept standard TTL logic levels and drive inductive loads such as relays, solenoids, DC and stepping motors. Besides, stabilizer shield has a standard XBee socket, so that you can use Bluetooth to control a self-balancing robot car moving forward and back.

Next step of this project:

  • Adjust demo, accurate the PID parameters to make the car self-stabilizing more stable
  • Use Smartphone to control the car move forward and back or even turn around.

Stay tuned!

Step 1: Hookup

Picture of Hookup

Things we use in this instructables:

Stabilizer is a plug-and-play arduino shield. Plug the shield in Iteaduino UNO mainboard, after assemble the car, you just need to connect motors to stabilizer shield by wires. Then the hardware hookup is done.

More about Stabilizer shield, visit its WIKI page

Step 2: Debug

Picture of Debug

The stabilizer shield demo download link:

http://blog.iteadstudio.com/wp-content/uploads/2015/03/Stabilizer.zip

Only upload the Stabilizer.ino to UNO, I2C.ino is a support file when Stabilizer.ino compile.

After we upload the demo to Iteaduino UNO, the rest work is to debug the hardware and software problems.

Now we will list some common problems we meet when we are making this project.

Hardware Problems

Stabilizer shield has been test strictly, so seldom hardware problems occur. But there are some tips.

1. The attitude data read normally, but motor doesn’t work. If this problem occurs, you’d better check the motor wire connection order. Please see the picture to learn what exactly each motor pin is.

When you use one pulse output signal, you can only measure the motor speed. If you use two pulse output signals, you can measure the motor speed and direction in the same time.

2. You can’t upload the demo to UNO when the XBee and jumper have already plugged in stabilizer shield. Since the serial ports have been taken, you’d better unplug jumper first, and then upload the demo.

3. When you want to make the XBee communicate with PC, you should plug jumpers in the B side. Since the serial ports have been taken, you’d better upload an empty application to arduino, and then they can be communicated normally.

Software Problems

Software debugging is very important. It divides into four parts.

  • First, read the MPU6050 accelerometer and gyroscope data through I2C to get the attitude data.
  • Second, Kalman filtering, namely integrate the data from accelerometer and gyroscope to reach to the real angle of inclination.
  • Third, calculate the speed and direction of wheels respectively. We use PID algorithm to calculate and control car’s movement, so that the car can keep self-balancing.
  • Forth, motor driver. The results of PID algorithm will turn into commands to drive the motor moves. You can see more codes details in the demo.

Tips for software debugging

1. Pay attention to data filtering of accelerometer and gyroscope. We use three-axis accelerometer and three-axis gyroscope. Refer to the top picture of our project, X is the car movement direction, Y is the direction of wheel and axle. The accelerometer data shall be collected between the X axis and Z axis, and turn the radian into angle. The direction of gyroscope shall keep in Y.

2. The angle direction calculated by accelerometer must be in accord with the angle direction calculated by gyroscope, if not, the car can’t be self-balancing.

3. When calculate the position of wheels, you should pay attention to the direction as well.

Step 3: Note

To debug the parameter of PID is the most important thing in this project. A decimal point or a number will decide whether the car can be self-balancing moved forward.

Meanwhile, the Bluetooth function has been written in the demo, but here we didn’t show up. Welcome more makers to hack this shield.

Last, I record a short video. The self-balancing car looks so stumble. I think maybe he has a very gentle temper or maybe he is gonna power off. : P What do you guys think? Hope you enjoy this project.

Main Contributor: Yun Li, hardware engineer from ITEAD studio.

Comments

tmoir (author)2015-03-29

Also could you please publish the Kalman filter equations you are using in the code. What is the state-space model you use? What is the Riccati equation. Thanks you.

ITEAD STUDIO (author)tmoir2015-03-29

Kalman filter equations using in the code:

/*************THE KALMAN FILERING ***************/

double P[2][2] = {{ 1, 0 },{ 0, 1 }};

double Pdot[4] ={ 0,0,0,0};

static const double Q_angle=0.001, Q_gyro=0.003, R_angle=0.5,dtt=0.01,C_0 = 1;

double q_bias, angle_err, PCt_0, PCt_1, E, K_0, K_1, t_0, t_1;

double angle,angle_dot,aaxdot,aax;

double position_dot,position_dot_filter,positiono;

/*************************************************************************/

void Kalman_Filter(double angle_m,double gyro_m)

{

angle+=(gyro_m-q_bias) * dtt;

Pdot[0]=Q_angle - P[0][1] - P[1][0];

Pdot[1]=- P[1][1];

Pdot[2]=- P[1][1];

Pdot[3]=Q_gyro;

P[0][0] += Pdot[0] * dtt;

P[0][1] += Pdot[1] * dtt;

P[1][0] += Pdot[2] * dtt;

P[1][1] += Pdot[3] * dtt;

angle_err = angle_m - angle;

PCt_0 = C_0 * P[0][0];

PCt_1 = C_0 * P[1][0];

E = R_angle + C_0 * PCt_0;

K_0 = PCt_0 / E;

K_1 = PCt_1 / E;

t_0 = PCt_0;

t_1 = C_0 * P[0][1];

P[0][0] -= K_0 * t_0;

P[0][1] -= K_0 * t_1;

P[1][0] -= K_1 * t_0;

P[1][1] -= K_1 * t_1;

angle+= K_0 * angle_err;

q_bias += K_1 * angle_err;

angle_dot = gyro_m-q_bias;

}

--------------------------

To adjust the balancing, adjust the numbers in these four lines code:

//The balance PID

k1=43;//24.80;

k2=1.2;//1.4;//9.66;

k3=-0.2;//-0.88 ;//4.14;

k4=-0.65;//-0.55;//0.99;

---------------------------

You seems have a very deep thought about the code. Recently, I try to modify the code and hope to make the self-balancing car to stand still and never fall, but it seems a little hard. No matter how hard I try, it will swing, and you know just like the video, move forward... So any suggestion?

tmoir (author)ITEAD STUDIO2015-03-30

I could see the code ok - I wondered if you could publish a PDF or something of the Kalman filter equations explaining your model, error covariance matrix and so on. What is your A,B,C,D matrices and so on. How did you get the noise variances.This would be a great help to people I think in understanding.

tmoir (author)2015-03-23

Is this an instructable or an ad? Where are the instructions, code etc. Do we have to buy a kit?

ITEAD STUDIO (author)tmoir2015-03-24

Code is in the second step, the download link is

http://blog.iteadstudio.com/wp-content/uploads/201...

Two files - Stabilizer.ino & I2C.ino have been compressed in a zip. There is no need to upload I2C.ino to UNO.

You can use Arduino UNO to replace Iteaduino UNO.

Yun Li develop this stabilizer shield aiming at simplifying his self-balancing car project, and he just share a part of it to us. The complete datasheet of this project can be found in its wiki page: http://wiki.iteadstudio.com/Stabilizer_Shield

There is no need to buy a kit, any DC motor with encoder will be OK. Just pay attention to the Pin order of the DC motor.

tmoir (author)ITEAD STUDIO2015-03-29

Thank you, I tried the code and the alarm goes off! I was just trying the Stabilizer code on its own - no motors etc. I take it this is because I haven't connected the motors and encoder output yet?

ITEAD STUDIO (author)tmoir2015-03-29

The code can be verified without connecting to motors. When you open the stabilizer.ino, I2C.ino will appears as well. Then Choose the board Arduino UNO. If I2C.ino didn't appear, the verification will be failed.

RtyQ (author)2015-03-21

two of those on top of each other to balance a drink, woul it work? (will look awesome at a party)

ITEAD STUDIO (author)RtyQ2015-03-22

Yes, I've seen a Chinese guy doing that before, and his robot car can self-balancing even a bottle of water placed on top, then he use a controller to make the car move around. He upload the video in youku.com - the chinese youtube. I've stunned and hope to copy what he has done.

cbrown_bh (author)2015-03-21

This is awesome!
What are the specs on the motors you used?

ITEAD STUDIO (author)cbrown_bh2015-03-22

Hi, the Kit of this self-balancing robot car will be available soon in ITEAD, and price will be favorable.

tytower (author)2015-03-22

Think I will wait for the technology to advance a bit before I ride one. After all I doubt god will come down and finger my wheel for me to help me out of trouble . Good stuff though , gotta have a looksee at the code

About This Instructable

21,224views

342favorites

License:

Bio: Love DIY, embedded, home automation, HMI, robotic.
More by ITEAD STUDIO:Remote Temperature Monitor with AlarmNextion Arduino Project: Whac-A-Mole Crazy Cony GameMaking a Mini Self-balancing Car in Two Steps
Add instructable to: