Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

PID Controller Simulation for Computer Science Students, Assignments of Computer Science

Information about a programming assignment for computer science students in which they are required to develop a program simulating the application of a proportional-integral-derivative (pid) controller to correct the error between a process variable and a desired setpoint. The concept of pid control, the role of its coefficients, and the steps needed to simulate it. Students are expected to use c++ features and create a class for the pid controller.

Typology: Assignments

Pre 2010

Uploaded on 08/18/2009

koofers-user-4fe-1
koofers-user-4fe-1 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Computer Science 145.002
Introduction to Computing for Engineers
Fall 2008
Programming Assignment 10
“PID Controller Simulator”
Due: 7:30 AM, Thursday, December 11, 2008
A Proportional-Integral-Derivative (PID) controller is a
mechanism that is commonly used in engineering control
systems to get a particular variable to achieve a specific
value by repeatedly adjusting its current value until the
error between the current value and the goal value is
virtually eliminated.
Your assignment is to develop a program that simulates
the application of a PID controller to correct the error
between a process variable (PV) and a desired setpoint
(SP) by calculating and then applying a corrective action
that can adjust the process appropriately. For example,
an automobile's cruise control could use the car's speed
as its PV, with the car's desired speed as its SP.
There are three coefficients used to adjust the PV and eliminate the error. The proportional coefficient (Kp) is the factor
by which the PV error is multiplied in a single time unit. For example, if a car is going 50 MPH, the SP is 70 MPH, the Kp is
set to 0.2, and the time unit is one second, then the car will accelerate from 50 MPH to 54 MPH (0.2 times the 20 MPH
error) in one second; it would then accelerate from 54 MPH to 57.2 MPH (0.2 times the 16 MPH error) in the next
second
In order to control wild oscillations between high and low PV values, the derivative coefficient (Kd) is used. This
coefficient determines the factor by which the rate of the error change (i.e., the derivative of the error will be adjusted.
So, if the previous example had also had a Kd value of -0.0002, then the car will still accelerate from 50 MPH to 54 MPH
in the first second since no rate of error change has been established yet), but the adjustment in the next second will
include 3.2 MPH from the proportional term, but also Kd*(new error - old error)/(time elapsed) = (-0.0002)*(16 MPH - 20
MPH)/(1/3600 hour which calculates to 2.88 MPH. So, instead of winding up at 57.2 MPH after two seconds, the car
would have a speed of 60.08 MPH at that time.
Finally, to get the PV to settle down to the SP more quickly, the integral coefficient (Ki) is used. This coefficient is used as
the factor by which the accumulated error over time is multiplied to attempt to equalize that accumulated error and
thus get the PV oscillations to settle down. For instance, in the previous (Kp - 0.2, Kd = -0.0002) example, if Ki is set to -
0.018, then the first second's accumulated error of (20 MPH)*(1/3600 hour) is multiplied by Ki to adjust the speed by -
0.0001, so the speed at the end of the first second is 53.9999 MPH (not 54 MPH). While that doesn't seem to be much of
a difference, additional iterations create a cumulative effect that yields a faster convergence of the PV to the SP.
While the above formula for the PID adjustment seems
rather complicated, the steps needed to simulate PID
control are reasonably straightforward, as outlined at right.
Use one second (1/3600 of an hour) as your time increment
(t), and repeat the process for one minute (i.e., 60
iterations). An example interactive session and its resulting
output file are displayed on back of this page.
Since this is your last programming assignment, you are
expected to use as many of the features of C++ as you can
in this program. Use a class (in its own header file) to
implement the PID controller, with a default constructor, an
initializing constructor, a copy constructor, and all accessor and mutator member functions fully implemented. Use
another class (with a two-dimensional array of characters as its data member and an overloaded output operator) to
hold the image array that will be sent to the user-specified output file. Use the primary output commands
(.setf, .precision, and setw) to format the output properly.
1)
1) Initialize
Initialize Error
Error to
to SP
SP
PV
PV
2)
2) Initialize
Initialize P
P,
, I
I, and
, and D
D to zero
to zero
3)
3) Set
Set PreviousError
PreviousError to
to Error
Error
4)
4) Set
Set P
P to
to K
Kp
p
*
* Error
Error
5)
5) Add
Add K
Ki
i *
* Error
Error *
*
t to
t to I
I
6)
6) Set
Set D
D to
to K
Kd
d * (
* (Error
Error -
- PreviousError
PreviousError) /
) /
t
t
7)
7) Add
Add P
P +
+ I
I +
+ D
D to
to PV
PV
8) Repeat starting at #3 for the next time interval
Repeat starting at #3 for the next time interval
Kpe
(
t
)
+Ki
0
t
e
(
τ
)
dττ +K
dτe (t)
dτt
pf2

