## 4 Implementation of System for Rapid Prototyping of Real-Time Control Algorithms  
The implementation of a high order real-time control algorithm in embedded platforms is a demanding technical task. Using conventional programming languages such as C or ST (for PLC programming) for the specification of the control algorithm might be error-prone due to the involved matrix arithmetic, hence, extensive testing and formal verification phases are obligatory before actual validation of the control algorithm with the plant. Additionally, hardware requirements of the platform might pose restrictions on the number representation standard (fixed-point or floating point), as well as introduce various rounding and precision errors, which, in some cases, might impact actual stability and performance of the designed controller. Therefore, various approaches for rapid (accelerated) prototyping of the control algorithms exists [32], depending on the application, hardware platforms, and controller design tools. In our case, we use the MATLAB(r)/Simulink environment, which offers several technologies for the simulation of the control algorithm with target hardware and plant, which are more or less bounded to a particular target hardware or application. However, there is no standard library in MATLAB(r) for controllers specialized for hydraulic applications such as Dantfoss(r) MC012-022. Hence, we developed a custom solution to enable a rapid experimental validation of the designed control algorithm. Our rapid prototyping system consists of the following elements:  
* An embedded program in the MC012-022 developed in Danfoss Plus+1 IDE to support data acquisition from pressure and flow rate sensors attached to the test bench, as well as to generate the control waveforms using PWM for the control valve amplifier;
* CAN-based communication channel between the microcontroller and the desktop workstation running MATLAB(r)/Simulink. The channel is supported with a CAN-USB converter on a physical level and respective driver modules in the workstation (Kvaser) and in the microcontroller. We developed a custom communication protocol to enable exchange the signal levels between the Simulink and the microcontroller in real-time;
* Simulink communication block, implemented as a m-function (Figure 12), which can access the CAN driver to handle the real-time communication protocol messages and to synchronize the execution of the host Simulink model with the real-time timestamps of the microcontroller. The microcontroller is responsible for the synchronization by sending data messages on fixed intervals, which, upon reception by host workstation, unblock the execution of the Simulink model for one sample interval. After that, the execution of the model is blocked again until the next message is received, hence, the timestamps of the Simulink diagram are synchronized with the time of the microcontroller;  
Figure 11: Structure of closed-loop system with \(H\)\(\infty\) controller. The purpose of the control system is to control the pump swash plate swivel angle in order to keep the flow rate at a predefined reference level. The reference signal for our experiments is selected as periodic square wave signal with period of 2.5 s. The square waveform as an input to the closed-loop system allows for the excitation of all frequencies of the loop, to evaluate its performance metrics. The key requirement for the closed-loop systems is the achievement of 0 steady-state error, i.e., the mean value of the flow rate to be equal to the commanded value. Another requirement is the settling time, or how fast the output of the system reaches its steady state. That metric is important not only for reaction time to changes in the reference signal, but also for the disturbance rejection capabilities of the system. Therefore, if the settling time is short, in an event of an external disturbance, such as instantaneous change of external loads or valve switching, the pump will respond to that disturbance seamlessly.