Lab 5 (Part 2)- PID control and Tuning
Lab 5 (Part 2)- PID control and Tuning
Lab 5 (Part 2)- PID control and Tuning
PID
Control
PID
(Proportional,
Integral,
Derivative)
control
is
a
method
used
to
achieve
and
maintain
a
desired
system
set
point
such
as
temperature,
speed,
or,
in
our
case,
angle.
A
PID
control
loop
consists
of
a
sensor,
a
PID
controller
and
a
process.
The
sensor
determines
the
measured
value
(angle,
temperature,
speed
etc).
The
PID
controller
then
compares
the
measured
value
with
the
desired
set
point
and
determines
what
process
is
required.
This
Information
is
sent
to
a
transducer
that
will
accomplish
the
required
process
(rotating
a
motor,
turning
up
the
heat
etc).
This
loop
can
be
seen
below.
Figure
1-‐
PID
Control
Loop
For
example,
if
you
wanted
to
control
speed,
the
set
point
would
be
the
speed
you
want
to
maintain.
The
PID
controller
would
determine
the
difference
between
your
current
speed
and
the
desired
speed.
From
there
it
would
determine
what
process
is
required,
i.e.
do
you
need
to
accelerate
or
decelerate
and
by
how
much.
The
controller
would
send
this
information
to
the
transducer,
let's
assume
it's
a
motor,
and
motor
would
change
its
velocity
in
order
to
reach
the
set
point.
The
PID
control
loop
is
really
just
an
equation
written
in
your
code
that
calculates
the
process
required
to
obtain
the
set
point,
given
the
measured
value.
The
general
form
of
this
equation
is:
𝑑
𝐶𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟
𝑂𝑢𝑡𝑝𝑢𝑡 = 𝐾. ∗ 𝐸𝑟𝑟𝑜𝑟 + 𝐾2 ∗ 𝐸𝑟𝑟𝑜𝑟 +
𝐾3 ∗ 𝐸𝑟𝑟𝑜𝑟
𝑑𝑡
Where
KP
is
the
proportional
gain,
KI
is
the
integral
gain,
KD
is
the
derivative
gain
and
Error
is
the
difference
between
the
current
value
and
the
desired
value
(Error
=
Value
-‐
SetPoint).
The
∑Error
is
the
summation
of
all
the
previous
error
values
and
dError/dt
is
the
rate
of
change
of
the
error.
For
our
purposes,
the
controller
output
would
be
the
PWM
signal
that
is
sent
to
the
motor.
Since
the
PWM
signal
needs
to
be
a
value
between
0-‐255,
a
scaling
factor
is
used
to
convert
the
controller
output
into
a
PWM
signal.
Determining
this
scaling
factor
and
the
gain
constants
(KP,
KI
and
KD)
is
called
tuning
the
control
loop.
We
will
learn
how
to
tune
our
PID
control
loop
in
the
following
sections
but
for
now
let's
just
set
up
the
basic
code
for
PID
control.
To
start
off,
you
can
just
choose
arbitrary
values
for
the
gain
constants.
We
will
set
the
KD=KI=
0
and
KP
=
50.
This
means
we
are
just
working
with
proportional
control.
The
PID()
function
below
shows
you
how
to
implement
the
basic
PID
equation
in
code.
It
calculates
the
error,
integral
of
the
error
and
the
derivative
of
the
error
and
uses
those
to
determine
the
PID
output.
In
this
case,
the
error
term
is
angle
that
was
found
from
the
accelerometer/gyroscope
functions
written
in
Lab
3.
The
output
value
(Drive)
will
need
to
multiplied
by
the
scale
factor
before
it
can
be
sent
as
a
PWM
signal
to
the
motor.
For
the
inverted
pendulum
lab,
your
angle
error
will
be
determined
by
reading
the
encoder
values
and
the
Drive
signal
will
be
sent
to
the
motor
which
will
move
the
slider.
Once you have set-‐up your basic PID control function, you are ready to start tuning it.
PID
Tuning
PID
tuning
is
the
process
of
determining
the
gain
constants
(KP,KI
and
KD)
in
the
PID
equation,
shown
below,
and
the
scaling
factors
required
to
convert
the
PID
output
into
the
desired
system
response.
𝑑
𝑃𝐼𝐷
𝑂𝑢𝑡𝑝𝑢𝑡 = 𝑲𝑷 ∗ 𝐸𝑟𝑟𝑜𝑟 + 𝑲𝑰 ∗ 𝐸𝑟𝑟𝑜𝑟 +
𝑲𝑫 ∗ 𝐸𝑟𝑟𝑜𝑟
𝑑𝑡
In
this
lab
we
will
learn
how
to
do
this
by
creating
and
tuning
the
PID
controls
for
the
inverted
pendulum.
The
inverted
pendulum
system
is
very
similar
to
the
balancing
robot
you
will
be
building
later
in
this
course
and
should
give
you
an
idea
of
how
to
tune
your
robot.
In
basic
PID
control
loops,
the
error
term
is
the
term
that
can
be
directly
controlled.
Ex.
If
you
want
to
maintain
a
set
speed,
you
can
directly
change
that
speed
depending
on
the
error
between
the
speed
you
want
and
the
speed
you
currently
have.
In
the
case
of
the
inverted
pendulum
and
balancing
robot,
we
want
to
control
the
angle,
but
cannot
directly
change
it.
We
must
change
the
linear
speed
of
the
cart
which
will
then
affect
the
angle.
This
makes
it
more
complicated
than
the
basic
PID
control
and
will
require
more
tuning.
PID
Constants
In
order
to
tune
your
PID,
you
need
to
understand
what
each
of
components
in
the
PID
control
does
and
how
it
affects
the
overall
output.
A
brief
summary
of
each
PID
component
can
be
found
below,
but
if
you
require
more
information,
the
PID
Control
Wikipedia
page
has
a
more
thorough
explanation.
It
can
be
found
at:
http://en.wikipedia.org/wiki/PID_controller
Proportional
Term
(
𝑲𝑷 ∗ 𝑬𝒓𝒓𝒐𝒓)
The
proportional
term
produces
an
output
value
that
is
proportional
to
the
current
error
value.
In
the
case
of
the
inverted
pendulum,
the
error
would
be
the
pendulum
angle.
This
is
useful
when
a
fast
response
is
required.
The
integral
term
is
proportional
to
the
magnitude
and
duration
of
the
error.
Increasing
the
integral
term
will
smooth
the
system
response.
In
the
case
of
the
inverted
pendulum
this
is
very
useful
because
you
want
smooth
transitions
between
speeds,
as
jerky
movements
would
cause
the
pendulum
to
fall
over.
This
term
is
useful
when
trying
to
eliminate
steady
state
error.
One
issue
that
you
need
to
be
aware
of
when
using
the
integral
term
is
"windup".
Since
the
integral
term
sums
up
the
error
history,
if
the
system
starts
very
far
from
the
desired
set
point,
i.e.
the
initial
error
terms
are
very
large,
the
integral
term
will
grow
very
quickly.
Once
this
occurs,
the
integral
term
will
stay
large
and
will
dominate
the
output
term.
This
can
prevent
the
system
from
achieving
the
desired
set
point.
This
problem
can
be
avoided
by
adding
a
threshold
value
in
the
code.
If
the
error
is
greater
than
the
threshold,
the
integral
term
is
set
to
zero.
That
way
the
integral
term
will
only
come
into
play
when
the
measured
value
is
close
to
the
set
point.
𝒅
Derivative
Term
(
𝑲𝑫 ∗ 𝑬𝒓𝒓𝒐𝒓)
𝒅𝒕
The
derivative
term
is
proportional
to
the
change
of
error
over
time
(the
slope
of
the
error).
It
can
often
make
motion
more
jerky.
It
is
likely
best
to
start
with
the
derivative
term
set
to
zero
and
add
it
in
afterwards.
Summary
The
table
below
summarizes
the
effects
of
each
gain
parameter
on
the
system
response.
Figure
2-‐
Effects
of
increasing
each
gain
term
separately.
Found
at:
http://en.wikipedia.org/wiki/PID_controller.
Scaling Factors
Scaling
factors
are
used
to
convert
the
PID
output
into
the
desired
signal.
For
example,
let's
say
your
PID
output
ranged
between
-‐4400
and
4400
and
you
wanted
to
convert
that
output
into
a
PWM
signal
to
send
the
motor.
The
PWM
signal
ranges
from
0-‐255
and
the
motor
can
run
in
either
direction.
That
means
the
PWM
signal
range
is
effectively
-‐255
to
255.
So
your
scaling
factor
would
need
to
be
17.25
(4400-‐-‐4400)/(255-‐-‐255).
Dividing
your
PID
output
signal
by
this
value
would
convert
it
into
the
necessary
PWM
signal.
If
your
PID
output
range
is
unpredictable
or
difficult
to
determine
you
may
want
to
cap
the
output
to
ensure
you
never
send
more
than
255
to
the
motor.
To
do
this
you
would
simply
use
an
if
statement
so
that
if
the
PWM
value
is
greater
than
255,
you
reset
it
to
255.
That
way
even
if
there
is
a
spike
in
your
PID
output,
your
PWM
value
will
still
remain
within
the
bounds.
Once
you
understand
the
constants
you
can
modify,
you
are
ready
to
begin
actually
tuning
your
control
loop.
There
are
many
different
ways
to
tune
a
PID
loop,
but
they
all
involve
some
sort
of
trial
and
error.
There
are,
however,
ways
to
minimize
the
trial
and
error
and
help
you
achieve
the
desired
result
more
quickly.
This
section
goes
over
a
few
tips
that
will
help
you
tune
your
system
more
effectively.
Constructing
a
model
in
Matlab
can
give
you
a
good
starting
point
for
your
PID
gain
constants,
reducing
the
amount
of
trial
and
error
required.If
you
wish
to
model
the
inverted
pendulum
system
in
Matlab,
the
following
link
may
be
useful
to
you:
http://ctms.engin.umich.edu/CTMS/index.php?example=InvertedPendulum§ion=SystemModeling
Remember
the
real
life
system
will
be
much
more
complex
than
the
model
due
to
motor
constraints,
friction
etc.
so
modelling
the
system
will
only
give
you
a
basic
starting
point.
The
more
complex
the
system,
the
more
difficult
it
is
to
model
and
the
less
accurately
the
model
will
reflect
real
life
Ziegler-‐‑Nichols
Method
The
Ziegler-‐Nichols
method
is
a
basic
process
for
tuning
PIDs.
It
starts
by
setting
the
integral
term
(KI)
and
the
derivative
term
(KD)
to
zero.
You
then
increase
the
proportional
term
(KP)
until
the
system
oscillates
at
a
constant
amplitude.
This
is
called
the
ultimate
gain
(KU).
Once
you
have
reached
this
point
you
use
the
KU
value
and
the
oscillation
period
(TU)
values
to
set
the
remaining
gains.
This
is
not
particularly
useful
for
the
inverted
pendulum
rig
used
in
this
lab
as
it
will
not
oscillate
due
to
motor
constraints,
but
it
may
be
useful
for
your
final
project
or
other
PID
problems.
The
purpose
of
this
lab
is
not
to
spend
hours
of
trial
and
error
trying
to
get
the
inverted
pendulum
to
balance.
It
is
to
understand
how
a
PID
system
works
and
how
to
properly
tune
one.
This
section
will
go
over
a
few
tricks
that
may
be
useful
as
well
as
summarize
some
of
the
system
constraints
that
you
will
need
to
consider
when
tuning.
System
Constraints
The
inverted
pendulum
uses
a
stepper
motor
that
can
operate
like
a
DC
motor.
This
motor,
however,
still
has
a
few
constraints.
Motion
limitations
The
code
below
is
an
example
of
how
you
could
do
it.
It
splits
the
code
up
into
two
situations.
The
first
is
when
the
pendulum
is
falling
forwards
and
the
rig
was
already
moving
forwards
and
the
second
is
when
the
pendulum
is
falling
forwards
and
the
rig
was
moving
backwards.
In
the
first
situation,
you
do
not
need
to
switch
direction
so
no
stopping
is
required.
In
the
second
situation
you
want
to
be
moving
forwards,
but
the
rig
is
moving
backwards
so
you
need
to
switch
direction.
This
means
you
need
to
first
stop,
delay
and
then
change
direction.
Calibrating
Limitations
Often
inverted
pendulums
can
move
360°,
meaning
they
usually
start
by
hanging
upside
down.
This
makes
balancing
more
difficult
because
the
inverted
pendulum
has
to
swing
up
and
then
balance.
However,
when
the
inverted
pendulum
is
hanging
upside
down
it
is
very
easy
to
calibrate
because
you
know
it
will
be
hanging
exactly
vertical.
In
our
inverted
pendulum
system,
the
pendulum
only
moves
about
60
degrees.
This
makes
balancing
it
easier,
but
makes
calibration
more
difficult.
In
order
to
calibrate
the
center
point,
you
to
either
hold
it
vertical
or
assume
the
center
lies
directly
in
between
the
two
stops.
This
increases
the
probability
of
creating
error
during
calibration,
which
can
cause
your
slider
to
consistently
move
in
one
direction
because
it
thinks
there
is
error
when,
in
fact,
the
pendulum
could
be
perfectly
vertical.
To
avoid
this,
try
using
a
set
point
shifting
function
that
shifts
the
set
point
to
the
left
or
the
right
depending
on
which
direction
the
slider
is
drifting.
Tuning
Pointers
In
the
case
of
the
inverted
pendulum,
smooth
motion
is
desired
to
keep
the
pendulum
from
falling
over.
Since
the
motor
needs
to
stop
in
order
to
switch
direction,
smooth
motion
is
difficult
to
obtain.
One
way
to
smooth
the
output
signal
is
to
double
integrate
the
error
term.
In
the
PID
controller,
the
integral
term
acts
like
a
low
pass
filter.
By
double
integrating
it,
you
are
adding
another
pole
to
the
low
pass
filter.
This
increases
the
filter
order
and
makes
the
attenuation
steeper.
If
you
chose
to
use
the
double
integration,
the
PID
control
loop
would
look
like
the
figure
below.
Previous Error
KD
Error
KP
KI
This
control
loop
could
be
referred
to
as
PII2,
assuming
the
KI
and
KI2
constants
are
different.
If
they
were
the
same
it
would
simply
be
PI3.
Using
another
integral
term
could
be
helpful
to
smooth
the
output
signal,
but
it
will
also
increase
the
processing
time.
This
is
just
a
suggestion.
You
may
find
that
double
integrating
the
signal
is
not
useful
for
you
or
that
basic
PID
control
works
just
fine.
Combining
PIDs
Sometimes
you
want
to
control
more
than
one
variable
in
your
system.
Let's
say
that
in
the
inverted
pendulum
lab,
you
wanted
to
balance
the
pendulum
and
keep
the
slider
in
the
middle.
You
do
not
need
to
keep
the
slider
in
the
middle
for
this
lab,
but
it
could
help
the
pendulum
balance
by
ensuring
it
is
farther
away
from
the
end
and
has
more
distance
to
travel.
If
you
want
to
control
both
slider
and
pendulum
position,
you
would
need
two
PIDs
control
loops:
a
balancing
PID
loop
and
a
centering
PID
loop.
If
you
had
the
two
PID
loops
trying
to
control
the
position
separately,
it
is
very
difficult
to
control
which
one
would
dominates
the
motion.
The
best
way
to
do
this
is
to
combine
the
output
of
the
PIDs
and
then
send
the
total
output
to
the
motor.
See
below.
Changing
the
constants
allows
you
to
control
which
PID
will
dominate.
A
little
hint
when
combining
a
centering
and
balancing
PID,
try
subtracting
the
centering
PID
from
the
balancing
one.
Lab
Deliverables
For
this
lab
you
will
need
to
use
the
tips
you
learned
above
to
balance
the
inverted
pendulum
for
~
10s.
The
pendulum
does
not
have
to
be
able
to
balance
when
subjected
to
an
external
disturbance
and
the
slider
does
not
need
to
remain
in
the
center.