In some scenario, PID control is not enough. Model Predictive Control (MPC) is another control algorithm that can handle complex systems.

What is Model Predictive Control (MPC)?Model Predictive Control (MPC) is an advanced control algorithm that uses a dynamic model of the system to predict future behavior and optimize control inputs. It is widely used in process control, robotics, automotive, and other fields where complex systems need to be controlled.

> graph LR B[Measure current state] B --> C[Define prediction horizon] C --> D[Use model to predict future states] D --> E[Define control objectives and constraints] E --> F[Optimize control inputs] F --> G[Apply optimized control inputs] G --> H[Move to next time step] H --> B G

The maths behind MPC is more complicated than PID. We will use an oven as an example to explain the MPC algorithm. Oven is a common household appliance and it is easy to understand as an example. Using a nuclear reactor as an example may be too daunting.

In this post, I will explain the MPC algorithm and how to implement it for an oven control system.

We will start by modeling the oven system, then discretize the model for implementation on a microcontroller. We will also consider disturbances like door opening and incorporate them into the model. Finally, we will simulate the MPC control strategy to regulate the oven temperature.

### Identify the Key Variables

First, we need to identify the key variables that describe the oven’s behavior. These include:

**State Variable**: Oven temperature $T(t)$**Control Input**: Heating element power $P(t)$**Disturbances**: Ambient temperature, door opening, etc.

The state variable represents the internal state of the system that we want to control.

The control input is the variable we can manipulate to achieve our control objectives.

Disturbances are external factors that affect the system but are not directly controlled. In the context of PID, disturbances are often either ignored, or handled by the integral term. In MPC, disturbances are explicitly modeled and accounted for in the control strategy. In the case of an oven, the ambient temperature and door opening can be considered as disturbances that affect the oven temperature.

### Basic Heat Transfer Principles

The temperature dynamics of an oven can be modeled using principles of heat transfer, typically focusing on conduction and convection. The rate of change of the oven temperature can be described by an energy balance equation.

Conduction refers to the transfer of heat through a solid material, such as the walls of the oven. Convection refers to the transfer of heat between a solid surface and a fluid (e.g., air) moving over the surface. In the case of an oven, convection is the dominant mode of heat transfer.

We would only consider convection in this simple model.

The heat transfer rate can be described by Newton’s Law of Cooling, which states that the rate of heat transfer is proportional to the temperature difference between the object and the surrounding environment. The simple equation of heat transfer can be given by:

$$

\text{Heat Transfer Rate} = U \cdot (T(t) - T_{\text{ambient}})

$$

Where:

- $U$ is the overall heat transfer coefficient.
- $T(t)$ is the oven temperature at time $t$.
- $T_{\text{ambient}}$ is the ambient temperature.

### Energy Balance Equation

$$

\frac{dT(t)}{dt} = \frac{1}{C} \left( P(t) \cdot \eta - U \cdot (T(t) - T_{\text{ambient}}) \right)

$$

Where:

- $T(t)$ is the oven temperature at time $t$.
- $P(t)$ is the power of the heating element at time $t$.
- $\eta$ is the efficiency of the heating element (fraction of electrical energy converted to heat).
- $C$ is the thermal capacity of the oven (amount of energy required to change the temperature).
- $U$ is the overall heat transfer coefficient (accounts for heat losses to the environment).
- $T_{\text{ambient}}$ is the ambient temperature.

$\frac{dT(t)}{dt}$ is the formula represents how fast the temperature $T$ changes over time $t$. It’s like looking at how quickly the temperature is going up or down.

Thermal Capacity $C$ is a property of the material or substance you’re dealing with. It tells you how much heat energy is needed to change the temperature of a certain amount of that material. For example, water has a high thermal capacity, which means it takes a lot of heat to make it get hotter or cooler compared to some other materials. Here, we’re using $\frac{1}{C}$ to represent how the material’s thermal capacity affects the rate of temperature change. If $C$ is large, it means it takes a lot of energy to change the temperature, so the rate of change $\frac{dT(t)}{dt}$ will be slower.

Heat Added $P(t) \cdot \eta$ shows how much heat is being added to the system. $P(t)$ represents the power being supplied, like the electricity going into a heater, and $\eta$ is a number between 0 and 1 that tells you how efficiently that power is being turned into heat. For example, if $\eta = 0.8$, it means 80% of the power is turning into heat, and the rest might be lost as other forms of energy like light or sound.

Heat Lost $U \cdot (T(t) - T_{\text{ambient}})$ shows how much heat is being lost from the system. $U$ represents a value called the heat transfer coefficient, which tells you how fast heat can move from one place to another. For example, if $U$ is high, it means heat can move quickly, so the system might cool down faster. $T(t) - T_{\text{ambient}}$ tells you the difference between the temperature of the system $T(t)$ and the temperature of the surrounding environment $T_{\text{ambient}}$. If the system is hotter than the surroundings, heat will be lost. If it’s colder, heat might be gained from the surroundings.

By putting them all together, the formula is like a balance between how fast heat is being added and how fast it’s being lost. If more heat is being added than lost, the temperature will go up $\frac{dT(t)}{dt} > 0$. If more heat is being lost than added, the temperature will go down $\frac{dT(t)}{dt} < 0$. If they’re equal, the temperature will stay the same $\frac{dT(t)}{dt} = 0$.

### Discretize the Model

Microcontrollers operate in a digital environment, meaning they process data in discrete time steps, e.g. every millisecond.

