Sie sind auf Seite 1von 39

Software PID Control of an

Inverted Pendulum Using the


PIC16F684
INTRODUCTION
ƣ The inverted pendulum consists of
three main parts:
1. the base platform ಎಚ౹‫ש‬
2. the pendulum მ
3. the controller board ࿳Ӽ‫ݽ‬ኈ
' SE PL TFORM
ƣ 3-point platform 3㔄౹‫ש‬
ƣ 2 wheels 2ⱐ
ƣ n audio jack ㅠ㆒တ‫ם‬
The audio jack serves 2 purposesŘ
1. it is used as the axis of rotation for
the base platform
2. it is used to bring commutatedņᄮᙟŇ
power to the controller board
MOTOR
PENDULUM
ƣThe pendulum is attachedņⳐ࿲Ňto
the base platform by a 360° free
rotating potentiometerņヅ̤‫ݽ‬Ň.
ƣwhen the pendulum is balanced
(completely vertical), the
potentiometer center tap is biased to
VREFņ‫≙א‬ヅࣱŇ/2.
CONTROLLER 'O RD
2 main functionsŘ
1. Measure Ė
2. drive the DC motor.
ƣ The motor is controlled by an H-bridge which is
driven by the PIC16F684 Enhancedņ࣑೼Ň
Capture/Compare/PWM Module (ECCP).
ƣ The outputs of the ECCP are connected to FETņࢍ
ᄓヅᇘ㋤Ňdrivers.
ƣ 5 potentiometers on the controller board
ƣ 3 of which are used for adjustingņ⦲ᄮŇthe PID
constants (KP, KI and KD) and one to measure Ė.
input filterņⱚҘᡇᘘ‫ݽ‬ŇŘ
1. cut-off frequency of 60
2. Voltage gain of 6
3. it has the best response to a step function
ƣ with no gain, one LSbņሱଅϊ̤ҀŇequals
0.35°
ƣ With the gain set to 6, the displacement
angle is limited to ±30° which gives a
resolution of 0.059° per LSbð
ƣ when the pendulum is vertical , the input
filterƠs reference to produce a true 0°
displacement angle.
Without this potentiometer Ř
1. slowly increase speed
2. take the system into an unstable state.
PID
ƣThe desired set point R(t) of this
system occurs when Ė = 0°. In this
state, the pendulum is balanced.
ƣSince the desired response of the
system is 0°, any angle measured
other than 0° is the error or Y(t) =
E(t).
In implementing the PID controller, there are
3 terms
which are based off the error measurement.
ƣIn this system, the sign of the
controllerƠs output, C(t),will determine
the direction in which the motor will turn.
ƣThe magnitude of C(t) directly corresponds
ņଃະŇto the duty cycle of the PWM in the
ECCP module, determining the speed at which
the motor will turn.
ƣPWMņ Pulse-width modulation Ň⋸⠢૰ಙ⦲

PID IN DIGIT L SYSTEM
ƣConverting over to a digital system,
Y(t) is measured by an /D converter.
ƣIn order to implement the PID
controller , the PICmicro9
microcontroller will have to do some
approximationsņⲖ̙ϊŇof integral
and derivative terms.
MODELING THE INVERTED
PENDULUM
ƣHere are some of the variables which need
to be looked at in order to model the
system:
ƣ 'asesƠposition
ƣ 'asesƠvelocity
ƣ 'asesƠacceleration
ƣ 'asesƠmoment of inertia ņⱧՒ๩⸇Ň
ƣ 'asesƠcoefficient of friction
ƣ 'asesƠmass
ƣ 'asesƠlength
ƣ EarthƠsgravitational constant ņ߸᫤೧Ի౑ᄲŇ
ƣPendulumƠs position
ƣPendulumƠs velocity
ƣPendulumƠs moment of inertia
ƣPendulumƠs coefficient of friction
ƣPendulumƠs mass
ƣPendulumƠs length
ƣThe hardware boundary for ĖM  is set by
the gain of the 'essel Filter; with a gain
of 6, the limit is ±30’ or ±0.523 radians
ƣThe software, further limits ĖM  to ±20’
or ±0.349 radians. This is done to
eliminateņᛤぜŇthe possibility of hitting
the hardware boundary.
ƣThe angular acceleration of the pendulum
with respect to the displacement angle is:
ƣWith a pendulum length of 0.5 meters and
ĖM  set to 20° or 0.349 radians.

ƣFrom Equation 5:
ƣ ĖƢM  = (g/R)Ė
ƣ ĖƢM  = (9.81/0.5)0.349
ƣ ĖƢM  = 6.845 radians/sec2
ƣ ĖƢM  = 3.425 meters/sec2

ƣThe model number is EPU9, with an 8.6 to 1


gear ratio attached to a 2 inch wheel.
ƣThe rated voltage for this motor is 4.8 to
7.2V, but for this example the motor will
run at 12V.
ƣRunning at approximately double the
designated input voltage is not a concern
because the motor will never be in a
constant state where the duty cycleņ̫ἇᕗŇ
of the PWM is greater than 50%.
ƣ If finding a motor that meets this
criterionņᐻ᜾Ňis difficult, there are
two solutionsŘ
1. Decreasing ĖM  in the software or increase the
length of the pendulum ; this will reduce the
maximum acceleration of the pendulum.
2. Increasing the coefficient of friction between
the drive wheel and the base. If the motor is of
ampleņ⭁चŇsize, change the coefficient of
friction by changing the drive wheel to a
different material or add an abrasiveņḇᵲŇ
surface to the platform.
ƣ To get a rough estimateņ⁏ᯆ̒⤺Ňof how fast the
PID loop needs to be updated, place an object,
similar in length of the pendulum, on end in the
palm of one hand and try balancing it.

