Implementation of Control Algorithms in Small Embedded Systems

This work describes how a control algorithm can be implemented in a small (8-bit) microcontroller for the main purpose of merging embedded systems and control theory in electrical engineering undergraduate classes. Two different methods for discretizing the control expression are compared: Euler transformation and bilinear transformation. The sampling rate’s impact on the algorithm is discussed and theoretical results are verified by an application to a temperature control system in a heating plant. Four control algorithms are compared: PID and PI algorithms discretized with Euler and bilinear transformation, respectively. It is shown that for the heating plant used in this work, a bilinear PI algorithm implemented in a small 8-bit microcontroller outperforms a commercial controller from Panasonic. It is also demonstrated that all the derived algorithms can be implemented using integer calculations only, obviating the need for expensive and time-consuming floating-point calculations. This work bridges the gap between control theory equations and the implementation of control systems in small embedded systems with no inherent floating-point processing power.


Introduction
Control theory and embedded systems are typically treated separately in electrical engineering programs and the implementation of control algorithms in digital computing targets is only "indicated" on a block diagram level. This work presents the details of the implementation of control algorithms in a small microcontroller which at the same time offers a challenging application for an em-Engineering bedded systems class.
Control systems are one of the most frequently occurring electro-mechanical systems in process industry [1]. Temperature, gas and liquid flow, liquid levels and rotation speeds (revolutions) are only some of the physical quantities that are frequently controlled. "Controlled" refers to the fact that the current process value is measured (by a sensor) and fed back and compared with the intended value (the set value) and the control system's task is to eliminate the difference between the set value and the process value. This is illustrated in Figure 1.
From Figure 1, we can see that the input to the control algorithm is the error signale (t), i.e. the difference between the set value and the process value. The control algorithm is typically a PIor a PID-system. P is the proportional band; the smaller P is, the greater is the amplification K P (of e(t)). I represents the integrating part of the control algorithm. This part is necessary in order to eliminate "remaining errors" (otherwise the set and process values may differ even in steady state). Finally, D represents the differential part of the algorithm. This part has two objectives; it makes the system more agile to abrupt changes in the set value or perturbations, but most of all, it helps to stabilize the system (all feedback systems run the risk of being instable).
The transfer functions of a PI and a PID system are represented by Equation (1) and Equation (2), respectively.
In Equation (1) and Equation (2), s is the Laplace variable (s = α + jω), K P is the amplification, T I is the integration time and T D is the derivation time. The {K P , T I , T D } set is referred to as the PID parameters and they need to be determined first; they depend on the physical properties of the process.
The "process" can be very simple or extremely complex, but most systems are approximated with either a first or a second order system and these models are necessary in order to determine the control parameters. This work is not primarily concerned with finding the PID parameters; the primary concern of this work is how to implement expressions such as Equation (1) and Equation (2) into a small embedded system. Having said that, control parameters are typically derived either by rule of thumb [2], by examining the step response [3] [4] or by more advanced system identification methods [5].
In this work, Ziegler-Nichols' step response method was applied in order to derive the PID parameters [6]. First, the open-loop step response of the process is registered, see Figure 2.
In a digital solution, y is the output from an n y -bit ADC (Analog-to-Digital Converter), i.e. an integer in the range 0 and 2 1 y n − . Correspondingly, the input step signal u is the output from an n u -bit DAC (Digital-to-Analog Converter):   . If a stepwise input change from u 0 to u ∞ results in an output change from y 0 to y ∞ , the open-loop gain is L and T in Figure 2 are the inherent deadtime and the time constant of the process, respectively. These parameters must be measured in a step response experiment and the parameter set {K, L, T} is used to find the appropriate PID parameters according to Table 1 [6].

