Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
BrakeSystemControl.c
Go to the documentation of this file.
1#include "../../../Inc/Outputs/DigitalOutput.h"
2#include "../../../Inc/Systems/Controller/BrakeSystemControl.h"
3#include "../../../Inc/Utils/Common.h"
4#include "../../../Inc/Sensors/AnalogSensors/BrakePressure.h"
5#include "../../../Inc/Sensors/AnalogSensors/Temperature.h"
6
7void initBrakeSystemControl(BrakeSystemControl *bsc, int hz, int maxTemp, int brakeLightActivationPoint, int heavyBrakingActivationPoint, int fbp_channel, int rbp_channel, int temp_channel, int light_port){
8 initControllerSystem(&bsc-> base, "Brake System Control", hz, c_BRAKES, updateBrakeSystemControl, bsc);
9 static BrakePressure frontPressure, rearPressure;
10 static Temperature temperature;
11 static DigitalOutput brakeLight;
12
13 bsc->frontPressure = &frontPressure;
14 bsc->rearPressure = &rearPressure;
15 initBrakePressure(bsc -> frontPressure, hz, fbp_channel);
16 initBrakePressure(bsc -> rearPressure, hz, rbp_channel);
17
18 bsc->temperature = &temperature;
19 initTemperature(bsc -> temperature, hz, temp_channel);
20
21 bsc->brakeLight = &brakeLight;
22 initDigitalOutput(bsc -> brakeLight, "Brake Light", hz, light_port);
23
24 bsc -> maxTemperatureAllowed = maxTemp;
25 bsc -> minPressure = 0;
26 bsc -> maxPressure = 2000;
27 bsc -> brakeLightActivationPoint = brakeLightActivationPoint;
28 bsc -> brakeLightActive = 0;
29 bsc -> brakeLightBlink = 0;
30 bsc -> heavyBrakingActivationPoint = heavyBrakingActivationPoint;
31 bsc -> heavyBraking = 0;
32 bsc -> status = BRAKES_OK;
33}
34
36 if (bsc -> base.safety == NULL){
37 printf("Safety system not set for Brake System Control\r\n");
38 return _FAILURE;
39 }
40 else if (bsc -> base.safety(&bsc->base) == _FAILURE){
41 printf("Brake System Control Actuator is not in a safe state\r\n");
42 return _FAILURE;
43 }
44 ENABLE(bsc -> base.system);
45 bsc -> base.state = c_idle;
46 return _SUCCESS;
47}
48
50 BrakeSystemControl *bsc = (BrakeSystemControl*)controller->child;
53 inHeavyBreaking(bsc);
54 bsc->base.state = c_computed;
56
57 #ifdef DEBUGn
58 printf("Brake System Control updated. Front Pressure: %f, Rear Pressure: %f, Temperature: %f\r\n", getBrakePressure(bsc -> frontPressure), getBrakePressure(bsc -> rearPressure), getTemperatureFahrenheit(bsc -> temperature));
59 printf("Brake Light Active: %d, Heavy Braking: %d\r\n", bsc -> brakeLightActive, bsc -> heavyBraking);
60 #endif
61
62 return _SUCCESS;
63}
64
69}
70
72 if (getBrakePressure(bsc -> frontPressure) > bsc -> brakeLightActivationPoint
73 || getBrakePressure(bsc -> rearPressure) > bsc -> brakeLightActivationPoint){
74 if (bsc->brakeLightBlink < 8) {bsc->brakeLightBlink++;}
75 if (bsc->brakeLightBlink%2 == 0) {
76 bsc -> brakeLightActive = 1;
77 return;
78 }
79 } else {
80 bsc -> brakeLightBlink = 0;
81 }
82 bsc -> brakeLightActive = 0;
83 return;
84}
85
87 if (getBrakePressure(bsc -> frontPressure) > bsc -> heavyBrakingActivationPoint || getBrakePressure(bsc -> rearPressure) > bsc -> heavyBrakingActivationPoint){
88 bsc -> heavyBraking = 1;
89 return;
90 }
91 bsc -> heavyBraking = 0;
92 return;
93}
94
96 float front = getBrakePressure(bsc -> frontPressure);
97 float rear = getBrakePressure(bsc -> rearPressure);
98 float temp = getTemperatureFahrenheit(bsc -> temperature);
99
100 if (front > bsc -> maxPressure || rear > bsc -> maxPressure){
101 return PRESSURE_OVER_LIMIT;
102 }
103 else if (front < bsc -> minPressure || rear < bsc -> minPressure){
105 }
106 else if (temp > bsc -> maxTemperatureAllowed){
108 }
109 else if (temp < 0){
111 } else {
112 return BRAKES_OK;
113 }
114}
115
116//The following functions below are for testing functionality and should not be used elsewhere
117
118void setFrontPressure(BrakeSystemControl *bsc, float pressure){
119 bsc -> frontPressure -> pressure = pressure;
120 inHeavyBreaking(bsc);
122}
123
124void setRearPressure(BrakeSystemControl *bsc, float pressure){
125 bsc -> rearPressure -> pressure = pressure;
126 inHeavyBreaking(bsc);
128}
129
130void setTemperature(BrakeSystemControl *bsc, float temperature){
131 bsc -> temperature -> degrees = temperature;
132 inHeavyBreaking(bsc);
134}
float getBrakePressure(BrakePressure *bp)
Gets the current brake pressure.
Definition: BrakePressure.c:17
void initBrakePressure(BrakePressure *bp, int hz, int channel)
Initializes the BrakePressure sensor with the given frequency and channel.
Definition: BrakePressure.c:11
void updateBrakePressure(void *bp)
Updates the brake pressure data.
Definition: BrakePressure.c:21
void activateBrakeLight(BrakeSystemControl *bsc)
Checks whether the line pressure is higher than the activation point specified by the user.
void setFrontPressure(BrakeSystemControl *bsc, float pressure)
void initBrakeSystemControl(BrakeSystemControl *bsc, int hz, int maxTemp, int brakeLightActivationPoint, int heavyBrakingActivationPoint, int fbp_channel, int rbp_channel, int temp_channel, int light_port)
Initializes the Braking System with initial settings.
void setTemperature(BrakeSystemControl *bsc, float temperature)
void setRearPressure(BrakeSystemControl *bsc, float pressure)
void inHeavyBreaking(BrakeSystemControl *bsc)
Checks whether the line pressure is higher than the activation point specified by the user.
void setSensorReadings(BrakeSystemControl *bsc)
Updates BrakeSystemStatus with current sensor data.
int updateBrakeSystemControl(ControllerSystem *controller)
Updates the Brake System Controller.
int startBrakeSystemControl(BrakeSystemControl *bsc)
Starts the Brake System Controller.
BrakeSystemStatus checkSensorLimits(BrakeSystemControl *bsc)
Checks if the brake system is within the defined limits and desired ranges.
BrakeSystemStatus
@ TEMPERATURE_OVER_LIMIT
@ PRESSURE_UNDER_LIMIT
@ TEMPERATURE_SENSOR_ERROR
@ BRAKES_OK
@ PRESSURE_OVER_LIMIT
#define _FAILURE
Definition: Common.h:6
#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_BRAKES
@ c_idle
@ c_computed
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
double getTemperatureFahrenheit(Temperature *temp)
Gets the current temperature in Fahrenheit.
Definition: Temperature.c:19
void initTemperature(Temperature *temp, int hz, int channel)
Initializes the Temperature sensor with the given frequency and channel.
Definition: Temperature.c:3
void updateTemperature(void *temp)
Updates the Temperature data.
Definition: Temperature.c:9
#define ENABLE(item_)
Definition: Updateable.h:10
BrakePressure * rearPressure
Temperature * temperature
BrakePressure * frontPressure
DigitalOutput * brakeLight
ControllerSystem base
ControllerState state