Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
Functions
ControllerSystem.c File Reference
#include "../../Inc/Systems/ControllerSystem.h"
#include "../../Inc/Utils/Common.h"
#include "../../Inc/Utils/Updateable.h"
#include "../../Inc/Scheduler/Task.h"
Include dependency graph for ControllerSystem.c:

Go to the source code of this file.

Functions

void initControllerSystem (ControllerSystem *controller, const char *name, int hz, ControllerType type, int(*updateController)(ControllerSystem *controller), void *child)
 Initializes the Controller System with initial settings. More...
 
int c_defaultAddMonitor (ControllerSystem *controller, MonitorSystem *monitor)
 Adds a monitor to the controller. More...
 
int c_defaultRemoveMonitor (ControllerSystem *controller, MonitorSystem *monitor)
 Removes a monitor from the controller. More...
 
int c_defaultUpdate (Updateable *updateable)
 Default update function for ControllerSystem objects. More...
 
int c_defaultSafety (ControllerSystem *controller)
 Default safety function for ControllerSystem objects. More...
 

Function Documentation

◆ c_defaultAddMonitor()

int c_defaultAddMonitor ( ControllerSystem controller,
MonitorSystem monitor 
)

Adds a monitor to the controller.

Parameters
ctlA pointer to the ControllerSystem structure.
mtrA pointer to the MonitorSystem structure to add.
Returns
_SUCCESS if the monitor was added, _FAILURE otherwise.

Definition at line 24 of file ControllerSystem.c.

24 {
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}
#define _FAILURE
Definition: Common.h:6
#define _SUCCESS
Definition: Common.h:5
#define MAX_MONITORS
Definition: Constants.h:21
MonitorSystem * monitors[MAX_MONITORS]
Here is the caller graph for this function:

◆ c_defaultRemoveMonitor()

int c_defaultRemoveMonitor ( ControllerSystem controller,
MonitorSystem monitor 
)

Removes a monitor from the controller.

Parameters
ctlA pointer to the ControllerSystem structure.
mtrA pointer to the MonitorSystem structure to remove.
Returns
_SUCCESS if the monitor was removed, _FAILURE otherwise.

Definition at line 33 of file ControllerSystem.c.

33 {
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}
Here is the caller graph for this function:

◆ c_defaultSafety()

int c_defaultSafety ( ControllerSystem controller)

Default safety function for ControllerSystem objects.

Parameters
selfPointer to the ControllerSystem object.
Returns
int Status of the ControllerSystem object.

Definition at line 69 of file ControllerSystem.c.

69 {
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}
@ c_computed
@ c_validated
ControllerState state
int(* runMonitor)(void *self)
Definition: MonitorSystem.h:30
Here is the caller graph for this function:

◆ c_defaultUpdate()

int c_defaultUpdate ( Updateable updateable)

Default update function for ControllerSystem objects.

Parameters
pointerto the Updateable object.
Returns
int _SUCCESS or _FAILURE.

Definition at line 47 of file ControllerSystem.c.

47 {
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}
Definition: System.h:17
void * child
Definition: System.h:20
void * child
Definition: Updateable.h:32
Here is the caller graph for this function:

◆ initControllerSystem()

void initControllerSystem ( ControllerSystem controller,
const char *  name,
int  hz,
ControllerType  type,
int(*)(ControllerSystem *controller)  updateController,
void *  child 
)

Initializes the Controller System with initial settings.

Parameters
controllerA pointer to the ControllerSystem structure.
nameThe name of the controller.
hzRate at which the controller is called (in hz).
typeThe type of controller (per ControllerType).
updateControllerThe function to update the controller.

Definition at line 6 of file ControllerSystem.c.

8 {
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}
int c_defaultRemoveMonitor(ControllerSystem *controller, MonitorSystem *monitor)
Removes a monitor from the controller.
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.
@ 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
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
Updateable updateable
Definition: System.h:18
int(* update)(struct Updateable *self)
Definition: Updateable.h:27
Here is the call graph for this function:
Here is the caller graph for this function: