RTES Lab Report 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

1

Table of Contents
1.1. Objectives: ................................................................................................................................ 2
1.2. Introduction: ............................................................................................................................ 2
1.2.1 Task Priorities: ...................................................................................................................... 2
1.2.2 Using “vTaskDelayUntil()”: ................................................................................................. 2
1.3. Tasks with Results and Analysis: ........................................................................................... 3
1.3.1 Task 1: .................................................................................................................................. 3
a) Circuit Diagram: .................................................................................................................. 3
b) Code: ..................................................................................................................................... 3
c) Output:.................................................................................................................................. 4
d) Problem: ............................................................................................................................... 5
1.3.1 Task 2: .................................................................................................................................. 5
a) Code: ..................................................................................................................................... 5
b) Circuit Diagram: .................................................................................................................. 6
c) Output:.................................................................................................................................. 7
d) Problem: ............................................................................................................................... 8
1.4. Conclusion:............................................................................................................................... 8
2

EXPERIMENT: 03

Experimenting with Priorities and using


vTaskDelayUntil()
1.1. Objectives:
In this lab, we will explore the use of vTaskDelayUntil() to create more precise timing in our
embedded applications. The main objectives of this experiment are:

 Understanding the significance of task priorities.


 Learning how to set task priorities in FreeRTOS.
 Experiment with vTaskDelayUntil() to create precise time intervals for task execution.

1.2. Introduction:
An RTOS, which stands for Real-Time Operating System, is a specialized software system designed
for real-time and embedded systems. In real-time systems, it's not just about getting the right response;
it's also about getting that response at exactly the right time. These systems have strict timing
requirements and need to respond to events or inputs very quickly, all within specified time limits.

1.2.1 Task Priorities:


In the previous experiment, we introduced the concept of multitasking using FreeRTOS. In this
experiment, we will take a closer look at task priorities. Task priorities are crucial in determining the
order in which tasks are executed by the scheduler. Tasks with higher priorities are executed before
tasks with lower priorities. It's important to carefully assign priorities to tasks to meet your
application's real-time requirements.

The FreeRTOS scheduler ensures that tasks in the Ready or Running state will always be given
processor (CPU) time in preference to tasks of a lower priority that are also in the ready state. In other
words, the task placed into the Running state is always the highest priority task that is able to run.

1.2.2 Using “vTaskDelayUntil()”:


vTaskDelayUntil() is a FreeRTOS function that allows you to precisely control the timing of your
tasks. Unlike vTaskDelay(), which introduces fixed delays, vTaskDelayUntil() lets you specify the
exact time at which a task should resume execution. This can be extremely useful when you need
tasks to operate on precise schedules.

Differences Between vTaskDelay() and vTaskDelayUntil()


vTaskDelay() results in the calling task entering into the Blocked state, and then remaining in the
Blocked state, for the specified number of ticks from the time vTaskDelay() was called. The time at
which the task that called vTaskDelay() exits the Blocked state is relative to when vTaskDelay() was
called.
On the other hand, vTaskDelayUntil() results in the calling task entering into the Blocked state, and
then remaining in the Blocked state, until an absolute time has been reached. The task that called
3

vTaskDelayUntil() exits the Blocked state exactly at the specified time, not at a time that is relative
to when vTaskDelayUntil() was called. Using VTaskDelayUntil in the task loop will make that task
run as close to that rate as possible.

1.3. Tasks with Results and Analysis:


To perform our lab tasks, we used Arduino IDE and then Proteus for simulations. In Proteus, we can
practically analyzed our tasks through simulations and can see the output (LED blinking) on the
circuit.

1.3.1 Task 1:
In the first lab task, we have to create two tasks in which the first task is responsible for printing a
line on the serial port every 0.5 seconds. The second task will handle the blinking of an LED with a
1-second interval. To achieve this, we made use of the vTaskDelay() function for task scheduling.
The exact timing of the LED blink delay are observed using an oscilloscope.

Using vTaskDelay():
In this task, we simply used vTaskDelay() function to implement the delays in our code. The LED
is connected at Arduino pin 13 and another is connected which turns ON and OFF only once. This
second LED shows that we are fetching data from some sensor.
a) Circuit Diagram:

Figure: 01 Circuit for task:1 in proteus


b) Code:
#include <Arduino.h>
#include <Arduino_FreeRTOS.h>
const int blinkled = 13;
void setup() {
Serial.begin(9600);
pinMode (blinkled, OUTPUT);
pinMode (12, OUTPUT);
xTaskCreate (led_blink,"blink", 128, NULL, 1,NULL);
xTaskCreate (Serial_print,"serial", 128, NULL, 1,NULL);
4

vTaskStartScheduler();
}
void loop() {
}
void led_blink() {
digitalWrite (blinkled, HIGH);
//sensor
digitalWrite (12, HIGH);
vTaskDelay (pdMS_TO_TICKS (100));
digitalWrite (12, LOW);
while (1) {
vTaskDelay (pdMS_TO_TICKS (1000));
digitalWrite (blinkled,!digitalRead(blinkled));
}
}
void Serial_print() {
while (1) {
Serial.println ("Blinking is happening as a multi- thread");
vTaskDelay (pdMS_TO_TICKS (500));
}
}
c) Output:

Figure: 02 Output of digital oscilloscope

Figure: 03 Output of Serial Print


5

Initially, we assigned equal priorities (1) to both tasks. The readings are:

Initial Delay at the time 0 Regular Delay


1.15 s 1s

Next, modify the priorities by assigning a high priority to the serial print task and a lower priority to
the blink task.

Initial Delay at the time 0 Regular Delay


1.16 s 1s

Swap the priorities a high priority to the blink task and a lower priority to the serial print task.

Initial Delay at the time 0 Regular Delay


1.16 s 1s

d) Problem:
Since we are using vTaskDelay() function to implement the delays in our code, one major problem
which arises is that the first delay for LED blinking is not 1 second but it is greater than 1 second.
Since vTaskDelay() introduces fixed delays. As we are extracting data from a sensor before we enter
the While() loop, the time which is used up in getting data from a sensor is also added in the 1 second
delay and hence our first time delay is 1.15 seconds.
The time at which the task that called vTaskDelay() exits the Blocked state is relative to when
vTaskDelay() was called.

1.3.1 Task 2:

Using vTaskDelayUntil():
In the this lab task, we have to same tasks as above but this time using vTaskDelayUntil() function
intead of vTaskDelay() for task scheduling. The exact timing of the LED blink delay are observed
using an oscilloscope. In this task, we simply used vTaskDelayUntil () function to implement the
delays in our code.

a) Code:
#include <Arduino.h>
#include <Arduino_FreeRTOS.h>
const int blinkled = 13; //define the led pin number
void setup()
{
Serial.begin(9600);
pinMode (blinkled, OUTPUT);
pinMode (12, OUTPUT);
xTaskCreate (blink_led,"blink", 128, NULL, 1,NULL);
xTaskCreate (Serial_print,"serial", 128, NULL, 1,NULL);

//start freertos scheduler


vTaskStartScheduler();
}
6

void loop() {
}
void blink_led(void *pvParameters) {
TickType_t xLastWakeTime;
xLastWakeTime = xTaskGetTickCount ();
const TickType_t xFrequency = pdMS_TO_TICKS(1000);//delay is in milliseconds
const TickType_t xFrequency1 = pdMS_TO_TICKS(100);
//sensor
digitalWrite (12, HIGH);
vTaskDelayUntil (&xLastWakeTime, xFrequency1);
digitalWrite (12, LOW);
while (1) {
digitalWrite (blinkled, !digitalRead(blinkled));
vTaskDelayUntil (&xLastWakeTime, xFrequency);
}
}

void Serial_print() {

TickType_t xLastWakeTime;
xLastWakeTime = xTaskGetTickCount ();
const TickType_t xFrequency2 = pdMS_TO_TICKS(500);
while (1){
Serial.println ("The led is Blinking");
vTaskDelayUntil (&xLastWakeTime, xFrequency2);
}
}

b) Circuit Diagram:

Figure: 01 Circuit for task:2 in proteus


7

c) Output:

Figure: 02 Output of digital oscilloscope

Figure: 03 Output of Serial Print

Initially, we assigned equal priorities (1) to both tasks. The readings are:

Initial Delay at the time 0 Regular Delay


1s 1s

Next, modify the priorities by assigning a high priority to the serial print task and a lower priority to
the blink task.

Initial Delay at the time 0 Regular Delay


1s 1s

Swap the priorities a high priority to the blink task and a lower priority to the serial print task.

Initial Delay at the time 0 Regular Delay


1s 1s
8

d) Problem:
Since we are using vTaskDelayUntil () function to implement the delays in our code, the problem
which arises in Task:01 that the first delay for LED blinking is greater than 1 second is now resolved.
Unlike vTaskDelay(), which introduces fixed delays, vTaskDelayUntil() lets us specify the exact time
at which a task should resume execution.
As in the code, we can see that there is a variable xLastWakeTime which counts the number of ticks
passed. The task that called vTaskDelayUntil() exits the Blocked state exactly at the specified time,
not at a time that is relative to when vTaskDelayUntil() was called. So when we are extracting data
from a sensor before we enter the While() loop, the time which is used up in getting data from a sensor
is not added in the 1 second delay and hence our first time delay is also 1 second.

1.4. Conclusion:

You might also like