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 "../../../Inc/Utils/Common.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...
 
int updateWheelSpeed (Updateable *updateable)
 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 10 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 76 of file WheelSpeed.c.

76 {
77 ws->pulses += num;
78}
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 25 of file WheelSpeed.c.

25 {
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}
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:10
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 13 of file WheelSpeed.c.

14 {
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}
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
int updateWheelSpeed(Updateable *updateable)
Updated the speed var in this sensor.
Definition: WheelSpeed.c:58
Updateable updateable
Definition: Sensor.h:15
int(* update)(struct Updateable *self)
Definition: Updateable.h:27
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 72 of file WheelSpeed.c.

72 {
73 ws->interval = interval + getCurrentTime();
74}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ updateWheelSpeed()

int updateWheelSpeed ( Updateable updateable)

Updated the speed var in this sensor.

Parameters
updateableA pointer to the updateable structure.
Returns
_SUCCESS if the speed was updated, _FAILURE otherwise.

Definition at line 58 of file WheelSpeed.c.

58 {
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}
#define _FAILURE
Definition: Common.h:6
#define _SUCCESS
Definition: Common.h:5
float calculateSpeed(WheelSpeed *ws)
Translates data to speed in mph.
Definition: WheelSpeed.c:25
Definition: Sensor.h:14
void * child
Definition: Sensor.h:17
void * child
Definition: Updateable.h:32
Here is the call graph for this function:
Here is the caller graph for this function: