# Application of the MiL and HiL Simulation Techniques in Stewart Platform Control Development
## 4 Hardware-in-the-Loop (HiL) Simulation of the Stewart Platform  
In this section, we present the HiL technique in the Stewart platform development procedure according to the model-based design approach. In this scenario, we can test and potentially validate the PLCnext controller for the Stewart platform control. In this case, the plant is generated as a code and uploaded to the real-time target dSPACE MicroLabBox (MLB). The inverse kinematics part of the control algorithm is generated as a code from the Matlab/Simulink environment as well and uploaded to the PLCnext as a subroutine. PLCnext consists of both generated Matlab/Simulink code and code programmed in its own programming environment PLCnext Engineer. PLCnext also allows running HMI for control and visualisation of the process. Since PLCnext has web server functionality, the HMI can be accessed through the web browser. In Figure 11 a workplace for the HiL simulation is shown. It consists of the PLCnext, the MLB, a PC, and a laptop. PLCnext and MLB are connected to each other through the I/O interface that consists of analog inputs and outputs operating in the range 0-10 V. PLCnext is also connected to the PC through Ethernet. This connection serves to upload the code to the controller and access the HMI, where an operator enters the desired position and orientation of the moving plate \(\mathbf{q_{w}}\). The MLB is also connected to the laptop via Ethernet. In addition to uploading the code to the real-time target, it also serves for visualising and data acquisition in the ControlDesk environment. The mentioned environment also allows the user to manipulate the parameters of the plant. The plant parameters have not been changed during this simulation experiment. The HiL simulation scheme is also described by the diagram in Figure 12. Where \(\mathbf{q_{w}}\) is a vector carrying information about the desired position and orientation of the moving plate. The operator enters \(\mathbf{q_{w}}\) values via the HMI interface on the PC. The controller PLCnext contains inverse kinematics and proportional feedback controller code and is based on the operator input and actual leg extension \(y_{1}\),..., \(y_{6}(t)\) calculates action values \(u_{1}\),..., \(u_{6}(t)\) for each linear actuator, which are part of the simulated plant in the real-time target MicroLabBox. MLB is connected with the laptop. As mentioned in the previous paragraph, the ControlDesk environment, which is running on the laptop, can be used for the data acquisition and the visualisation of the action values, actual piston rod extensions and an actual position and orientation of the moving plate \(\mathbf{q_{y}}\). Figure 11: HiL simulation workplace. Code generation allows us to seamlessly transit from the design phase to the system integration phase. This approach saves a significant amount of time that would otherwise be required if both real-time target and controller had to be coded manually. The main reason is that manual coding is prone to human errors and is also very technically demanding, especially in the case of the plant code. Moreover, PLC programming languages typically do not support tools for linear algebra (i.e., matrix operations), which would make implementing inverse kinematics challenging as well. With the described tools, we can test and validate the chosen controller in this early phase of development before the first prototype has been built, which can significantly reduce time and costs. Nevertheless, we must be sure that the fidelity of the simulated plant is accurate enough before we draw any important conclusions. In Figure 13 the inverse kinematics algorithm implemented in the Simulink environment can be seen. The implementation of the inverse kinematics is based on equations presented in Section 2; however, they are adjusted for the Simulink implementation. The terms of the vector \(\mathbf{q_{w}}\) are defined as inputs, and the calculated outputs \(w_{1}\),..., \(w_{6}\) are the desired piston rod extensions.