Partial preview of the text

Download PID Controller Simulation for Computer Science Students and more Assignments Computer Science in PDF only on Docsity!

Computer Science 145.

Introduction to Computing for Engineers

Fall 2008

Programming Assignment 10

“PID Controller Simulator”

Due: 7:30 AM, Thursday, December 11, 2008

A Proportional-Integral-Derivative (PID) controller is a

mechanism that is commonly used in engineering control

systems to get a particular variable to achieve a specific

value by repeatedly adjusting its current value until the

error between the current value and the goal value is

virtually eliminated.

Your assignment is to develop a program that simulates

the application of a PID controller to correct the error

between a process variable ( PV ) and a desired setpoint

( SP ) by calculating and then applying a corrective action

that can adjust the process appropriately. For example,

an automobile's cruise control could use the car's speed

as its PV , with the car's desired speed as its SP.

There are three coefficients used to adjust the PV and eliminate the error. The proportional coefficient ( Kp ) is the factor

by which the PV error is multiplied in a single time unit. For example, if a car is going 50 MPH, the SP is 70 MPH, the Kp is

set to 0.2, and the time unit is one second, then the car will accelerate from 50 MPH to 54 MPH (0.2 times the 20 MPH

error) in one second; it would then accelerate from 54 MPH to 57.2 MPH (0.2 times the 16 MPH error) in the next

second

In order to control wild oscillations between high and low PV values, the derivative coefficient ( Kd ) is used. This

coefficient determines the factor by which the rate of the error change (i.e., the derivative of the error will be adjusted.

So, if the previous example had also had a Kd value of -0.0002, then the car will still accelerate from 50 MPH to 54 MPH

in the first second since no rate of error change has been established yet), but the adjustment in the next second will

include 3.2 MPH from the proportional term, but also Kd (new error - old error)/(time elapsed) = (-0.0002)(16 MPH - 20

MPH)/(1/3600 hour which calculates to 2.88 MPH. So, instead of winding up at 57.2 MPH after two seconds, the car

would have a speed of 60.08 MPH at that time.

Finally, to get the PV to settle down to the SP more quickly, the integral coefficient ( Ki ) is used. This coefficient is used as

the factor by which the accumulated error over time is multiplied to attempt to equalize that accumulated error and

thus get the PV oscillations to settle down. For instance, in the previous ( Kp - 0.2, Kd = -0.0002) example, if Ki is set to -

0.018, then the first second's accumulated error of (20 MPH)*(1/3600 hour) is multiplied by Ki to adjust the speed by -

0.0001, so the speed at the end of the first second is 53.9999 MPH (not 54 MPH). While that doesn't seem to be much of

a difference, additional iterations create a cumulative effect that yields a faster convergence of the PV to the SP.

While the above formula for the PID adjustment seems

rather complicated, the steps needed to simulate PID

control are reasonably straightforward, as outlined at right.

Use one second (1/3600 of an hour) as your time increment

(t), and repeat the process for one minute (i.e., 60

iterations). An example interactive session and its resulting

output file are displayed on back of this page.

Since this is your last programming assignment, you are

expected to use as many of the features of C++ as you can

in this program. Use a class (in its own header file) to

implement the PID controller, with a default constructor, an

initializing constructor, a copy constructor, and all accessor and mutator member functions fully implemented. Use

another class (with a two-dimensional array of characters as its data member and an overloaded output operator) to

hold the image array that will be sent to the user-specified output file. Use the primary output commands

(.setf, .precision, and setw) to format the output properly.

1)1) InitializeInitialize ErrorError toto SPSP – – PVPV

2)2) InitializeInitialize PP ,, II , and, and DD to zeroto zero

3)3) SetSet PreviousErrorPreviousError toto ErrorError

4)4) SetSet PP toto KKpp ** ErrorError

5)5) AddAdd KKii ** ErrorError ** t tot to II

6)6) SetSet DD toto KKdd * (* ( ErrorError - - PreviousErrorPreviousError ) /) / tt

7)7) AddAdd PP ++ II ++ DD toto PVPV

8) Repeat starting at #3 for the next time intervalRepeat starting at #3 for the next time interval

K p e ( t ) + Ki ∫

0 t

e ( τ ) dττ + K dτ

dτe ( t )

dτt

Name your project PA10_ Lastname where Lastname is your last

name (e.g., Kris Kringle’s project would be PA10_Kringle). Be sure

to include adequate explanatory comments in your code.

Zip-compress the entire project folder and place the zipped folder

on your Moodle dropbox by 7:30 AM on Thursday, December 11,

Initial Speed : 50.0000000000 MPH After Second # 1: 54.0001000000 MPH After Second # 2: 60.0803319995 MPH After Second # 3: 66.4422622371 MPH After Second # 4: 71.7346469473 MPH After Second # 5: 75.1984732624 MPH After Second # 6: 76.6529462778 MPH After Second # 7: 76.3697570495 MPH After Second # 8: 74.8920570026 MPH After Second # 9: 72.8498247155 MPH After Second # 10: 70.8095614237 MPH After Second # 11: 69.1787644190 MPH After Second # 12: 68.1689466482 MPH After Second # 13: 67.8082066353 MPH After Second # 14: 67.9869615695 MPH After Second # 15: 68.5184119440 MPH After Second # 16: 69.1975203686 MPH After Second # 17: 69.8471249168 MPH After Second # 18: 70.3455665286 MPH After Second # 19: 70.6354807760 MPH After Second # 20: 70.7172692943 MPH After Second # 21: 70.6328459975 MPH After Second # 22: 70.4456316890 MPH After Second # 23: 70.2218484856 MPH After Second # 24: 70.0164912094 MPH After Second # 25: 69.8654719734 MPH After Second # 26: 69.7837806464 MPH After Second # 27: 69.7683447602 MPH After Second # 28: 69.8037011269 MPH After Second # 29: 69.8685576240 MPH After Second # 30: 69.9416835726 MPH After Second # 31: 70.0061386282 MPH After Second # 32: 70.0514595991 MPH After Second # 33: 70.0739395775 MPH After Second # 34: 70.0754776758 MPH After Second # 35: 70.0616296236 MPH After Second # 36: 70.0394728451 MPH After Second # 37: 70.0157649422 MPH After Second # 38: 69.9956817314 MPH After Second # 39: 69.9822249626 MPH After Second # 40: 69.9762306748 MPH After Second # 41: 69.9768083496 MPH After Second # 42: 69.9820024186 MPH After Second # 43: 69.9894815675 MPH After Second # 44: 69.9971101968 MPH After Second # 45: 70.0033207406 MPH After Second # 46: 70.0072681374 MPH After Second # 47: 70.0087965527 MPH After Second # 48: 70.0082775743 MPH After Second # 49: 70.0063882267 MPH After Second # 50: 70.0038900509 MPH After Second # 51: 70.0014531344 MPH After Second # 52: 69.9995477007 MPH After Second # 53: 69.9984060237 MPH After Second # 54: 69.9980425947 MPH After Second # 55: 69.9983122000 MPH After Second # 56: 69.9989836774 MPH After Second # 57: 69.9998102122 MPH After Second # 58: 70.0005830823 MPH After Second # 59: 70.0011627370 MPH After Second # 60: 70.0014873398 MPH