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#include "../../../Inc/Utils/Common.h"
5
6#include <stdio.h>
7#include <math.h>
8
9#ifndef M_PI
10#define M_PI 3.14159265358979323846
11#endif
12
13void initWheelSpeed(WheelSpeed* ws, int hz, int port, float radius, int numTeeth,
14 WHEEL_LOCATION location) {
15 initDigitalSensor(&ws->base, "Wheel Speed", hz, port, ws);
17 ws->radius = radius;
18 ws->wheel_location = location;
19 ws->numTeeth = numTeeth;
20 ws->pulses = 0;
21 ws->speed = 0.0f;
23}
24
26 // Check if the pointer is null
27 if (ws == NULL) {
28 fprintf(stderr, "Error: Null pointer passed to calculateSpeed\n");
29 return -1; // Or handle the error as appropriate
30 }
31
32 int numTeeth = ws->numTeeth;
33 float radius = ws->radius;
34 int pulses = ws->pulses;
35 float delta = measureInterval(ws->interval);
36
37 // Reset pulse count and elapsed time for the next measurement interval
39 ws->pulses = 0;
40
41 // Verify inputs
42 if (numTeeth <= 0 || radius <= 0.0f || pulses <= 0 || delta <= 0.0) {
43 printf("Error: Invalid argument. "
44 "NumTeeth = %d, Radius = %.2f, Pulses = %d, Time Delta = %.2f\n",
45 numTeeth, radius, pulses, delta);
46 return 0.0f;
47 }
48
49 // Calculations
50 float circumference = 2 * M_PI * radius;
51 float distancePerPulse = circumference / numTeeth;
52 float speedMetersPerSecond = (distancePerPulse * pulses) / delta;
53 float speed = convertMpsToMph(speedMetersPerSecond);
54
55 return speed;
56}
57
58int updateWheelSpeed(Updateable* updateable) {
59 // Cast the updateable pointer to a wheel speed sensor
60 Sensor* sensor = (Sensor*)updateable->child;
61 DigitalSensor* digitalSensor = (DigitalSensor*)sensor->child;
62 WheelSpeed* ws = (WheelSpeed*)digitalSensor->child;
63
64 ws->speed = calculateSpeed(ws);
65 if (ws->speed < 0) {
66 printf("Error: Speed calculation failed\n");
67 return _FAILURE;
68 }
69 return _SUCCESS;
70}
71
72void setTimeInterval(WheelSpeed* ws, float interval) {
73 ws->interval = interval + getCurrentTime();
74}
75
76void addPulse(WheelSpeed* ws, int num) {
77 ws->pulses += num;
78}
#define _FAILURE
Definition: Common.h:6
#define _SUCCESS
Definition: Common.h:5
float convertMpsToMph(float metersPerSecond)
Definition: Conversions.c:5
void initDigitalSensor(DigitalSensor *digitalsensor, const char *name, int hz, int port, void *child)
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:72
float calculateSpeed(WheelSpeed *ws)
Translates data to speed in mph.
Definition: WheelSpeed.c:25
void addPulse(WheelSpeed *ws, int num)
Add pulses to sensor;.
Definition: WheelSpeed.c:76
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:13
#define M_PI
Definition: WheelSpeed.c:10
int updateWheelSpeed(Updateable *updateable)
Updated the speed var in this sensor.
Definition: WheelSpeed.c:58
Definition: Sensor.h:14
void * child
Definition: Sensor.h:17
Updateable updateable
Definition: Sensor.h:15
int(* update)(struct Updateable *self)
Definition: Updateable.h:27
void * child
Definition: Updateable.h:32
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