270 lines
8.1 KiB
C++
270 lines
8.1 KiB
C++
#include "adc.h"
|
|
#include "Can.h"
|
|
#include "FOC.h"
|
|
#include "Filter.h"
|
|
#include "config.h"
|
|
#include "math.h"
|
|
#include "systick.h"
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <gd32c10x_dma.h>
|
|
|
|
constexpr float ADC_SCALE = 3.3f / 4096.0f;
|
|
constexpr float TEMP_FACTOR = 1000.0f / 4.3f;
|
|
constexpr float VCC_FACTOR = 11.0f;
|
|
constexpr float CURRENT_FACTOR =
|
|
1.0f / 50.0f * 100.0f; // 50倍放大,100倍电流采样电阻, 单位mA
|
|
extern CAN_transmiter CAN0_transmiter;
|
|
extern FOC_Controlroller FOC;
|
|
sampler sampler; // 采样器初始化
|
|
u16 adc_value[6] = {0}; // 显式初始化数组元素为0
|
|
/*!
|
|
\brief configure the DMA peripheral
|
|
\param[in] none
|
|
\param[out] none
|
|
\retval none
|
|
*/
|
|
void dma_config(void) {
|
|
/* enable DMA0 clock */
|
|
rcu_periph_clock_enable(RCU_DMA0);
|
|
/* ADC_DMA_channel configuration */
|
|
dma_parameter_struct dma_data_parameter;
|
|
|
|
/* ADC DMA_channel configuration */
|
|
dma_deinit(DMA0, DMA_CH0);
|
|
|
|
/* initialize DMA single data mode */
|
|
dma_data_parameter.periph_addr = (uint32_t)(&ADC_RDATA(ADC0));
|
|
dma_data_parameter.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
|
|
dma_data_parameter.memory_addr = (uint32_t)(adc_value);
|
|
dma_data_parameter.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
|
|
dma_data_parameter.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
|
|
dma_data_parameter.memory_width = DMA_MEMORY_WIDTH_16BIT;
|
|
dma_data_parameter.direction = DMA_PERIPHERAL_TO_MEMORY;
|
|
dma_data_parameter.number = 6;
|
|
dma_data_parameter.priority = DMA_PRIORITY_HIGH;
|
|
dma_init(DMA0, DMA_CH0, &dma_data_parameter);
|
|
|
|
dma_circulation_enable(DMA0, DMA_CH0);
|
|
|
|
/* enable DMA channel */
|
|
dma_channel_enable(DMA0, DMA_CH0);
|
|
}
|
|
|
|
/*!
|
|
\brief configure the ADC peripheral
|
|
\param[in] none
|
|
\param[out] none
|
|
\retval none
|
|
*/
|
|
void adc_config(void) {
|
|
/* enable ADC clock */
|
|
rcu_periph_clock_enable(RCU_ADC0);
|
|
/* config ADC clock */
|
|
rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV4);
|
|
|
|
/* 复位ADC */
|
|
adc_deinit(ADC0);
|
|
/* ADC模式配置 */
|
|
adc_mode_config(ADC_MODE_FREE);
|
|
/* 使能ADC连续转换功能 */
|
|
adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, ENABLE);
|
|
/* 使能ADC扫描模式 */
|
|
adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE);
|
|
/* ADC数据右对齐配置 */
|
|
adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
|
|
/* 使能ADC温度传感器和内部参考电压 */
|
|
adc_tempsensor_vrefint_enable();
|
|
|
|
/* 配置ADC通道长度 */
|
|
adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 6);
|
|
/* 配置ADC规则通道 */
|
|
adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_16,
|
|
ADC_SAMPLETIME_55POINT5);
|
|
adc_regular_channel_config(ADC0, 1, ADC_CHANNEL_9, ADC_SAMPLETIME_28POINT5);
|
|
adc_regular_channel_config(ADC0, 2, ADC_CHANNEL_3, ADC_SAMPLETIME_28POINT5);
|
|
adc_regular_channel_config(ADC0, 3, ADC_CHANNEL_4, ADC_SAMPLETIME_28POINT5);
|
|
adc_regular_channel_config(ADC0, 4, ADC_CHANNEL_5, ADC_SAMPLETIME_28POINT5);
|
|
adc_regular_channel_config(ADC0, 5, ADC_CHANNEL_6, ADC_SAMPLETIME_28POINT5);
|
|
|
|
/* ADC触发配置 */
|
|
adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL,
|
|
ADC0_1_EXTTRIG_REGULAR_NONE);
|
|
adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);
|
|
|
|
/* 使能ADC接口 */
|
|
adc_enable(ADC0);
|
|
/* ADC校准 */
|
|
adc_calibration_enable(ADC0);
|
|
/* 使能ADC DMA功能 */
|
|
adc_dma_mode_enable(ADC0);
|
|
/* 使能ADC软件触发 */
|
|
adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);
|
|
}
|
|
/**
|
|
* @brief 采样类初始化
|
|
*/
|
|
u8 sampler::init() {
|
|
dma_config(); // DMA采样ADC初始化
|
|
adc_config(); // ADC初始化
|
|
delay_1ms(1);
|
|
// 初始采样
|
|
for (int i = 0; i < ADC_FILTER_SIZE + 1; i++) {
|
|
mcu_temp_samples[i] =
|
|
(1.43f - adc_value[0] * ADC_SCALE) * TEMP_FACTOR + 35.0f;
|
|
|
|
currU_samples[i] =
|
|
(adc_value[2] * ADC_SCALE + CURRENT_OFFSET) * CURRENT_FACTOR;
|
|
currV_samples[i] =
|
|
(adc_value[3] * ADC_SCALE + CURRENT_OFFSET) * CURRENT_FACTOR;
|
|
currW_samples[i] =
|
|
(adc_value[4] * ADC_SCALE + CURRENT_OFFSET) * CURRENT_FACTOR;
|
|
|
|
vbus_samples[i] = adc_value[5] * ADC_SCALE * VCC_FACTOR;
|
|
|
|
float R_now = (float)adc_value[1] / (4096 - (float)adc_value[1]) * 4700;
|
|
drv_temp_samples[i] =
|
|
1.0f / (0.00335f + 0.00025f * log(R_now / 10000.0f)) - 273.15f;
|
|
delay_1ms(1);
|
|
}
|
|
return 0;
|
|
}
|
|
/**
|
|
* @brief 获取MCU温度
|
|
* @return float
|
|
* 温度值,单位为摄氏度,范围应在-40到145之间,如果读取失败则返回0.0f
|
|
*/
|
|
float sampler::get_mcu_temp() {
|
|
tempora_sample = medium_filter(mcu_temp_samples, ADC_FILTER_SIZE + 1);
|
|
// 检查温度读数是否在有效范围内
|
|
if (tempora_sample < MIN_TEMP or tempora_sample > MAX_TEMP) {
|
|
return ERROR_ADC_VAL;
|
|
}
|
|
return tempora_sample;
|
|
}
|
|
|
|
/**
|
|
* @brief 获取电机驱动温度
|
|
* @return float 温度值,单位为摄氏度
|
|
*
|
|
* 通过热敏电阻分压电路计算温度值,使用Steinhart-Hart方程进行温度转换。
|
|
* 如果计算结果超出有效范围(-40到255摄氏度),则重新采样计算。
|
|
*/
|
|
float sampler::get_drv_temp() {
|
|
tempora_sample = medium_filter(drv_temp_samples, ADC_FILTER_SIZE + 1);
|
|
// 检查温度读数是否在有效范围内
|
|
if (tempora_sample < MIN_TEMP || tempora_sample > MAX_TEMP) {
|
|
return ERROR_ADC_VAL;
|
|
}
|
|
return tempora_sample;
|
|
}
|
|
|
|
/**
|
|
* @brief 获取电流值
|
|
* @param type 电流类型,'U'表示U相电流,'V'表示V相电流,'W'表示W相电流
|
|
* @return float 电流值,单位为安培
|
|
*
|
|
* 通过ADC采样计算各相电流值
|
|
*/
|
|
float sampler::get_current(char type) {
|
|
|
|
u8 idx = current_sample_index;
|
|
switch (type) {
|
|
case 'U': {
|
|
// return currU_samples[current_sample_index];
|
|
// tempora_sample = medium_filter(currU_samples, ADC_FILTER_SIZE + 1);
|
|
tempora_sample = currU_samples[idx];
|
|
break;
|
|
}
|
|
case 'V': {
|
|
// return currV_samples[current_sample_index];
|
|
// tempora_sample = medium_filter(currV_samples, ADC_FILTER_SIZE + 1);
|
|
tempora_sample = currV_samples[idx];
|
|
break;
|
|
}
|
|
case 'W': {
|
|
// return currW_samples[current_sample_index];
|
|
// tempora_sample = medium_filter(currW_samples, ADC_FILTER_SIZE + 1);
|
|
tempora_sample = currW_samples[idx];
|
|
break;
|
|
}
|
|
default: {
|
|
return ERROR_ADC_VAL;
|
|
}
|
|
}
|
|
if (tempora_sample < MIN_CURRENT or tempora_sample > MAX_CURRENT) {
|
|
return ERROR_ADC_VAL;
|
|
}
|
|
|
|
return tempora_sample;
|
|
}
|
|
|
|
/**
|
|
* @brief 获取供电电压
|
|
* @return float 电压值,单位为伏特
|
|
*
|
|
* 通过ADC采样计算供电电压值,使用迭代方式确保获取有效的电压读数。
|
|
* 如果计算结果超出有效范围(-40到255伏特),则重新采样计算。
|
|
*/
|
|
float sampler::get_vbus_voltage() {
|
|
tempora_sample = medium_filter(vbus_samples, ADC_FILTER_SIZE + 1);
|
|
// 检查电压读数是否在有效范围内
|
|
if (vbus_samples[ADC_FILTER_SIZE / 2] > MAX_VCC or
|
|
vbus_samples[ADC_FILTER_SIZE / 2] < MIN_VCC) {
|
|
return ERROR_ADC_VAL;
|
|
}
|
|
return tempora_sample;
|
|
}
|
|
|
|
/**
|
|
* @brief 更新MCU温度
|
|
*
|
|
* 通过ADC采样计算MCU温度值,使用Steinhart-Hart方程进行温度转换。
|
|
* 如果计算结果超出有效范围(-40到255摄氏度),则重新采样计算。
|
|
*/
|
|
void sampler::update_mcu_temp() {
|
|
memmove(mcu_temp_samples, mcu_temp_samples + 1,
|
|
ADC_FILTER_SIZE * sizeof(float));
|
|
mcu_temp_samples[ADC_FILTER_SIZE] =
|
|
(1.43f - adc_value[0] * ADC_SCALE) * TEMP_FACTOR + 35.0f;
|
|
}
|
|
|
|
/**
|
|
* @brief 更新MCU温度
|
|
*/
|
|
void sampler::update_drv_temp() {
|
|
memmove(drv_temp_samples, drv_temp_samples + 1,
|
|
ADC_FILTER_SIZE * sizeof(float));
|
|
float R_now = (float)adc_value[1] / (4096 - (float)adc_value[1]) * 4700;
|
|
drv_temp_samples[ADC_FILTER_SIZE] =
|
|
1.0f / (0.00335f + 0.00025f * log(R_now / 10000.0f)) - 273.15f;
|
|
}
|
|
|
|
/**
|
|
* @brief 更新相电流
|
|
*/
|
|
void sampler::update_current() {
|
|
u8 idx = current_sample_index;
|
|
extern FOC_Controller FOC;
|
|
// 优化计算
|
|
const float scale = ADC_SCALE * CURRENT_FACTOR;
|
|
const float offset = CURRENT_OFFSET * CURRENT_FACTOR;
|
|
|
|
currU_samples[idx] = adc_value[2] * scale + offset;
|
|
currV_samples[idx] = adc_value[3] * scale + offset;
|
|
currW_samples[idx] = adc_value[4] * scale + offset;
|
|
|
|
// 更新索引
|
|
current_sample_index++;
|
|
if (current_sample_index >= ADC_FILTER_SIZE - 1) {
|
|
current_sample_index = 0;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief 更新供电电压
|
|
*/
|
|
void sampler::update_vbus_voltage() {
|
|
memmove(vbus_samples, vbus_samples + 1, ADC_FILTER_SIZE * sizeof(float));
|
|
vbus_samples[ADC_FILTER_SIZE] = adc_value[5] * ADC_SCALE * VCC_FACTOR;
|
|
} |