Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
Apps.c
Go to the documentation of this file.
1#include "../../../Inc/Systems/Controller/Apps.h"
2#include "../../../Inc/Utils/Common.h"
3
4#include <math.h>
5
14void checkAppsLimit(Apps* apps) {
15 float pos1 = getAppPosition(apps->app[0]);
16 float pos2 = getAppPosition(apps->app[1]);
17
18 // Resonable Value check
19 if (pos1 > 100 || pos1 < 0 || pos2 > 100 || pos2 < 0) {
20 apps->status = APPS_FAULT;
21 }
22
23 // In sync check
24 float difference = fabs(pos1 - pos2);
25 if (difference > APPS_DIFFERENCE * 100) {
26 apps->status = APPS_FAULT;
27 }
28}
29
30void initApps(Apps* apps, int hz, int channel1, int channel2) {
31 // Allocate memory for the app instances
32 apps->app[0] = (App*)malloc(sizeof(App));
33 apps->app[1] = (App*)malloc(sizeof(App));
34
35 // Check if memory allocation was successful
36 if (apps->app[0] == NULL || apps->app[1] == NULL) {
37 return;
38 }
39
40 // System setup
41 MonitorSystem monitor;
42 initMonitorSystem(&monitor, "APPs Monitor", 200, m_APPS, VEHICLE_SHUTDOWN);
43 initControllerSystem(&apps->base, "APPS", hz, c_APPS);
44 defaultAddMonitor(&apps->base, &monitor);
45
46 // Controller setup
49 apps->status = APPS_OK;
50 initApp(apps->app[0], hz, channel1);
51 initApp(apps->app[1], hz, channel2);
52}
53
54void updateApps(void* apps) {
55 Apps* appsPtr = (Apps*) apps;
56 updateApp(&appsPtr->app[0]);
57 updateApp(&appsPtr->app[1]);
58 if (appsPtr->base.safety(apps) == FAILURE) {
59 printf("APPs is not in a safe state\n");
60 return;
61 }
62 checkAppsLimit(appsPtr);
63 if (appsPtr->status != APPS_OK) return;
64}
65
66float getAppsPosition(Apps* apps) {
67 checkAppsLimit(apps);
68 if (apps->status != APPS_OK) return 0.0f;
69 float pos1 = getAppPosition(apps->app[0]);
70 float pos2 = getAppPosition(apps->app[1]);
71 return fabs(pos1 + pos2) / 2;
72}
73
74int appsSafetyCheck(void* apps) {
75 Apps* appsPtr = (Apps*)apps;
76 if(appsPtr->base.num_monitors == 0) {
77 printf("No monitors set for Apps\n");
78 return FAILURE;
79 }
80 else if (appsPtr->status != APPS_OK) {
81 printf("Apps is not in a safe state\n");
82 return FAILURE;
83 }
84 return SUCCESS;
85}
float getAppPosition(App *app)
Gets the current position of the APP.
Definition: App.c:11
void initApp(App *app, int hz, int channel)
Initializes the APP with the given frequency and channel.
Definition: App.c:5
void updateApp(void *app)
Updates the APP data.
Definition: App.c:15
int appsSafetyCheck(void *apps)
Checks the safety of Apps.
Definition: Apps.c:74
void initApps(Apps *apps, int hz, int channel1, int channel2)
Initializes the APPs with the given frequency and channel.
Definition: Apps.c:30
void updateApps(void *apps)
Updates the APPS based on both sensor outputs.
Definition: Apps.c:54
float getAppsPosition(Apps *apps)
Averages app sensors to get pedal position.
Definition: Apps.c:66
void checkAppsLimit(Apps *apps)
Checks App limits for faults.
Definition: Apps.c:14
@ APPS_OK
Definition: Apps.h:17
@ APPS_FAULT
Definition: Apps.h:18
#define APPS_DIFFERENCE
Definition: Apps.h:14
#define FAILURE
Definition: Common.h:6
#define SUCCESS
Definition: Common.h:5
int defaultAddMonitor(void *self, MonitorSystem *monitor)
Adds a monitor to the controller.
@ c_APPS
void initControllerSystem(ControllerSystem *controller, const char *name, int hz, ControllerType type)
Initializes the Controller System with initial settings.
void initMonitorSystem(MonitorSystem *monitor, const char *name, int hz, MonitorType type, FaultType fault)
Initializes the Monitor System with initial settings.
Definition: MonitorSystem.c:3
@ m_APPS
Definition: MonitorSystem.h:12
@ VEHICLE_SHUTDOWN
Definition: MonitorSystem.h:21
Definition: App.h:6
Definition: Apps.h:21
App * app[2]
Definition: Apps.h:24
AppsStatus status
Definition: Apps.h:23
ControllerSystem base
Definition: Apps.h:22
int(* safety)(void *self)
Updateable updateable
Definition: System.h:18
void(* update)(void *self)
Definition: Updateable.h:26