#include <Arduino_FreeRTOS.h>
#include <task.h>
// Définition des périodes et temps d'exécution en millisecondes
const unsigned long T1_PERIOD = 50; // 50 ms
const unsigned long T2_PERIOD = 100; // 100 ms
const unsigned long T3_PERIOD = 200; // 200 ms
const unsigned long T1_EXEC_TIME = 20; // 20 ms
const unsigned long T2_EXEC_TIME = 25; // 25 ms
const unsigned long T3_EXEC_TIME = 50; // 50 ms
// Définition des tâches FreeRTOS
TaskHandle_t task1Handle = NULL;
TaskHandle_t task2Handle = NULL;
TaskHandle_t task3Handle = NULL;
// Fonction de la tâche 1
void task1(void *pvParameters) {
unsigned long last_run_time = 0;
while (1) {
unsigned long current_time = millis();
if (current_time - last_run_time >= T1_PERIOD) {
Serial.print("Tâche T1 exécutée à ");
Serial.print(current_time);
Serial.println(" ms");
last_run_time = current_time;
vTaskDelay(T1_EXEC_TIME / portTICK_PERIOD_MS); // Simuler le temps d'exécution de la tâche
}
vTaskDelay(1); // Laisser le CPU à d'autres tâches
}
}
// Fonction de la tâche 2
void task2(void *pvParameters) {
unsigned long last_run_time = 0;
while (1) {
unsigned long current_time = millis();
if (current_time - last_run_time >= T2_PERIOD) {
Serial.print("Tâche T2 exécutée à ");
Serial.print(current_time);
Serial.println(" ms");
last_run_time = current_time;
vTaskDelay(T2_EXEC_TIME / portTICK_PERIOD_MS); // Simuler le temps d'exécution de la tâche
}
vTaskDelay(1); // Laisser le CPU à d'autres tâches
}
}
// Fonction de la tâche 3
void task3(void *pvParameters) {
unsigned long last_run_time = 0;
while (1) {
unsigned long current_time = millis();
if (current_time - last_run_time >= T3_PERIOD) {
Serial.print("Tâche T3 exécutée à ");
Serial.print(current_time);
Serial.println(" ms");
last_run_time = current_time;
vTaskDelay(T3_EXEC_TIME / portTICK_PERIOD_MS); // Simuler le temps d'exécution de la tâche
}
vTaskDelay(1); // Laisser le CPU à d'autres tâches
}
}
void setup() {
Serial.begin(9600); // Initialisation de la communication série
Serial.println("Démarrage du système temps réel avec FreeRTOS...");
// Créer les tâches avec des priorités différentes
xTaskCreate(task1, "Tâche 1", 128, NULL, 3, &task1Handle); // Priorité élevée
xTaskCreate(task2, "Tâche 2", 128, NULL, 2, &task2Handle); // Priorité moyenne
xTaskCreate(task3, "Tâche 3", 128, NULL, 1, &task3Handle); // Priorité basse
}
void loop() {
// FreeRTOS gère l'ordonnancement des tâches
// Petit délai pour stabiliser l'ordonnancement
delay(100);
}