LOW COST HARDWARE IN THE LOOP CONTROL SYSTEM USING MICROCONTROLLERS

Control systems engineering is a very interesting engineering disciplines. It requires proper tools to teach it effectively, one of the tools is experimental setups. They are needed to understand and apply different control strategies and to connect theory with application. However, such setups might not be easy to reach for a number of reasons such as their relatively high prices. Hardware in the Loop (HIL) is a way to replace an experimental setup to be studied with an equivalent hardware, such as a PC or a microcontroller. In this paper a microcontroller is used to replace two different physical systems. In addition to simulating the systems, another microcontroller is also used to replace the control loop, i.e. the summing node and the controller. The simulation of the systems and the whole control loop on the microcontroller was carried out using difference equation method and Ordinary Differential Equations method whenever it is applicable. The interface between the two microcontrollers was established using serial communication protocol and ESP NOW. The performance of the control system is compared with MATLAB and showed very high accuracy. The simulation was run in real time, with a sample time of 10ms.


INTRODUCTION
Modelling and simulation are useful tools to study, and learn about dynamic systems as well as testing different controllers on them. Beside the standard well known experimental setups used to teach and test different control strategies, what is called Hardware in the Loop (HIL) can also be used [1]. It is a technique for testing a controller on a virtual environment representing the real system. HIL is desirable as the hardware might be expensive to build or run into error, or A platform called RTSDS is proposed to be used for embedded control algorithms. It consists of Intel DN2800MT Mini-ITX platform as a main control architecture with Arduino Mega and Arduino Uno as data acquisition modules and a PSOC CY8CKIT-001 kit as a signal generation module. It was used to simulate a water tank system and a vehicle control system [7].
A HIL for a chemical system simulation based on Ordinary Differential Equations ODEs solved using Runge-kutta method on 8 bits and 32 bits microcontrollers [8]. The authors studied the speed and error truncation performance for the microcontrollers. The serial communication was used to output the data which resulted in slowing the performance by 16 times and 42 times than without the serial communication. System dynamics simulation on microcontrollers using ODEs can be carried out using Euler method. In which the authors emulated a spring mass damper system, and an RLC circuit. The work was carried out on a 16-bit PIC microcontroller running at 16MHz clock [9].
The mathematical model of a stirred tank heater is simulated on MATLAB/Simulink and an Arduino Uno is used as a controller. Sliding mode controller is implemented and used to control the simulated system [10]. An Arduino Uno board is used as an interface between a host PC and another target PC programmed using MATLAB/Simulink/xPC Target. The target PC is used to simulate an active suspension system [11]. A mobile robot with wheels and an inverted pendulum is simulated using MATLAB/Simulink environment and the controller was implemented on an Arduino Yun board [12]. The communication between them was established using TCP/IP protocol. Hardware in the loop to simulate an autonomous under water vehicle using beagle bone and programmed using MATLAB/Simulink [13]. Two microcontroller boards, namely STM32F446RE Nucleo, were used to simulate a control system of an inverted pendulum. Where, one is used as a controller and the other is used as the system, in which, everything was coded on MATLAB/Simulink and then deployed on the boards [14].
In this paper, two microcontrollers are used to simulate closed loop control systems. Where, two well-known controllers, namely the Lead and the PID, are used to control two well-known models. The first model is a single input single output system which is a DC-Motor position system, and the second is a single input multi output system which is the inverted pendulum. This paper is organized as follows; the microcontrollers used for the simulation are presented in section 2, while the methods used for simulation are presented in section 3; the two models with their simulation on MATLAB and microcontrollers are presented in section 4, while the two controllers are presented in section 5; finally the results and the discussion and the conclusion are in sections 5 and 6 respectively.

SIMULATION ON MICROCONTROLLERS
In control systems, it is desired for a plant to follow a reference input, usually a step function, with specific characteristics, such as rise time, and settling time. The error between the reference input and the output of the plant is fed to a controller which is designed to shape the response of the system to get the desired response of the plant. This is called a control loop, see Figure 1. Because of the cost of the laboratory experimental setups, it is proposed to use a widely used micro-controller to replace the plant, see Figure 1. It is also Connecting the two controllers, one for the plant and one for the controller, can be carried out in different methods, depending on the micro-controller. For example two Arduino UNO boards can be connected using serial communication protocol, but cannot be connected using analogue signals as the Arduino UNO does not output a real analogue signal rather than Pulse Width Modulation PWM [15]. However, other boards, such as Arduino Due boards, can be connected using analogue signals as they have true analogue outputs. In this work, two Arduino UNO boards are connected together to simulate the plant and the controller through serial communication. The hardware serial pins, i.e. pins 0 and 1, are not used to connect the two boards together as they are used to send the output signal for visualization. Rather, a soft serial library is used to connect the two Arduino UNO boards at a baud rate of 56700bps using pins 9 and 10, see Figure 2. Also, two ESP32 microcontrollers are connected together using ESP-NOW protocol. It is a protocol by Esspressif used to easily exchange data not larger than 250Bytes at a time between two or more ESP32 boards [16]. The communication protocol is Wi-Fi based, and a board needs only the mac address of the board to be sent the information to. The ESP32 boards have more than one hardware serial port, and hence can be connected together without using software serial library.

+ -
In order to simulate a system, whether it is a plant or a controller, the following steps are proposed: 1. Enter the initial conditions of the system 2. Get the input value 3. Calculate the next value of the simulation 4. Send the output value 5. Wait until a specific time is passed 6. Go to step 2 Note that the simulation must be carried in an accurately timed manner, real time, as making it faster will make the system faster than it should be and vice versa.

DYNAMIC SYSTEMS SIMULATION
Dynamic systems can be modelled using ordinary differential equations or transfer functions. The differential equations are simulated on the microcontrollers' using Runge-Kutta method [17], while the transfer function is simulated by discretizing it.

Differential Equations
Solving systems represented by differential equations, is equivalent to simulating them. They are assumed to be represented in state-space form, where the state and output equations are given as follows: Where, is the state matrix, is the input matrix, is the output matrix, and is the feed forward matrix. This system of differential equations is solved (simulated) numerically using the fourth order Runge-Kutta method, sometimes referred to as RK4 [18], [19].
Where is a function of the variables , the initial time, and is the initial condition.
Given the initial condition (value), of the equation and the variable(s) at the initial time, the solution iteratively approximates the next value of the solution at time , where is the step size of the simulation, by following the following procedure: Where, means ( ), and is the slope at the beginning, and are the slopes at mid-point, and is the slope at the end of the interval.
The above procedure is repeated until the simulation time is satisfied. Note that this is applied to the case when there is only one differential equation. For a system of higher number of equations, four slopes for each equation are needed to be calculated. For a system of n equations, the procedure is as follows: Where,

Transfer Function
This form of representation method, the relationship between the input and the output is discretized at a sample time of can be given in the following form: Where, represents one time delay unit, and are the coefficients of the denominator and the numerator respectively.
The discretized transfer function is then transformed into difference equation in the following form Where, ( ) and ( ) are the output of the system and the input of the system respectively at sample .
Such system can be simulated by simply plugging in the values of the previous values of the input and the output in the difference equation (17) [20]. Note that to ensure an accurate simulation, the result of the equation should be used every time interval of . This method and the previous one are used to implement the mathematical model of a single input single output model, namely DC-motor position system, and a single input multi output model, namely inverted pendulum.

DC Motor
DC motor model is considered one of the most widely used example in control books to learn modelling and control [20][21][22]. It is used to demonstrate modelling of systems with both electrical and mechanical parts. It is also used as a part of case studies in control.

A. Modelling
The system considered is shown in Figure 3. Othman E. Aburas The mathematical model of the DC motor, assuming the output is position, can be given as follows:

