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/Systems/Monitor/AppsMonitor.h"
3#include "../../../Inc/Utils/Common.h"
4
5#include <math.h>
6
7void initApps(Apps* apps, int hz, int channel1, int channel2) {
8 initControllerSystem(&apps->base, "Apps", hz, c_APPS, updateApps, apps);
9 // Allocate memory for the app instances
10 static App app1, app2;
11 apps->app[0] = &app1;
12 apps->app[1] = &app2;
13
14 // Check if memory allocation was successful
15 if (apps->app[0] == NULL || apps->app[1] == NULL) {
16 return;
17 }
18
19 // Controller setup
20 apps->status = APPS_OK;
21 initApp(apps->app[0], hz, channel1);
22 initApp(apps->app[1], hz, channel2);
23 ENABLE(apps->base.system);
24}
25
26int updateApps(ControllerSystem* controller) {
27 Apps* appsPtr = (Apps*)controller->child;
28 updateApp(appsPtr->app[0]);
29 updateApp(appsPtr->app[1]);
30
31 // Set to computed
32 appsPtr->base.state = c_computed;
33
34
35 #ifdef DEBUGn
36 printf("Apps updated. #1: %f, #2: %f\r\n", getAppPosition(appsPtr->app[0]), getAppPosition(appsPtr->app[1]));
37 #endif
38
39 return _SUCCESS;
40}
41
42float getAppsPosition(Apps* apps) {
43 if (apps->base.state != c_validated) {
44
45 printf("Apps Controller value has not been validated\r\n");
46 return 0.0f;
47 }
48 checkAppsLimit(apps);
49 if (apps->status != APPS_OK) return 0.0f;
50 float pos1 = getAppPosition(apps->app[0]);
51 float pos2 = getAppPosition(apps->app[1]);
52 return fabs(pos1 + pos2) / 2;
53}
float getAppPosition(App *app)
Gets the current position of the APP.
Definition: App.c:11
void updateApp(App *app)
Updates the APP data.
Definition: App.c:15
void initApp(App *app, int hz, int channel)
Initializes the APP with the given frequency and channel.
Definition: App.c:5
void checkAppsLimit(Apps *apps)
Checks App limits for faults.
Definition: AppsMonitor.c:37
void initApps(Apps *apps, int hz, int channel1, int channel2)
Initializes the APPs with the given frequency and channel.
Definition: Apps.c:7
int updateApps(ControllerSystem *controller)
Updates the APPS based on both sensor outputs.
Definition: Apps.c:26
float getAppsPosition(Apps *apps)
Averages app sensors to get pedal position.
Definition: Apps.c:42
@ APPS_OK
Definition: Apps.h:17
#define _SUCCESS
Definition: Common.h:5
void initControllerSystem(ControllerSystem *controller, const char *name, int hz, ControllerType type, int(*updateController)(ControllerSystem *controller), void *child)
Initializes the Controller System with initial settings.
@ c_APPS
@ c_computed
@ c_validated
#define ENABLE(item_)
Definition: Updateable.h:10
Definition: App.h:6
Definition: Apps.h:22
App * app[2]
Definition: Apps.h:25
AppsStatus status
Definition: Apps.h:24
ControllerSystem base
Definition: Apps.h:23
ControllerState state