Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
ControllerSystem.c
Go to the documentation of this file.
1#include "../../Inc/Systems/ControllerSystem.h"
2#include "../../Inc/Utils/Common.h"
3#include "../../Inc/Utils/Updateable.h"
4#include "../../Inc/Scheduler/Task.h"
5
6void initControllerSystem(ControllerSystem* controller, const char* name, int hz,
7 ControllerType type, int (*updateController)(ControllerSystem* controller),
8 void* child) {
9 initSystem(&controller->system, name, hz, CONTROLLER, controller);
10 controller->type = type;
11 controller->num_monitors = 0;
12 controller->safety = c_defaultSafety;
13 controller->addMonitor = c_defaultAddMonitor;
15 controller->state = c_computed;
16 controller->updateController = updateController;
17
18 // Set the updateable function to generic controller update/safety
20 controller->child = child;
21
22}
23
25 if (controller->num_monitors >= MAX_MONITORS) {
26 printf("Cannot add more monitors to the controller\n");
27 return _FAILURE;
28 }
29 controller->monitors[controller->num_monitors++] = monitor;
30 return _SUCCESS;
31}
32
34 for (int i = 0; i < controller->num_monitors; i++) {
35 if (controller->monitors[i] == monitor) {
36 for (int j = i; j < controller->num_monitors - 1; j++) {
37 controller->monitors[j] = controller->monitors[j + 1];
38 }
39 controller->num_monitors--;
40 return _SUCCESS;
41 }
42 }
43 printf("Monitor not found in the controller\n");
44 return _FAILURE;
45}
46
47int c_defaultUpdate(Updateable* updateable) {
48 // Cast the updateable pointer to a controller system
49 System* system = (System*)updateable->child;
50 ControllerSystem* controller = (ControllerSystem*)system->child;
51
52 // Perform the controller update
53 if (controller->updateController(controller) == _FAILURE) {
54 return _FAILURE;
55 }
56
57 // Set the controller to computed
58 controller->state = c_computed;
59
60 // Perform the safety check
61 if (controller->safety(controller) == _FAILURE) {
62 return _FAILURE;
63 }
64
65 // Return _SUCCESS
66 return _SUCCESS;
67}
68
70
71 if (controller->state != c_computed) {
72 printf("Controller not computed new value\n");
73 return _FAILURE;
74 }
75
76 if (controller->num_monitors == 0) {
77 printf("No monitors set for Controller\n");
78 return _FAILURE;
79 }
80
81 for (int i = 0; i < controller->num_monitors; i++) {
82 if (controller->monitors[i]->runMonitor(controller->monitors[i]) == _FAILURE) {
83 return _FAILURE;
84 }
85 }
86 controller->state = c_validated;
87 return _SUCCESS;
88}
#define _FAILURE
Definition: Common.h:6
#define _SUCCESS
Definition: Common.h:5
#define MAX_MONITORS
Definition: Constants.h:21
int c_defaultRemoveMonitor(ControllerSystem *controller, MonitorSystem *monitor)
Removes a monitor from the controller.
void initControllerSystem(ControllerSystem *controller, const char *name, int hz, ControllerType type, int(*updateController)(ControllerSystem *controller), void *child)
Initializes the Controller System with initial settings.
int c_defaultAddMonitor(ControllerSystem *controller, MonitorSystem *monitor)
Adds a monitor to the controller.
int c_defaultUpdate(Updateable *updateable)
Default update function for ControllerSystem objects.
int c_defaultSafety(ControllerSystem *controller)
Default safety function for ControllerSystem objects.
ControllerType
@ c_computed
@ c_validated
@ CONTROLLER
Definition: System.h:14
void initSystem(System *system, const char *name, int hz, SystemType type, void *child)
Initializes a System object.
Definition: System.c:3
MonitorSystem * monitors[MAX_MONITORS]
int(* addMonitor)(struct ControllerSystem *controller, MonitorSystem *monitor)
int(* safety)(struct ControllerSystem *controller)
int(* updateController)(struct ControllerSystem *controller)
int(* removeMonitor)(struct ControllerSystem *controller, MonitorSystem *monitor)
ControllerType type
ControllerState state
int(* runMonitor)(void *self)
Definition: MonitorSystem.h:30
Definition: System.h:17
Updateable updateable
Definition: System.h:18
void * child
Definition: System.h:20
int(* update)(struct Updateable *self)
Definition: Updateable.h:27
void * child
Definition: Updateable.h:32