Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
AnalogSensor.c
Go to the documentation of this file.
1#include "../../Inc/Sensors/AnalogSensor.h"
2
3#include <stdio.h>
4#include <string.h>
5
6#ifndef TEST_MODE
7#include "stm32f7xx_hal.h"
8#endif
9
10
11// Circular buffer to store ADC samples
13static uint32_t buffer_head = 0;
14static uint32_t buffer_tail = 0;
15static uint32_t buffer_count = 0;
16
17// The AnalogSensor system is configured to handle 16 channels (0-15) across three ADCs:
18// - ADC1: Channels 0-5 (PA0-PA5)
19// - ADC2: Channels 6-7 (PA6-PA7), 8-9 (PB0-PB1), 14-15 (PC4-PC5)
20// - ADC3: Channels 10-13 (PC0-PC3)
21
22// The initAnalogSensor function sets up GPIO pins for all these channels.
23// The ProcessADCData function handles data from all three ADCs.
24// The getAnalogSensorData function can retrieve data for any channel (0-15).
25
26// Initialization code in main.c should set up the ADCs to continuously sample
27// their respective channels and use DMA to transfer the data to the appropriate buffers:
28// - adc1_buffer for ADC1 (6 channels)
29// - adc2_buffer for ADC2 (6 channels)
30// - adc3_buffer for ADC3 (4 channels)
31
32
44void initAnalogSensor(AnalogSensor* analogSensor, const char* name, int hz, int channel) {
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}
83
92static void addSampleToBuffer(ADCSample sample) {
95
98 } else {
99 // Buffer is full, update tail to overwrite the oldest data
101 }
102}
103
114void ProcessADCData(uint16_t* adc1_data, uint16_t* adc2_data, uint16_t* adc3_data) {
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}
143
144
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}
162
173uint32_t getRecentSamples(ADCSample* samples, uint32_t num_samples) {
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}
183
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}
int getAnalogSensorData(AnalogSensor *sensor)
Retrieves analog sensor data for a specific channel.
Definition: AnalogSensor.c:193
void initAnalogSensor(AnalogSensor *analogSensor, const char *name, int hz, int channel)
Initializes an analog sensor and configures the corresponding GPIO pin.
Definition: AnalogSensor.c:44
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.
Definition: AnalogSensor.c:114
ADCSample getLatestSample(void)
Retrieves the latest ADC sample from the buffer.
Definition: AnalogSensor.c:152
uint32_t getRecentSamples(ADCSample *samples, uint32_t num_samples)
Retrieves a specified number of recent samples from the buffer.
Definition: AnalogSensor.c:173
static void addSampleToBuffer(ADCSample sample)
Adds a sample to the circular buffer.
Definition: AnalogSensor.c:92
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
char uart_buf[UART_BUF_SIZE]
@ 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
uint16_t adc[ADC_CHANNELS]
Definition: AnalogSensor.h:33
Sensor sensor
Definition: AnalogSensor.h:28