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 <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)
 Initializes an analog sensor and configures the corresponding GPIO pin. More...
 
static void addSampleToBuffer (ADCSample sample)
 Adds a sample to the circular buffer. More...
 
void ProcessADCData (uint16_t *adc1_data, uint16_t *adc2_data, uint16_t *adc3_data)
 Processes ADC data from all three ADCs and stores it in the circular buffer. More...
 
ADCSample getLatestSample (void)
 Retrieves the latest ADC sample from the buffer. More...
 
uint32_t getRecentSamples (ADCSample *samples, uint32_t num_samples)
 Retrieves a specified number of recent samples from the buffer. More...
 
int getAnalogSensorData (AnalogSensor *sensor)
 Retrieves analog sensor data for a specific channel. More...
 

Variables

static ADCSample adc_circular_buffer [BUFFER_SIZE]
 
static uint32_t buffer_head = 0
 
static uint32_t buffer_tail = 0
 
static uint32_t buffer_count = 0
 

Function Documentation

◆ addSampleToBuffer()

static void addSampleToBuffer ( ADCSample  sample)
static

Adds a sample to the circular buffer.

Parameters
sampleADCSample to be added to the buffer

This function adds a new sample to the circular buffer. If the buffer is full, it overwrites the oldest data.

Definition at line 92 of file AnalogSensor.c.

92 {
95
98 } else {
99 // Buffer is full, update tail to overwrite the oldest data
101 }
102}
static uint32_t buffer_count
Definition: AnalogSensor.c:15
static ADCSample adc_circular_buffer[BUFFER_SIZE]
Definition: AnalogSensor.c:12
static uint32_t buffer_tail
Definition: AnalogSensor.c:14
static uint32_t buffer_head
Definition: AnalogSensor.c:13
#define BUFFER_SIZE
Definition: AnalogSensor.h:24
Here is the caller graph for this function:

◆ getAnalogSensorData()

int 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 193 of file AnalogSensor.c.

193 {
194 ADCSample latest = getLatestSample();
195 if (sensor->channel >= 0 && sensor->channel < 16) {
196 return latest.adc[sensor->channel];
197 }
198 return 0; // Default return for invalid channels
199}
ADCSample getLatestSample(void)
Retrieves the latest ADC sample from the buffer.
Definition: AnalogSensor.c:152
uint16_t adc[ADC_CHANNELS]
Definition: AnalogSensor.h:33
Here is the call graph for this function:

◆ getLatestSample()

ADCSample getLatestSample ( void  )

Retrieves the latest ADC sample from the buffer.

Returns
ADCSample The most recent ADC sample added to the buffer

If the buffer is empty, this function returns a sample with all channels set to 0.

Definition at line 152 of file AnalogSensor.c.

152 {
153 if (buffer_count > 0) {
154 uint32_t latest = (buffer_head - 1 + BUFFER_SIZE) % BUFFER_SIZE;
155 return adc_circular_buffer[latest];
156 }
157
158 // Return a default sample if buffer is empty
159 ADCSample empty_sample = {0};
160 return empty_sample;
161}
Here is the caller graph for this function:

◆ getRecentSamples()

uint32_t getRecentSamples ( ADCSample samples,
uint32_t  num_samples 
)

Retrieves a specified number of recent samples from the buffer.

Parameters
samplesPointer to an array to store the retrieved samples
num_samplesNumber of samples to retrieve
Returns
uint32_t Actual number of samples retrieved

This function copies the most recent samples from the circular buffer to the provided array. It returns the actual number of samples copied, which may be less than requested if the buffer is not full.

Definition at line 173 of file AnalogSensor.c.

173 {
174 uint32_t samples_to_copy = (num_samples < buffer_count) ? num_samples : buffer_count;
175
176 for (uint32_t i = 0; i < samples_to_copy; i++) {
177 uint32_t index = (buffer_head - 1 - i + BUFFER_SIZE) % BUFFER_SIZE;
178 samples[i] = adc_circular_buffer[index];
179 }
180
181 return samples_to_copy;
182}

◆ initAnalogSensor()

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

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 44 of file AnalogSensor.c.

