chargeController/APP/functionalModule/Src/capture.c

483 lines
18 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "capture.h"
#include "HD_ADC.h"
/* 温度的adc值的个数 */
#define mosTemperADCLen 241
///* 3.3V参考电压温度adc值 */
//const uint16_t mosTemperADC[mosTemperADCLen] = {
// 3707, 3697, 3687, 3676, 3665, 3654, 3643, 3632, 3620, 3608, /* -20 - -15.5 ℃ */
// 3596, 3584, 3571, 3559, 3546, 3533, 3519, 3506, 3492, 3478, /* -15 - -10.5 ℃ */
// 3464, 3449, 3434, 3419, 3404, 3389, 3373, 3358, 3341, 3325, /* -10 - -5.5 ℃ */
// 3309, 3292, 3275, 3258, 3241, 3223, 3205, 3187, 3169, 3151, /* -5 - -0.5 ℃ */
// 3132, 3114, 3095, 3075, 3056, 3037, 3017, 2997, 2977, 2957, /* 0 - 4.5 ℃ */
// 2936, 2916, 2895, 2874, 2854, 2832, 2811, 2790, 2768, 2747, /* 9 - 9.5 ℃ */
// 2725, 2703, 2681, 2659, 2637, 2615, 2592, 2570, 2548, 2525, /* 10 - 14.5 ℃ */
// 2503, 2480, 2457, 2435, 2412, 2389, 2366, 2343, 2321, 2298, /* 15 - 19.5 ℃ */
// 2275, 2252, 2229, 2207, 2184, 2161, 2138, 2116, 2093, 2071, /* 20 - 24.5 ℃ */
// 2048, 2026, 2003, 1981, 1959, 1937, 1914, 1893, 1871, 1849, /* 25 - 29.5 ℃ */
// 1827, 1806, 1784, 1763, 1742, 1721, 1700, 1679, 1658, 1638, /* 30 - 34.5 ℃ */
// 1617, 1597, 1577, 1557, 1537, 1518, 1498, 1479, 1460, 1441, /* 35 - 39.5 ℃ */
// 1422, 1403, 1385, 1366, 1348, 1330, 1312, 1295, 1277, 1260, /* 40 - 44.5 ℃ */
// 1243, 1226, 1209, 1192, 1176, 1160, 1144, 1128, 1112, 1097, /* 45 - 49.5 ℃ */
// 1081, 1066, 1051, 1036, 1022, 1007, 993, 979, 965, 951, /* 50 - 54.5 ℃ */
// 938, 924, 911, 898, 885, 872, 860, 848, 835, 823, /* 55 - 59.5 ℃ */
// 811, 800, 788, 777, 765, 754, 743, 732, 722, 711, /* 60 - 64.5 ℃ */
// 701, 691, 681, 671, 661, 651, 642, 632, 623, 614, /* 65 - 69.5 ℃ */
// 605, 596, 588, 579, 571, 562, 554, 546, 538, 530, /* 70 - 74.5 ℃ */
// 522, 515, 507, 500, 493, 486, 478, 471, 465, 458, /* 75 - 79.5 ℃ */
// 451, 445, 438, 432, 426, 420, 414, 408, 402, 396, /* 80 - 84.5 ℃ */
// 390, 385, 379, 374, 368, 363, 358, 353, 348, 343, /* 85 - 89.5 ℃ */
// 338, 333, 328, 324, 319, 315, 310, 306, 301, 297, /* 90 - 94.5 ℃ */
// 293, 289, 285, 281, 277, 273, 269, 266, 262, 258, /* 95 - 99.5 ℃ */
// 255 /* 100 ℃ */
//};
/* 3.0V参考电压温度adc值 */
const uint16_t mosTemperADC[mosTemperADCLen] = {
4077, 4066, 4055, 4043, 4031, 4019, 4007, 3994, 3982, 3969, /* -20 - -15.5 ℃ */
3955, 3942, 3928, 3914, 3900, 3885, 3871, 3856, 3841, 3825, /* -15 - -10.5 ℃ */
3809, 3794, 3777, 3761, 3744, 3727, 3710, 3693, 3675, 3657, /* -10 - -5.5 ℃ */
3639, 3621, 3602, 3583, 3564, 3545, 3525, 3506, 3486, 3465, /* -5 - -0.5 ℃ */
3445, 3424, 3404, 3383, 3361, 3340, 3318, 3296, 3274, 3252, /* 0 - 4.5 ℃ */
3230, 3207, 3184, 3162, 3138, 3115, 3092, 3068, 3045, 3021, /* 5 - 9.5 ℃ */
2997, 2973, 2949, 2925, 2900, 2876, 2851, 2827, 2802, 2777, /* 10 - 14.5 ℃ */
2752, 2728, 2703, 2678, 2653, 2628, 2602, 2577, 2552, 2527, /* 15 - 19.5 ℃ */
2502, 2477, 2452, 2427, 2402, 2377, 2352, 2327, 2302, 2277, /* 20 - 24.5 ℃ */
2253, 2228, 2203, 2179, 2154, 2130, 2106, 2082, 2057, 2034, /* 25 - 29.5 ℃ */
2010, 1986, 1962, 1939, 1916, 1893, 1869, 1847, 1824, 1801, /* 30 - 34.5 ℃ */
1779, 1757, 1734, 1713, 1691, 1669, 1648, 1626, 1605, 1584, /* 35 - 39.5 ℃ */
1564, 1543, 1523, 1503, 1483, 1463, 1443, 1424, 1405, 1386, /* 40 - 44.5 ℃ */
1367, 1348, 1330, 1312, 1293, 1276, 1258, 1241, 1223, 1206, /* 45 - 49.5 ℃ */
1189, 1173, 1156, 1140, 1124, 1108, 1092, 1077, 1061, 1046, /* 50 - 54.5 ℃ */
1031, 1017, 1002, 988, 974, 960, 946, 932, 919, 905, /* 55 - 59.5 ℃ */
892, 879, 867, 854, 842, 829, 817, 806, 794, 782, /* 60 - 64.5 ℃ */
771, 760, 749, 738, 727, 716, 706, 696, 685, 675, /* 65 - 69.5 ℃ */
666, 656, 646, 637, 627, 618, 609, 600, 592, 583, /* 70 - 74.5 ℃ */
575, 566, 558, 550, 542, 534, 526, 519, 511, 504, /* 75 - 79.5 ℃ */
496, 489, 482, 475, 468, 461, 455, 448, 442, 435, /* 80 - 84.5 ℃ */
429, 423, 417, 411, 405, 399, 394, 388, 382, 377, /* 85 - 89.5 ℃ */
372, 366, 361, 356, 351, 346, 341, 336, 332, 327, /* 90 - 94.5 ℃ */
322, 318, 313, 309, 305, 300, 296, 292, 288, 284, /* 95 - 99.5 ℃ */
280 /* 100 ℃ */
};
// /* 2.5V参考电压温度adc值 */
// const uint16_t mosTemperADC[mosTemperADCLen] = {
// 4893, 4879, 4866, 4852, 4838, 4823, 4808, 4793, 4778, 4762, /* -20 - -15.5 ℃ */
// 4746, 4730, 4714, 4697, 4680, 4663, 4645, 4627, 4609, 4590, /* -15 - -10.5 ℃ */
// 4571, 4552, 4533, 4513, 4493, 4473, 4452, 4431, 4410, 4389, /* -10 - -5.5 ℃ */
// 4367, 4345, 4323, 4300, 4277, 4254, 4231, 4207, 4183, 4159, /* -5 - -0.5 ℃ */
// 4134, 4109, 4084, 4059, 4034, 4008, 3982, 3956, 3929, 3903, /* 0 - 4.5 ℃ */
// 3876, 3849, 3821, 3794, 3766, 3738, 3710, 3682, 3654, 3625, /* 5 - 9.5 ℃ */
// 3596, 3568, 3539, 3510, 3480, 3451, 3422, 3392, 3362, 3333, /* 10 - 14.5 ℃ */
// 3303, 3273, 3243, 3213, 3183, 3153, 3123, 3093, 3063, 3033, /* 15 - 19.5 ℃ */
// 3003, 2972, 2942, 2912, 2882, 2852, 2822, 2792, 2762, 2733, /* 20 - 24.5 ℃ */
// 2703, 2673, 2644, 2614, 2585, 2556, 2527, 2498, 2469, 2440, /* 25 - 29.5 ℃ */
// 2412, 2383, 2355, 2327, 2299, 2271, 2243, 2216, 2189, 2162, /* 30 - 34.5 ℃ */
// 2135, 2108, 2081, 2055, 2029, 2003, 1977, 1952, 1926, 1901, /* 35 - 39.5 ℃ */
// 1876, 1852, 1827, 1803, 1779, 1755, 1732, 1709, 1686, 1663, /* 40 - 44.5 ℃ */
// 1640, 1618, 1596, 1574, 1552, 1531, 1510, 1489, 1468, 1447, /* 45 - 49.5 ℃ */
// 1427, 1407, 1387, 1368, 1349, 1330, 1311, 1292, 1274, 1256, /* 50 - 54.5 ℃ */
// 1238, 1220, 1203, 1185, 1168, 1151, 1135, 1119, 1102, 1086, /* 55 - 59.5 ℃ */
// 1071, 1055, 1040, 1025, 1010, 995, 981, 967, 953, 939, /* 60 - 64.5 ℃ */
// 925, 912, 898, 885, 872, 860, 847, 835, 822, 810, /* 65 - 69.5 ℃ */
// 799, 787, 775, 764, 753, 742, 731, 721, 710, 700, /* 70 - 74.5 ℃ */
// 690, 679, 670, 660, 650, 641, 631, 622, 613, 604, /* 75 - 79.5 ℃ */
// 596, 587, 578, 570, 562, 554, 546, 538, 530, 523, /* 80 - 84.5 ℃ */
// 515, 508, 500, 493, 486, 479, 472, 466, 459, 452, /* 85 - 89.5 ℃ */
// 446, 440, 433, 427, 421, 415, 409, 404, 398, 392, /* 90 - 94.5 ℃ */
// 387, 381, 376, 371, 366, 361, 355, 350, 346, 341, /* 95 - 99.5 ℃ */
// 336 /* 100 ℃ */
// };
enum {
WORK_VOLT_NUM = 0,
DSG_CURR_NUM = 1,
PV_VOLT_IN_NUM = 2,
CHG_CURR_NUM = 3,
};
int16_t adcBuff[4];
/* 指向adcCapture中的inData16数组中的第一位也是最后一位 */
uint8_t pointer;
adcCapture WORK_VOLT_capture = {0};
adcCapture DSG_CURR_capture = {0};
adcCapture PV_VOLT_IN_capture = {0};
adcCapture CHG_CURR_capture = {0};
/* 电流电压采集转换的 */
static float P_CHG_CURR = 0;
static float P_PV_VOLT_OUT = 0;
static float P_DSG_CURR = 0;
static float P_PV1_VOLT_IN = 0;
static float P_PV_VOLT_IN1 = 0;
/* 2.5为adc的电压4095是2^adc的位数 - 1 */
// const float32_t Proportion = 2.5 / 4095;
const float32_t Proportion = 3.0 / 4095.0;
/* matlab生成的5阶滤波器系数 */
// const int firLen = 6;
// const float firLP[6] = {
// 0.01861755922, -0.1146286726, 0.5962908864, 0.5962908864, -0.1146286726,
// 0.01861755922
// };
/* matlab生成的3阶滤波器系数 */
const int firLen = 4;
const float firLP[4] = {
0.178709805, 0.3671073616, 0.3671073616, 0.178709805
};
void captureFirInit(void);
/**
* @brief 初始化adc
* @param
* @retval None
*/
void ADC_Capture_Init(void)
{
HD_adc_Init();
/* 将回调函数与dma的转换完成中断绑定 */
// hdma_adc1.XferCpltCallback = dmaFerCpltCallback;
HAL_Delay(100);
/* adc校准 */
HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED);
HAL_ADCEx_Calibration_Start(&hadc2, ADC_SINGLE_ENDED);
/* 初始化滤波器 */
captureFirInit();
HAL_TIM_Base_Start(&htim6);
HAL_ADC_Start_DMA(&hadc1, (uint32_t *)adcBuff, 4);
// /* 光伏充电输出电流比例,放大倍数*电阻 */
// P_CHG_CURR = (1.0 / (50 * (1 / (1 / 0.01 + 1 / 0.002)))) * Proportion;
// /* 充电控制盒输出电压比例,分压系数 */
// P_PV_VOLT_OUT = ((47.0 + 10.0) / 10.0) * Proportion;
// /* 放电电流采集电流倍数 */
// P_DSG_CURR = (1.0 / (50 * (1 / (1 / 0.002 * 2)))) * Proportion;
// /* 光伏板输出电压比例 */
// P_PV1_VOLT_IN = ((47.0 + 4.7) / 4.7) * Proportion;
// /* 系统电源电压比例 */
// P_PV_VOLT_IN1 = ((47 + 4.7) / 4.7) * Proportion;
}
/**
* @brief 初始化电流电压转换的比例(及放大或者缩小倍数)
* @param
* @retval None
*/
void proportionalInt(int mode)
{
/* 仅充当电源盒 */
if (mode) {
/* 光伏充电输出电流比例,放大倍数*电阻 */
P_CHG_CURR = (1.0 / (50 * (1 / (1 / 0.01 + 1 / 0.002)))) * Proportion;
/* 充电控制盒输出电压比例,分压系数 */
P_PV_VOLT_OUT = ((47.0 + 10.0) / 10.0) * Proportion;
/* 放电电流采集电流倍数 */
P_DSG_CURR = (1.0 / (50 * (1 / (1 / 0.002 * 2)))) * Proportion;
/* 光伏板输出电压比例 */
P_PV1_VOLT_IN = ((47.0 + 4.7) / 4.7) * Proportion;
/* 系统电源电压比例 */
P_PV_VOLT_IN1 = ((47 + 4.7) / 4.7) * Proportion;
}
/* 电源盒外还有网关功能 */
else {
/* 光伏充电输出电流比例,放大倍数*电阻 */
P_CHG_CURR = (1.0 / (50 * 0.005)) * Proportion;
/* 光伏充电输出电压比例,分压系数 */
P_PV_VOLT_OUT = ((47.0 + 4.7) / 4.7) * Proportion;
/* 放电电流采集电流倍数 */
P_DSG_CURR = (1.0 / (50 * 0.005)) * Proportion;
/* 光伏1开路输出电压比例 */
P_PV1_VOLT_IN = ((47.0 + 4.7) / 4.7) * Proportion;
/* 系统电源电压比例 */
P_PV_VOLT_IN1 = ((47 + 4.7) / 4.7) * Proportion;
}
}
#define N 4
/**
* @brief 中位值平均滤波
* @param funtion 获取ADC的函数
* @param ADC_Channel ADC通道
* @retval None
*/
static int16_t middleAverageFilter(int16_t funtion(uint32_t Channel), uint32_t ADC_Channel)
{
int16_t i,j,k;
int16_t temp,sum = 0;
int16_t value_buf[N];
for (i = 0; i < N; ++i) {
value_buf[i] = funtion(ADC_Channel);
}
/*从小到大冒泡排序*/
for(j = 0; j < N-1; ++j) {
for (k = 0; k < N-j-1; ++k) {
if(value_buf[k] > value_buf[k+1]) {
temp = value_buf[k];
value_buf[k] = value_buf[k+1];
value_buf[k+1] = temp;
}
}
}
for(i = 1; i < N-1; ++i) {
sum += value_buf[i];
}
return sum / (N-2);
}
/**
* @brief 获取adc2的采集值
* @param Channel ADC通道
* @retval None
*/
static int16_t ADC2_Capture(uint32_t Channel)
{
return middleAverageFilter(get_adc2Value, Channel);
}
// /**
// * @brief 获取adc1的采集值
// * @param Channel ADC通道
// * @retval None
// */
// static int16_t ADC1_Capture(uint32_t Channel)
// {
// return middleAverageFilter(get_adc1Value, Channel);
// }
/**
* @brief 得到充电电流
* @param
* @retval I 电流值
*/
float get_CHG_CURR(void)
{
float I;
I = CHG_CURR_capture.outData * P_CHG_CURR;
// I = ADC1_Capture(ADC_CHANNEL_11) * P_CHG_CURR;
#ifdef enable_Printf_VI
debug("\n CHG_CURR ADC : %d \n", (int)CHG_CURR_capture.outDataF);
debug(" CHG_CURR I : %f \n", I);
#endif
return I;
}
/**
* @brief 得到光伏输出电压
* @param
* @retval V 电压值
*/
float get_PV_VOLT_OUT(void)
{
float V;
V = WORK_VOLT_capture.outData * P_PV_VOLT_OUT;
// V = ADC1_Capture(ADC_CHANNEL_7) * P_PV_VOLT_OUT;
#ifdef enable_Printf_VI
debug("\n PV_VOLT_OUT ADC : %d \n", (int)WORK_VOLT_capture.outDataF);
debug(" PV_VOLT_OUT V : %f \n", V);
#endif
return V;
}
/**
* @brief 得到放电电流
* @param
* @retval I 电流值
*/
float get_DSG_CURR(void)
{
float I;
I = DSG_CURR_capture.outData * P_DSG_CURR;
// I = ADC1_Capture(ADC_CHANNEL_8) * P_DSG_CURR;
#ifdef enable_Printf_VI
debug("\n DSG_CURR ADC : %d \n", (int)DSG_CURR_capture.outDataF);
debug(" DSG_CURR I : %f \n", I);
#endif
return I;
}
/**
* @brief 得到光伏电压
* @param
* @retval V 电压值
*/
float get_PV1_VOLT_IN(void)
{
float V;
V = PV_VOLT_IN_capture.outData * P_PV1_VOLT_IN;
// V = ADC1_Capture(ADC_CHANNEL_15) * P_PV1_VOLT_IN;
#ifdef enable_Printf_VI
debug("\n PV1_VOLT_IN ADC : %d \n", (int)PV_VOLT_IN_capture.outDataF);
debug(" PV1_VOLT_IN V1 : %f \n", V);
#endif
return V;
}
/**
* @brief 得到系统电压电压
* @param
* @retval V 电压值
*/
float get_PV_VOLT_IN1(void)
{
float V;
uint16_t V_ADC;
V_ADC = ADC2_Capture(SYS_VOLT_IN_CHANNEL);
V = (float)(V_ADC) * P_PV_VOLT_IN1;
#ifdef enable_Printf_VI
debug("\n PV_VOLT_IN1 ADC : %d \n", V_ADC);
debug(" PV_VOLT_IN1 V : %f \n", V);
#endif
return V;
}
//const float Rp = 10000.0; //10K
//const float T2 = (273.15+25.0);//T2
//const float Bx = 3950.0;//B
//const float Bx = 3435.0;//B
//const float Ka = 273.15;
/**
* @brief 得到温度
* @param
* @retval V 电压值
*/
float get_MOSFET_Temper(void)
{
float T = 0;
uint16_t T_ADC;
T_ADC = ADC2_Capture(MOSFET_Temper_CHANNEL);
for (int i = 0; i < mosTemperADCLen; ++i) {
if (T_ADC >= mosTemperADC[i]) {
T = -20 + i * 0.5;
break;
}
}
#ifdef enable_Printf_VI
debug("\n MOSFET_Temper ADC : %d \n", T_ADC);
debug(" MOSFET_Temper T : %f \n", T);
#endif
return T;
}
/**
* @brief adc转换并传输完成后进入该回调函数
* @param hdma dma
* @retval None
*/
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hdma)
{
if (hdma->Instance == ADC1) {
WORK_VOLT_capture.totalInData -= WORK_VOLT_capture.inData16[pointer];
DSG_CURR_capture.totalInData -= DSG_CURR_capture.inData16[pointer];
PV_VOLT_IN_capture.totalInData -= PV_VOLT_IN_capture.inData16[pointer];
CHG_CURR_capture.totalInData -= CHG_CURR_capture.inData16[pointer];
WORK_VOLT_capture.inData16[pointer] = adcBuff[WORK_VOLT_NUM];
DSG_CURR_capture.inData16[pointer] = adcBuff[DSG_CURR_NUM];
PV_VOLT_IN_capture.inData16[pointer] = adcBuff[PV_VOLT_IN_NUM];
CHG_CURR_capture.inData16[pointer] = adcBuff[CHG_CURR_NUM];
WORK_VOLT_capture.totalInData += WORK_VOLT_capture.inData16[pointer];
DSG_CURR_capture.totalInData += DSG_CURR_capture.inData16[pointer];
PV_VOLT_IN_capture.totalInData += PV_VOLT_IN_capture.inData16[pointer];
CHG_CURR_capture.totalInData += CHG_CURR_capture.inData16[pointer];
pointer++;
if (pointer >= indata16_size) {
pointer = 0;
}
arm_copy_f32(WORK_VOLT_capture.IODataF + 1, WORK_VOLT_capture.IODataF, 3);
arm_copy_f32(DSG_CURR_capture.IODataF + 1, DSG_CURR_capture.IODataF, 3);
arm_copy_f32(PV_VOLT_IN_capture.IODataF + 1, PV_VOLT_IN_capture.IODataF, 3);
arm_copy_f32(CHG_CURR_capture.IODataF + 1, CHG_CURR_capture.IODataF, 3);
WORK_VOLT_capture.IODataF[3] = (float32_t)WORK_VOLT_capture.totalInData / indata16_size;
DSG_CURR_capture.IODataF[3] = (float32_t)DSG_CURR_capture.totalInData / indata16_size;
PV_VOLT_IN_capture.IODataF[3] = (float32_t)PV_VOLT_IN_capture.totalInData / indata16_size;
CHG_CURR_capture.IODataF[3] = (float32_t)CHG_CURR_capture.totalInData / indata16_size;
}
}
static arm_fir_instance_f32 armFirInstanceF32;
static float32_t *inputF32, *outputF32;
static uint32_t blockSize = 1;
static float32_t firStateF32[4]; /* 状态缓存 */
static float32_t outputf;
void captureFirInit(void)
{
/* 初始化结构体 */
arm_fir_init_f32(&armFirInstanceF32,
firLen,
(float_t *)&firLP[0],
&firStateF32[0],
blockSize);
}
/**
* @brief 将采集得到的adc进行滤波
* @param None
* @retval None
*/
void adcCaptureFir(void)
{
/* 初始化输入输出缓存指针 */
inputF32 = &WORK_VOLT_capture.IODataF[0];
outputF32 = &outputf;
arm_fir_f32(&armFirInstanceF32, inputF32, outputF32, blockSize);
WORK_VOLT_capture.outData = (int16_t)outputf;
/* 初始化输入输出缓存指针 */
inputF32 = &DSG_CURR_capture.IODataF[0];
outputF32 = &outputf;
arm_fir_f32(&armFirInstanceF32, inputF32, outputF32, blockSize);
DSG_CURR_capture.outData = (int16_t)outputf;
/* 初始化输入输出缓存指针 */
inputF32 = &PV_VOLT_IN_capture.IODataF[0];
outputF32 = &outputf;
arm_fir_f32(&armFirInstanceF32, inputF32, outputF32, blockSize);
PV_VOLT_IN_capture.outData = (int16_t)outputf;
/* 初始化输入输出缓存指针 */
inputF32 = &CHG_CURR_capture.IODataF[0];
outputF32 = &outputf;
arm_fir_f32(&armFirInstanceF32, inputF32, outputF32, blockSize);
CHG_CURR_capture.outData = (int16_t)outputf;
}