Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
Functions | Variables
AnalogSensor.c File Reference
#include "../../Inc/Sensors/AnalogSensor.h"
#include "../../Inc/Utils/Constants.h"
#include <stdio.h>
#include <string.h>
#include "stm32f7xx_hal.h"
Include dependency graph for AnalogSensor.c:

Go to the source code of this file.

Functions

void initAnalogSensor (AnalogSensor *analogSensor, const char *name, int hz, int channel, void *child)
 Initializes an analog sensor and configures the corresponding GPIO pin. More...
 
void ProcessADCData (uint32_t *adc1_buffer, uint32_t *adc2_buffer, uint32_t *adc3_buffer)
 Processes ADC data from all three ADCs and stores it in the circular buffer. More...
 
float getAnalogSensorData (AnalogSensor *sensor)
 Retrieves analog sensor data for a specific channel. More...
 

Variables

static uint16_t adc_samples [ADC_CHANNELS]
 

Function Documentation

◆ getAnalogSensorData()

float getAnalogSensorData ( AnalogSensor sensor)

Retrieves analog sensor data for a specific channel.

Parameters
sensorPointer to the AnalogSensor structure
Returns
int The current ADC value for the specified channel

This function returns the latest ADC value for the channel specified in the AnalogSensor structure. If an invalid channel is specified, it returns 0.

Definition at line 89 of file AnalogSensor.c.

89 {
90 if (sensor->channel >= 0 && sensor->channel < ADC_CHANNELS) {
91 return ((float)adc_samples[sensor->channel]/4096)*ADC_VREF;
92 }
93 printf("Invalid channel specified for AnalogSensor %s\r\n", sensor->sensor.updateable.name);
94 return 0; // Default return for invalid channels
95}
static uint16_t adc_samples[ADC_CHANNELS]
Definition: AnalogSensor.c:13
#define ADC_CHANNELS
Definition: Constants.h:5
#define ADC_VREF
Definition: Constants.h:6
Sensor sensor
Definition: AnalogSensor.h:23
Updateable updateable
Definition: Sensor.h:15
char name[MAX_NAME_LENGTH]
Definition: Updateable.h:24
Here is the caller graph for this function:

◆ initAnalogSensor()

void initAnalogSensor ( AnalogSensor analogSensor,
const char *  name,
int  hz,
int  channel,
void *  child 
)

Initializes an analog sensor and configures the corresponding GPIO pin.

Parameters
sensorPointer to the AnalogSensor structure to initialize
nameName of the sensor (string)
hzSampling frequency in Hertz
channelADC channel number for the sensor (0-16)

This function initializes the base sensor properties, sets the ADC channel, and configures the corresponding GPIO pin based on the channel number.

Definition at line 42 of file AnalogSensor.c.

42 {
43 initSensor(&analogSensor->sensor, name, hz, s_ANALOG, analogSensor);
44 analogSensor->channel = channel;
45 analogSensor->child = child;
46}
@ s_ANALOG
Definition: Sensor.h:10
void initSensor(Sensor *sensor, const char *name, int hz, SensorType type, void *child)
Initializes a sensor with the given parameters.
Definition: Sensor.c:4
void * child
Definition: AnalogSensor.h:25
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ProcessADCData()

void ProcessADCData ( uint32_t *  adc1_buffer,
uint32_t *  adc2_buffer,
uint32_t *  adc3_buffer 
)

Processes ADC data from all three ADCs and stores it in the circular buffer.

Parameters
adc1_dataPointer to ADC1 data buffer (channels 0-5)
adc2_dataPointer to ADC2 data buffer (channels 6-11)
adc3_dataPointer to ADC3 data buffer (channels 12-15)

This function combines data from all three ADCs into a single ADCSample and adds it to the circular buffer. It also sends debug information via UART.

Definition at line 58 of file AnalogSensor.c.

58 {
59
60 // Channels 0, 2, 6, 8, 19, 12, 14 on ADC1
61 for (int i = 0; i < ADC1_CHANNEL_SIZE; i++) {
63 }
64
65 // Channels 1, 3, 7, 9, 10, 13, 15 on ADC2
67 adc_samples[i] = adc2_buffer[i-7];
68 }
69
70 // Channels 4, 5, 6, 7, 8, 9, 14, 15 on ADC3
71 for (int i = ADC1_CHANNEL_SIZE+ADC2_CHANNEL_SIZE; i < ADC_CHANNELS; i++) {
72 adc_samples[i] = adc3_buffer[i-14];
73 }
74
75// printf("8: %10d, 4: %10d, 12: %10d, 19: %10d, 21: %10d, 18: %10d, 10: %10d\r\n, 5: %10d\r\n, 6: %10d\r\n",
76// adc_samples[8], adc_samples[4], adc_samples[12], adc_samples[19], adc_samples[21], adc_samples[18], adc_samples[10], adc_samples[5], adc_samples[6]);
77
78}
uint32_t adc3_buffer[8]
Definition: main.c:115
uint32_t adc1_buffer[7]
Definition: main.c:113
uint32_t adc2_buffer[7]
Definition: main.c:114
#define ADC2_CHANNEL_SIZE
Definition: Constants.h:3
#define ADC1_CHANNEL_SIZE
Definition: Constants.h:2
Here is the caller graph for this function:

Variable Documentation

◆ adc_samples

uint16_t adc_samples[ADC_CHANNELS]
static

Definition at line 13 of file AnalogSensor.c.