755
Where, is the motor's constant, is the back electromotive force constant, and are the resistance and the inductance of the armature respectively, is the inertia, and is the friction.
B. Simulation To simulate the system given by equation (19), it is transformed in state-space form which can be represented as following: [ ] The differential equations, given by (20), are evaluated first using Runge-Kutta method, when implementing this system on a micro-controller; then the output of the system is the first state times 10, derived from the matrix in equation (21). The difference equation of the system after discretizing it using Tustin method [20] with a sample time of is: Note that, in order to get an accurate enough response, the digits considered, excluding the exponent part, must be at least 6 fraction digits after the decimal point.
The response of the model to the standard step input, carried out using the step command in MATLAB environment, is shown in Figure 4, in solid line. The implementation on micro-controller is carried out, Runge-Kutta and difference equation, and the output is shown in Figure 4, in dotted and dashed lines respectively. Note that in case of Runge-Kutta, the order of evaluation of the constants is were computed first, then and finally . Also the output of the system was the first state times 10, from the matrix. The error between the Runge-Kutta implementation on the microcontroller and the MATLAB's step command is around 0.1% of MATLAB's response; while the error in case of the difference equation on the microcontroller is around 0.8%, the error might be due to the precision of the microcontroller.

Inverted Pendulum Model
In addition to the DC motor, the inverted pendulum system is also one of the widely used models in learning and applying control strategies. This system has a pendulum on a moving base, see Figure 5. It is an example of single input, multi-output system, where the input is the force applied to the base, and the outputs are the inclination angle and the distance travelled by the wheels [24].  The system can be represented by the following state-space mathematical model, assuming there is no friction, and the input is the force applied to the base [24]: Where, ( ) The value of the terms in equation (23)

Volume (6) Issue 5 (December 2021)
The output is desired to be the angle, i.e. the third state, and the distance, i.e. the first state, then the output equation can be written as B. Simulation The response is evaluated on the microcontroller using Runge-Kutta method, once and the difference equation method. The system is represented by 4 differential equations, i.e. equation (24), which requires the evaluation of 16 equations for the slopes. It is noted that the fractional parts of the numbers in equation (24) are all repeating and can be represented as fractions. That can be useful when implementing on microcontrollers as they can be more precise than using float numbers. Equation (24) can be rewritten using only fractions as follows: Since the system has two outputs, then two discretized transfer functions are needed to represent the system, one for the inclination angle and one for the distance. The discretization uses Tustin method [20] with a sample time of Othman E. Aburas

Journal of Alasmarya University: Basic and Applied Sciences
. The difference equations of the system, one for each transfer function, can be given by: The response of the system, given by equations (26) and (25), is simulated using MATLAB's step command is shown in Figure 6, in solid line. The response of the system implemented on the Arduino UNO and ESp32 micro-controller, in dotted and dot-dashed lines respectively, using Runge-Kutta and difference equation using equations (27) and (28), is shown in Figure 6. The error of using the Runge-Kutta implementation on the microcontroller is around , and for using difference equation for the angle output. And the error of the distance using Runge-Kutta is around , and using difference equation. The error in case of using the difference equation is high, this is due to the resolution of the double and float numbers on the Arduino Uno boards. To decrease this error, a better microcontroller, ESP32, in terms of computation power is used. The error is improved by around 10 folds, to be for the inclination angle and 5 folds, to be , for the distance output. Step response of the inverted pendulum using MATLAB, and Runge-Kutta and difference equation on two microcontrollers, the inclination angle is above and the distance is below

CONTROLLERS
Similar to the dynamic system's simulation, two controllers are implemented using the two methods previously mentioned, i.e. Runge-Kutta and the difference equation. Note that the Rung-Kutta method is not applicable to Othman E. Aburas 555 ‫العلىم‬ ‫األسوريت:‬ ‫الجاهعت‬ ‫هجلت‬ ‫والتطبيقية‬ ‫األساسية‬ Journal of Alasmarya University: Basic and Applied Sciences transfer functions with numerator order larger than denominator order such as the well-known PID controller. The implementation is carried out for two widely studied controllers, namely the Lead and the PID controllers [21,22,25].

