Professional Documents
Culture Documents
RTES Lab Report 3
RTES Lab Report 3
RTES Lab Report 3
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
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.
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.
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.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:
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:
Initially, we assigned equal priorities (1) to both tasks. The readings are:
Next, modify the priorities by assigning a high priority to the serial print task and a lower priority to
the blink task.
Swap the priorities a high priority to the blink task and a lower priority to the serial print task.
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);
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:
c) Output:
Initially, we assigned equal priorities (1) to both tasks. The readings are:
Next, modify the priorities by assigning a high priority to the serial print task and a lower priority to
the blink task.
Swap the priorities a high priority to the blink task and a lower priority to the serial print task.
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: