PID Implementation

Embed Size (px)

Citation preview

  • 7/27/2019 PID Implementation

    1/14

    12/6/04 BAE 3023 1

    Advanced Embedded Systems Design

    Lecture 14 Implementation of a PID controller

    BAE 5030 - 003

    Fall 2004

    Instructor: Marvin Stone

    Biosystems and Agricultural EngineeringOklahoma State University

  • 7/27/2019 PID Implementation

    2/14

    12/6/04 BAE 3023 2

    Goals for Class Today

    Questions over reading / homework (CAN Implementation)

    Zigbee and 802.14.5 (Kyle)

    PID implementation (Stone)

  • 7/27/2019 PID Implementation

    3/14

    12/6/04 BAE 3023 3

    Elements of a feedback control system

    Review elements and

    variables

    Gc G2

    G3

    Error

    Manipulated

    Variable

    D

    Controlled

    Variableout

    +

    +

    G1

    out

    -

    +

    out

    Load

    in

    Setpointset

    outout_measured

  • 7/27/2019 PID Implementation

    4/14

    12/6/04 BAE 3023 4

    )1()1( 232

    23

    1GGG

    GG

    GGG

    G

    c

    cset

    c

    inout

    )( _measuredoutsetcGD

    21 DGGinout

    3_ Goutmeasuredout

    Output (out) is readily calculated as a function of:Load (

    in) and

    Setpoint (set

    )

    Manipulation is a simple function of the controller TF and error.

  • 7/27/2019 PID Implementation

    5/14

    12/6/04 BAE 3023 5

    Digital form of a classic feedback controlled system

    If sampling rate is fast and holds are employed, this

    system approaches the analog system

    Gc G2

    G3

    Error

    ManipulatedVariable

    D* D

    ControlledVariable

    out

    +

    +

    G1

    out

    -

    +

    out

    Load

    in

    Setpoint

    set

    out

    out_measured

    out_measured

    *

    Computer based controller

  • 7/27/2019 PID Implementation

    6/14

    12/6/04 BAE 3023 6

    One of the conventional models used to express a PID controller is:

    dt

    deedteKM d

    t

    ti

    c

    0

    1

    rateDerivitive

    rateReset

    signalErrore

    gainControllerK

    onManipulatiM

    d

    i

    c

    Time Domain PID Controller Equation

    Where:

  • 7/27/2019 PID Implementation

    7/14

    12/6/04 BAE 3023 7

    Derivitive Form of a PID Controller

    A convenient way to implement this equation in a controller is as the

    derivative of manipulation known as the velocity form of the equationas shown below:

    2

    2

    dt

    ede

    dt

    deK

    dt

    dMd

    i

    c

    In a practical system this equation will work well and does not require

    any steady-state references, but eliminating the iand

    dterm

    completely results in:

    dtdeK

    dtdM c deKdM cor,

    This equation has no positional reference and error accumulation is a

    problem. Use velocity form only for PI or PID modes.

  • 7/27/2019 PID Implementation

    8/14

    12/6/04 BAE 3023 8

    Conversion of the DE to a Difference Equation

    To begin the conversion of the PID equation to a difference

    equation, the equation is multiplied by dt.

    dt

    ded

    edtdeKdM d

    i

    c

    Note that since Mis a differential and ess

    is zero, this equation

    conveniently applies to the absolute variables as well as the

    deviation variables.

    For small Dt, the equation can be approximated as:

    D

    DD

    DDD

    t

    tKm d

    i

    c

  • 7/27/2019 PID Implementation

    9/14

    12/6/04 BAE 3023 9

    Representation with Discrete Time Variables

    Each of the differences (D) can be expressed as discrete values ofeach of the variables (mand ) at the times 0, 1, and 2 as shownbelow:

    t0 t2t1

    M

    1

    2

    0

    M0 M

    1

    M2

    The equation can be simplified with the assumption that Dtisconstant:

    DD

    DDDD

    t

    tKm d

    i

    c

  • 7/27/2019 PID Implementation

    10/14

    12/6/04 BAE 3023 10

    Discrete form of PID controller

    Replacement of the differences (D) with the discretevariables ( m and ) results in:

    DD

    D

    D 12

    21212 )(

    t

    tKmm d

    i

    c

    D

    D 0112

    21212 )(

    t

    tKmm d

    i

    c

    DD

    01221212 2)(

    t

    tKmm d

    i

    c

    Note that D is assumed to be a constant. IfDtvaries, the equationsshould be derived with that in mind.

  • 7/27/2019 PID Implementation

    11/14

    12/6/04 BAE 3023 11

    Discrete form of PID controller

    This equation can be solved for the current manipulation, m2, in terms

    of values known at time t2: m

    1,e

    2, e

    1, and e

    0.

    The other parameters in the equation are constants.

    D

    D

    D

    D

    01212

    211

    ttt

    tKmm ddd

    ic

    Where C1,C2, and C3are constants and the current manipulation isexpressed in terms of known values, the current and past errors.

    03122112 cccKmm c or,

  • 7/27/2019 PID Implementation

    12/14

    12/6/04 BAE 3023 12

    Translation of PID Equation into Algorithm

    This equation may be translated directly into a computer language, for

    example:

    m2 = m1 + k*(C1*e2 C2*e1 + C3*e0);

    Within a computer program, the current error

    is calculated from the current measurement ofthe controlled variable and the setpoint, for example:

    e2 = T_setpoint T_measured;

    The current manipulation m2 is then computed using the previouscontroller equation, and finally, at the end of the time step, each of thevariables is shifted forward for the next calculation.

  • 7/27/2019 PID Implementation

    13/14

    12/6/04 BAE 3023 13

    Translation of PID Equation into Algorithm

    For example in C, the code might look like:

    measure_and_manipulate() //Call once per delta T

    begin

    T_measured = measure_T(); //Get the measured temperature

    e2 = T_setpoint T_measured //Calculate the current error

    m2 = m1 + k*(C1*e2 C2*e1 + C3*e0); //Calculate the manipulation

    set_manupilation(m2); //Output the manipulatione0 = e1; //Shift the error and manipulation

    e1 = e2; //forward one time step

    m1= m2;

    end;

    Note that the time step Dtis controlled by the time required to executethe loop. C1,C2 and C3 are all functions ofDt. The equation willprobably be executed as floats! (Or very special care must be taken

    with scaling.

  • 7/27/2019 PID Implementation

    14/14

    12/6/04 BAE 3023 14

    Assignment

    Complete CAN message demo

    Turn in course portfolio by 5:00 PM Wednesday Dec.

    8th