Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
Scheduler.c
Go to the documentation of this file.
1#include <time.h>
2#include <stdint.h>
3#include "../../Inc/Scheduler/Scheduler.h"
4// FIXME: Was supposed to be timer include.
5// #include "../../Inc/stm32f7xx_hal_conf.h"
6
7// Timer flag declared in the interrupt handler
8// extern volatile uint32_t timer_flag;
9
14void SchedulerInit(Scheduler* scheduler, Updateable* updatableArray[]) {
15 PQInit(&scheduler->tasks);
16 scheduler->running = false;
17
18 for (int i = 0; updatableArray[i] != NULL; i++) {
19 if (i >= MAX_SENSORS) {
20 printf("Warning: Number of sensors exceeds MAX_SENSORS. "
21 "Some sensors will not be scheduled.\n");
22 break;
23 }
24
25 Updateable* updateable = updatableArray[i];
26 if (updateable->hz <= 0 || updateable->hz > MAX_HZ) {
27 continue; // Skip invalid frequencies
28 }
29
30 Task task;
31 TaskInit(&task, updateable, updateable->hz);
32 int hz = 1000 / updateable->hz;
33 int initialPriority = clock() / (CLOCKS_PER_SEC / 1000) + hz;
34 PQPush(&scheduler->tasks, task, initialPriority);
35 }
36}
37
43void SchedulerRun(Scheduler* scheduler) {
44 scheduler->running = true;
45 Task currentTask;
46
47 while (scheduler->running) {
48 // if (timer_flag < 0) continue;
49 // timer_flag--;
50 // FIXME: Re-implement timer.
51 // int currentTime = HAL_GetTick();
52 int currentTime = 0;
53
54 while (!PQIsEmpty(&scheduler->tasks) &&
55 PQPeek(&scheduler->tasks, &currentTask) &&
56 currentTask.nextExecTime <= currentTime) {
57 if (PQPop(&scheduler->tasks, &currentTask)) {
58 TaskExecute(&currentTask);
59 currentTask.nextExecTime = currentTime + (1000 / currentTask.hz);
60 PQPush(&scheduler->tasks, currentTask, currentTask.nextExecTime); // Reinsert task with new priority
61 }
62 }
63 }
64}
65
66void SchedulerStop(Scheduler* scheduler) {
67 scheduler->running = 0;
68}
bool PQPeek(PriorityQueue *pq, Task *task)
Peeks at the highest priority task in the priority queue without removing it.
Definition: PriorityQueue.c:56
bool PQIsEmpty(PriorityQueue *pq)
Checks if the priority queue is empty.
Definition: PriorityQueue.c:62
#define MAX_SENSORS
Definition: PriorityQueue.h:8
bool PQPop(PriorityQueue *pq, Task *task)
Pops the highest priority task from the priority queue.
Definition: PriorityQueue.c:48
bool PQPush(PriorityQueue *pq, Task task, int priority)
Pushes a task with a given priority onto the priority queue.
Definition: PriorityQueue.c:41
void PQInit(PriorityQueue *pq)
Initializes the priority queue.
Definition: PriorityQueue.c:3
void SchedulerInit(Scheduler *scheduler, Updateable *updatableArray[])
Initializes the scheduler with the given sensors.
Definition: Scheduler.c:14
void SchedulerStop(Scheduler *scheduler)
Stops the scheduler.
Definition: Scheduler.c:66
void SchedulerRun(Scheduler *scheduler)
Runs the scheduler, executing tasks based on their priority.
Definition: Scheduler.c:43
#define MAX_HZ
Definition: Scheduler.h:9
void TaskInit(Task *task, Updateable *updateable, int hz)
Initializes a task with the given sensor and update frequency.
Definition: Task.c:3
void TaskExecute(Task *task)
Executes the task by calling the sensor's update function.
Definition: Task.c:9
PriorityQueue tasks
Definition: Scheduler.h:12
bool running
Definition: Scheduler.h:13
Definition: Task.h:8
int hz
Definition: Task.h:9
highResTime nextExecTime
Definition: Task.h:10