Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
BrakePressure.c
Go to the documentation of this file.
1#include "../../../Inc/Sensors/AnalogSensors/BrakePressure.h"
2
3#include <stdio.h>
4
5// Constants for brake pressure calculations
6static const float kOffsetVoltage = 0.5; // Volts
7static const float kVoltsPerPSIA = 0.002;
8static const float kLowOutputSaturation = 0.45; // Output saturation for undersupplied sensor
9static const float kHighOutputSaturation = 4.65; // Output saturation for oversupplied sensor
10
11void initBrakePressure(BrakePressure* bp, int hz, int channel) {
12 initAnalogSensor(&bp->base, "BrakePressure", hz, channel, bp);
13 bp->pressure = -1;
15}
16
18 return bp->pressure;
19}
20
21void updateBrakePressure(void* bp) {
22 BrakePressure *brakePressure = (BrakePressure *)bp;
23 float rawData = getAnalogSensorData(&brakePressure->base);
24
25 #ifdef DEBUGn
26 printf("BrakePressure::update rawVal: %f\r\n", rawData);
27 #endif
28 brakePressure->pressure = transferFunctionBrakePressure(rawData);
29}
30
31float transferFunctionBrakePressure(float rawVal) {
32 if (rawVal < kLowOutputSaturation) {
33
34 #ifdef DEBUGn
35 printf("BrakePressure::transfer_function rawVal is too low\r\n");
36 #endif
37
38 return -1;
39 }
40 else if (rawVal > kHighOutputSaturation) {
41
42 #ifdef DEBUGn
43 printf("BrakePressure::transfer_function rawVal is too high\r\n");
44 #endif
45
46 return 2001;
47 }
48 else {
49 return ((rawVal - kOffsetVoltage) / kVoltsPerPSIA);
50 }
51}
float getAnalogSensorData(AnalogSensor *sensor)
Retrieves analog sensor data for a specific channel.
Definition: AnalogSensor.c:89
void initAnalogSensor(AnalogSensor *analogSensor, const char *name, int hz, int channel, void *child)
Initializes an analog sensor and configures the corresponding GPIO pin.
Definition: AnalogSensor.c:42
float transferFunctionBrakePressure(float rawVal)
Converts raw brake pressure data to a meaningful pressure value.
Definition: BrakePressure.c:31
static const float kLowOutputSaturation
Definition: BrakePressure.c:8
static const float kHighOutputSaturation
Definition: BrakePressure.c:9
float getBrakePressure(BrakePressure *bp)
Gets the current brake pressure.
Definition: BrakePressure.c:17
static const float kVoltsPerPSIA
Definition: BrakePressure.c:7
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
static const float kOffsetVoltage
Definition: BrakePressure.c:6
Sensor sensor
Definition: AnalogSensor.h:23
AnalogSensor base
Definition: BrakePressure.h:7
Updateable updateable
Definition: Sensor.h:15
int(* update)(struct Updateable *self)
Definition: Updateable.h:27