Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
RTD.c
Go to the documentation of this file.
1#include "../../../Inc/Utils/Constants.h"
2#include "../../../Inc/Systems/Controller/RTD.h"
3#include "../../../Inc/Systems/ControllerSystem.h"
4#include "../../../Inc/Utils/Common.h"
5
6void initRTD(RTD* rtd, Apps* apps, BrakeSystemControl* bsc, int hz, int buttonPort, int piezoPort) {
7 initControllerSystem(&rtd->base, "RTD", hz, c_RTD, updateRTD, rtd);
8 rtd->readyToDrive = 0;
9 rtd->precharged = 0;
10 rtd->counter = 0;
11
12 static Button button;
13 rtd->button = &button;
14 initButton(rtd->button, "RTD Button", hz, 0);
15
16 static DigitalOutput piezo;
17 rtd->piezo = &piezo;
18 initDigitalOutput(rtd->piezo, "Piezo", hz, piezoPort);
19 rtd->apps = apps;
20 rtd->bsc = bsc;
21}
22
23int startRTD(RTD* rtd) {
24 if (rtd->base.safety == NULL) {
25 printf("Safety system not set for RTD\r\n");
26 return _FAILURE;
27 }
28 else if (rtd->base.safety(&rtd->base) == _FAILURE) {
29 printf("RTD Actuator is not in a safe state\r\n");
30 return _FAILURE;
31 }
32 ENABLE(rtd->base.system);
33 rtd->base.state = c_idle;
34 return _SUCCESS;
35}
36
37int updateRTD(ControllerSystem* controller) {
38 RTD* rtd = (RTD*)controller->child;
39 if (rtd->apps->base.state != c_validated || rtd->bsc->base.state != c_validated) {
40 printf("RTD: Apps or Brake System Control Actuator is not validated\r\n");
41 return _FAILURE;
42 }
43
45 printf("RTD: APPS position is too high\r\n");
46 return _FAILURE;
47 }
48
51 printf("RTD: Brake pressure is too low\r\n");
52 return _FAILURE;
53 }
54
55 if (rtd->readyToDrive == 0) {
56 rtd->readyToDrive = getButtonReading(rtd->button); // and precharged, etc.
57 }
58
59 if (rtd->readyToDrive == 1 && rtd->counter < 30) {
60 rtd->counter++;
61 // Turn on piezo
63 } else {
64 // Turn off piezo
66 }
67
68 rtd->base.state = c_computed;
69
70 #ifdef DEBUGn
71 printf("RTD updated. Ready to Drive: %d\r\n", rtd->readyToDrive);
72 #endif
73
74 return _SUCCESS;
75}
76
77int checkRTD(void* rtd) {
78 RTD* rtdPtr = (RTD*)rtd;
79 if (rtdPtr->readyToDrive == 1) {
80 printf("RTD is ready to drive\r\n");
81 return _SUCCESS;
82 }
83 return _FAILURE;
84}
float getAppsPosition(Apps *apps)
Averages app sensors to get pedal position.
Definition: Apps.c:42
void initButton(Button *button, const char *name, int hz, int port)
Definition: Button.c:4
int getButtonReading(Button *button)
Definition: Button.c:8
#define _FAILURE
Definition: Common.h:6
#define _SUCCESS
Definition: Common.h:5
#define MIN_BRAKE_PRESSURE
Definition: Constants.h:18
#define MAX_A_PEDAL_POSITION
Definition: Constants.h:17
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_RTD
@ c_idle
@ c_computed
@ c_validated
int writeDigitalOutputData(DigitalOutput *output, int data)
Writes data to the buffer of the digital output.
Definition: DigitalOutput.c:18
void initDigitalOutput(DigitalOutput *digitaloutput, const char *name, int hz, int port)
Initializes a digital output with the given parameters.
Definition: DigitalOutput.c:4
int checkRTD(void *rtd)
Checks if RTD is in ready to drive state.
Definition: RTD.c:77
int startRTD(RTD *rtd)
Starts the RTD Actuator.
Definition: RTD.c:23
int updateRTD(ControllerSystem *controller)
Updates the RTD Actuator.
Definition: RTD.c:37
void initRTD(RTD *rtd, Apps *apps, BrakeSystemControl *bsc, int hz, int buttonPort, int piezoPort)
Initializes the RTD Actuator with initial settings.
Definition: RTD.c:6
#define ENABLE(item_)
Definition: Updateable.h:10
Definition: Apps.h:22
ControllerSystem base
Definition: Apps.h:23
BrakePressure * rearPressure
BrakePressure * frontPressure
ControllerSystem base
Definition: Button.h:6
int(* safety)(struct ControllerSystem *controller)
ControllerState state
Definition: RTD.h:11
BrakeSystemControl * bsc
Definition: RTD.h:19
Apps * apps
Definition: RTD.h:18
ControllerSystem base
Definition: RTD.h:12
uint8_t precharged
Definition: RTD.h:14
uint8_t readyToDrive
Definition: RTD.h:13
DigitalOutput * piezo
Definition: RTD.h:17
Button * button
Definition: RTD.h:16
uint8_t counter
Definition: RTD.h:15