Review of Previous/Related Works
Implementation of control algorithms in single-chip microcontrollers has been reported ever since they were introduced on the market. All ranges and brands have been used. For example, 8-bit microcontrollers were used in [7]- [12], Aslam et al. used a 16-bit PIC controller [13] and Krivic et al. [14] and Arzak et al. [15] used 32-bit ARM controllers. A lot of PID implementations have been reported in FPGAs, either using soft CPUs like Xlinix's Pico Blaze [16] [17] or Altera's Nios processor [18]. Direct implementations in embedded hardware, such as FPGAs [18] [19] and FPAAs [20] have also been reported.  [14], DC-DC output voltage [12] and position control of magnetically levitated balls [11] have been reported. One of the most salient works is the implementation of a microcontroller PID system for controlling the movement of a wheelchair by EEG waves (for quadriplegic patients) [15].  [14]. Kheriji suggested a Model Predicted Control algorithm which is an extension to the Euler-transformed PID [22].
In spite of an extensive data base search, no works have been found that utilize bilinear transformation of control algorithms; Euler transformation is apparently prevalent. This work remediates this gap and demonstrates the advantages of bilinear transformation. There is also a paucity of implementation details in previous works; exactly how are control equations translated into C code (without floating-point support) and exactly how is the hardware details designed. For example, how are the algorithm details affected by analogue-to-digital and digital-to-analogue converters? This work will cover all these implementation details.

Theory
The problem addressed in this work is how control equations, such as those represented by Equation (1) and Equation (2), are best implemented in a small, 8-bit microcontroller (non-digital signal-processing chip). Two methods are proposed and compared: Euler transformation and bilinear transformation.

Euler Transformation
In Equation (1) and Equation (2), G(s) represents the transfer function from e(t) to u(t) in Figure 1 (i.e. U(s)/E(s)). Taking the inverse Laplace transform of expressions (1) and (2) gives us the time domain signals: In Figure 3, the error signal e(t) has been sampled with a sampling time T S . From Figure 3, it is obvious that the integral of e(t) can be approximated by the sum of the indicated rectangles, i.e.
Correspondingly, in Figure 4, we can see that the derivative of e(t) at some Hence, when we discretize the control algorithms (4) and (5) using Euler transformations, we get the following expressions: Expressions (8) and (9) are "computer-friendly" expressions that can be readily implemented in an embedded system. Notice how the sampling time becomes a crucial design parameter.

