Skip to content

Commit

Permalink
修复ADC模块的代码编译错误
Browse files Browse the repository at this point in the history
  • Loading branch information
g122622 committed Apr 24, 2024
1 parent 9e547b0 commit 5af75b3
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 50 deletions.
70 changes: 24 additions & 46 deletions main/BMS/ADC/batteryVoltage.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,23 @@
* Created Date: 2024-04-24 13:47:31
* Author: Guoyi
* -----
* Last Modified: 2024-04-24 14:07:13
* Last Modified: 2024-04-24 17:54:26
* Modified By:
* -----
* Copyright (c) 2024 Guoyi Inc.
*
* ------------------------------------
*/

/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "soc/soc_caps.h"
#include "esp_log.h"
#include "esp_adc/adc_oneshot.h"
#include "esp_adc/adc_cali.h"
#include "esp_adc/adc_cali_scheme.h"

const static char *TAG = "EXAMPLE";
const static char *BMS_ADC_LOG_TAG = "BMS_ADC";

/*---------------------------------------------------------------
ADC General Macros
Expand All @@ -38,38 +32,48 @@ const static char *TAG = "EXAMPLE";

static int adc_raw[2][10];
static int voltage[2][10];
adc_oneshot_unit_handle_t adc1_handle;
adc_cali_handle_t adc1_cali_handle = NULL;
bool do_calibration1_chan0 = false;

static bool battery_adc_calibration_init(adc_unit_t unit, adc_channel_t channel, adc_atten_t atten, adc_cali_handle_t *out_handle);
static void battery_adc_calibration_deinit(adc_cali_handle_t handle);

void BMS_ADC_Init(void)
{
//-------------ADC1 Init---------------//
adc_oneshot_unit_handle_t adc1_handle;
adc_oneshot_unit_init_cfg_t init_config = {
.unit_id = ADC_UNIT_1,
};
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config, &adc1_handle));

//-------------ADC1 Config---------------//
adc_oneshot_chan_cfg_t config = {
.bitwidth = ADC_BITWIDTH_DEFAULT,
.atten = BATTERY_ADC_ATTEN,
.bitwidth = ADC_BITWIDTH_DEFAULT,
};
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, BATTERY_ADC1_CHANNEL, &config));

//-------------ADC1 Calibration Init---------------//
bool do_calibration1_chan0 = battery_adc_calibration_init(ADC_UNIT_1, BATTERY_ADC1_CHANNEL, BATTERY_ADC_ATTEN, &adc1_cali_handle);
do_calibration1_chan0 = battery_adc_calibration_init(ADC_UNIT_1, BATTERY_ADC1_CHANNEL, BATTERY_ADC_ATTEN, &adc1_cali_handle);
}

// 如果读取数据时还未校准,那么返回-1
int BMS_ADC_ReadBatteryVoltage()
{
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, BATTERY_ADC1_CHANNEL, &adc_raw[0][0]));
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, adc_raw[0][0], &voltage[0][0]));
return voltage[0][0];
if (do_calibration1_chan0)
{
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, BATTERY_ADC1_CHANNEL, &adc_raw[0][0]));
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, adc_raw[0][0], &voltage[0][0]));
return voltage[0][0];
} else {
// 如果读取数据时还未校准,那么返回-1
return -1;
}
return -1;
}

int BMS_ADC_Deinit()
void BMS_ADC_Deinit()
{
// Tear Down
ESP_ERROR_CHECK(adc_oneshot_del_unit(adc1_handle));
Expand All @@ -86,28 +90,9 @@ battery_adc_calibration_init(adc_unit_t unit, adc_channel_t channel, adc_atten_t
esp_err_t ret = ESP_FAIL;
bool calibrated = false;

#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
if (!calibrated)
{
ESP_LOGI(TAG, "calibration scheme version is %s", "Curve Fitting");
adc_cali_curve_fitting_config_t cali_config = {
.unit_id = unit,
.chan = channel,
.atten = atten,
.bitwidth = ADC_BITWIDTH_DEFAULT,
};
ret = adc_cali_create_scheme_curve_fitting(&cali_config, &handle);
if (ret == ESP_OK)
{
calibrated = true;
}
}
#endif

#if ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED
if (!calibrated)
{
ESP_LOGI(TAG, "calibration scheme version is %s", "Line Fitting");
ESP_LOGI(BMS_ADC_LOG_TAG, "calibration scheme version is %s", "Line Fitting");
adc_cali_line_fitting_config_t cali_config = {
.unit_id = unit,
.atten = atten,
Expand All @@ -119,33 +104,26 @@ battery_adc_calibration_init(adc_unit_t unit, adc_channel_t channel, adc_atten_t
calibrated = true;
}
}
#endif

*out_handle = handle;
if (ret == ESP_OK)
{
ESP_LOGI(TAG, "Calibration Success");
ESP_LOGI(BMS_ADC_LOG_TAG, "Calibration Success");
}
else if (ret == ESP_ERR_NOT_SUPPORTED || !calibrated)
{
ESP_LOGW(TAG, "eFuse not burnt, skip software calibration");
ESP_LOGW(BMS_ADC_LOG_TAG, "eFuse not burnt, skip software calibration");
}
else
{
ESP_LOGE(TAG, "Invalid arg or no memory");
ESP_LOGE(BMS_ADC_LOG_TAG, "Invalid arg or no memory");
}

return calibrated;
}

static void battery_adc_calibration_deinit(adc_cali_handle_t handle)
{
#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
ESP_LOGI(TAG, "deregister %s calibration scheme", "Curve Fitting");
ESP_ERROR_CHECK(adc_cali_delete_scheme_curve_fitting(handle));

#elif ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED
ESP_LOGI(TAG, "deregister %s calibration scheme", "Line Fitting");
ESP_LOGI(BMS_ADC_LOG_TAG, "deregister %s calibration scheme", "Line Fitting");
ESP_ERROR_CHECK(adc_cali_delete_scheme_line_fitting(handle));
#endif
}
6 changes: 6 additions & 0 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,22 @@
#include "FlightController/motor/motor.h"
/* 各任务 */
#include "tasks/controllerTickLoop.h"
#include "tasks/BMSTaskLoop.h"

