**EE403
ROBOTICS**

**Project
#1: **Inverted
Pendulum Cart

**Final Report
**2.5.2012

**
2005502016 - **Adnan Alper DEMIRCI

2006502071 -

**ABSTRACT**

Nowadays, there are some considerable applications
of inverted pendulum*. *The
dynamics of inverted pendulum simulates the dynamics of robotic arm in the
condition when the center of pressure lies below the centre of gravity for the
arm so that the system is also unstable. Robotic arm behaves very much like inverted pendulum*
*under this condition. Also, the inverted pendulum* *is widely accepted as an adequate model
of a human standing still (quiet standing). Inverted pendulum uses at in the
design of several early Seismometers, too. In addition to the applications, The
Segway PT is a two-wheeled, self-balancing
transportation machine. The dynamics of the Segway PT
are similar to a classic control problem, the inverted pendulum. Like these
applications, the dynamics of inverted pendulum can be used at different
applications.

This report is about a robotics project that tries
to realize an inverted pendulum.

The inverted pendulum system is a popular
demonstration of using feedback control to stabilize an open-loop unstable
system. It is simply a stick on a moving surface and desired to be in balance
perpendicular to its surface. It is often useful to demonstrate concepts in
linear control such as the stabilization of unstable systems. Different
mechanisms can be constructed to demonstrate the inverted pendulum,
therefore the moving cart is selected in this project to realize the inverted
pendulum.

In this system, an inverted pendulum is attached to
a cart equipped with a DC motor that drives it along a circular track. The cart
tries to hold the inverted pendulum in a desired interval of angle by moving
left or right on the circle.

Aim of the project is to control of an inverted
pendulum. There are some reasons to select an inverted pendulum which are it is
the most available system for laboratory usage and It is a nonlinear system,
which can be treated to be linear, without much error, for quite a wide range
of variation and an inverted pendulum
provides a good practice for prospective control engineers. This project
contains theoretical and experimental analyses of the system dynamics and
control methods. In this system, there is a bar which behaves as an inverted
pendulum. This bar is attached to a cart which is controlled by a PIC
microcontroller and a pivotal system to make inverted pendulum stable.

**Figure
1.1:**
A block diagram to the system.

**2.1.
Mathematical Analyses of Inverted Pendulum and Cart**

In this section, the suitable mathematical model
for an inverted pendulum system is derived. System model is shown below in
Figure 2.1.

**Figure
2.1:**
Free body diagrams of the system.

When the forces in the Free Body Diagram of the cart
in the horizontal direction are summed, the following equation of motion will
be found:

Also, we can say that forces in vertical direction
are unnecessary because of that summing of forces in vertical direction is not
being added to mathematical model for an inverted pendulum.

The force exerted in the horizontal direction due to
the moment on the pendulum is determined as follows:

Component of this force in the direction of N
is

The component of the centripetal force acting along
the horizontal axis is as follows:

Component of this force in the direction of N
is

When the forces in the Free Body Diagram of the cart
in the horizontal direction are summed, the following equation is founded for
N:

If this equation (6) is substituted into the first
equation (1), the first equation of motion for this system is founded.

To get the second equation of motion, the forces
perpendicular to the pendulum is summed. The vertical components of those
forces are considered here to get the following equation:

To get rid of the P and N terms in the equation above,
sum the moments around the centered of the pendulum to get the following
equation:

The second dynamic equation is founded with
combining these last two equations:

The set of equations completely defining the
dynamics of the inverted pendulum are:

These two equations are non-linear and need to be linearized for the operating range. Since the pendulum is
being stabilized at an unstable equilibrium position, which is ‘Pi’ radians
from the stable equilibrium position, this set of equations should be linearized about theta = Pi. Assume that theta = Pi + ø,
(where ø represents a small angle from the vertical upward direction).

Therefore, cos (theta) =
-1, sin (theta) = -ø, and (d(theta)/dt)^2 = 0.

After linearization the two equations of motion become
(where u represents the input):

Laplace transform of the system equations are taken
to obtain the transfer function of the linearized
system equations analytically the. The Laplace transforms are:

**2.2. Transfer Function of
Inverted Pendulum and Cart**