Bilinear Transformation
In the Euler transform, we first apply the inverse (Laplace) transform and then discretize the time. In the bilinear transformation, we do exactly the opposite; we first transform the continuous-frequency expressions (1) and (2)  T S e' n Engineering [23]. In the z transform, the frequency variable is the normalized frequency variable Ω (normalized to the sampling rate): Since ω is limited to frequencies < 2π × f S /2, due to the sampling theorem [24], Ω is limited to ±π (z transforms are periodic with period 2π to represent the periodicity of sampled signals in frequency space).
Hence, a transformation from continuous-time to discrete-time becomes cumbersome, since only a limited number of frequencies can be carried over to a discrete-time representation. To overcome this, we have to "squeeze" in all frequencies in the continuous time-domain, from -∞ to +∞, into the ±π interval in the z domain. The following expression accomplishes this: Equation (11) maps ω to Ω as: This is illustrated in Figure 5.
In Equation (11), we solve for ω (and multiply both sides by the imaginary number j): For α = 0, s = jω and we can write Equation (12) (13). We determine it so that we get a good mapping for the lowest frequencies; if Ω is "small" then, tan (Ω/2) ≈ Ω/2 and expression (12) is now Solving for k, and remembering that Ω is the normalized frequency variable, we get:  Combining Equation (13) and Equation (15), we finally get our transformation expression: This is the substitution we need to do in expressions (1) and (2) to translate the control frequency functions to discrete-time (sampled) space. We do Equation (1) first: Equation (1) represents the transfer function from e(t) to u(t) in Figure 1.
Taking the inverse z transform of Equation (19), and remembering that in the z transform, z −m represents a time delay of m sample times ( [25]

K u u T T e T T e T
Notice an important difference between the Euler expression (8) and the bilinear expression (20); in Equation (20) we need old samples of the control signal (u n−1 ). This implies poles in the z plane, i.e. the system could become instable (if the poles are outside the unit circle in the z plane). With the Euler transform, we always get inherently stable systems (no poles).
Next, we need to do the same substitution in Equation (2). This will give us the following expression:

Hardware
The hardware setup is illustrated in Figure 6. The heart of the control system is a microcontroller from Microchip; the 20-pin, 8-bit PIC16F1769 mid-range controller [26] (list price is $1.92). The process to control (the "plant") is the temperature of a power resistor. Its temperature is measured by a temperature sensor, LM35 [27], and fed back to the control algorithm. The sensor's sensitivity is 10 mV/˚C and its signal is amplified 10 times by a non-inverting amplifier (taking advantage of an op amp embedded into the microcontroller). The output is connected to one of the analog input channels; this represents the process value and since the maximum allowed input is +5 volts, the temperature range is 0..50˚C.
The output from the control algorithm is sent to a pulse width modulated (PWM) output (period = 4.4 seconds). This output is connected to a semiconductor relay [28] that controls the dc power source (V p ) that heats the plant resistor. The V P voltage was adjusted so that a PWM duty cycle of 100% would correspond to a plant temperature of +50˚C; V P = 8.0 volts. The set value is adjusted by a 10-turn potentiometer connected to one of the analog inputs (AN2) and the plant temperature is registered on analog channel AN3.

Software
The PWM duty cycle value can only be updated at the end of each period and hence it makes no sense to read the set and plant temperatures more often than the PWM period (i.e. the sampling time equals the PWM period). The software was designed to generate an interrupt at the end of each PWM period and the control algorithms were implemented in the ISR (interrupt service routine; the software is background/foreground oriented ( [29], pp. 83-84)) and the ISR is illustrated in Figure 7.
The heart of the algorithm is the calculation of the new output value and the update of the old samples. This part of the C code is illustrated below. u_temp = u2 + 5892*e−7154*e1 + 2172*e2; //calculate new if (u_temp > 65535) //limit the output to u_temp = 65535; //a 16-bit range else if (u_temp < 0) u_temp = 0; u = (unsigned int)u_temp; //type conversion PWM6_DutyCycleSet(u); PWM6_LoadBufferSet(); //Update output e2 = e1; //Update old samples e1 = e; u2 = u1; u1 = u; The program was developed in Microchip's MPLABX  IDE using the XC8 C compiler. The microcontroller ran at 16 MHz and the first C code line above (calculating the new output value), was timed in the simulator to take 52 μs.

Step Response
The open-loop step response was measured using two analog channels on a Tektronix MSO2012B oscilloscope. Since the relay that controls the plant is con-   memory stick. Graphs were plotted in MATLAB to facilitate a convenient estimation of the K, T and L parameters in Figure 2 and Table 1. The input step change was generated by changing the PWM duty cycle from 20% to 60%.

Performance/Verification
The performance of the derived control algorithms was evaluated by registering the control system's response to 1) a positive step change in the set value (from 30˚C to 40˚C), 2) a negative step change in the set value (from 40˚C to 30˚C) and finally, 3) a perturbation test where the plant was subjected to an instant cooling perturbation (using cold spray). All responses were observed for 17 minutes.
Also, in order to benchmark our control algorithms, their performance was compared to that of a commercial temperature controller from Panasonic (KT4) [30]. The Panasonic controller controlled the temperature of the same power resistor. The only difference in the setup was that the Panasonic controller only L. Bengtsson DOI: 10.4236/eng.2020.129044 633 Engineering accepts thermocouples or Pt-100 temperature sensors as input (a type K thermocouple was used); the thermocouple was attached to the power resistor (but the LM35 resistor was not removed, it was used to register the temperature on the oscilloscope). Other than that, the exact same setup was used except that the relay in Figure 6 was removed; the Panasonic controller also has a PWM output with an internal relay. The Panasonic controller was first set to "autotuning mode" where it evaluates the connected systems and finds its own PID parameters (by some built-in rules of thumb). Figure 8 illustrates the open-loop step response when the input PWM duty cycle (u(t)) changes instantly from 20% to 60%. (In Figure 8, the step signal is the output from an auxiliary I/O pin that only marks the start of the step change in the PWM signal; its amplitude has been manipulated in MATLAB to fit the graph).

Control Parameters
From Figure 8, we can see that the output changes from 3.03 volts to 3.95 volts. For a 10-bit ADC, with a reference voltage of 5.0 volts, that corresponds to seconds. Finally, combining these parameters with Table 1 and expressions (8), (9), (20) and (21), we get four different control algorithms according to Table 2.
(Notice that Table 2 indicates that only integer operations are required).    In the Euler case, the PID algorithm appears to be slightly better than the PI algorithm; both oscillate a little in the steady state, but the oscillation amplitude is slightly less in the PID case (≈1˚C).
For the algorithms based on bilinear transformation, the PID algorithm is outperformed by the PI algorithm. Overall, the bilinear PI algorithm exhibits the best performance; it has the same steady-state oscillation as the Euler PID algorithm, but much smaller initial overshoot. Figure 10 below illustrates the negative step response of the bilinear PI algorithm and Figure 11 illustrates the perturbation response.

Performance, Panasonic Controller
After the autotuning was completed, the Panasonic PID parameters were set to P = 22.2˚C, I = 34 seconds and D = 8 seconds. Figures 12-14 below illustrate the positive and negative step responses and the perturbation response of the Panasonic controller. Figures 12-14 should primarily be compared to Figure 9(c), Figure 10 and Figure 11, which illustrate the corresponding performance of the bilinear PI algorithm.

Discussion
From Figures 9-11, it must be concluded that the bilinear PI algorithm exhibits the overall best performance of all the four suggestions in Table 2. From Figures 12-14, Figure 10. The negative step response of the bilinear PI algorithm. Figure 11. The perturbation response of the bilinear PI algorithm.     it is also clear that it performs even better than a commercial temperature controller from Panasonic.
In all controllers, the plant temperature oscillates a little in steady state. In Figure 9(c), the amplitude of the oscillations seems to increase with time. For that reason, the output of this controller was carefully monitored for two hours and the oscillations petered out almost completely.
The first version of this design did not work as expected; the microcontroller was not able to control the temperature. After extensive debugging, the error was found in the software; the delay between the sampling of AN2 and AN3 was in-

Conclusion
This work has presented detailed instructions on how to implement PI and PID algorithms into small embedded systems and it has been proven that a bilinear transformation of the control expressions yields a better controller performance than the general modus operandi of Euler transformations. The reason why a bilinear transformation has better performance than the Euler transformation lies probably in the fact that the bilinear transformation produces algorithms with feedback; they utilize previous samples of the output signal. Herein lies also the weakness; control algorithms derived by bilinear transformation may become inherently unstable (which cannot happen with Euler algorithms). This work has also demonstrated that a complete controller can be implemented in an inexpensive, analog/digital hybrid 8-bit microcontroller, costing less than $2. That will make it less expensive than any other implementations in ARM-or FPGA-based solution and in terms of performance, it has been demonstrated that it can outperform a commercial desktop controller. This also makes it readily available for undergraduate classes in electrical engineering. Here, the application was a temperature plant, but the same system has been successfully implemented also on a water level plant and the fact that the arithmetic is based on integer calculations indicate that it should not be a problem to apply it also to DC motor control. The fact that only integer calculations are required (see Table  2) eliminates the need for expensive floating-point processors.

Conflicts of Interest
The author has no conflicts of interest to report.