// 任务优先级数值越小,任务优先级越低
#define tskHIGH_PRIORITY 10
#define tskMEDIUM_PRIORITY 5

TaskHandle_t controllerTickLoopHandle = NULL;
TaskHandle_t BMSTaskLoopHandle = NULL;

/* 启动任务 */
void Tasks_Init()
{
xTaskCreatePinnedToCore(controllerTickLoop, "controllerTickLoop",
4096, NULL, tskHIGH_PRIORITY, controllerTickLoopHandle, 1);
xTaskCreatePinnedToCore(BMSTaskLoop, "BMSTaskLoop",
4096, NULL, tskMEDIUM_PRIORITY, BMSTaskLoopHandle, 1);
}

void app_main(void)
Expand Down
35 changes: 35 additions & 0 deletions main/tasks/BMSTaskLoop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* File: \BMSTask.h
* Project: tasks
* Created Date: 2024-04-24 17:28:06
* Author: Guoyi
* -----
* Last Modified: 2024-04-24 17:31:36
* Modified By:
* -----
* Copyright (c) 2024 Guoyi Inc.
*
* ------------------------------------
*/

#ifndef TASKS_BMS_H
#define TASKS_BMS_H

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "../BMS/ADC/batteryVoltage.h"

void BMSTaskLoop(void *argument)
{
BMS_ADC_Init();
while (1)
{
BMS_ADC_ReadBatteryVoltage();
int volt = BMS_ADC_ReadBatteryVoltage();
printf("volt=%d", volt);
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}

#endif
6 changes: 2 additions & 4 deletions main/tasks/controllerTickLoop.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created Date: 2024-03-11 13:27:21
* Author: Guoyi
* -----
* Last Modified: 2024-04-12 18:58:00
* Last Modified: 2024-04-24 17:36:38
* Modified By: Guoyi
* -----
* Copyright (c) 2024 Guoyi Inc.
Expand All @@ -21,15 +21,13 @@

#include "mpu6050/motionData.h"

#define INTERVAL_MS (5)

void controllerTickLoop(void *argument)
{
MotionData_Init();
vTaskDelay(200 / portTICK_PERIOD_MS);
while (1)
{
vTaskDelay(INTERVAL_MS / portTICK_PERIOD_MS);
vTaskDelay(5 / portTICK_PERIOD_MS);
controllerTick();
}
}
Expand Down

0 comments on commit 5af75b3

Please sign in to comment.