The Self Balancing Robot Computer Science Essay

Published: Last Edited:

This essay has been submitted by a student. This is not an example of the work written by our professional essay writers.

This final year project has been designed for balancing the robot in to two wheels to achieve the research target; it has been looked up the previously designed robots on a balancing technology which based on wheels. It was necessary to study the methodology of balancing robots and how they work.

A final design was then laid out with decision on construction materials and power sources. CAD drawings, ORCAD 9.0, C++, microcontroller, MPLAB compiler IC burning software's being used to produce the final robot.

Testing the PCB circuitry, gyro sensors and DC gear motors carried out throughout the designing and construction process.



The study on balancing technology has been taken over all over the world in last ten years in the field of robotics. Because of the natural unbalanced dynamics of the system. Few robots are designed and categorized by having the ability to balance its self on two wheels and spin on the spot and few are categorized by following the laser light towards the line. This additional feature allows easy navigation on various terrains. These aptitudes have the ability to resolve the number of challenges in industrial automation and worldwide society. For example, an automated wheel chair using this robotic technology will gives the opportunity to the operator and also pick and place most of the disabled bodied to their right position. Small robotic carts being built by utilising this technology allows human to travel short distance in a covered area instead of using polluted cars buggies which is harmful for environment. This robot using PI controlled differential method of projectile trajectory. A gyroscope is used to measure the tilt of the robot and the encoders on the motors to measure the wheel's rotation.

Literature Review:

Conducting literature review prior to undertaking research projects is critical as this will provide the researcher with much needed information on the technology available and methodologies used by other research counterparts around the world on the topic. This stage provides a squashed summary of literature reviews on key topics related to balancing a two-wheeled self-balancing robot.

Balancing Robots Methodology:

The inverted pendulum problem is not very famous in the field of control engineering. The uniqueness and wide application of technology derived from this unstable system has drawn interest from many researches and robotics enthusiasts around the world. In current years, researchers have applied the idea of a mobile inverted pendulum model to various problems like designing the walking robot, robotic wheelchairs and personal transport systems and much more for disable people like robotic arm which can communicate with the thinking of human, robotic legs and much more in this robotic era. Such as an example given below.

Fig 1a Fig 1b Fig 1c

The paper 'Cooperative Behaviour of a Wheeled Inverted Pendulum for Object Transportation' presented by Shiroma et al. in 1996 shows the interaction of forces between objects and the robot by taking into account the stability effects due to these forces. This research highlights the possibility of cooperative transportation between two similar robots and between a robot and a human. The increasing of the old aged population like china, USA, Japan so the researcher has point out to made rest of the life on robots. According to this era robotic wheel chairs, robotic arms etc overcome the lots of difficulties and problems around the world. In advanced level the motion of a human modelled as an inverted pendulum designed on the real time motion of human robot that controls the centre of gravity which is 9.8m/sec by indirect calculation of zero point moment like a sea saw where the fulcrum on a pivot is stationary.

The Balancing Robot System

The balancing robot system is built as part of the thesis requirement to test out the performance of linear state space controllers in balancing an unstable system. It can be define as "a process for estimating the value of parameters in the presence of noise and time delays". The design of the system is kept as simple as possible but not compromising the aim of the project.

Robot Chassis

The robot's chassis design is based on an aluminium structure of 2 feet by 2 feet square shape with the components placed on the surface of wooden piece to prevent short circuitry and conduction. This simple design enables quick installation of component and the height of the robot can be increased or lowered when desired by removing the top fibre cover. The height of the chassis from ground level to the top is about 1 feet and 18 inches and the wheel is of 6 inch in diameter.

The drive train of the robot resembles a two wheeled differential drive robot, but the balancing robot balances the load with its wheels instead dragging the weight around on a pivot in a regular differential drive robot.

The motors are fitted into the socket like motor mounts machined from aluminium. This provides a stronger grip on the motors apart from the screw holes on the motor to avoid misalignment of the motors. The motor mounts are designed to have a maximum tilt angle of 30 degrees when the wheels are affixed; this is due to the limited measuring range of the sensors. The whole structure is held together with nuts both ends of the shaft.


Proportional Integral Offshoot:

Proportional-Integral-Offshoot can be defined as a control method where the generated signal is the functional error, the back ground of the error and the rate of change of error.

Loop diagram of PID

There are three unconnected parameters; proportional, integral and offshoot values. The proportional value will show the feed back to the current error, the integral value identify the reaction based on all of the current errors, and the offshoot value find out the reaction rate at which the error is being changed. The subjective amount of these three behaviour is used to regulate the procedure by the use of a organize component such as the position of a control device or the power supply of a heat factor.

