measure refact
This commit is contained in:
parent
bc0830f81d
commit
629c8aa441
157
main/Alim1000W.c
157
main/Alim1000W.c
@ -11,13 +11,6 @@
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/portmacro.h"
|
||||
#include "host/ble_uuid.h"
|
||||
#include "spinlock.h"
|
||||
|
||||
#include "esp_adc/adc_continuous.h"
|
||||
#include "esp_adc/adc_cali.h"
|
||||
#include "esp_adc/adc_cali_scheme.h"
|
||||
#include "hal/adc_types.h"
|
||||
#include "soc/soc_caps.h"
|
||||
|
||||
#include "nvs_flash.h"
|
||||
|
||||
@ -27,29 +20,14 @@
|
||||
#include "simple_gatt_value_server.h"
|
||||
|
||||
#include "BLE.h"
|
||||
#include "measure.h"
|
||||
|
||||
#define TAG "Main"
|
||||
|
||||
#define ADC_IN_PIN_NUM 5
|
||||
|
||||
static uint8_t channel_index[ADC_IN_PIN_NUM];
|
||||
static uint32_t meas_res[ADC_IN_PIN_NUM];
|
||||
static uint32_t meas_nb[ADC_IN_PIN_NUM];
|
||||
|
||||
static uint32_t switching_frequency = CONFIG_SW_FREQ;
|
||||
static uint32_t duty_cycle = 50000000;
|
||||
|
||||
static bool IRAM_ATTR on_conv_done(adc_continuous_handle_t handle, const adc_continuous_evt_data_t *edata, void *user_data){
|
||||
adc_digi_output_data_t* res = (adc_digi_output_data_t*)edata->conv_frame_buffer;
|
||||
|
||||
if(res->type2.channel < 0 || res->type2.channel >= ADC_IN_PIN_NUM) return false; //invalid channel
|
||||
|
||||
int index = channel_index[res->type2.channel];
|
||||
meas_res[index] += res->type2.data;
|
||||
meas_nb[index]++;
|
||||
|
||||
return false;
|
||||
}
|
||||
static uint8_t adc_pins[] = {CONFIG_OUT_VOLT_PIN, CONFIG_OUT_CURR_PIN, CONFIG_PHASE1_CURR_PIN, CONFIG_PHASE2_CURR_PIN, CONFIG_PHASE3_CURR_PIN};
|
||||
|
||||
void on_char_read_handler(int svc_ind, int chr_ind, void** value, size_t* value_size){
|
||||
ESP_LOGI(TAG, "char read handler");
|
||||
@ -82,10 +60,12 @@ int on_char_write_handler(int svc_ind, int chr_ind, struct os_mbuf* os){
|
||||
;
|
||||
rc = gatt_svr_chr_write_get_data(os, sizeof(val), sizeof(val), &val, NULL);
|
||||
switching_frequency = val;
|
||||
// TODO update running val
|
||||
break;
|
||||
case DUTY_CYCLE_CHR_ID:
|
||||
rc = gatt_svr_chr_write_get_data(os, sizeof(val), sizeof(val), &val, NULL);
|
||||
duty_cycle = val;
|
||||
// TODO update running val
|
||||
break;
|
||||
}
|
||||
break;
|
||||
@ -95,69 +75,13 @@ int on_char_write_handler(int svc_ind, int chr_ind, struct os_mbuf* os){
|
||||
}
|
||||
|
||||
void app_main(void){
|
||||
adc_continuous_handle_cfg_t adc_h_conf = {
|
||||
.conv_frame_size = SOC_ADC_DIGI_DATA_BYTES_PER_CONV,
|
||||
.max_store_buf_size = SOC_ADC_DIGI_DATA_BYTES_PER_CONV * ADC_IN_PIN_NUM,
|
||||
};
|
||||
|
||||
adc_continuous_handle_t adc_handle;
|
||||
ESP_ERROR_CHECK(adc_continuous_new_handle(&adc_h_conf, &adc_handle));
|
||||
|
||||
uint8_t adc_pins[] = {CONFIG_OUT_VOLT_PIN, CONFIG_OUT_CURR_PIN, CONFIG_PHASE1_CURR_PIN, CONFIG_PHASE2_CURR_PIN, CONFIG_PHASE3_CURR_PIN};
|
||||
|
||||
adc_digi_pattern_config_t adc_pattern[ADC_IN_PIN_NUM];
|
||||
for(int i = 0; i < ADC_IN_PIN_NUM; i++){
|
||||
adc_channel_t channel;
|
||||
adc_unit_t unit;
|
||||
adc_continuous_io_to_channel(adc_pins[i], &unit, &channel);
|
||||
|
||||
if(unit != ADC_UNIT_1){
|
||||
ESP_LOGE(TAG, "Wrong unit for one of the selected pins");
|
||||
}
|
||||
|
||||
channel_index[channel] = i;
|
||||
|
||||
adc_pattern[i] = (adc_digi_pattern_config_t){
|
||||
.atten = ADC_ATTEN_DB_0,
|
||||
.bit_width = ADC_BITWIDTH_12,
|
||||
.channel = channel,
|
||||
.unit = ADC_UNIT_1,
|
||||
};
|
||||
}
|
||||
|
||||
adc_continuous_config_t adc_conf = {
|
||||
.adc_pattern = adc_pattern,
|
||||
.pattern_num = ADC_IN_PIN_NUM,
|
||||
.conv_mode = ADC_CONV_SINGLE_UNIT_1,
|
||||
.format = ADC_DIGI_OUTPUT_FORMAT_TYPE2,
|
||||
.sample_freq_hz = SOC_ADC_SAMPLE_FREQ_THRES_HIGH,
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(adc_continuous_config(adc_handle, &adc_conf));
|
||||
|
||||
adc_cali_handle_t adc_cali_handles[ADC_IN_PIN_NUM];
|
||||
init_adc(adc_pins);
|
||||
init_adc_cali();
|
||||
adc_start();
|
||||
|
||||
for(int i = 0; i < ADC_IN_PIN_NUM; i++){
|
||||
adc_cali_curve_fitting_config_t conf = {
|
||||
.atten = ADC_ATTEN_DB_0,
|
||||
.bitwidth = ADC_BITWIDTH_12,
|
||||
.unit_id = ADC_UNIT_1,
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(adc_cali_create_scheme_curve_fitting(&conf, &adc_cali_handles[i]));
|
||||
}
|
||||
|
||||
spinlock_t adc_res_mutex;
|
||||
spinlock_initialize(&adc_res_mutex);
|
||||
|
||||
adc_continuous_evt_cbs_t adc_event = {
|
||||
.on_conv_done = &on_conv_done,
|
||||
.on_pool_ovf = NULL,
|
||||
};
|
||||
|
||||
nvs_flash_init();
|
||||
|
||||
uint32_t raw_mv_meas[ADC_IN_PIN_NUM];
|
||||
uint32_t raw_mv_meas[CONFIG_ADC_IN_PIN_NUM];
|
||||
|
||||
struct char_pres_format mv_char_pres_format = {
|
||||
.format = FORMAT_UINT32,
|
||||
@ -168,9 +92,9 @@ void app_main(void){
|
||||
.descrL = (NSP_DESC_MAIN>>8) & 0xff,
|
||||
};
|
||||
|
||||
gatt_value_server_handle_t raw_mv_serve_handle = simple_gatt_identical_values_server(raw_mv_meas, sizeof(uint32_t), ADC_IN_PIN_NUM, &mv_char_pres_format, &gatt_svcs[RAW_VALUES_SVC_ID], RAW_VALUES_SVC_ID, BLE_UUID16_DECLARE(VOLTAGE_CHAR));
|
||||
gatt_value_server_handle_t raw_mv_serve_handle = simple_gatt_identical_values_server(raw_mv_meas, sizeof(uint32_t), CONFIG_ADC_IN_PIN_NUM, &mv_char_pres_format, &gatt_svcs[RAW_VALUES_SVC_ID], RAW_VALUES_SVC_ID, BLE_UUID16_DECLARE(VOLTAGE_CHAR));
|
||||
|
||||
uint32_t calc_meas[ADC_IN_PIN_NUM];
|
||||
uint32_t calc_meas[CONFIG_ADC_IN_PIN_NUM];
|
||||
|
||||
struct char_pres_format mA_char_pres_format = {
|
||||
.format = FORMAT_UINT32,
|
||||
@ -181,11 +105,11 @@ void app_main(void){
|
||||
.descrL = (NSP_DESC_MAIN>>8) & 0xff,
|
||||
};
|
||||
|
||||
struct char_pres_format* calc_meas_formats[ADC_IN_PIN_NUM];
|
||||
struct char_pres_format* calc_meas_formats[CONFIG_ADC_IN_PIN_NUM];
|
||||
|
||||
ble_uuid_t* calc_meas_char_uuid[ADC_IN_PIN_NUM];
|
||||
ble_uuid_t* calc_meas_char_uuid[CONFIG_ADC_IN_PIN_NUM];
|
||||
|
||||
for(int i = 0; i < ADC_IN_PIN_NUM; i++){
|
||||
for(int i = 0; i < CONFIG_ADC_IN_PIN_NUM; i++){
|
||||
switch(adc_pins[i]){
|
||||
case CONFIG_OUT_VOLT_PIN:
|
||||
calc_meas_formats[i] = &mv_char_pres_format;
|
||||
@ -197,7 +121,7 @@ void app_main(void){
|
||||
}
|
||||
}
|
||||
|
||||
gatt_value_server_handle_t calc_meas_serve_handle = simple_gatt_value_server(calc_meas, sizeof(uint32_t), ADC_IN_PIN_NUM, calc_meas_formats, &gatt_svcs[CALC_VALUES_SVC_ID], CALC_VALUES_SVC_ID, calc_meas_char_uuid);
|
||||
gatt_value_server_handle_t calc_meas_serve_handle = simple_gatt_value_server(calc_meas, sizeof(uint32_t), CONFIG_ADC_IN_PIN_NUM, calc_meas_formats, &gatt_svcs[CALC_VALUES_SVC_ID], CALC_VALUES_SVC_ID, calc_meas_char_uuid);
|
||||
|
||||
set_on_char_read_handler(on_char_read_handler);
|
||||
set_on_char_write_handler(on_char_write_handler);
|
||||
@ -206,54 +130,13 @@ void app_main(void){
|
||||
|
||||
initBLE("ALIM");
|
||||
|
||||
ESP_ERROR_CHECK(adc_continuous_register_event_callbacks(adc_handle, &adc_event, NULL));
|
||||
ESP_ERROR_CHECK(adc_continuous_start(adc_handle));
|
||||
|
||||
while(1){
|
||||
uint32_t meas_res_buff[ADC_IN_PIN_NUM];
|
||||
uint32_t meas_nb_buff[ADC_IN_PIN_NUM];
|
||||
|
||||
portENTER_CRITICAL(&adc_res_mutex);
|
||||
for(int i = 0; i < ADC_IN_PIN_NUM; i++){
|
||||
meas_res_buff[i] = meas_res[i];
|
||||
meas_nb_buff[i] = meas_nb[i];
|
||||
|
||||
meas_res[i] = 0;
|
||||
meas_nb[i] = 0;
|
||||
}
|
||||
portEXIT_CRITICAL(&adc_res_mutex);
|
||||
|
||||
for(int i = 0; i < ADC_IN_PIN_NUM; i++){
|
||||
int mv;
|
||||
uint32_t val = meas_res_buff[i] / meas_nb_buff[i];
|
||||
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc_cali_handles[i], val, &mv));
|
||||
raw_mv_meas[i] = mv;
|
||||
switch(adc_pins[i]){
|
||||
case CONFIG_OUT_VOLT_PIN:
|
||||
ESP_LOGI(TAG, "Output voltage %d mV", mv);
|
||||
calc_meas[i] = mv * 33;
|
||||
break;
|
||||
case CONFIG_OUT_CURR_PIN:
|
||||
ESP_LOGI(TAG, "Output current %d mV", mv);
|
||||
calc_meas[i] = mv * 10;
|
||||
break;
|
||||
case CONFIG_PHASE1_CURR_PIN:
|
||||
ESP_LOGI(TAG, "Phase 1 current %d mV", mv);
|
||||
calc_meas[i] = mv * 10;
|
||||
break;
|
||||
case CONFIG_PHASE2_CURR_PIN:
|
||||
ESP_LOGI(TAG, "Phase 2 current %d mV", mv);
|
||||
calc_meas[i] = mv * 10;
|
||||
break;
|
||||
case CONFIG_PHASE3_CURR_PIN:
|
||||
ESP_LOGI(TAG, "Phase 3 current %d mV", mv);
|
||||
calc_meas[i] = mv * 10;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
simple_gatt_value_server_notify(raw_mv_serve_handle, ADC_IN_PIN_NUM);
|
||||
simple_gatt_value_server_notify(calc_meas_serve_handle, ADC_IN_PIN_NUM);
|
||||
fetch_measures();
|
||||
get_measures_raw_mV(raw_mv_meas);
|
||||
get_measures_calc(calc_meas);
|
||||
|
||||
simple_gatt_value_server_notify(raw_mv_serve_handle, CONFIG_ADC_IN_PIN_NUM);
|
||||
simple_gatt_value_server_notify(calc_meas_serve_handle, CONFIG_ADC_IN_PIN_NUM);
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(CONFIG_UPD_DELAY));
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
idf_component_register(
|
||||
SRCS "Alim1000W.c" "BLE.c"
|
||||
SRCS "Alim1000W.c" "BLE.c" "measure.c"
|
||||
INCLUDE_DIRS "."
|
||||
)
|
||||
|
@ -1,5 +1,9 @@
|
||||
menu "This project config"
|
||||
menu "Pins definition"
|
||||
config ADC_IN_PIN_NUM
|
||||
int "Number of analog inputs"
|
||||
default 5
|
||||
|
||||
config OUT_VOLT_PIN
|
||||
int "Output voltage"
|
||||
default 2
|
||||
|
140
main/measure.c
Normal file
140
main/measure.c
Normal file
@ -0,0 +1,140 @@
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "esp_adc/adc_continuous.h"
|
||||
#include "esp_adc/adc_cali.h"
|
||||
#include "esp_adc/adc_cali_scheme.h"
|
||||
#include "hal/adc_types.h"
|
||||
#include "soc/soc_caps.h"
|
||||
|
||||
#include "spinlock.h"
|
||||
#include "freertos/portmacro.h"
|
||||
|
||||
#include "measure.h"
|
||||
#include <stdint.h>
|
||||
|
||||
#define TAG "Measure"
|
||||
|
||||
static uint8_t channel_index[CONFIG_ADC_IN_PIN_NUM];
|
||||
static uint32_t meas_res[CONFIG_ADC_IN_PIN_NUM];
|
||||
static uint32_t meas_nb[CONFIG_ADC_IN_PIN_NUM];
|
||||
spinlock_t adc_res_mutex;
|
||||
|
||||
uint32_t meas_res_buff[CONFIG_ADC_IN_PIN_NUM];
|
||||
uint32_t meas_nb_buff[CONFIG_ADC_IN_PIN_NUM];
|
||||
|
||||
adc_continuous_handle_t adc_handle;
|
||||
adc_cali_handle_t adc_cali_handles[CONFIG_ADC_IN_PIN_NUM];
|
||||
|
||||
static uint8_t* adc_pins;
|
||||
|
||||
static bool IRAM_ATTR on_conv_done(adc_continuous_handle_t handle, const adc_continuous_evt_data_t *edata, void *user_data){
|
||||
adc_digi_output_data_t* res = (adc_digi_output_data_t*)edata->conv_frame_buffer;
|
||||
|
||||
if(res->type2.channel < 0 || res->type2.channel >= CONFIG_ADC_IN_PIN_NUM) return false; //invalid channel
|
||||
|
||||
int index = channel_index[res->type2.channel];
|
||||
meas_res[index] += res->type2.data;
|
||||
meas_nb[index]++;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void init_adc(uint8_t* _adc_pins){
|
||||
adc_pins = _adc_pins;
|
||||
adc_continuous_handle_cfg_t adc_h_conf = {
|
||||
.conv_frame_size = SOC_ADC_DIGI_DATA_BYTES_PER_CONV,
|
||||
.max_store_buf_size = SOC_ADC_DIGI_DATA_BYTES_PER_CONV * CONFIG_ADC_IN_PIN_NUM,
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(adc_continuous_new_handle(&adc_h_conf, &adc_handle));
|
||||
|
||||
adc_digi_pattern_config_t adc_pattern[CONFIG_ADC_IN_PIN_NUM];
|
||||
for(int i = 0; i < CONFIG_ADC_IN_PIN_NUM; i++){
|
||||
adc_channel_t channel;
|
||||
adc_unit_t unit;
|
||||
adc_continuous_io_to_channel(adc_pins[i], &unit, &channel);
|
||||
|
||||
if(unit != ADC_UNIT_1){
|
||||
ESP_LOGE(TAG, "Wrong unit for one of the selected pins");
|
||||
}
|
||||
|
||||
channel_index[channel] = i;
|
||||
|
||||
adc_pattern[i] = (adc_digi_pattern_config_t){
|
||||
.atten = ADC_ATTEN_DB_0,
|
||||
.bit_width = ADC_BITWIDTH_12,
|
||||
.channel = channel,
|
||||
.unit = ADC_UNIT_1,
|
||||
};
|
||||
}
|
||||
|
||||
adc_continuous_config_t adc_conf = {
|
||||
.adc_pattern = adc_pattern,
|
||||
.pattern_num = CONFIG_ADC_IN_PIN_NUM,
|
||||
.conv_mode = ADC_CONV_SINGLE_UNIT_1,
|
||||
.format = ADC_DIGI_OUTPUT_FORMAT_TYPE2,
|
||||
.sample_freq_hz = SOC_ADC_SAMPLE_FREQ_THRES_HIGH,
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(adc_continuous_config(adc_handle, &adc_conf));
|
||||
|
||||
adc_continuous_evt_cbs_t adc_event = {
|
||||
.on_conv_done = &on_conv_done,
|
||||
.on_pool_ovf = NULL,
|
||||
};
|
||||
ESP_ERROR_CHECK(adc_continuous_register_event_callbacks(adc_handle, &adc_event, NULL));
|
||||
|
||||
spinlock_initialize(&adc_res_mutex);
|
||||
}
|
||||
|
||||
void init_adc_cali(){
|
||||
for(int i = 0; i < CONFIG_ADC_IN_PIN_NUM; i++){
|
||||
adc_cali_curve_fitting_config_t conf = {
|
||||
.atten = ADC_ATTEN_DB_0,
|
||||
.bitwidth = ADC_BITWIDTH_12,
|
||||
.unit_id = ADC_UNIT_1,
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(adc_cali_create_scheme_curve_fitting(&conf, &adc_cali_handles[i]));
|
||||
}
|
||||
}
|
||||
|
||||
void adc_start(){
|
||||
ESP_ERROR_CHECK(adc_continuous_start(adc_handle));
|
||||
}
|
||||
|
||||
void fetch_measures(){
|
||||
portENTER_CRITICAL(&adc_res_mutex);
|
||||
for(int i = 0; i < CONFIG_ADC_IN_PIN_NUM; i++){
|
||||
meas_res_buff[i] = meas_res[i];
|
||||
meas_nb_buff[i] = meas_nb[i];
|
||||
|
||||
meas_res[i] = 0;
|
||||
meas_nb[i] = 0;
|
||||
}
|
||||
portEXIT_CRITICAL(&adc_res_mutex);
|
||||
}
|
||||
|
||||
void get_measures_raw_mV(uint32_t values[]){
|
||||
for(int i = 0; i < CONFIG_ADC_IN_PIN_NUM; i++){
|
||||
int mv;
|
||||
uint32_t val = meas_res_buff[i] / meas_nb_buff[i];
|
||||
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc_cali_handles[i], val, &mv));
|
||||
values[i] = mv;
|
||||
}
|
||||
}
|
||||
|
||||
void get_measures_calc(uint32_t values[]){
|
||||
for(int i = 0; i < CONFIG_ADC_IN_PIN_NUM; i++){
|
||||
int mv;
|
||||
uint32_t val = meas_res_buff[i] / meas_nb_buff[i];
|
||||
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc_cali_handles[i], val, &mv));
|
||||
switch(adc_pins[i]){
|
||||
case CONFIG_OUT_VOLT_PIN:
|
||||
values[i] = mv * 33;
|
||||
break;
|
||||
default:
|
||||
values[i] = mv * 10;
|
||||
}
|
||||
}
|
||||
}
|
7
main/measure.h
Normal file
7
main/measure.h
Normal file
@ -0,0 +1,7 @@
|
||||
#include <stdint.h>
|
||||
void init_adc(uint8_t* adc_pins);
|
||||
void init_adc_cali();
|
||||
void adc_start();
|
||||
void fetch_measures();
|
||||
void get_measures_raw_mV(uint32_t values[]);
|
||||
void get_measures_calc(uint32_t values[]);
|
Loading…
x
Reference in New Issue
Block a user