# Application of the MiL and HiL Simulation Techniques in Stewart Platform Control Development
## 4 Hardware-in-the-Loop (HiL) Simulation of the Stewart Platform
The block actuator body size shown in Figure 13 is equal to the length of the linear actuator body \(h_{k}\). The signal representing the piston rod extensions is scaled to the required range. There is also a saturation block that prevents the output signal from being of a higher magnitude than allowed. When we generate the code out of this model, we get a library block that can be used in the PLCnext code. Programming is done in PLCnext Engineer, which is a software platform for Phoenix Contact automation controllers. In this environment, we can use the generated library block and use it as a subroutine in PLCnext. We must specify the task to which it belongs to and assign the input and output variables. The inner structure of the inverse kinematics library cannot be investigated in this environment. Only its inputs and outputs are available. Generated libraries, therefore, act like a black box. Figure 12: Stewart platform HiL simulation scheme. In the PLCnext Engineer, we have used the generated inverse kinematics block as a single subroutine. The feedback controller part has been coded directly in the new subroutine. The inverse kinematics subroutine for calculating desired leg lengths runs in the same cycle as the feedback controller subroutine with 1 ms time period. The snippet of the controller code is shown in Figure 14.  
In Figure 14 the signal from analog input is shown to be processed and scaled. The same goes for the output signal. In Figure 15 is a simple HMI interface that is used to define \(\mathbf{q_{w}}\). As mentioned before, HiL allows us to design and test graphical user interfaces and control systems higher in the control hierarchy even before the real prototype is built. In this configuration, it would also be possible to collect data from the simulation model and  
Figure 14: Part of the code of the Stewart platform controller. Figure 13: The inverse kinematics algorithm coded in the Matlab/Simulink environment. compare it to the data from the real Stewart platform, and thus this configuration could also be a significant part in the emerging research topic of the Digital twin [30; 31; 32]. In the HiL plant simulation, authors used Runge-Kutta (ode4) solver with the fixed-step size 2 ms, as in the MiL scenario. In Figure 16 is the HiL simulation model of the Stewart platform created in the Matlab/Simulink. This simulation model is adjusted for the real-time simulation in the MLB. The Stewart platform plant is the same as in the MiL scenario, only the blocks for the inverse kinematics and the feedback control have been adjusted and moved to the PLC controller. In the HiL simulation model, there are blocks for analog inputs that receive a signal that carries information of the action values \(u\). The blocks for the output signals, on the other hand, carry the information regarding the actual piston rod extension \(y\). Both input and output signals need to be scaled and processed accordingly. In Figure 17 the transient responses of the moving plate for the step change of the position and orientation of the Stewart platform obtained during the HiL simulation can be seen. The initial and desired position and orientation \(\mathbf{q_{0}}\) and \(\mathbf{q_{1}}\) of the Stewart platform are the same as in the MiL scenario and the same operation has been performed as well but with the different timing. Since the desired value signal is not transferred to the MicroLabBox, we cannot display it in the same graph as \(\mathbf{q_{y}}\) values. Figure 16: Simulink block diagram of the Stewart platform for the HiL simulation. Figure 15: The HMI interface for the Stewart platform control. The important conclusion from Figure 17 is that analogically to the MiL scenario the error in linear actuator control results in the similar deviations of position and orientation of the moving plate. However, it must be noted that the steady state deviation in the platform orientation is more prominent and the transient responses are slightly oscillating. In Figure 18 are transient responses of the piston rod extensions.