By means of alteration the three constants or invariables in PID controller algorithm that can provide control action designed for precise process requirements. The controller response can be defined in terms of the reactivity of controller to an error. This is the degree at which the controller overshoots the set point and the degree of system fluctuation. Note that the usage of PID algorithm for control process does not ensure the optimal control of the system stability.

A number of applications may need using only couples of modes to give the suitable system rule. This is can be obtained by adjusting the gain of unneeded output control to zero value. In the lack of individual control manners sometimes PID controller called as PD, PI, P or I. Normally PI controllers are particularly common, since offshoot action is sensitive to measurement noise, while due to control action the absence of an integral value may protect the system from reaching its target value.

Note: For the relevant variables many naming conventions are in common use because of the range of control theory and applications.

Controlling Theory of PID:

The controlling theory of PID defines the comparable or non-interacting the PID controller. Hence:


Pout, Iout, and Dout conditions are defined below.

Proportional term

Graph showing the PV vs Time, for three different values of Kp (Ki and Kd are constants)

The proportional term and sometimes it is called as gain makes a change to the output i.e. directly proportional to the current value of error. This proportional gain can be obtained by multiplying the error with a constant Kp.

The proportional term is shown by:


Pout is a proportional output .

Kp is proportional gain i.e. a tuning parameter.

e represents Error i.e. SP - PV.

t represents time or instantaneous time.

The above graph shows that a high proportional gain always results in a large change of the output for a given error change. If the proportional gain is too small in the value, the control action may be too low when reacting to system disturbances similarly if the gain of proportionality will be high and a huge amount of change in the output for the given change of error the system will go towards non-stability. So we can say that a small gain always leads in a small output response to a large or huge input error, or may be less sensitive for a controller. If there are no disturbances, then proportional control will not adjust at its target value and will retain a stable state error i.e. the process gain and a function of the proportional. Industrial practice and tuning theory both suggest that it is the proportional term that should contribute the bulk of output change, regardless the steady-state offset.

Integral term

Graph of PV versus Time, for three different values of Ki while Kp and Kd held constant

The involvement of the integral term sometimes called a reset is directly proportional to the duration and magnitude of error. The contribution magnitude of this integral term with overall control procedure is determined by the gain of integral term Ki.

The integral term is given by:


Iout represents integral term for output

Ki represents integral gain i.e. a tuning parameter

e is a Error and its value equals to SP − PV

t is an instantaneous time (the present)

Ï„ is a integration variable

Offshoot term

Graph of PV versus Time, for three values of Kd now Kp and Ki kept as a constant

The change rate of the process error is determined by calculating the slope of error over time (i.e. its first offshoot with respect to time) and multiplying the rate of change by the offshoot gain Kd. This magnitude's contribution of offshoot term (many times called by rate) with overall control action is called as the offshoot gain, Kd.

The offshoot term is given by:


Dout represents offshoot term of output

Kd represents offshoot gain i.e. a tuning parameter

e is error and it equals to SP − PV

t is an instantaneous time or time at present


The proportional, integral, and offshoot all terms are added together to calculate the PID controller's output. Suppose u(t) is the controller output, then the final equation of the PID algorithm will be:

While the tuning parameters Kp (the proportional gain), Ki (the integral gain) and Kd (the offshoot gain) are defined below :

Kp (The Proportional Gain)

As we know that larger the error value the larger the proportional term compensation therefore larger values mean faster the response.

Ki (The Integral Gain)

Larger values mean steady state errors are eradicated more fastly and rapidly. And any negative error integrated during transient reaction must be unified away by positive error before reaching the steady state.

Kd (The offshoot Gain)

The Larger values decrease the overshoot but always decelerate the transient response and it may lead to instability of system due to signal noise amplification in differentiation of the error.

Output Offshoot

In offshoot of the output the PID controller measures the offshoot output quantity, rather than the offshoot of the error. The output remains continuous it means it never has a step change. Effectively, this output should have the same sign as the offshoot of the error.

Set point ramping

In this modification, the set point is steadily moved from its old value to a newly specified value by using a first order differential ramp function, and it avoids the discontinuity present in a step change.

Set point weighting

This uses the many different multipliers for the error depending on the component of the controller used. The error present in integral term must be the true control error in order to keep off the steady-state control errors. This will affect the controller's set point response, while these parameters don't affect the reaction to measurement noise and load disturbances.

Comparison of step response of the controlled second order process:

Zeigler Nichols controller can be used in order to get a smaller maximum overshoot and it also is found that the proposed controller is better option than directly tuned PID controller of Kitamori's. In Ziegler Nichols control rule the time constant of integral part is always four times larger as the offshoot time constant.

Limitations of PID control

PID controllers overcome many of the problems but they can perform poorly in some applications.

In this gyro PI based sensor having a set +/- 10 degrees of the tilt angle when it fall down more than 10 degrees there is another supportive circuitry can take over it for balancing to make it within the limit. When the PID controllers, being used independently, can give poor performance where the loop gains must be reduced so that the control system does not overshoot, oscillate or hunt about the control set point value. The control system performance can be improved by combining the feedback (or closed-loop) control of a PID controller with feed-forward (or open-loop) control. The feed-forward value alone can often provide the major portion of the controller output. The PID controller can then be used primarily to respond to whatever difference or error remains between the set point (SP) and the actual value of the process variable (PV). Since the feed-forward output is not affected by the process feedback, it can never cause the control system to oscillate, thus improving the system response and stability.

Physical implementation of PID control

Ideal versus standard PID form

The form of the PID controller most often encountered in industry, and the one most relevant to tuning algorithms is the standard form. In this form the Kp gain is applied to the Iout, and Dout terms, yielding:


Ti is the integral time

Td is the offshoot time

In the ideal parallel form, shown in the controller theory section

the gain parameters are related to the parameters of the standard form through and . This parallel form, where the parameters are treated as simple gains, is the most general and flexible form. However, it is also the form where the parameters have the least physical interpretation and is generally reserved for theoretical treatment of the PID controller. The standard form, despite being slightly more complex mathematically, is more common in industry.


Theory of Inverted Pendulum:

The inverted pendulum can be defined as the inversion of ordinary pendulum. The ordinary pendulum is falls down to earth due to the attraction of centre of gravity and its graph is based on velocity versus time that how many seconds it will take to complete one oscillation. And suppose the ordinary pendulum is turned to upward means inversely proportional to the earth is called inverted pendulum. Variations on this problem include multiple links, allowing the motion of the cart to be commanded while maintaining the pendulum, and balancing the cart-pendulum system on a see-saw. The inverted pendulum is related to rocket or missile management where driving force is actuated at the bottom of a tall vehicle. The largest implemented use is on huge lifting cranes on shipyards. When moving the shipping containers back and forth, the cranes move the box accordingly so that it never swings or sways. It always stays absolutely positioned under the operator even when moving or stopping quickly.

And it is obvious that inverted pendulum can be attract by the gravity which 9.8m/s so it will try to come down as a steady state condition but to maintain its position there is a huge roll of controller which is called PID can be discussed in chapter 2. There is a different mechanism to stabilize the inverted pendulum without any feedback loop or controlling theory which is providing the oscillation the support very quickly up and down. When the number of oscillations is If the oscillation is satisfactorily strong with respect to acceleration and amplitude then the inverted pendulum can recover perturbations in a conservative manner. If the driving point moves in simple harmonic motion, then the motion of pendulum can be derive by Mathieu equation.

Normally the inverted pendulum is frequently made up of an aluminium rod which is fixed with ball bearing pivot to force the oscillatory motion very conveniently.


Steady state condition of pivot:

The equation of motion is similar to that for an uninverted pendulum except that the sign of the angular position as measured from the vertical unstable equilibrium position:

= 0

To find out the angular acceleration the negative effect of gravity with respect to length along y axis goes to another side to get a equation of angular acceleration.

Thus, the inverted pendulum will accelerate away from the vertical unstable equilibrium in the direction initially displaced, and the acceleration is inversely proportional to the length. Tall pendulums fall more slowly than short ones.

Pendulum with oscillatory base

A schematic diagram of the inverted pendulum on an oscillatory base. The principle of this pendulum to less the weight of rod where the mass of the rod considered as "m" and the length of the rod denoted by "L".

The equation of motion for a pendulum with an oscillatory base is derived the same way as with the pendulum on the cart, using the Lagrangian. The position of the point mass is now given by:

And the velocity is found by taking the first offshoot of the position:

The Lagrangian for this system can be written as:

And the equation of motion follows from:

Resulting in:

Here y denotes a SHM(simple harmonic motion)

, y = asinωt, the following differential equation is:

Graph Plots for the inverted pendulum on an oscillatory base.

First graph plot shows the response of the pendulum on slow oscillations. Where gradually increments in oscillation shown by second graph plot.

An explanation for the above equation shows that the inverted pendulum holds upwards for fast oscillations. The first graph plot shows when y is moving gradually upwards towards 90 degrees and when it reaches at 90 degrees it will quickly fall over when disturbed from the upright position. While the holding angle 90 degrees exceeds after a bit, which means pendulum falls down towards the ground due to the centre of gravity. If y tends to be a fast moving oscillation the pendulum can be kept stable over the vertical position. The second graph plot shows that it get a disturbance from the vertically positioned around the angle (θ = 0) then the angle of deviation stays small and the pendulum does not drop down to the earth.


Principle of operation:

In this project the DC geared motors driven by the L298 IC SGS Thomson chip it drives up to 3A current dual full bridge driver based on H bridge BJT transistor which is able to drive high speed. It can be designed by FET but BJT has been chosen to design because FET is for voltage driving and BJT are for current so basically in this circuitry depend on current controlling.

Why L298 being used?

"If separated BJT being mounted and connected to link circuitry and it will create complicated circuitry instead of for BJTs used one IC based BJTs circuitry which can drive two motor at a time."

To get a logic pulses at high and low rate provided by 40106 IC this is Schmitt trigger hex inverter. It provides switches at different points for the positive and negative going input pulses.

LM324 is used as an operational amplifier. Before opamp there is a variable potentio meter being attached to control the intensity of light to trigging the Schmitt IC for moving the motor in both direction. And to control the sensitivity of IR. This circuit is open loop because of the sensitivity of IR and the gain of this open loop is max with respect to the reference voltage that is 0 to 1vdc. Why? Because the maximum response of IR is 0.6v. and have a 60% adjustable area. And the rest of the 40% is left as a ground voltage. The response of the opamp is like a sinusoidal but we need quick and accurate response for the control process implement a Schmitt trigger to get a accurate square waves to get a 0 or 1 not a gradually increment on the pulses.

After getting an accurate pulses connect to the PIC 16F84A controller. This whole proximity get by PIC from left side and same situation can be done for right side. Using 16F84A controlled the L298 IC , Gyro sensor which sense the rate of gyro that what kind of pulses it can get @ 1.2Khz, and both IR sensors.

There are two IR transmitter and receiver mounted on the surface of the chassis where two of them LED's transmit the light at 90 degrees and the middle one IC can receive the light and pass it to the opamp. And due to photoelectric effect PIC can read it and drive the motors according to the pulses. PIC can trig by 2.3 voltages. There are two out puts goes to H-bridge to give the direction to the motors by means of inverting the polarities.

Now the operation of gyro is to identify the tilt angle of the robot. There are four terminals of gyro being used one is for positive voltage , second for negative voltage , third for signal input which is directly connected to the PIC and fourth is for phase angle â±· it gives 120 pulses/degree it is basically 1.2Khz gyro.

How it works?

1.2 KHz / 120 = 10 degrees

Means left and right 10 degrees of freedom, if it falls down to right side the pulses on the left side will be increasing and decreasing the phase pulses like inversely proportional to each other. Basically if gyro is at 0 degree 1.2Khz pulses on both phases. And if gyro is go down by 1 degree so its means deduction of 120 pulses from phases one and additions of 120 pulses on phase two. This is PI controlled gyro. When the tilt angle frequencies being vanished where the gyro is going to unstable then the frequencies of IR being starting for responding the system. And automatically take over the balancing system.

Why use rate mechanical gyro instead of IC based gyro?

Reason for this drawback is availability, and financially sponsorship this can be designed and recommended for further advancement to get fully balanced robot without a degree of tilt.

Calculation to find out gain:

Formula of gain is Av = -R2 / R1


R2 = 100k

R1 = 10k

Both are connected to opamp as a potentio meter


Av = -10v

DC geared Motor:

It can operate a single battery of 12v DC able to carry out the peak current of 6A which is required to power up the system.

The DC geared motor is a 12v DC specified having a 30: 1 ratio of reduction gear box. This motor is bi directional motor for small and medium robotic project applications.

Operating Voltage 12Vdc

Operating Torque 1.5kg/cm

Shaft maximum rotation 170rpm

Maximum derive current 530mA

Current with no load 150mA

Power rated 4.22W

Revolution counts 360


There are two wheel mounted underneath the chassis fixed with iron strip to prevent with ply or jerk on the system between the geared motor and wheel there are two wheel bearing has been pressed to divide the load and to prevent the burning of the motor windings because the purpose for that to rotate the shaft without any load torque that helps the system for balancing.

The diameter of the wheel measured approximately with 5mm hub ready fitted mounts directly to the drive shafts of the geared motors.

Rate Gyro:

Ziegler-Nichols' Method:

There are two different methods to identify the parameters of PID controller were presented by ZIEGLER and NICHOLS methods in 1942. These methods are widely used either in original form or in some modification. They often form the basis for tuning procedures used by controller manufacturers and process industry. The controller parameters expressed by simple formulas.

The PID controller parameter obtained from the Ziegler Nichols step response method.

G(s) = 1/(s+1)3

Table 4.1:

Controller K Ti Td Tp

P 1/a 4L

PI 0.9/a 3L 5.7

PID 1.2/a 2L L/2 3.4L

Measurements on the step response give the parameters a = 0.218 and L = 0.806. now the controller parameters can be simulated by the given table 4.1 , the parameters of PID controller are K = 5.50 so, Ti = 1.61, Td = 0.403.


Programming of this robot is done on C++ because it is easy to compile and ease of controlling. There are lots of ways to do programming like assembly language, lab view, and java something like that.

INCLUDE "Modedefs.Bas"

DEFINE OSC 4 ' Set Xtal Frequency

' ** Declare Variables **


Hits_Left VAR BYTE

Hits_Right VAR BYTE


Gyro_Left VAR BYTE

Gyro_Right VAR BYTE

Gyro_Stbl VAR BYTE

Gyro_Stbl1 VAR BYTE

Left_Led VAR PORTB.0

Centre_Led VAR PORTB.1

Right_Led VAR PORTB.2




TRISB.3=1 ' Set PortB.3 as an Input (IR_Sensor)


GoSub Gyro



For Sample=1 TO 10

GoSub Ping_Left

IF IR_Sensor=0 Then Hits_Left=Hits_Left+1

GoSub Ping_Right

IF IR_Sensor=0 Then Hits_Right=Hits_Right+1


Pause 20

IF Hits_Left>=7 AND Hits_Right>=7 Then

Low Left_Led

Low Right_Led

High Centre_Led

GoTo Main


IF Hits_Left=10 Then

Low Right_Led

Low Centre_Led

High Left_Led

GoTo Main

IF Hits_Right=10 Then

Low Left_Led

Low Centre_Led

High Right_Led

GoTo Main


Low Left_Led ' Turn off all 3 Leds

Low Right_Led

Low Centre_Led

GoTo Main


For Ping_Dur= 1 TO 14

High Left_IR

@ Nop

@ Nop

@ Nop

@ Nop

@ Nop

@ Nop

@ Nop

@ Nop

Low Left_IR

@ Nop

@ Nop

@ Nop

@ Nop

@ Nop

@ Nop

@ Nop


Return ' Exit the subroutine


For Ping_Dur= 1 TO 14

High Right_IR

@ Nop

@ Nop

@ Nop

@ Nop

@ Nop

@ Nop

@ Nop

@ Nop

Low Right_IR

@ Nop

@ Nop

@ Nop

@ Nop

@ Nop

@ Nop

@ Nop



Gyro :

PulsIn PORTA.0,1,Gyro_Stbl

PulsIn PORTA.1,1,Gyro_Stbl1

IF Gyro_Stbl=80 Then



IF Gyro_Stbl= 70 Then

High PORTA.3


IF Gyro_Stbl = 90 Then

High PORTA.4





IC CD40106

IC L298

IC LM234





Rate gyro sensor (PI)


IC CD40106:

This IC called a Schmitt trigger IC. The purpose of this IC is to shaping the wave forms getting from the operational amplifiers and providing to the controller with high or low bits to drive the robot with respect to the motors. The logic diagram of that IC is given below.

IC L298:

IC L298 is a dual full bridge driver designed for motors with four BJT to accept the transistor transistor logic and there are two inputs to derive the DC motors one is for enable for giving a direction towards forward or reverse and second is for supply voltage.

PIC Micro controller 16F84A:

It is 18 pin enhanced electronically erasable programmable read only memory of 8 bit Microcontroller which is equivalent to a BYTE.

How programming works?

Firstly include file of Modedefs wchich is a base file to control the I/Os and define the oscillator to set the frequency of crystal oscillator which is physically builtin in the circuitry now to declare the variables to represent the functions of operations. Ping_Dur help to determines the address which is equal to BYTE so all the addresses like Hits_left, Hits_right , stabl of gyro towards left or right being initialized by "BYTE" consider 8 bits of data. Purpose of using left, centre or right LED is to identify the system where its actually falls. And these are all decleared by variable by using the ports of PIC. Now the sensors are being initialized or portB as a variable if system is out of stable to control by the IR sensors to get the data on port B.3 and there are right and