After finding the transfer function, initial
conditions are assumed to be zero. The transfer function is the ratio of
desired position [Output] with the force on the cart [Input].

If the angle Phi is as the output, the first
equation for X(s) will be as following equation,

Then, substituting into the second equation will
yield:

where,
*q *= (*M *+*m*)(*I *+*ml *^{2}) - (*ml*)^{2}.

If the transfer function will be simple, the
following equation is obtained:

The transfer function can thus be simplified as:

where,

If we NEGLECT THE FRICTION in the system, that is,
we take the coefficient of friction **b=0**, then

**2.3. State Space of Inverted
Pendulum and Cart**

There are several different ways to describe a system of linear
differential equations. The state-space
representation is given by the equations:

Where x is an n by 1 vector representing the state (commonly position and
velocity variable in mechanical systems), u is a scalar representing the input
(commonly a force or torque in mechanical systems), and y is a scalar
representing the output. The matrices A (n by n), B (n x 1), and C (1 x n)
determine the relationships between the state and input and output variable.
Note that there are n first-order differential equations. State space
representation can also be used for systems with multiple inputs and outputs
(MIMO), but we will only use single-input, single-output (SISO) systems in
these tutorials.

The linearized system
equations can also be represented in state-space form:

The C matrix is 2 by 4, because both the cart's
position and the pendulum's position are part of the output. For the
state-space design problem we will be controlling a multi-output system so we
will be observing the cart's position from the first row of output and the
pendulum's with the second row.

**2.4. PID Controller**

A proportional–integral–derivative
controller (PID controller)
is a generic control loop feedback mechanism widely used in industrial control
systems. A PID is the most commonly used feedback controller. A PID controller
calculates an "error" value as the difference between a measured
process variable and a desired set point. The controller attempts to minimize
the error by adjusting the process control inputs.

The PID controller calculation (algorithm) involves
three separate constant parameters, and is accordingly sometimes called three-term control: the proportional,
the integral and derivative values, denoted *P,* *I,* and *D.*
Heuristically, these values can be interpreted in terms of time: *P*
depends on the *present* error, *I* on the accumulation of *past*
errors, and *D* is a prediction of future
errors, based on current rate of change. The weighted sum of these three
actions is used to adjust the process via a control element such as the
position of a control valve, or the power supplied to a heating element.

In the absence of knowledge of the underlying
process, a PID controller is the best controller. By tuning the three
parameters in the PID controller algorithm, the controller can provide control
action designed for specific process requirements. The response of the
controller can be described in terms of the responsiveness of the controller to
an error, the degree to which the controller overshoots the setpoint
and the degree of system oscillation. Note that the use of the PID algorithm
for control does not guarantee optimal control of the system or system
stability.

Some applications may require using only one or two
actions to provide the appropriate system control. This is achieved by setting
the other parameters to zero. A PID controller will be called a PI, PD, P or I
controller in the absence of the respective control actions. PI controllers are
fairly common, since derivative action is sensitive to measurement noise,
whereas the absence of an integral term may prevent the system from reaching
its target value due to the control action.

**Figure
2.2.** A block diagram of a PID controller

**2.4.1. Proportional term**

The proportional term makes a change to the output
that is proportional to the current error value. The proportional response can
be adjusted by multiplying the error by a constant *K _{p}*, called the proportional gain.

The proportional term is given by:

A high proportional gain results in a large change
in the output for a given change in the error. If the proportional gain is too
high, the system can become unstable. In contrast, a small gain results in a
small output response to a large input error, and a less responsive or less
sensitive controller. If the proportional gain is too low, the control action
may be too small when responding to system disturbances. Tuning theory and
industrial practice indicate that the proportional term should contribute the
bulk of the output change.

**2.4.2. Integral term**

The contribution from the integral term is
proportional to both the magnitude of the error and the duration of the error.
The integral in a PID controller is the sum of the instantaneous error over
time and gives the accumulated offset that should have been corrected
previously. The accumulated error is then multiplied by the integral gain (*K _{i}*) and added to the controller
output.

The integral term is given by:

