Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
Classes | Macros | Typedefs | Functions
Scheduler.h File Reference
#include <stdbool.h>
#include "PriorityQueue.h"
Include dependency graph for Scheduler.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  Scheduler
 

Macros

#define MAX_SENSORS   10
 
#define MAX_HZ   1000
 

Typedefs

typedef struct Scheduler Scheduler
 

Functions

void SchedulerInit (Scheduler *scheduler, Updateable *updatableArray[])
 Initializes the scheduler with the given sensors. More...
 
void SchedulerRun (Scheduler *scheduler)
 Runs the scheduler, executing tasks based on their priority. More...
 
void SchedulerStop (Scheduler *scheduler)
 Stops the scheduler. More...
 

Macro Definition Documentation

◆ MAX_HZ

#define MAX_HZ   1000

Definition at line 9 of file Scheduler.h.

◆ MAX_SENSORS

#define MAX_SENSORS   10

Definition at line 8 of file Scheduler.h.

Typedef Documentation

◆ Scheduler

typedef struct Scheduler Scheduler

Function Documentation

◆ SchedulerInit()

void SchedulerInit ( Scheduler scheduler,
Updateable updatableArray[] 
)

Initializes the scheduler with the given sensors.

Parameters
schedulerPointer to the Scheduler structure to initialize.
updateableArrayArray of pointers to Updateable structures to be scheduled.

Initializes the priority queue and schedules tasks based on the given sensors and their update frequencies. Limits the number of sensors to MAX_SENSORS.

Definition at line 14 of file Scheduler.c.

14 {
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}
#define MAX_SENSORS
Definition: PriorityQueue.h:8
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
#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
PriorityQueue tasks
Definition: Scheduler.h:12
bool running
Definition: Scheduler.h:13
Definition: Task.h:8
Here is the call graph for this function:

◆ SchedulerRun()

void SchedulerRun ( Scheduler scheduler)

Runs the scheduler, executing tasks based on their priority.

Parameters
schedulerPointer to the Scheduler structure to run.

Runs the scheduler, repeatedly executing tasks based on their scheduled execution times. Uses Sleep or usleep to avoid busy-waiting if no tasks are ready to execute.

Definition at line 43 of file Scheduler.c.

43 {
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}
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
bool PQPop(PriorityQueue *pq, Task *task)
Pops the highest priority task from the priority queue.
Definition: PriorityQueue.c:48
void TaskExecute(Task *task)
Executes the task by calling the sensor's update function.
Definition: Task.c:9
int hz
Definition: Task.h:9
highResTime nextExecTime
Definition: Task.h:10
Here is the call graph for this function:

◆ SchedulerStop()

void SchedulerStop ( Scheduler scheduler)

Stops the scheduler.

Parameters
schedulerPointer to the Scheduler structure to stop.

Definition at line 66 of file Scheduler.c.

66 {
67 scheduler->running = 0;
68}