44 {
45 initSensor(&analogSensor->sensor, name, hz, ANALOG);
46 analogSensor->channel = channel;
47
48 #ifndef TEST_MODE
49 GPIO_InitTypeDef GPIOXout_Struct = {0};
50 GPIOXout_Struct.Mode = GPIO_MODE_ANALOG;
51 GPIOXout_Struct.Pull = GPIO_NOPULL;
52 GPIOXout_Struct.Speed = GPIO_SPEED_FREQ_HIGH;
53
54 // Map channels to appropriate GPIO pins based on ADC configuration
55 if (channel >= 0 && channel <= 5) {
56 // ADC1: PA0-PA5
57 __HAL_RCC_GPIOA_CLK_ENABLE();
58 GPIOXout_Struct.Pin = GPIO_PIN_0 << channel;
59 /*HAL_GPIO_Init(GPIOA, &GPIOXout_Struct);*/ // Commented out due to not being recognized
60 } else if (channel == 6 || channel == 7) {
61 // ADC2: PA6-PA7
62 __HAL_RCC_GPIOA_CLK_ENABLE();
63 GPIOXout_Struct.Pin = GPIO_PIN_6 << (channel - 6);
64 /*HAL_GPIO_Init(GPIOA, &GPIOXout_Struct);*/
65 } else if (channel == 8 || channel == 9) {
66 // ADC2: PB0-PB1
67 __HAL_RCC_GPIOB_CLK_ENABLE();
68 GPIOXout_Struct.Pin = GPIO_PIN_0 << (channel - 8);
69 /*HAL_GPIO_Init(GPIOB, &GPIOXout_Struct);*/
70 } else if (channel >= 10 && channel <= 13) {
71 // ADC3: PC0-PC3
72 __HAL_RCC_GPIOC_CLK_ENABLE();
73 GPIOXout_Struct.Pin = GPIO_PIN_0 << (channel - 10);
74 /*HAL_GPIO_Init(GPIOC, &GPIOXout_Struct);*/
75 } else if (channel == 14 || channel == 15) {
76 // ADC2: PC4-PC5
77 __HAL_RCC_GPIOC_CLK_ENABLE();
78 GPIOXout_Struct.Pin = GPIO_PIN_4 << (channel - 14);
79 /*HAL_GPIO_Init(GPIOC, &GPIOXout_Struct);*/
80 }
81 #endif
82}
@ ANALOG
Definition: Sensor.h:10
void initSensor(Sensor *sensor, const char *name, int hz, SensorType type)
Initializes a sensor with the given parameters.
Definition: Sensor.c:4
Sensor sensor
Definition: AnalogSensor.h:28
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ProcessADCData()

void ProcessADCData ( uint16_t *  adc1_data,
uint16_t *  adc2_data,
uint16_t *  adc3_data 
)

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 114 of file AnalogSensor.c.

114 {
115 ADCSample sample = {0}; // Initialize all channels to 0
116
117 // Process ADC1 data (PA0-PA5: channels 0-5)
118 for (int i = 0; i < 6; i++) {
119 sample.adc[i] = adc1_data[i];
120 }
121
122 // Process ADC2 data (PA6-PA7: channels 6-7, PB0-PB1: channels 8-9, PC4-PC5: channels 14-15)
123 sample.adc[6] = adc2_data[0]; // PA6
124 sample.adc[7] = adc2_data[1]; // PA7
125 sample.adc[8] = adc2_data[2]; // PB0
126 sample.adc[9] = adc2_data[3]; // PB1
127 sample.adc[14] = adc2_data[4]; // PC4
128 sample.adc[15] = adc2_data[5]; // PC5
129
130 // Process ADC3 data (PC0-PC3: channels 10-13)
131 for (int i = 0; i < 4; i++) {
132 sample.adc[i + 10] = adc3_data[i];
133 }
134
135 addSampleToBuffer(sample);
136
137 // Optional: UART debug output (adjust as needed)
138 char uart_buf[100];
139 snprintf(uart_buf, sizeof(uart_buf), "ADC0: %4d, ADC7: %4d, ADC10: %4d\r\n",
140 sample.adc[0], sample.adc[7], sample.adc[10]);
141
142}
static void addSampleToBuffer(ADCSample sample)
Adds a sample to the circular buffer.
Definition: AnalogSensor.c:92
char uart_buf[UART_BUF_SIZE]
Here is the call graph for this function:

Variable Documentation

◆ adc_circular_buffer

ADCSample adc_circular_buffer[BUFFER_SIZE]
static

Definition at line 12 of file AnalogSensor.c.

◆ buffer_count

uint32_t buffer_count = 0
static

Definition at line 15 of file AnalogSensor.c.

◆ buffer_head

uint32_t buffer_head = 0
static

Definition at line 13 of file AnalogSensor.c.

◆ buffer_tail

uint32_t buffer_tail = 0
static

Definition at line 14 of file AnalogSensor.c.