Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
WheelSpeed.c
Go to the documentation of this file.
1#include "../../../Inc/Sensors/DigitalSensors/WheelSpeed.h"
2#include "../../../Inc/Utils/Conversions.h"
3#include "../../../Inc/Utils/TimeUtils.h"
4
5#include <stdio.h>
6#include <math.h>
7
8#ifndef M_PI
9#define M_PI 3.14159265358979323846
10#endif
11
12void initWheelSpeed(WheelSpeed* ws, int hz, int port, float radius, int numTeeth,
13 WHEEL_LOCATION location) {
14 initDigitalSensor(&ws->base, "Wheel Speed", hz, port);
16 ws->radius = radius;
17 ws->wheel_location = location;
18 ws->numTeeth = numTeeth;
19 ws->pulses = 0;
20 ws->speed = 0.0f;
22}
23
25 // Check if the pointer is null
26 if (ws == NULL) {
27 fprintf(stderr, "Error: Null pointer passed to calculateSpeed\n");
28 return -1; // Or handle the error as appropriate
29 }
30
31 int numTeeth = ws->numTeeth;
32 float radius = ws->radius;
33 int pulses = ws->pulses;
34 float delta = measureInterval(ws->interval);
35
36 // Reset pulse count and elapsed time for the next measurement interval
38 ws->pulses = 0;
39
40 // Verify inputs
41 if (numTeeth <= 0 || radius <= 0.0f || pulses <= 0 || delta <= 0.0) {
42 printf("Error: Invalid argument. "
43 "NumTeeth = %d, Radius = %.2f, Pulses = %d, Time Delta = %.2f\n",
44 numTeeth, radius, pulses, delta);
45 return 0.0f;
46 }
47
48 // Calculations
49 float circumference = 2 * M_PI * radius;
50 float distancePerPulse = circumference / numTeeth;
51 float speedMetersPerSecond = (distancePerPulse * pulses) / delta;
52 float speed = convertMpsToMph(speedMetersPerSecond);
53
54 return speed;
55}
56
57void updateWheelSpeed(void* ws) {
58 WheelSpeed* wsPtr = (WheelSpeed*)ws;
59 wsPtr->speed = calculateSpeed(ws);
60}
61
62void setTimeInterval(WheelSpeed* ws, float interval) {
63 ws->interval = interval + getCurrentTime();
64}
65
66void addPulse(WheelSpeed* ws, int num) {
67 ws->pulses += num;
68}
float convertMpsToMph(float metersPerSecond)
Definition: Conversions.c:5
void initDigitalSensor(DigitalSensor *digitalsensor, const char *name, int hz, int port)
Initializes a digital sensor with the given parameters.
Definition: DigitalSensor.c:3
double measureInterval(double lastIntervalTime)
Measures the time interval since the last recorded time.
Definition: TimeUtils.c:7
double getCurrentTime()
Retrieves the current time in seconds.
Definition: TimeUtils.c:3
WHEEL_LOCATION
Definition: WheelLocation.h:4
void setTimeInterval(WheelSpeed *ws, float interval)
Set time interval.
Definition: WheelSpeed.c:62
void updateWheelSpeed(void *ws)
Updated the speed var in this sensor.
Definition: WheelSpeed.c:57
float calculateSpeed(WheelSpeed *ws)
Translates data to speed in mph.
Definition: WheelSpeed.c:24
void addPulse(WheelSpeed *ws, int num)
Add pulses to sensor;.
Definition: WheelSpeed.c:66
void initWheelSpeed(WheelSpeed *ws, int hz, int port, float radius, int numTeeth, WHEEL_LOCATION location)
Initialization function for a wheel speed sensor.
Definition: WheelSpeed.c:12
#define M_PI
Definition: WheelSpeed.c:9
Updateable updateable
Definition: Sensor.h:15
void(* update)(void *self)
Definition: Updateable.h:26
WHEEL_LOCATION wheel_location
Definition: WheelSpeed.h:9
float interval
Definition: WheelSpeed.h:12
float speed
Definition: WheelSpeed.h:10
int pulses
Definition: WheelSpeed.h:14
float radius
Definition: WheelSpeed.h:11
DigitalSensor base
Definition: WheelSpeed.h:8
int numTeeth
Definition: WheelSpeed.h:13