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#include "../../../Inc/Utils/MessageFormat.h"
3#include "../../../Inc/Utils/Telemetry.h"
4
5#include <stdio.h>
6
7// Constants for brake pressure calculations
8static const float kOffsetVoltage = 0.5; // Volts
9static const float kVoltsPerPSIA = 0.002;
10static const float kLowOutputSaturation = 0.45; // Output saturation for undersupplied sensor
11static const float kHighOutputSaturation = 4.65; // Output saturation for oversupplied sensor
12
13void initBrakePressure(BrakePressure* bp, int hz, int channel, char* name) {
14 initAnalogSensor(&bp->base, name, hz, channel, bp);
15 bp->pressure = -1;
17
18 // Setup telemetry signals
19 bp->telem_raw = registerTelemetrySignal(name, TELEMETRY_SENSOR, UNIT_VOLTS, 1000/hz, 0.4f, 4.7f);
20 bp->telem_psi = registerTelemetrySignal(name, TELEMETRY_SENSOR, UNIT_PSI, 1000/hz, 0.0f, 2000.0f);
21}
22
24 return bp->pressure;
25}
26
27void updateBrakePressure(void* bp) {
28 BrakePressure *brakePressure = (BrakePressure *)bp;
29 float rawData = getAnalogSensorData(&brakePressure->base);
30
31 // Send Telemetry float
32 sendTelemetryValue(brakePressure->telem_raw, rawData);
33
34 brakePressure->pressure = transferFunctionBrakePressure(rawData);
35 if (brakePressure->pressure == -1) {
37 "Brake pressure reading below sensor minimum (%.3f V)", rawData);
38 } else if (brakePressure->pressure == 2001) {
40 "Brake pressure reading above sensor maximum (%.3f V)", rawData);
41 }
42
43 sendTelemetryValue(brakePressure->telem_psi, brakePressure->pressure);
44}
45
46float transferFunctionBrakePressure(float rawVal) {
47 if (rawVal < kLowOutputSaturation) {
48 return -1;
49 }
50 else if (rawVal > kHighOutputSaturation) {
51 return 2001;
52 }
53 else {
54 return ((rawVal - kOffsetVoltage) / kVoltsPerPSIA);
55 }
56}
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:46
static const float kLowOutputSaturation
Definition: BrakePressure.c:10
static const float kHighOutputSaturation
Definition: BrakePressure.c:11
float getBrakePressure(BrakePressure *bp)
Gets the current brake pressure.
Definition: BrakePressure.c:23
static const float kVoltsPerPSIA
Definition: BrakePressure.c:9
void updateBrakePressure(void *bp)
Updates the brake pressure data.
Definition: BrakePressure.c:27
void initBrakePressure(BrakePressure *bp, int hz, int channel, char *name)
Initializes the BrakePressure sensor with the given frequency and channel.
Definition: BrakePressure.c:13
static const float kOffsetVoltage
Definition: BrakePressure.c:8
@ MSG_WARNING
Definition: MessageFormat.h:12
void sendMessage(const char *sender, MessageType type, const char *format,...)
Definition: MessageFormat.c:5
@ TELEMETRY_SENSOR
Definition: Telemetry.h:9
void sendTelemetryValue(TelemetrySignal *signal, float value)
Definition: Telemetry.c:59
TelemetrySignal * registerTelemetrySignal(const char *name, TelemetryType type, UnitId unit_id, uint32_t expected_rate_ms, float custom_min, float custom_max)
Definition: Telemetry.c:23
@ UNIT_PSI
Definition: Units.h:49
@ UNIT_VOLTS
Definition: Units.h:45
Sensor sensor
Definition: AnalogSensor.h:23
TelemetrySignal * telem_raw
Definition: BrakePressure.h:10
AnalogSensor base
Definition: BrakePressure.h:8
TelemetrySignal * telem_psi
Definition: BrakePressure.h:11
Updateable updateable
Definition: Sensor.h:15
int(* update)(struct Updateable *self)
Definition: Updateable.h:27
char name[MAX_NAME_LENGTH]
Definition: Updateable.h:24