Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
Macros | Functions
WheelSpeed.c File Reference
#include "../../../Inc/Sensors/DigitalSensors/WheelSpeed.h"
#include "../../../Inc/Utils/Conversions.h"
#include "../../../Inc/Utils/TimeUtils.h"
#include <stdio.h>
#include <math.h>
Include dependency graph for WheelSpeed.c:

Go to the source code of this file.

Macros

#define M_PI   3.14159265358979323846
 

Functions

void initWheelSpeed (WheelSpeed *ws, int hz, int port, float radius, int numTeeth, WHEEL_LOCATION location)
 Initialization function for a wheel speed sensor. More...
 
float calculateSpeed (WheelSpeed *ws)
 Translates data to speed in mph. More...
 
void updateWheelSpeed (void *ws)
 Updated the speed var in this sensor. More...
 
void setTimeInterval (WheelSpeed *ws, float interval)
 Set time interval. More...
 
void addPulse (WheelSpeed *ws, int num)
 Add pulses to sensor;. More...
 

Macro Definition Documentation

◆ M_PI

#define M_PI   3.14159265358979323846

Definition at line 9 of file WheelSpeed.c.

Function Documentation

◆ addPulse()

void addPulse ( WheelSpeed wf,
int  num 
)

Add pulses to sensor;.

Note
For testing and debugging.
Parameters
wsA pointer to the WheelSpeed structure.
numNumber of pulses to add.

Definition at line 66 of file WheelSpeed.c.

66 {
67 ws->pulses += num;
68}
int pulses
Definition: WheelSpeed.h:14
Here is the caller graph for this function:

◆ calculateSpeed()

float calculateSpeed ( WheelSpeed ws)

Translates data to speed in mph.

Parameters
wsA pointer to the WheelSpeed structure.
Returns
The speed in miles per hour.

Definition at line 24 of file WheelSpeed.c.

24 {
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}
float convertMpsToMph(float metersPerSecond)
Definition: Conversions.c:5
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
#define M_PI
Definition: WheelSpeed.c:9
float interval
Definition: WheelSpeed.h:12
float radius
Definition: WheelSpeed.h:11
int numTeeth
Definition: WheelSpeed.h:13
Here is the call graph for this function:
Here is the caller graph for this function:

◆ initWheelSpeed()

void initWheelSpeed ( WheelSpeed ws,
int  hz,
int  port,
float  radius,
int  numTeeth,
WHEEL_LOCATION  location 
)

Initialization function for a wheel speed sensor.

Parameters
wsA pointer to the WheelSpeed structure. @pram hz Rate at which the sensor is called (in hz). @pram port Location of sensor. @pram radius Radius of wheel (in mm). @pram numTeeth Number of teeth on reluctor wheel. @pram location Location of sensor on car.

Definition at line 12 of file WheelSpeed.c.

13 {
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}
void initDigitalSensor(DigitalSensor *digitalsensor, const char *name, int hz, int port)
Initializes a digital sensor with the given parameters.
Definition: DigitalSensor.c:3
void updateWheelSpeed(void *ws)
Updated the speed var in this sensor.
Definition: WheelSpeed.c:57
Updateable updateable
Definition: Sensor.h:15
void(* update)(void *self)
Definition: Updateable.h:26
WHEEL_LOCATION wheel_location
Definition: WheelSpeed.h:9
float speed
Definition: WheelSpeed.h:10
DigitalSensor base
Definition: WheelSpeed.h:8
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setTimeInterval()

void setTimeInterval ( WheelSpeed ws,
float  interval 
)

Set time interval.

Note
For testing and debugging.
Parameters
wsA pointer to the WheelSpeed structure.
intervalTime interval to set.

Definition at line 62 of file WheelSpeed.c.

62 {
63 ws->interval = interval + getCurrentTime();
64}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ updateWheelSpeed()

void updateWheelSpeed ( void *  ws)

Updated the speed var in this sensor.

Parameters
wsA pointer to the WheelSpeed structure.

Definition at line 57 of file WheelSpeed.c.

57 {
58 WheelSpeed* wsPtr = (WheelSpeed*)ws;
59 wsPtr->speed = calculateSpeed(ws);
60}
float calculateSpeed(WheelSpeed *ws)
Translates data to speed in mph.
Definition: WheelSpeed.c:24
Here is the call graph for this function:
Here is the caller graph for this function: