An Improved Task Scheduling Algorithm for Intelligent Control in Tiny Mechanical System

Wireless sensor network (WSN) has been already widely used in many fields in terms of industry, agriculture, and military, and so forth. The basic composition is WSN nodes that are capable of performing processing, gathering information, and communicating with other connected nodes in the network. The main components of a WSN node are microcontroller, transceiver, and some sensors. Undoubtedly, it also can be added with some actuators to form a tiny mechanical system. Under this case, the existence of task preemption while executing operating system will not only cost more energy for WSN nodes themselves, but also bring unacceptable system states caused by vibrations. However for these nodes, task I/O delays are inevitable due to the existence of task preemption, which will bring extra overhead for the whole system, and even bring unacceptable system states caused by vibrations. This paper mainly considers the earliest deadline first (EDF) task preemption algorithm executed in WSN OS and proposes an improved task preemption algorithm so as to lower the preemption overhead and I/O delay and then improve the system performance. The experimental results show that the improved task preemption algorithm can reduce the I/O delay effectively, so the real-time processing ability of the system is enhanced.


Introduction
WSN consists of spatially distributed autonomous sensors to monitor physical or environmental conditions, such as temperature, sound, and pressure.These WSN nodes cooperatively pass their data through the network to a main location.Energy issue and system performance of WSN have always been considered because WSN nodes always work in some harsh environment.Energy is the scarcest resource for WSN nodes, and it finally determines the lifetime of WSNs.Energy consumption of the sensor nodes should be minimized since their limited energy resource determines their lifetime [1].For the WSN OS used in some practical application of connecting some actuators, the task preemption will inevitably bring I/O delay and even unacceptable system states caused by the vibrations [2].Nowadays, while WSN nodes formed a tiny mechanical system by adding actuators, how to decrease the system extra overhead and improve the system performance caused by the vibrations becomes very important.
Currently, there are many researches about improving system performance for WSN nodes; for example, [3] used error and kernel density approach of sleeping nodes in cluster-based WSNs; [4] used low energy online self-test of embedded processor in dependable WSN nodes.Reference [5] studied low-energy symmetric key distribution in WSN; [6] used a dynamic programming approach for QoS-aware power management in wireless video sensor.Reference [7] used virtual backbone scheduling to maximize the lifetime of WSNs.Reference [8] used hybrid-storage units to extend runtime of low-power WSN nodes.Reference [9] studied distributed tracking with energy management in wireless sensor networks.Reference [10] used joint routing and sleep scheduling to maximize lifetime of WSN.Thus it can be seen that most solutions are implemented by the way of utilizing extra resources.But the innovation for this paper is to improve the lifetime of WSN nodes in terms of real-time operating system itself and the task scheduling algorithm to be exact.The decrease of system overhead and the improvement of system performance are implemented by reducing  Task Figure 1: Execution case of EDF task scheduling before and after optimization.
the overhead of task preemptions.The most important is that this method can be combined with all of the others to achieve better system performance in tiny mechanical system composed of WSN nodes, as this improvement is focused on the real-time operating system itself.

EDF Task Scheduling Analysis
Compared with fixed priority task scheduling algorithm, dynamic priority algorithm allocates different priorities to every period task, so the task priority is constantly changing.EDF task scheduling algorithm has already been widely used as a classic dynamic priority scheduling algorithm in WSN [11].EDF task scheduling algorithm assigns tasks with different priority according to execution deadline of task, the earlier task execution deadline, and the higher task priority.In a word, system allocates the highest priority to task having the earliest task execution.

Judging Whether the
System Is Schedulable.Supposing that system has  independent and preemptive period tasks   (1 ≤  ≤ ).  ,   , and   represent the execution time, relative deadline, and period of task   , respectively.The sufficient condition for EDF task scheduling algorithm in single processor is given by While task   (1 ≤  ≤ ) has max block time   , the above sufficient condition will be expressed as If this formula can be met for all tasks, the system is regarded as schedulable.The all below discussions are all based on formula (2).

Task Preemption Optimization about EDF Task Scheduling
Algorithm.Use the attribute set (  ,   ) to describe a period task, where   is the period of task and   is the execution time of the task, while using  , to describe one specific job in a task, which means that the task  has  jobs totally.For example, supposing that there are three jobs in the task  1 , the first one is  1,1 , the second one is  1,2 , and the last one is  1,3 .
As Figure 1(a) shows, task  1 ,  2 , and  3 are all released at time 0, but their execution deadline is 2, 3, and 5, so task  1 has the highest priority and it begins to run firstly.At time 0.8, task  2 begins to run as it has higher priority than task  3 .Till time 1.2, task  3 begins to run.While at time 2, task  1 is released again, task  3 is preempted by task  1 then.Figure 1(a) shows the whole execution case before optimization for these three tasks during the time period [0, 10].
Figure 1(b) shows the execution case after optimization during the time period [0, 10]; the beginning time for jobs of tasks  1 ,  2 , and  3 becomes {0, 2.2, 4.5, 6, 8.6}, {0.8, 3, 6.8, 9.4}, and {1.2, 3.4, 5.3, 7.2}, respectively.Under this case, task  3 is preempted 2 times totally, and in the meanwhile, task  1 and  2 do not exceed their deadline.This is implemented by postponing the execution of some jobs for task  1 and  2 , which means that nonpreemptive scopes are added for some jobs in task  3 , and the below discussion will describe the process of implementation in detail.

Extra Overhead
Caused by Task Preemption.Pree , is a task preemptive set, which describes that task   is executed  times totally.The number of elements for Pree , is signed as  , , which means the times that task   is preempted totally, so the total amount for all these tasks are given by If a low priority task is preempted by another one, then the extra cost for the preemption is  pree .While there is no preemption exists and then the extra cost is  nonpree , for any task, no matter whether it is preempted, the total extra cost for the system is  pree −  nonpree [11].
If the phase position is  for the real-time task set  0 , . . .,  −1 , the extra overhead caused by task preemption during the time period [0, ] is described as So we can know that the task preemption will bring extra overhead for the whole system.As saving energy is very important for WSN nodes that formed a tiny mechanism system, how to reduce the task preemption becomes very necessary.

Analysis of I/O Delay for EDF Task Scheduling
For the release time of all jobs in set , there exists a max I/O delay for certain job [11,12].

Analysis of I/O Delay
Caused by Task Preemption.Supposing that allocated preemptive threshold PT  equals   and ignoring the restrain of preemptive threshold time, then according to the analysis of time requirement, the max I/O delay for preemptive task scheduling of   is given by where  is the release time of job; then,   () can be calculated by While  (+1)  () =  ()  (), the above iterative calculation will end.
For task   , while the release time of its one job is , the job load of higher priority can be represented as A preemptive threshold PT  (0 ≤ PT  ≤   ) is assigned to task   , and the I/O delay should be calculated by the preemptive part and nonpreemptive part, respectively, as below [11].
The max I/O delay of preemptive part for task   is given by where  pree  () is the max value of formula (9), and it can be calculated by the initialization value  pree(0)  = 0.The max I/O delay of nonpreemptive part for task   is represented as so the max I/O delay for task   based on EDF scheduling algorithm is described as While PT  ≥  pree Under this case, the I/O delay is a constant value, and it is irrelevant to task attribute.
While PT  <  pree From the above formulas, we can know that PT  is the main factor for  max  , and the next section will describe how to assign PT  for jobs. ( The max execution time of nonpreemptive part is   − PT  for task   , so the necessary condition that task can be schedulable is there is existing  ( ∈ ), which meets the below formula: as  is the detecting set, and it is given by where  ∈ , and it meets the following formula: The necessary and sufficient condition for EDF task scheduling is described as Then   can be calculated by To make the task   schedulable, the min value of PT +1 is given by So to make all tasks schedulable, the min value of PT +1 is represented as From the above analyses, system allocates the PT 1 = PT min * 1 =  1 to the task  1 that has the smallest relative deadline.The PT value of other jobs will also be allocated by formula (21).
The system obtains block time   for every task and calculates threshold of tasks whose relative time slices are larger than PT  according to   and then allocates threshold to task  = 2, 3, . . .,  by Algorithm 1.
Algorithm 1 implements an optimization allocation of preemptive time threshold for each job.From the above formula ( 18) and ( 19), we can know that the time complexity of this algorithm is ().TID = {tid 1 , tid 2 , . . ., tid  } which corresponds to task release time.If several tasks are released at the same time, then the system uses the task TID having the highest priority.
For any one task set based on EDF task scheduling algorithm, system can calculate the amount of task preemption by task attribute during the time period [0, ] according to Algorithm 2.
The above algorithms describe the implementation of postponing the execution of some jobs for some tasks; at the meanwhile, nonpreemptive scope is added for some jobs in others tasks, and the below discussion will describe the experiments about the performance test.

Experimental Platform
In order to test the performance of the improved task scheduling algorithm, we used the hardware platform of Webit&NEU [13] and its operating system Webit&NEU OS which were implemented by our China Liaoning Province Embedded Technique Key Laboratory.
4.1.Hardware Platform.Webit&NEU (Atmega128L Processor) uses the basic structure of Webit2.0.It can collect and handle the data collected from the CC2520 and store the core data to data memory.
Webit2.0 (AT90S8515 Processor) is an embedded Internet device that has implemented the function about making the standard industry device that can access the Internet.Webit2.0 has successfully been developed by our China Liaoning Province Embedded Technique Key Laboratory and has been manufactured by our China Shenyang Neu-Era Information Technology Stock CO., LTD.The most important is that Webit2.0 has also achieved the China's product patent (numbers ZL 00 2 52482.1 and ZL 01 2 29725.9)[14].

Software Platform.
A real-time operating system must have the ability to respond to external events fast.In order to coordinate and execute tasks effectively, Webit&NEU OS system provides the following services: interrupt handlers, intertask communication facilities, task synchronization, internal memory management, timing services, task priority assignment, and so on as Table 1 shows [15,16].Webit&NEU OS supports up to 16 tasks, which is committed to achieve low cost application in the IoT.These tasks have 8 different priorities of 0-7.The bigger the value, and higher the priority.
All of these functions implemented by the unique design of system call can easily execute real-time applications.System call is a program supported to user by operating system, while users need to implement certain system function; then, what they need to do is to call the accordingly system call in the application program; thus, it is very convenient for users to use the system kernel.
All the system calls are written in AVR assembly language.One of the important advantage for AVR assembly language is that it can flexibly and easily manage a certain storage location in memory, even its certain bit of the storage location, so system can effectively handle task, interruption, message, timing, and so on.
All of these functions can easily implement real-time applications, so the Webit&NEU OS system can well be

Performance Analysis of Improved EDF Scheduling Algorithm
For system load  = 0.05, 0.1, 0.15, . . ., 0.9, the system generates 18 period task sets totally, and each set includes 6 period tasks generated at random.Table 2 shows the case of task preemption comparison before and after algorithm optimization.
From the experimental results, we can know that the optimization scope ranges from 50.0% to 33.3%; with the increase of system load, the preemption optimization rate decreases correspondingly, which means that system can get better optimization while system load is lower [17][18][19].
We used the tools of logic analyzer (TLA603), arbitrary waveform generator (AWG2021), and digital storage oscilloscope (TDS1012).In the test programs, we added some control codes by transforming the value of high and low levels of pulses through I/O pin while task is preempted.
The I/O pin of Webit&NEU is connected to the digital storage oscilloscope (TDS1012).While there are the task preemptions happening, the system will alter the current value of pulses when certain task is about to be preempted; accordingly, the system will also change the current value of pulses again while the preempted task begins to execute again.So from the waveform of the experimental results in Under the case of different system load, we obtained the I/O delay according to the above method, and the experimental results are shown in Figure 2.
As Figure 2 shows, while the system load is 0.05, system can achieve the best optimization rate of 50.8%; however, even the system load is 0.9 and the optimization rate can still reach 31.6%.Obviously, the proposed improved task scheduling algorithm can well decrease the I/O delay for the real-time operating system.The system I/O delay is obviously reduced because of the decrease of task preemption, which also means that the system overhead is also reduced.Consequently system real-time ability is enhanced, and at the meanwhile, energy consumption is also obviously decreased [20].
Table 3 shows the system kernel code size of Webit5.0OS (the fifth version operation system for Webit2.0),Webit&NEU OS, and C/OS-II separately.Because Webit&NEU OS supports only up to 16 tasks, it is committed to low cost application in the IoT, so its kernel code size is smaller than C/OS-II who manages up to 64 tasks.This is just the design goal of light-weight operating systems for Webit&NEU OS.
As the code memory of Atmega128L is 128 KB, the code size of Webit&NEU OS with improved algorithm (4.031 KB) is 4.34% larger than Webit&NEU OS's (3.856 KB); its size only occupies 3.15% of the whole code memory, so it still leaves enough space for user applications.

Conclusions
Compared with other current common solutions to increase the lifetime of WSN nodes, this paper aims to improve the lifetime and system performance by optimizing task scheduling algorithm when WSN nodes are applied to actuator applications.On the premise that these jobs can be executed within their execution deadline, system postpones executing some jobs to ensure that more jobs can better be executed, which can reduce task preemption greatly.By executing the improved task scheduling algorithm in the platform of Webit&NEU and Webit&NEU OS both designed and implemented by our laboratory, the experimental results show that the system I/O delay and overhead are decreased effectively and the occurrences of unacceptable system states caused by vibrations are also reduced, due to the reduction of task preemption in tiny mechanical system composed of WSN nodes.
CREATE TASK: Creating and scheduling a system task dynamically.(ii) DELETE TASK: Deleting a specific system task.(iii) GET FUNCTION ID: Obtaining task's function ID (00H to 0FFH).(iv) SUSPEND TASK: Suspending a task of being executed.(b) Intertask communication services (i) ALLOCATE: Allocating buffer space where a task creates a message for sending.(ii) SEND MESSAGE: Sending a message to the specific destination.(iii) WAIT MESSAGE: Causing a task to wait for receiving a message.(iv) DEALLOCATE: Returning a buffer allocated to the system buffer.(c) Internal memory management services (i) GET MEMORY: Getting the address of a block of internal memory with specific length is currently available in the system.(ii) RELESE MEMORY: Returning a block of memory of specific length to the system memory pool.(d) Interrupt-handling services (i) DISABLE INTERRUPT: Disabling specific interrupts.(ii) ENABLE INTERRUPT: Reenabling disabled interrupts.(iii) SYNCHRONIZE: Synchronizing tasks with interrupts.(e) Timing services (i) SET INTERVAL: Setting a time interval when the interval event has occurred.(ii) WAIT TIME: Waiting for interval event or timeout to occur.

Figure 2 :
Figure 2: I/O delay comparison before and after algorithm optimization.

Table 1 :
The structure of Webit&NEU OS kernel.

Table 2 :
Case of task preemption comparison before and after algorithm optimization.

Table 3 :
Comparison of system kernel size (KB).oscilloscope, we can obtain the whole time   for the whole process.Besides, we can also get the whole execution time of the preemption task   .So I/O delay time  I/O delay can be calculated by  I/O delay =   −   .