None Notebook

This notebook contains material from CBE30338; content is available on Github.

< 4.0 PID Control | Contents | Tag Index | 4.2 PID Control with Setpoint Weighting >

Open in Colab

Download

4.1 Implementing PID Controllers with Python Yield Statement

Up to this point we have been implementing simple control strategies where the manipulated variable depends only on current values of the process variable and setpoint. Relay control, for example, can be expressed mathematically as

$$MV = Kp (SP - PV)$$

and implemented in Python as

def proportional(PV, SP):
    MV = Kp * (SP - PV)
    return MV

Python functions carry no persistent memory from one use to the next, which is fine if the control algorithm requires no knowledge of the past.

The problem, however, is that PID control requires use of past measurements. Proportional-integral control, for example, tracks the cumulative sum of the differences between setpoint and the process variable. Because a Python function disappears completely after the return statement, the value of the cumulative sum must be stored somewhere else in code. The coding problem is to figure out how and where to store that information between calls to the algorithm. We seek coding methods that encapsulate the state of control algorithms.

There are several ways persistence can be provided in Python (roughly in order of increasing capability and complexity):

Classes, in particular, are a tool for encapsulating data and functionality within a single software entity. Classes are widely used in Python programming, and should eventually become part of every Python programmer's toolkit.

As we demonstrate below, however, the Python yield statement is often sufficient to write self-contained implementations of control algorithm.

4.1.1 Python Yield Statement

The cells below provide a very brief introduction to Python yield statement. More information can be found here

A function incorporating a yield statement creates a persistent object called a generator. Like the return statement, yield says to return a value and control to the calling routine. Unlike the return statement, however, the generator goes into a suspended state following the yield. On the next use it will pick up execution right where execution left off.

Here's an example produces a generator for four numbers.

To use this, first create the actual generator object that will do the work.

There are several ways to get values from a generator. One way is using the next() function which executes generator until the next yield statement is encountered, then return the value.

The generator object numbers has a send method for sending information to the generator and causing it to execute until the next yield. For this case all we need to send is None.

The send method provides two-way communication with a generator. Here's an example of how that works.

There's a subtle detail here that easy to miss. The first send starts the generator which then executes up to the first yield statement. At that point execution halts and the message "Started" is sent to the calling routine. The second send causes execution to pick up again and puts the message "Hello, World" into variable a.

An important point is that the first send to a generator must always be None.

In the next example yield is placed inside an infinite loop. This function creates generators that return the square of the number sent.

Let's now consider how generators may be used for process control applications. The next cell defines a function that will create generators that perform as proportional controllers with specified gain $K_p$ and setpoint $SP$. When sent a current value of the process variable PV, the controllers will return a value for the manipulated variable MV.

Let's create and initialize two controllers with the same setpoint but different gains.

Let's see how these controllers would respond to a PV value of 35.

This is a important feature from a coding and maintenance perspective. We need to create and maintain only one copy of the control algorithm.

4.1.2 PID Control Implementation

A simple form of Proportional-Integral control is an example of a controller with internal state. In velocity form,

\begin{align} MV_k & = \overline{MV} + K_P e_k + K_I \sum_{k'=0}^k e_{k'}(t_k - t_{k-1}) + K_D \frac{e_k - e_{k-1}}{t_k - t_{k-1}} \end{align}

where $e_k$ is the difference between setpoint and measured process variable

\begin{align} e_k = SP_k - PV_k \end{align}

$K_P$, $K_I$, and $K_D$ are control constants, and $t_k$ is the sampling time.

The following cell provides a direct implementation of this algorithm as a Python generator.

4.1.3 Simulation

Let's see how well this PID implementation works. We'll perform the simulation with a setpoint that starts at room temperature, then rises to 50°C at $t = 50$ seconds. The data historian will record values of the setpoint, process variable, computed manipulated variable, and the actual value of the manipulated variable.

4.1.4 Analysis

The good news is that the controller did steer the heater to the desired setpoint. There was some overshoot and undershoot along the way, but eventually the system did get there.

In examining the response more closely, however, we can see at least two issues that must be addressed before this control algorithm could be applied to large scale indusrial control:

  1. A sudden adn abrubt "kick" to the manipulated variable when the setpoint changes.
  2. A mismatch between computed and actually feasible control actions.

< 4.0 PID Control | Contents | Tag Index | 4.2 PID Control with Setpoint Weighting >

Open in Colab

Download