## 5 Case Study: COTS-based Embedded Real-Time Control of a Time Mash Delivery Machine
### The COTS-Based Controller
#### 5.2.2 COTS-Based Control Software  
Within this COTS's hardware infrastructure, the real-time supportive software (middleware layer) is based upon a customized version of the open-source UBUNTU 18.04 LTS Linux distribution available for embedded IoT devices and boards such as RPis, PDAs etc. UBUNTU 18.04.2 LTS (kernel version 4.15.0.1041-raspi2 #44 Ubuntu SMP PREEMPT armv7l) supports real-time interface circuitry with peripherals (e.g., sensors, actuators) and system applications written in standard high-level languages such as C/C++. The Linux kernel is a low-latency preemptible kernel by default, capable of satisfying soft real-time requirements of control applications. Thus, the default scheduler cannot provide the fixed and predictable latency required e.g., for real-time data sampling. For this reason, PREEMPT_RT real-time patch was installed, to provide Linux OS with deterministic scheduling and hard real-time capabilities of a fully preemptible kernel. This ensures reliable real-time performance in the controls of electronic devices and other control gears and sensors, which have more strict timing requirements in the transmission of signals. In many cases, the PREEMPT_RT patched Linux is a suitable alternative for proprietary real-time operating systems. The system's application control software (developed in C) consisted of tasks (implemented as software threads) that perform all the processing and machine control operations (including motor on/off, motor speed regulation). A master software module generates the PWM signals for the control of the AC motor, thus regulating the flow rate of lime slurry delivered. The RPi3 supports a PWM hardware peripheral (through general-purpose onput/output GPIO18) but is also capable of generating more software PWM signals needed for the frequency control of the AC motor. In our approach, a  
Figure 8: COTS-based control system. single process is created that handles three software threads (tasked as user-level threads, based on pthread library) to generate multiple PWM signals. At least one processor core is dedicated (CPU affinity is set for each task) to run each multithreaded task that generates the PWM signal, in order to ensure its timing execution. For this purpose, any task attempts to execute non-preemptible (real-time) code in other cores are not allowed. In this way, tasks running on these cores perform predictably with minimum latencies. The majority of latencies on the RPi3 with real-time support OS are considerably lower and below 50 usecs. The average maximum observed latency is still significantly lower than the one observed under the default Linux (without real-time support). Even in the worst-case scenario, the results indicate that a value of about 150 usecs, as a lower bound, could be an acceptable safety margin for such low frequencies in most real-time systems structured in this way, as long as the frequency-time step value is higher. However, an embedded systems developer should consider any application latency specific requirement, and not rely on this indicative latency bound. Overall, the latencies and particularly the maximums are reduced in the RPi3 with real-time support, thus proving that is suitable for use in time-sensitive (real-time) embedded control systems. The flow of the system control software routines is shown in Figure 9.  
Figure 9: The flowchart of the system control software. PWM involves the modulation of the signal's pulse. Initially, after experimentation, the operating PWM frequency (period of the cycle) was set at a high-frequency rate of 1000 Hz. The percentage of time the signal is ON of the total cycle (duty cycle) spans from a minimum (0%) to a maximum value (100%), and initially was set to 100% (no modulation). While running the control software, this ratio (ON_time/100) will be changing (increasing/decreasing), thus changing the speed (frequency) of the motor's rotation according to the desired flow rate value of the lime slurry delivered volume.