parent
3b75bca5c3
commit
8c1f11d172
@ -0,0 +1,91 @@
|
||||
#include "adc_read.h"
|
||||
#include "io.h"
|
||||
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "freertos/task.h"
|
||||
#include "driver/adc.h"
|
||||
#include "esp_adc_cal.h"
|
||||
|
||||
#define SYS_VOLTS_PER_ADC_MV ((((2.2+10.0)/2.2)/1000.0)*1.042)
|
||||
#define MOTOR_AMPS_PER_ADC_MV ((2.5/1000.0)*1.042)
|
||||
|
||||
#define ADC_TASK_STACK_SIZE 2048
|
||||
|
||||
static xQueueHandle motor_current_queue[3];
|
||||
static xQueueHandle system_voltage_queue;
|
||||
|
||||
float get_motor_current(motor_id_t motor_id) {
|
||||
float motor_current;
|
||||
xQueuePeek(motor_current_queue[motor_id], &motor_current, 0);
|
||||
return motor_current;
|
||||
}
|
||||
|
||||
float get_system_voltage() {
|
||||
float system_voltage;
|
||||
xQueuePeek(system_voltage_queue, &system_voltage, 0);
|
||||
return system_voltage;
|
||||
}
|
||||
|
||||
|
||||
static esp_adc_cal_characteristics_t adc1_11db_chars;
|
||||
static esp_adc_cal_characteristics_t adc1_6db_chars;
|
||||
|
||||
static void motor_control_task(void *pvParameters) {
|
||||
TickType_t xLastWakeTime;
|
||||
xLastWakeTime = xTaskGetTickCount();
|
||||
int adc_raw;
|
||||
float result;
|
||||
for (;;) {
|
||||
adc_raw = adc1_get_raw(ADC_ROLLER1_IS);
|
||||
result = esp_adc_cal_raw_to_voltage(adc_raw, &adc1_11db_chars) * MOTOR_AMPS_PER_ADC_MV;
|
||||
xQueueOverwrite(motor_current_queue[0], &result);
|
||||
|
||||
adc_raw = adc1_get_raw(ADC_ROLLER2_IS);
|
||||
result = esp_adc_cal_raw_to_voltage(adc_raw, &adc1_11db_chars) * MOTOR_AMPS_PER_ADC_MV;
|
||||
xQueueOverwrite(motor_current_queue[1], &result);
|
||||
|
||||
adc_raw = adc1_get_raw(ADC_ROLLER3_IS);
|
||||
result = esp_adc_cal_raw_to_voltage(adc_raw, &adc1_11db_chars) * MOTOR_AMPS_PER_ADC_MV;
|
||||
xQueueOverwrite(motor_current_queue[2], &result);
|
||||
|
||||
adc_raw = adc1_get_raw(ADC_SYS_VOLTAGE);
|
||||
result = esp_adc_cal_raw_to_voltage(adc_raw, &adc1_11db_chars) * SYS_VOLTS_PER_ADC_MV;
|
||||
xQueueOverwrite(system_voltage_queue, &result);
|
||||
|
||||
// Suspend task until the next control loop iteration
|
||||
vTaskDelayUntil(&xLastWakeTime, pdMS_TO_TICKS(ADC_UPDATE_PERIOD_MS));
|
||||
}
|
||||
}
|
||||
|
||||
void adc_read_setup(void) {
|
||||
|
||||
// Allocate queues
|
||||
motor_current_queue[0] = xQueueCreate(1, sizeof(float));
|
||||
motor_current_queue[1] = xQueueCreate(1, sizeof(float));
|
||||
motor_current_queue[2] = xQueueCreate(1, sizeof(float));
|
||||
system_voltage_queue = xQueueCreate(1, sizeof(float));
|
||||
|
||||
// Prime queues
|
||||
float initial_motor_current = 0.0;
|
||||
xQueueOverwrite(motor_current_queue[0], &initial_motor_current);
|
||||
xQueueOverwrite(motor_current_queue[1], &initial_motor_current);
|
||||
xQueueOverwrite(motor_current_queue[2], &initial_motor_current);
|
||||
float initial_system_voltage = 0.0;
|
||||
xQueueOverwrite(system_voltage_queue, &initial_system_voltage);
|
||||
|
||||
// Init ADC
|
||||
// If our ESP32 doesn't have calibration fuses set, this will use the default VREF value we give it here
|
||||
esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, 1158, &adc1_11db_chars);
|
||||
esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_6, ADC_WIDTH_BIT_12, 1158, &adc1_6db_chars);
|
||||
|
||||
adc1_config_width(ADC_WIDTH_BIT_12);
|
||||
adc1_config_channel_atten(ADC_ROLLER1_IS, ADC_ATTEN_DB_11);
|
||||
adc1_config_channel_atten(ADC_ROLLER2_IS, ADC_ATTEN_DB_11);
|
||||
adc1_config_channel_atten(ADC_ROLLER3_IS, ADC_ATTEN_DB_11);
|
||||
adc1_config_channel_atten(ADC_SYS_VOLTAGE, ADC_ATTEN_DB_11);
|
||||
|
||||
// Kick off our ADC task
|
||||
xTaskCreate(motor_control_task, "ADC", ADC_TASK_STACK_SIZE, NULL, 5, NULL);
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
#ifndef adc_read_h
|
||||
#define adc_read_h
|
||||
|
||||
#include "../common/shared_structs.h"
|
||||
|
||||
#define ADC_UPDATE_PERIOD_MS 100
|
||||
|
||||
void adc_read_setup(void);
|
||||
|
||||
float get_motor_current(motor_id_t m_id);
|
||||
|
||||
float get_system_voltage();
|
||||
|
||||
#endif
|
||||
Loading…
Reference in new issue