ƣ the shortest length balanced for a sustainedņྃ↲Ň


period was 0.5 meters in length. 'alancing an
object in such a fashion predominantlyņʙ⣬Ň
relies on the sense of sightņ⣶⤌Ň.

ƣ Since human visionņ⣶ԻŇcan only process


information at approximately 30 Hz, 30 Hz will be
the baseline for the minimum speed the control
loop needs to run at.
C CODE FLOW CH RTC˩ᷪᙟỄߧ
SU'TLETIES IN THE SOFTW RE
Ⱆ㋤ʑ᱿െ।
ƣThe interrupt service routineņʑᅜ̗ሺỄ಍Ň
is used to control the speed of the PID
loop.
ƣThe interrupt service routine is set to run
off the Timer0 Interrupt. Timer0 is an 8-
bit timer that will increment the TMR0
register every instruction clockņ྆˪ᆹ
⾝Ň.
ƣThe speed at which the interrupt should
occur is every 3.9 milliseconds (1/256 Hz).
ƣ change in the proportional constant will
not affect the poleņᏎŇresponse of the
controller.
ƣThe relationship to the proportional
constant and the supply voltage to the
motor would be inversely proportional.
ƣThe derivative termņ଄ᄲㅮŇis crucialņ〦
⼫Ňin order to bring the inherentlyņ቏⫨Ň
unstable system into stability. In any PID
control the derivative terms acts as an
anticipatorņㅷ̒‫ݽ‬Ň.
ƣthe controller can tell if the error term
is getting bigger or smaller.
ƣIf the error term is getting larger, the
derivative term adds to the output of the
controller much like that of the
proportional and integralņỬӠŇ terms,
but to a lesser effect.
ƣIf the error term is getting smaller, this
term will subtract from the output of the
control in anticipationņㅷቅŇ of an
overshootņ⬢⬤⸇Ňcondition.
The following is the actual C code used to calculate
the derivative term.
ƣWith a 10-bit /D, KD is measured as an
integer between 0 and 1023. The  term
allows for a fractional representation of
KD.
ƣThe 3*TScomes from the fact that we are
tripling our sampling periodņ‫ח‬ᑂⳕቅŇby
using E(n)ƝE(n-3) not E(n)ƝE(n-1).Why?
The main reason for doing this is to limit
the variation in the error angle
measurement.
ƣthe error angle measurement ŋ /D error,
'essel filter throughputņᙟⳇ᪓Ň,
mechanical vibration ņྚՒŇ, etc.
ƣThis uncertainty can be decreased by
effectively tripling the sampling period.
ƣThe real key is that the derivative term is
still updated at 256 Hz rate.
ƣKP**3*TS has been precalculated to be 32
to increase the speed of the PID loop.
TUNING THE PID CONTROLLER
⦲⧒PID࿳Ӽ‫ݽ‬
ƣUse the following steps to tune the PID
constants:
1. Turn the KP, KI and KD potentiometers
counterclockwise as far as they will turn. This
sets all the constants to zero.
2. Power the device using a 12V (minimum of a 3 amp)
power supply.
3. Holding the Reset button down, lift the pendulum
to the vertical position and release the Reset
button and pendulum at the same time.
4. The pendulum should free fall and the base will
not move. This verifiesņ㊹⨢Ňthat all constants
are properly read as zeros.
5. Increase the KP constant by turning the
potentiometer counter clockwise and repeat step 3.

6. If the KP term is too small, the base platform


will chase the top of the pendulum while Ė
continues to increase. KP will be too large if
the drive wheel breaks free or the base
oscillates at a high rate of speed.

7. Start increasing the KI the same way as KP until


the pendulum can be balanced for several seconds
under a constant oscillating condition.
7. When the KI is added, the base will now
accelerate faster than the pendulum causing Ė to
change from a positive angle to a negative angle
(or vice versa).

ƣ The base should change directions and, again,


accelerate faster than the pendulum until Ė
changes signs and the whole cycle repeats. This
is known as the Overshootņ⬢⬤⸇Ňcondition.

8. Increase KD in the same manner as KP and KI until


the Overshoot condition is gone and the pendulum
remains balanced.

9. Once all overshoot is gone, the PID controller is


tuned.
CODE CONVERSION TO SSEM'LY
˩ᷪⱧဘӷ⃅‫׶‬
ƣWhen programming in assembly, it is
essential to make sure that the results of
the math functions have the proper sign and
the math registers ņᇺએ‫ݽ‬Ň never
overflow ņᝉ̤Ň.

ƣIn order to speed up the PID loop, all the


multiply routines have been limited to an
8x8 signed multiply routine with a 16-bit
signed result.
ƣThe 8-bit /D result was then converted to
an 8-bit signed numberņ̤ᄲŇby adding 128
decimalņ֓Ⳗ̤Ňand ignoring the carry
flagņⳖ̤ᐻ⦒Ň.

ƣThe constants, KP, KI and KD are all limited


to a positive 8-bit signed number, or 0-
127. Other than these 2 key changes, the
assembly program follows the same flow
chart as the C code.
CONCLUSION
⃌⧄
ƣ'y using the PIC16F684 deviceƠs ECCP and
/D modules we are able to demonstrateņㆴ
ḻŇhow to implement a positionalņહ̤᱿Ň
PID controller to bring an inherentlyņ቏⫨
Ňunstable system into stability.

ƣThe keys to implementing this control is to


have a basic understanding of the
mechanical system, and identifying the
derivative term would be a criticalņℤ⣬Ň
factor in the overall stability of the
system.
ƣThe other keys, with respect to the
software, were making sure our registers
never overflow, and picking the frequency
of the PID loop that is a power of 2 so we
could have a fast multiply and divide
routine using the left and right shift.

Das könnte Ihnen auch gefallen