The integral term accelerates the movement of the
process towards setpoint and eliminates the residual
steady-state error that occurs with a pure proportional controller. However,
since the integral term responds to accumulated errors from the past, it can
cause the present value to overshoot the set point value

**2.4.3. Derivative term**

The derivative of the process error is calculated by
determining the slope of the error over time and multiplying this rate of
change by the derivative gain *K _{d}*. The magnitude of the
contribution of the derivative term to the overall control action is termed the
derivative gain,

The derivative term is given by:

The derivative term slows the rate of change of the controller
output. Derivative control is used to reduce the magnitude of the overshoot
produced by the integral component and improve the combined controller-process
stability. However, the derivative term slows the transient response of the
controller. Also, differentiation of a signal amplifies noise and thus this
term in the controller is highly sensitive to noise in the error term, and can
cause a process to become unstable if the noise and the derivative gain are
sufficiently large. Hence an approximation to a differentiator with a limited
bandwidth is more commonly used. Such a circuit is known as a phase-lead
compensator.

**3.1. How It Works**

An inverted pendulum stick is connected to the cart
with a potentiometer that is used for converting the resistance value to the
angle information of the inverted pendulum. The output of the potentiometer is
an analog signal, it is converted to digital at the
built-in ADC of PIC microcontroller. Then, according to the angle value, it is
decided whether there is a disturbance on the stick. If there is a disturbance,
the cart is moved on the direction which is same as the disturbance. This
movement creates a force on the stick and it goes towards the normal reference
position. This is repeated until the balance of the stick is provided.

**3.2. Used Parts**

A wooden, round table (radius is 35cm) is
constructed to prepare the base for the cart. The cart is basically in
triangular shape, and it is connected to the table centrically. There are two
wheels on the other corners of the cart. Therefore, the car makes a pivotal
motion around the table. The PCB card is mounted on the cart.

The inverted pendulum is attached to the cart via a
feedback potentiometer and it is 60cm long. PID constants (K, K_{i} and K_{d})
are represented by three potentiometers on the device. This allows the
constants to be adjusted so the PID control can be tuned for optimal
performance.

A 120rpm DC motor with 100:1 gear ratio is used in
order to move the cart. It is attached to one wheel.

PIC 16F887 is used in the project as a
microcontroller to control the data in overall system. Its built-in ADC and
ECCP (Enhanced Capture/Compare/PWM) modules have been very useful for this
project. Angle detection is provided by the potentiometer which is connected to
the inverted pendulum. The output voltage of the potentiometer is sent to the
ADC of the microcontroller. After it is digitized, it is converted to the
corresponding value of angle. Motor driving is also done with the
microcontroller using the ECCP module.

**Figure
3.1:**
Pin descriptions of PIC16F887.

The algorithm used to steady the pendulum is very susceptible
to high frequency noise from the feedback potentiometer. In order to prevent
this, a low pass filter was placed between this potentiometer and the A/D input
port on the microcontroller. The filter has a cutoff frequency at about 60Hz.

The H-bridge is simply a collection of electronic
switches to drive the motor and very popular in robotics. It allows the one to
move motors forward, backward and with varying speeds through PWM (pulse width
modulation). The switches on the H-bridge are built by TC4428, 1.5A dual high
speed power MOSFET drivers.

**Figure
3.2:**
H-Bridge schematic.

At
the end of the project, the hardware and mechanical parts are obtained and
tested for proving that they are working. But the overall inverted pendulum
system could not reach to work successfully because of struggles with software
programming and time constraints.

[1]
XK-DLB1
Inverted Pendulum Model, http://sdxk.en.alibaba.com/product/493712072-210752993/XK_DLB1_Inverted_Pendulum_Model.html

[2]
Inverted Pendulum Tutorial Web Page, http://www.library.cmu.edu/ctms/ctms/examples/pend/invpen.htm

[3] KATSUHIKO OGATA, __Modern Control Engineering__,
4 Edition, Pearson
Education International, U.S.A, 2002. pp 86-90, 681-703.

[4] A
block diagram of a PID controller, __http://radhesh.wordpress.com/2008/05/11/pid-controller-simplified/__

You
can download the ISIS schematic here.

You
can download the PCB layout here.

You
can download the program codes here.