## 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
Next in the software flow, three software threads are created and synchronized (joined) within their parent process. Further on, these threads are enabled to generate the PWM signals (writing 1s to GPIOs outputs) with the given PWM frequency and duty cycle. At the same time, the flowmeter's flow rate reading is checked against the desired volume, and accordingly, the duty cycle is changed (increased/decreased). This process runs repeatedly in a loop until it is done. The algorithm that describes the basic functionality of these system control structures is shown as pseudocode in Algorithm 1:  
``` structure{gpio_output[3]; freqeucycle_ratio, threads[3]}PWM; \(\leftarrow\) PWMs structure functionPWM_initiate pwm = malloc(sizeof*pwm); \(\leftarrow\) allocate memory pwm->gpio_output[i] = gpio; \(\leftarrow\) enable GPIO outputs pwm->frequency = frequency; \(\leftarrow\) set frequency (initially set to 1000 Hz) pwm->dutycycle_ratio = 1; \(\leftarrow\) set duty cycle (initially set to 100%--no modulation) return pwm; end function PWM_initiate function PWM_thread pthread_setschedparam = SCHED_RR, 99 \(\leftarrow\) set real-time scheduling policy and high priority pthread_setaffinity = 1:3 \(\leftarrow\) set each thread to run on a separate CPU core (actually cores 2 to 4) while (!done) period = 1.0/pwm->frequency; \(\leftarrow\) define run period ratio = pwm->dutycycle_ratio; \(\leftarrow\) define ratio time_ON = period* ratio; \(\leftarrow\) define time run for threads_count = 1:3 starts PWMs signals execution/generation  gpio_write (pwm->gpio_output[1:3],1); \(\leftarrow\) logical bit 1; time_wait(time_ON);  gpio_write(pwm->gpio_output[1:3],0); \(\leftarrow\) logical bit 0; time_wait(period-time_ON);  end for end while end function PWM_thread function PWM_master mlockall(MCL_CURRENT | MCL_FUTURE); \(\leftarrow\) lock current & future task's memory allocations sched_setscheduler = SCHED_FIFO; \(\leftarrow\) set process real-time scheduling policy PWM_initiate; \(\leftarrow\) PWMs structures creation and initialisation gpio_init; \(\leftarrow\) GPIOs input & output initialisations for threads_count =1:3 \(\leftarrow\) PWMs signals creation  pthread_create(pwm->thread[1:3],PWM_thread,pwm); \(\leftarrow\) threads creation and execution  pthread_join(pwm->thread[1:3], NULL); \(\leftarrow\) threads synchronisation end for gpio_read (volume, pwm->gpio_input); \(\leftarrow\) get flowrate volume if (volume<setvalue) pwm->dutycycle_ratio++; \(\leftarrow\) increase flowrate else if (volume>setvalue) pwm->dutycycle_ratio-; \(\leftarrow\) decrease flowrate else if (done) { free(pwm); exit(0);} end function PWM_master

### Experimental Results

A simple graphic application (GUI), shown in Figure 10, was also created by using the Tkinter module in Python, to ease this experimentation process by providing a user-friendly interaction with the control system and for future remote control purposes. This Python GUI module uses ctypes foreign function library to communicate with the RPi3 controller by calling appropriate C functions of the control software in C. For this purpose, these C functions are defined within a shared library generated as a shared object that the Python GUI module calls to execute. The simplicity of its use allows any user to perform easily the control operations. Experiments were carried out with the actual lime mash/slurry storage and delivery machine. Overall, the performance results provided evidence that the proposed control system performs correctly and can efficiently and effectively manage the machine's operations. In particular, the performance of the COTS-based control system (RPi3 controller) was evaluated under real-time operating conditions. Certain tests and measurements were performed and the results were compared against those obtained from the custom-built controller (80C188 EB controller).