Lead controller
The phase Lead controller has two zeros and two poles, it can be given in the following form: Where, and represent the zero and pole locations of the lead controller respectively.
To illustrate the accuracy of implementing the lead controller on microcontrollers, this controller is designed to control the DC-motor given by equation (19). The response is desired to have an overshoot of , and a settling time of s. The designed lead controller's transfer function is The equation can be simulated using Runge-Kutta method by solving the state space form given as following Note that the symbols and here refer to the input and the output of the controller.
The system is also simulated by transforming equation (30) into difference equation, which can be presented in the following form The DC-motor's equations are implemented on one microcontroller and the controller is implemented on another as previously explained in section 2. The response of the whole system, plant with the controller carried on MATLAB in solid line, and on the microcontrollers, using Runge-Kutta, in dashed line, and difference equation using dashed line are shown in Figure 7. and respectively compared to s on MATLAB.

PID controller
The PID controller sometimes called the three terms controller, it is considered to be one of the most widely used controllers in industry. It comprises of three parts, namely a proportional gain, an integrator, and a differentiator. It can be given using the following equation: Where, and are gains. Othman E. Aburas

Journal of Alasmarya University: Basic and Applied Sciences
Applying Runge-Kutta method to this controller is not possible, because the order of the numerator of the transfer function of the controller is higher than the denominator, and cannot transform it to ODEs. Only the difference equation is used to simulate the system on the microcontroller.
A PID controller was designed, so that the system has an overshoot of 10% and a settling time of 1 sec. The gains of the PID controller are . Plugging in the gains in equation (34) and discretizing the transfer function, yields the following difference equation: Note that the symbols and here refer to the input and the output of the controller.
The response of the whole system, controller with the inverted pendulum, simulated on MATLAB and the microcontroller are shown in Figure 8.

RESULTS AND DISCUSSION
The results obtained using microcontrollers are very close to that obtained using MATLAB. The Runge-Kutta method of simulating systems on microcontrollers showed a better results than that obtained using difference equations method. That might be because the Runge-Kutta method has numbers of close logarithmical scale, in the order of 0 and 2 in case of the DC-motor model. On the contrary, the difference equations for the simulated systems with a 10ms sampling time have logarithmically diverse numbers, in the order of 0 to -6 (3.9 and ). The difference causes a loss in precision as the microcontrollers have only 6 digits after the decimal point, unlike the MATLAB environment which has 15 digits after the decimal point precision. That was confirmed when the same equations are implemented on an 8-bit microcontroller, (Arduino UNO), and then on a 32-bit microcontroller (ESP32). The results were up to 10 folds better in case of the inverted pendulum case.
The speed of the loop, from reading a value till outputting the response, is tested for a sample time of 10ms. However, by removing the delay restrictions, the maximum sample time obtained was approximately 3.5ms on the Arduino UNO board for the Runge-Kutta method. The fastest sample time that obtained using difference equation was 2.4ms. Note that no techniques to optimize the speed of the loop, such as transforming the division into multiplication, were used. The fastest simulation time obtained was 1.06ms using the difference equations of the DC-Motor on the ESP32 board.

CONCLUSIONS
In this work, microcontrollers are used to simulate dynamic systems. In which it can be used as a testing platform instead of experimental setups. Two typical case studies and two controllers were simulated on microcontrollers. The connection between the plants and the controllers were once a serial communication protocol and another time a Wi-Fi based communication protocol. The accuracy of the simulation is highly accurate when compared with MATLAB. The work can serve an excellent setup for researchers and students to apply and learn digital control systems principles, such as the effect of sampling time and aliasing on systems, and implement different controllers on microcontrollers.
The implementation presented is for linear systems, however, nonlinear properties such as saturation, and backlash can be added in programming to