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#include "../../Inc/Utils/Constants.h"
3
4#include <stdio.h>
5#include <string.h>
6
7#ifndef TEST_MODE
8#include "stm32f7xx_hal.h"
9#endif
10
11
12// Circular buffer to store ADC samples
13static uint16_t adc_samples[ADC_CHANNELS];
14
15// The AnalogSensor system is configured to handle 16 channels (0-15) across three ADCs:
16// - ADC1: Channels 0-5 (PA0-PA5)
17// - ADC2: Channels 6-7 (PA6-PA7), 8-9 (PB0-PB1), 14-15 (PC4-PC5)
18// - ADC3: Channels 10-13 (PC0-PC3)
19
20// The initAnalogSensor function sets up GPIO pins for all these channels.
21// The ProcessADCData function handles data from all three ADCs.
22// The getAnalogSensorData function can retrieve data for any channel (0-15).
23
24// Initialization code in main.c should set up the ADCs to continuously sample
25// their respective channels and use DMA to transfer the data to the appropriate buffers:
26// - adc1_buffer for ADC1 (6 channels)
27// - adc2_buffer for ADC2 (6 channels)
28// - adc3_buffer for ADC3 (4 channels)
29
30
42void initAnalogSensor(AnalogSensor* analogSensor, const char* name, int hz, int channel, void* child) {
43 initSensor(&analogSensor->sensor, name, hz, s_ANALOG, analogSensor);
44 analogSensor->channel = channel;
45 analogSensor->child = child;
46}
47
58void ProcessADCData(uint32_t* adc1_buffer, uint32_t* adc2_buffer, uint32_t* adc3_buffer) {
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}
79
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}
float getAnalogSensorData(AnalogSensor *sensor)
Retrieves analog sensor data for a specific channel.
Definition: AnalogSensor.c:89
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.
Definition: AnalogSensor.c:58
static uint16_t adc_samples[ADC_CHANNELS]
Definition: AnalogSensor.c:13
void initAnalogSensor(AnalogSensor *analogSensor, const char *name, int hz, int channel, void *child)
Initializes an analog sensor and configures the corresponding GPIO pin.
Definition: AnalogSensor.c:42
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 ADC_CHANNELS
Definition: Constants.h:5
#define ADC_VREF
Definition: Constants.h:6
#define ADC1_CHANNEL_SIZE
Definition: Constants.h:2
@ 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
Sensor sensor
Definition: AnalogSensor.h:23
Updateable updateable
Definition: Sensor.h:15
char name[MAX_NAME_LENGTH]
Definition: Updateable.h:24