Continuous-time models, like differential equations, cannot be directly implemented on digital systems without discretization. For implementation in a digital controller like MPC, the continuous-time model needs to be discretized.

Assuming a sampling time $\Delta t$, the discrete-time model can be approximated as:

$$

T[k+1] = T[k] + \Delta t \cdot \frac{1}{C} \left( P[k] \cdot \eta - U \cdot (T[k] - T_{\text{ambient}}) \right)

$$

Where:

- $T[k]$ is the oven temperature at the $k$-th sampling time.
- $P[k]$ is the heating power at the $k$-th sampling time.

To use this model, the parameters $C$, $\eta$, $U$, and $\Delta t$ need to be determined. These parameters can be obtained through system identification techniques involving experimental data collection and parameter estimation.

### Example Calculation

Assume we have the following parameters:

- Thermal capacity $C = 500 , \text{J/°C}$
- Efficiency $\eta = 0.8$
- Heat transfer coefficient $U = 10 , \text{W/°C}$
- Sampling time $\Delta t = 1 , \text{second}$
- Ambient temperature $T_{\text{ambient}} = 25 , \text{°C}$

For an initial oven temperature $T[0] = 180 , \text{°C}$ and heating power $P[0] = 1000 , \text{W}$:

$$

T[1] = T[0] + \Delta t \cdot \frac{1}{C} \left( P[0] \cdot \eta - U \cdot (T[0] - T_{\text{ambient}}) \right)

$$

$$

T[1] = 180 + 1 \cdot \frac{1}{500} \left( 1000 \cdot 0.8 - 10 \cdot (180 - 25) \right)

$$

$$

T[1] = 180 + 0.002 \left( 800 - 1550 \right)

$$

$$

T[1] = 180 + 0.002 \cdot (-750)

$$

$$

T[1] = 180 - 1.5

$$

$$

T[1] = 178.5 , \text{°C}

$$

### Refining the Model

In practice, the model can be refined by considering additional factors like heat losses due to door openings, non-linear effects, and more complex heat transfer mechanisms.

In the following section, I will use adding door opening as a disturbance to refine the model.

#### Adding Door Opening as a Disturbance

Let’s denote the door opening event as a binary variable $D(t)$ . $D(t) = 1$ when the door is open and $D(t) = 0$ when the door is closed.

To know that the door is opened, an additional sensor is also needed, such as a switch or a proximity sensor.

Next, we should model the heat loss due to the door opening. When the door is opened, a significant amount of heat escapes. This can be modeled as a term proportional to the temperature difference between the oven interior and the ambient temperature. This model only considers the convective heat loss through the open door.

Let $Q_{\text{loss}}$ represent the rate of heat loss when the door is opened. A simple way to model this is:

$$

Q_{\text{loss}} = K_{\text{door}} \cdot D(t) \cdot (T(t) - T_{\text{ambient}})

$$

Where:

- $K_{\text{door}}$ is a coefficient representing the rate of heat loss when the door is open.

Next, we incorporate this heat loss term into the energy balance equation.

Originally, the energy balance equation was:

$$

\frac{dT(t)}{dt} = \frac{1}{C} \left( P(t) \cdot \eta - U \cdot (T(t) - T_{\text{ambient}}) \right)

$$

The modified energy balance equation including the door opening effect becomes, adding the heat loss term $Q_{\text{loss}}$:

$$

\frac{dT(t)}{dt} = \frac{1}{C} \left( P(t) \cdot \eta - U \cdot (T(t) - T_{\text{ambient}}) - Q_{\text{loss}} \right)

$$

Expand the heat loss term $Q_{\text{loss}}$, then we get:

$$

\frac{dT(t)}{dt} = \frac{1}{C} \left( P(t) \cdot \eta - U \cdot (T(t) - T_{\text{ambient}}) - K_{\text{door}} \cdot D(t) \cdot (T(t) - T_{\text{ambient}}) \right)

$$

Then, discretize the modified continuous-time model to get the discrete-time model:

$$

T[k+1] = T[k] + \Delta t \cdot \frac{1}{C} \left( P[k] \cdot \eta - U \cdot (T[k] - T_{\text{ambient}}) - K_{\text{door}} \cdot D[k] \cdot (T[k] - T_{\text{ambient}}) \right)

$$

The maths is done, now let’s see an example calculation.

Let’s assume the following parameters for the door opening effect:

- $K_{\text{door}} = 50 , \text{W/°C}$

Given:

- Initial oven temperature $T[0] = 180 , \text{°C}$
- Heating power $P[0] = 1000 , \text{W}$
- Ambient temperature $T_{\text{ambient}} = 25 , \text{°C}$
- Door opening status $D[0] = 1$ (door is open)

Using the modified discrete-time model:

$$

T[1] = T[0] + \Delta t \cdot \frac{1}{C} \left( P[0] \cdot \eta - U \cdot (T[0] - T_{\text{ambient}}) - K_{\text{door}} \cdot D[0] \cdot (T[0] - T_{\text{ambient}}) \right)

$$

Substitute the values:

$$

T[1] = 180 + 1 \cdot \frac{1}{500} \left( 1000 \cdot 0.8 - 10 \cdot (180 - 25) - 50 \cdot 1 \cdot (180 - 25) \right)

$$

$$

T[1] = 180 + 0.002 \left( 800 - 1550 - 7750 \right)

$$

$$

T[1] = 180 + 0.002 \cdot (-8500)

$$

$$

T[1] = 180 - 17

$$

$$

T[1] = 163 , \text{°C}

$$

Here, the oven temperature drops significantly due to the heat loss when the door is opened.