CoDIT'13 Implementation of a Predictive controller on the STM32 board 1 Smeti A.,2Chagra W., 3Ksouri M. Laboratory of Analysis Conception and Control of Systems, 1 : Faculty of Sciences of Tunis, 2 : Preparatory Institute for Engineering Studies El Manar, Tunis 3 : National School of Engineering of Tunis, Tunisia. [email protected], [email protected], [email protected] Absract— In the literature, there is few practical works about the implementation on miniaturized devices like FPGA (Field Programmable Gate Arrays) and microcontrollers of the Generalized Predictive Control (GPC) technique. The reason can be attributed to its programming complexity. In this work, we took advantage of the advancement in the field of embedded systems and we realized an application based on a GPC implemented on an STM32 microcontroller to control a basic second order system. Also, a PID (Proportional-IntegralDerivative) controller has been implemented in the STM32 to make a performance comparison with the GPC. Key words- Predictive control; PID; STM32; Microcontroller I. INTRODUCTION Implementation of control techniques on miniaturized devices is often used because it saves a lot of space thus reducing clutter [1], [5], [6], [7], [8], [9], [10]. In addition, there exist several softwares that facilitate the communication and the programming of the mentioned devices. Another reason is the appearance of several types of microcontrollers with low cost that are becoming more and more efficient, thanks to a decreased time processing. The STM microcontrollers are an example of the advancement in the embedded technology that combines low cost, low consumption and more performance. In this paper, the implementation of the GPC (Generalized Predictive Control) control of a system of second order on the STM32VLDiscovery board is performed using the software IAR Embeded Workbench. The results will encourage us to look for the implementation of more advanced control techniques on the mentioned microcontroller applied to nonlinear and complex systems. The paper is organized as follows. In section II, we present the GPC controller. The section III deals the stm32 board and Iar Embeded Workbench. In the section IV, we present the experimental results. 978-1-4673-5549-0/13/$31.00 ©2013 IEEE II. THEORICAL REVIEW OF GPC We consider systems that can be modelized by the following discrete state space model: x ( k + 1) = F x ( k ) + G Δ u ( k ) (1) y (k ) = H x(k ) The matrices F, G and H, are given as mentioned in [2]: ⎛ a1 1 " 0 ⎞ ⎛ b0 ⎞ ⎜ ⎟ ⎜ ⎟ # # " #⎟ # ⎟ F= ⎜ , G= ⎜ , ⎜ an−1 0 " 1 ⎟ ⎜ bn− 2 ⎟ ⎜ ⎟ ⎜ ⎟ ⎝ an 0 " 0 ⎠ ⎝ bn−1 ⎠ H= (1 0 " 0) H ∈ R (1,n ) , F ∈ R ( n ,n ) , G ∈ R ( n ,1) . The state of the system is x(k)∈ Rn, the measured 1 output is y (k ) ∈ R1 , the control increment is with Δu(k) and Δ = 1 − z −1 is an operator that denotes the integral action which ensures static error elimination. Using the equation (1) we obtain the state expression at (k+j): j −1 x(k + j / k ) = F j x(k ) + ∑ F j−1−iGΔu(k + i) i =0 (2) The use of both equations (1) and (2) leads to the output j-step ahead predictor: j −1 y(k + j / k ) = HF j x(k ) + HF j −1−iGΔu(k + i) (3) ∑ i=0 The GPC is based on the minimization of a quadratic cost function wich is given by: Hp Hc j=1 j=1 J =∑(y(k + j / k) −wk ( + j))2 +λ∑Δu(k + j −1)2 (4) 250 CoDIT'13 where Hp is the prediction horizon; Hc is the control horizon, λ is the weighting factor, w(k+j) denotes the set-point at time k+j and y(k + j / k) and j∈[1,Hp] is described in equation (3). In this section, we adopt the output deviation method [2]. Using F, G and H above , we define the following matrices: 0 ⎛ HG ⎜ HG L= ⎜ HFG ⎜ # # ⎜⎜ H p −1 H p −2 ⎝ HF G HF G ⎞ ⎟ # 0 ⎟; ⎟ # # ⎟ H −H # HF p c G ⎟⎠ # 0 ⎡ HF ⎤ ⎢ 2 ⎥ M= ⎢ HF ⎥ ⎢ # ⎥ ⎢ Hp ⎥ ⎣⎢ HF ⎦⎥ The matrix L has dimension (Hp, Hc) where Hc is the control horizon and the vector M has Hp elements. The output sequence can be written as follows: (5) Y = LΔu + Mx Where: T y = ⎡ y(k +1/ k ) y(k + 2 / k ) " y(k + H p / k ⎤ ⎣ ⎦ Δu = [ Δu(k) Δu(k +1) " Δu(k + Hc −1] The STM32 BOARD III. EMBEDDE WORKBENCH AND IAR A microcontroller is a computer system that includes a single integrated circuit memory ROM (Read Only Memory) and RAM (Random Access Memory), a processor that executes information and input-output circuits for the interaction with the outside environment. A. STM32F100RB microcontroller The choice of the STM32 value line discovery is based on the following characteristics: low cost, low power consumption, as well as a great performance. Indeed, the mentioned card includes a microcontroller type STM32F100rb, where it integrates the cortex M3 32-bit processor. The cortex M3 is based on the RISC (Reduced Instruction Set Computer) architecture, which constitutes the main cause of the success of the combination between low cost and fast processing and enables also the STM32vldiscovery to reach a frequency speed of 74 MHz. The mentioned microcontroller has also a 128 kb flash memory and 8 kb RAM [3]. Adding to this, the board includes a large number of input pins / outputs, some of which are connected to the ADC and DAC via a bus 32 MHz. Power can be provided either through a USB cable or an external power supply from 3.3V to 5V. For debugging, the card has an interface called STlink that enables communication between the PC and board. Fig.1 shows the stm32 boards. T The objective function (4) can be expressed as: J = (Y − W ) T (Y − W ) + λ Δ u T Δ u (6) In which W = ⎡⎣ w( k + 1) w( k + 2) ... w(k + H p ) ⎤⎦ Minimizing the equation (6), we obtain the optimal control sequence: −1 Δ u = ⎡⎣ LT L + λ I H c ⎤⎦ LT [W − Mx ( k ) ] Where I Hc ∈ R ( Hc , Hc ) is the identity matrix. Finally, we have the recursive form of the command described by the following equation: u (k ) = u ( k − 1) + Δu (1) In wich Δu(1) is the first element of Δu. 978-1-4673-5549-0/13/$31.00 ©2013 IEEE Fig 1. STM32VLDiscovery board T W = ⎡⎣ w(k + 1) w(k + 2) " w(k + H p ) ⎤⎦ B. IAR Tool Chain IAR Embedded Workbench is a development tool easy to use. It allows debugging embedded applications using assembler, C and C++. Programming becomes easier using a standard library given by the manufacturer which provides a firmware of initializing [3], the peripherals of the microcontroller. Table I shows the source code provided by Stmicroelectronics. 251 CoDIT'13 TABLE I. Source Code Provided By STMicroelectronics File name Roles of peripherals p stm32f10x_rcc stm32f10x_adc stm32f10x_dac stm32f10x_gpio stm32f10x_dma Configurattion of all the clocks that will bee used Initialization of Analog / Digital connverter Initialization of the Digital / Analog connverter Configurattion of input and output portts Initialization the DMA (Direct Memory Access) A ⎡ 2.586 1 0 ⎤ F = ⎢⎢ −2.2129 0 1 ⎥⎥ , ⎣⎢ 0.6269 0 0 ⎥⎦ ⎡ 0.022 ⎤ G = ⎢⎢0.0188⎥⎥ , ⎢⎣ 0 ⎥⎦ H = [1 0 0] . In order to use the GPC coontroller algorithm, the steps illustrated in the flow wchart, (Fig.2) must be followed: After that, we need routines for matrices computation. Routines will be sum mmarized in the following table: File name Zero_matrix Product_matrix Sum_matrix Tr_matrices Prod_vect_mat IV. TABLE II. GPC firmwaree Roles of peeripherals Initializatioon all the matrices at zeros Multiplicatiion of two matrices Addition off two matrices Transposition of a matrice Multiplicatiion of a matrix with a vectoor EXPERIMENTAL RESUL LTS In this work, we are interestedd in the control results when using the STM32 miccrocontroller, so we chose a basic second order system s an RLC serie. We configured the pin PC4 in input mode and connected it to the capacity output and a the pin PA4 in output mode and connected it too the resistance. The process has a second order diffeerential equation that leads to the following transfer function: fu H ( p) = Vs ( p) 1 = 2 p +1 Ve ( p ) LCp + RCp In our work, we have consideredd the following values: R=268; L= 11.48 H and C= 680 μF w a sampling The discretized transfer function with time Te = 0.02s, is given by the following expression: G( z) = 0.02197 z −1 + 0.0188 z −2 1 − 1.586 z −1 + 0.6269 z −2 The matrices F, G and H are given by: b 978-1-4673-5549-0/13/$31.00 ©2013 IEEE Fig 2. Flowchart of the GPC method In order to evaluate the perfformance of the control technique mentioned above we w use the well known PID (Proportional-Integral-D Derivative) controller. The expression of the discrrete PID controller has only the next form: u(k) = kp (e(k) + k T n e( j) + d (e(k) −e(k −1))) ∑ ki j=0 T where: Te is the sample timee, u is the control input and e is the difference between the reference input and the system output. The latter was implemeented on the same microcontroller. The param meters of the PID are obtained by the method off Takahashi [4]. The closed loop simulation resultts are obtained with the following GPC and PID parrameters: Hp = 4; Hc = 1; λ = 1; kp = 0.22; ki=0.00998; kd=0.0392, where kp is the proportional gain, ki iss the integral gain, kd is the derivative gain. The results are displayed inn Fig. 3, Fig. 4, Fig. 5 and Fig. 6, where we can see s the behavior of the RLC system output, the refference output and the 252 CoDIT'13 control input using respectively the GPC and the PID controller. Fig 3. RLC system response (vs(k) solid line and ve(k) dashed line) (GPC controller) Based on this table, we note that the computing time of the GPC is greater than that of the PID. Indeed, the GPC algorithm has several matrix resolutions, see Table II. The implementation of the PID and the GPC controller on STM32 was successful. From the experimental results we can see that both controllers have a good tracking of the reference input. Although the execution time of the PID algorithm is less than that of the GPC; the latter has the advantage of anticipating the change in the reference input and thus acts before it occurs, which is not the case of the PID controller. V. Fig 4. GPC control input Conclusion In this work, we have implemented a PID and a GPC controllers on a STM32 microcontroller. The IAR embeded workbench was used to load the firmware which enables the microcontroller to control a second order system. The presented work is the first step that enables us to consider in the future the control of complex and nonlinear systems with the GPC strategy implemented on the STM32 microcontroller. REFERENCES [1] E.Aridhi, M. Abbes, A. Mami. “FPGA Implementation of Predictive Control”, MELLECON’2012, March 25-28, Hammamet Tunisia. Fig 5. RLC system response (vs(k)solid line and ve(k) dashed line) (PID controller) Fig 6. PID control input In the table below, we compare the performance of execution time between PID and GPC controller. TABLE III. Performance comparison between GPC and PID Controller PID GPC controller Average execution time (μ s) 4.613 532.142 978-1-4673-5549-0/13/$31.00 ©2013 IEEE [2] K. Watanabet, K. Ikeda, T. Fukuda, S.G. Tzafestas. “Adaptive Generalized Predictive Control using A State-Space Approach”,IEEE International Workshop on Intelligent Robots and Systems IROS’91, p. 1609-1614, November 3-5, 1991, Osaka Japan. [3] STMicroelectronics. Datasheet STM32F100, 2009. www.st.com [4] M. Mathelin and I. Bara. Commande numériques des systèmes. Cours, Ecole Nationale Supérieure de Physique de Strasbourg, 2ème année, 2007 [5] J. Ghabi. Synthèse et implémentation sur FPGA de la commande prédictive robuste des systèmes linéaires multivariables. PhD thesis, Ecole Nationale d’Ingénieurs de Monastir, 2010. [6] A. Kheriji, F. Bouani, and M. Ksouri, “Efficient implementation of constraind robust model predictive control using a state space model,” in Proceedings of International Conference on Informatics in Control, Automation and Robotics (ICINCO), 2010. [7] Y. F. Chan, M. Moallem, W. Wang, “Efficient implementation of PID control algorithm using FPGA technology”, Proceedings of the 43ed IEEE Conference on Decision and Control, V5, PP. 4885-4890, Bahamas2004. [8] U. R. Y. Jayaraman, “Fpga implementation of predictive control strategy for power factor correction,” in Proceedings of World Academy of Science, Engineering and Technology, 2008. [9] G. Palomo, K. Hilton, and J. Rossiter, “Predictive control implementation in a plc using the iec 1131.3 programming standard,” in Proceedings of American Control Conference, 2009. [10] G. Pannochia, “Robust model predictive control with guaranteed set point tracking,” Journal of Process Control, vol. 14,pp.927–937, 2004 253