# Self balancing robot which uses PID and inverted pendulum The Problem statement

This blog is going to be long and I am very hopeful that all of you will find it useful. Once you have made some basic robots like the ones mentioned in our easy robot ideas here you would want to make something more challenging. At this juncture, we would recommend that you try making a balancing robot.

Yes, as the name suggest this robot will balance its body weight and would be balanced over two wheels. Such a kind of model is generally expressed in Physics as an inverted pendulum and it is considered to be a highly unstable system in itself as it has more weight over its head and can not balance by itself.

This also poses a fundamental problem statement in control system and amateur robotics use number of ways to solve this problem. We replace the problem of the inverted pendulum in a balancing robot and use a common way called as PID to devise a self-balancing robot.

Before more of theories and jargons have a look at the below video which shows one of our favourite robotic toys called as MIP which balances itself as a pro. We love this robot. Now that you have seen a balancing robot which uses the concept of an inverted pendulum let us try to understand what actually is an inverted pendulum from the pages of physics. Look at the picture below which is an inverted pendulum over a cart. The cart is part of the overall control system just as our self-balancing robot. The way to balance this system is to find the speed of rotation and direction of the wheels when there is an angle of tilt. If the head tilts towards forward then to balance it the cart has to move forward at a given speed and vice versa if the head moves by an angle backwards.

We replace the problem of the inverted pendulum in a balancing robot and use a common way called as PID to devise a self-balancing robot.

This forms the problem statement of a self-balancing robot.

So what is the solution?

One of the solutions for such a system is to rely back on a closed loop control system mechanism called as PID. A PID controller continuously calculates an error value e(t) as the difference between the desired set point and a measured process variable and applies a correction based on proportional, integral, and derivative terms (sometimes denoted P, I, and D respectively) which give their name to the controller type. We would need to implement this PID system in our robot to achieve success. A PID controller continuously calculates an error value e(t) as the difference between the desired setpoint and a measured process variable and applies a correction based on proportional, integral, and derivative terms.

P accounts for present values of the error. For example, if the error is large and positive, the control output will also be large and positive.

I accounts for past values of the error. For example, if the current output is not sufficiently strong, the integral of the error will accumulate over time, and the controller will respond by applying a stronger action.

D accounts for possible future trends of the error, based on its current rate of change.

The formula using these parameters for PID is:

u(t)=Kpe(t) + Ki(intregal)e(t) + Kd*de(t)

where, Kp,Ki,Kd are all non-negative, denote the coefficients for the P,I,D as stated above.

Now that we know how to solve this and also have an equation we need to model this concept. You may use Matlab, GNU Octave or even an excel to simulate this formula. Below is a link to an excel version of PID simulation. Once you have mastered the basic model you should attempt to code in the language you plan to control the microcontroller and this would be generally C++ considering how popular Arduino is. The good news is Arduino provides libraries and you may also do without library as we will show next.

u(t)=Kpe(t) + Ki(intregal)e(t) + Kd*de(t)

where, Kp,Ki,Kd are all non-negative, denote the coefficients for the P,I,D as stated above.

Dummy code block 1 #

Here you may need to define the senor pin which would read the physical value - for a balancing robot this can be done using a gyrospcope. You would also need to define your motor pins and values of Kp,Ki,Kd. Please donot miss to declare the PID variables as float and do not declare as integers.

int sensorGyroscope_pin = A1; //initialize sensor pin float sensor_read = 0 ; //sensor data int motor_pin = 2; //initialize motor pin int dir_pin = 3; float kp = 1 , ki=1 , kd=0.1;

Dummy code block 2 #

Next you would need to devise the feedback part for sampling and error. This can be done as shown below.

delta_t = micros() - time_old; //calculate sampling time sensor_read = analogRead(sensor_pin); float error = reference - sensor_read ; //calc. error "e" float E = E + (error * delta_t); //calc. error integration float e_dot = error - error_old;

Dummy code block 3 #

Finally you would need to apply the formula to get the actual value of the control variable. Depending on the value of the control variable from PID you may do some actions as move the motors forward by a certain RPM or reverse.

int_term = E ; //calc. itegrator term float u = kp* error + guard * ki * int_term + kd * diff_term ; //apply PID

if ( u < 0 ){ //reverse digitalWrite(dir_pin , HIGH); }else ( digitalWrite(dir_pin , LOW); }

if ( u < -255){ // Do motor actions

.......... analogWrite (motor_pin , abs(u));

So the coding is now done and we need to consider the electronics especially the senor to use and the type of motors. There are couple of sensors to consider here. The most common ones which are used are -

GY-80 BMP085 9-Axis Magnetic Acceleration Gyroscope Module for Arduino,

3 Axis MPU-6050 Accelerometer Sensor & Gyroscope 6DOF Module 3.3V-5V,

9DOF MPU-9150 3 Axis Gyroscope Accelerometer Magnetic Field Arduino.

You can use anyone of the above three. All are cheap sensors and ideal for experiments. Just bear in mind that you would need to solder the header stacks pins else you will not get reading and please do follow the voltage requirement and use logic converts as needed. For the motors we recommend that you use geared DC motors for motor precision control. You may also devise a RC or WIFI enabled version by attaching the RF or Wifi add on and making a controller. Please note we rate this kind of robot as

float u = kp* error + guard * ki * int_term + kd * diff_term

intermediate level and if you are just starting focus on something simpler like this robot working on TV remote control. Hope this blog helped you in getting all information in one place. Please feel free to comment and like.