|
@@ -1,300 +0,0 @@
|
|
|
-/*********************************************************************
|
|
|
- * INCLUDES
|
|
|
- */
|
|
|
-#include "usr_config.h"
|
|
|
-#include "hal_battery.h"
|
|
|
-#include "cli.h"
|
|
|
-#define PRINTBLE 0
|
|
|
-
|
|
|
-#include "ble_comm.h"
|
|
|
-#include "exception.h"
|
|
|
-
|
|
|
-#include "ringframe.h"
|
|
|
-RINGFRAME_DEF(battlog,ringframe_size_4096);
|
|
|
-char logbuftemp[50];
|
|
|
-int logbuftemp_len=0;
|
|
|
-#define log(...) {logbuftemp_len = sprintf(logbuftemp,__VA_ARGS__); ringframe_in(&battlog,logbuftemp,logbuftemp_len);}
|
|
|
-
|
|
|
-#include "hal_ble_client.h"
|
|
|
-
|
|
|
-char print_log=0;
|
|
|
-void cb_BLE_Client_ERR(void* handle)
|
|
|
-{
|
|
|
- BLE_Client_Rx_t *target = handle;
|
|
|
- DEBUG_LOG("cb_BLE_Client_ERR:%d,%d\n",1,1);
|
|
|
- print_log=1;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-int16_t ADC_GetValue(uint32_t channel)
|
|
|
-{
|
|
|
- int16_t temp=0;
|
|
|
- ADC_Read(channel, &temp);
|
|
|
- return temp;
|
|
|
-}
|
|
|
-
|
|
|
-//返回5V信号,有5V的话返回1,没有的话返回0
|
|
|
-static char charge_in(void)
|
|
|
-{
|
|
|
- uint32_t ch = nrf_gpio_pin_read(PIN_CHARGING);
|
|
|
- if(ch)return 1;
|
|
|
- else return 0;
|
|
|
-}
|
|
|
-
|
|
|
-static float filter(float value, float kg, float* preBestResult)
|
|
|
-{
|
|
|
- float new_v = value;
|
|
|
- new_v = *preBestResult * (1.0f - kg) + value * kg;
|
|
|
- *preBestResult = new_v;
|
|
|
- return new_v;
|
|
|
-}
|
|
|
-
|
|
|
-//负载测量仪
|
|
|
-//const float poo1o[]={5.10168165379740,5.34733044915476,5.61704724780813,5.91504952455148,6.24538762712249,6.61232594416931,7.01953712767654,7.46935265033756,7.96335122494516,8.50358200192486,9.09281468086492,9.73412484740972,10.4325753748233,11.1947240789575,12.0279998795062,12.9374916137117,13.9260172775225,14.9884510507725,16.1112400764950,17.2842706470249,18.5004202529463,19.7491314797588,21.0185653416731,22.2986943158191,23.5786963439054,24.8484876852771,26.0960349502041,27.3050010170951,28.4610454562188,29.5600116621742,30.6034682628590,31.5984702746944,32.5538106818571,33.4783670170583,34.3806559960012,35.2670128624846,36.1402606107567,36.9999854839693,37.8491813155179,38.6980882892879,39.5608382688438,40.4501589172903,41.3790038698800,42.3638365959298,43.4226235933126,44.5726867709324,45.8261573855592,47.1820084404052,48.6371187751347,50.1935842257010,51.8472994098979,53.5916193269800,55.4227167186607,57.3375320331711,59.3313953215555,61.3855139357523,63.4578870950691,65.4854598022728,67.4115262370806,69.2059495638218,70.8663781140949,72.4135268452713,73.8842849282509,75.3217897769173,76.7641290674375,78.2395953684845,79.7625546270391,81.3527328594521,83.0420141741465,84.8459239717299,86.7331023820235,88.6178189933847,90.3918821756187,91.9708771194967,93.3175662114905,94.4438984171043,95.3852594026215,96.1782551857171,96.8552759200241,97.4399007649065,97.9470337398631,98.3861216083791,98.7637272009236,99.0856995099079,99.3556936180377,99.5764952915733,99.7517164714185,99.8847888907893,99.9783503988427,100.038705667285,100.073274028685,100.088914482705,100.091844988568,100.087235862329,100.078726223926,100.068495717630,100.057471632431,100.046085695070,100.034540863310,100.022996031552,100.011610094200};
|
|
|
-
|
|
|
-//鞋子ADC
|
|
|
-static const float poo1o[] = {0, 0, 0.0279893723606430, 0.174605323652602, 0.325796538285416, 0.495164949358988, 0.661918800578876, 0.829024800123971, 1.00225498989324, 1.17936073685608, 1.37258677752597, 1.56525700069634, 1.78680072433224, 2.00361106262195, 2.24466616203811, 2.46699160701705, 2.77834696254638, 3.12186809827754, 3.58442625993982, 4.15025435558636, 4.75855743544068, 5.51189718744822, 6.35834306864975, 7.38461196888009, 8.48997478633724, 9.43096936165977, 10.3817319764220, 11.4116388420216, 12.3939372566211, 13.5048186806524, 14.5904959858255, 15.5237940825920, 16.4790857938893, 17.8137595522187, 18.9982251103467, 20.3392608271850, 21.5817542329461, 22.7218253119165, 23.9444316340532, 25.2939077624602, 26.6264082603126, 27.6802415218000, 29.0022881606974, 30.1783424265851, 31.1179209268523, 32.2887764986448, 33.3732790985050, 34.2380544358441, 35.2041112278740, 36.0163848326001, 36.8624779801428, 37.6634899287154, 38.5186413495501, 39.4878256764553, 40.2471232681709, 41.2081417271725, 42.3322924899204, 43.7047997876243, 44.9058976548061, 46.5044971286874, 47.8927266715832, 49.8558978793141, 51.9022338412845, 54.2586141300707, 56.3903798469888, 58.7696803719223, 60.8764981712366, 62.2358527791606, 63.8383633243999, 65.5021323737117, 67.1556090613014, 69.0159229136298, 70.1420773342446, 71.2282683025524, 72.4548338447843, 73.6556507850819, 74.8128040906371, 75.8695501837768, 77.1323517287879, 78.6365237973046, 80.3752005495001, 82.8468947240450, 86.6163997907370, 91.2910588313494, 93.9702969410882, 95.4930183746766, 96.9114001488224, 97.8493292727541, 98.7169169431273, 99.3270162091455, 99.6869018017068, 99.9917942993789, 99.9689500363163, 99.97, 99.98, 99.98, 99.98, 99.98, 99.99, 99.99, 99.99};
|
|
|
-static float interp1(float x)
|
|
|
-{
|
|
|
- int absx = (int)x - 320;
|
|
|
- float max = poo1o[absx + 1];
|
|
|
- float min = poo1o[absx];
|
|
|
- float temp = x - (float)absx - 320.0f;
|
|
|
- return (max - min) * temp + min;
|
|
|
-}
|
|
|
-//返回电压百分比
|
|
|
-static float preBestResult_Voltage2power = 0;
|
|
|
-static float Voltage2power(float mV)
|
|
|
-{
|
|
|
- float rev = 0;
|
|
|
- float k = 0;
|
|
|
- static char init = 1;
|
|
|
- if (init)
|
|
|
- {
|
|
|
- preBestResult_Voltage2power = mV / 10;
|
|
|
- init = 0;
|
|
|
- }
|
|
|
- k = filter(mV / 10, 0.01, &preBestResult_Voltage2power);
|
|
|
- if (k < 320.0f)
|
|
|
- {
|
|
|
- rev = 0;
|
|
|
- }
|
|
|
- else if (k > 420.0f)
|
|
|
- {
|
|
|
- rev = 100;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- rev = interp1(k);
|
|
|
- }
|
|
|
- return rev;
|
|
|
-}
|
|
|
-
|
|
|
-static const float chargeV2P[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0.606184684199367, 0.938307744774148, 1.30769659743727, 1.87378869014097, 2.39210582883903, 2.92471759425620, 3.79782257541779, 4.54241638183623, 5.09886597521223, 5.98930945903392, 6.49613916536905, 7.57967916311061, 8.42757721701290, 9.26589943580526, 10.3408809248851, 11.4278317273525, 12.3098089504989, 13.8095299712012, 15.4373756551577, 16.8805115746597, 18.4871688551768, 20.0578462608782, 21.6336381358003, 22.9916066660508, 24.4652374859292, 25.6565902258290, 26.8453581267414, 27.9070922567514, 28.9713295466563, 30.0209078713242, 30.9380166385130, 32.0419258329631, 33.1827748787620, 33.8691297160915, 34.8949274023278, 35.8840553861847, 36.9638277084030, 37.8687458690322, 38.9939274175310, 40.4336758333268, 41.1114865403869, 42.6160733592240, 44.2493873543177, 45.6026105469954, 47.4664557187522, 49.0734043728910, 51.2998758679562, 52.8880367841713, 54.7587352342972, 57.0178625862682, 58.8608281071146, 60.3657347075748, 62.4281274954232, 64.3086188700345, 65.8044424890286, 68.6475622104224, 72.7352289117192, 76.9893033499930, 84.0930819869950, 92.2923770276700, 96.3286261252036, 99.7542287364423};
|
|
|
-static float interp1_chargeV2P(float x)
|
|
|
-{
|
|
|
- int absx = (int)x - 350;
|
|
|
- float max = chargeV2P[absx + 1];
|
|
|
- float min = chargeV2P[absx];
|
|
|
- float temp = x - (float)absx - 350.0f;
|
|
|
- return (max - min) * temp + min;
|
|
|
-}
|
|
|
-//返回电压百分比
|
|
|
-static float preBestResult_chargeV2P_f = 0;
|
|
|
-static float chargeV2P_f(float mV)
|
|
|
-{
|
|
|
- float rev = 0;
|
|
|
- float k = 0;
|
|
|
- static char init = 1;
|
|
|
- if (init)
|
|
|
- {
|
|
|
- preBestResult_chargeV2P_f = mV / 10;
|
|
|
- init = 0;
|
|
|
- }
|
|
|
- k = filter(mV / 10, 0.05, &preBestResult_chargeV2P_f);
|
|
|
- if (k < 350.0f)
|
|
|
- {
|
|
|
- rev = 0;
|
|
|
- }
|
|
|
- else if (k > 420.0f)
|
|
|
- {
|
|
|
- rev = 100;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- rev = interp1_chargeV2P(k);
|
|
|
- }
|
|
|
- return rev;
|
|
|
-}
|
|
|
-
|
|
|
-//返回电量百分比
|
|
|
-static float Voltage2mah(float mah, float storage_capacity)
|
|
|
-{
|
|
|
- return mah / storage_capacity * 100.0f;
|
|
|
-}
|
|
|
-//返回电压剩余绝对容量
|
|
|
-static float mah2Voltage(float P, float storage_capacity)
|
|
|
-{
|
|
|
- return storage_capacity * P / 100.0f;
|
|
|
-}
|
|
|
-
|
|
|
-static void Charge(float mV, float* mAh, float interval_s)
|
|
|
-{
|
|
|
- float A = mV / 3000.0f * 1.1f;
|
|
|
- float dmAh = A * 1000.0f * interval_s / 3600.0f;
|
|
|
- *mAh = *mAh + dmAh;
|
|
|
-}
|
|
|
-static char sta = 0;float P_mAh = 0;static float kg = 0;
|
|
|
-static float P2 = 0;
|
|
|
-static float Power_management(float mV_Battery, float mV_Charge)
|
|
|
-{
|
|
|
- static float P1 = 100;
|
|
|
-
|
|
|
-
|
|
|
- float storage_capacity = 350;
|
|
|
- static float mAh = 0;
|
|
|
-
|
|
|
-
|
|
|
- switch (sta)
|
|
|
- {
|
|
|
- case 0:
|
|
|
- if (mV_Charge > 20)
|
|
|
- {
|
|
|
- sta = 2; //充电过程
|
|
|
- P1=chargeV2P_f(mV_Battery);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- P1 = Voltage2power(mV_Battery);
|
|
|
- sta = 1; //放电过程
|
|
|
- }
|
|
|
- break;
|
|
|
- case 1://放电
|
|
|
- if (mV_Charge > 20)
|
|
|
- {
|
|
|
- sta = 2; //充电过程
|
|
|
- kg = 0;
|
|
|
-
|
|
|
- //解决插上充电没接电池状态下突然接上电池时电量不衔接的问题
|
|
|
- if((mV_Battery<4000)&&(P1>95.0f))
|
|
|
- {
|
|
|
- P1=chargeV2P_f(mV_Battery);
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- P2 = Voltage2power(mV_Battery);
|
|
|
- if (P1 > P2) //过滤刚拔掉充电线时候的虚高
|
|
|
- {
|
|
|
- P1 = P2;
|
|
|
- }
|
|
|
-
|
|
|
- //解决充满电后充电器不拔出来的情况显示不到100%的情况
|
|
|
- if(charge_in()&&(P1>95.0f))
|
|
|
- {
|
|
|
- P1=100.0f;
|
|
|
- }
|
|
|
- }
|
|
|
- break;
|
|
|
- case 2://充电
|
|
|
- if (mV_Charge < 20)
|
|
|
- {
|
|
|
- sta = 1; //放电过程
|
|
|
- //初始化滤波器波器
|
|
|
- preBestResult_Voltage2power = mV_Battery / 10;
|
|
|
- if (P1 > 99.1f)
|
|
|
- {
|
|
|
- P1 = 100.0f;
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- //---------------------------------------------------
|
|
|
- mAh = mah2Voltage(P1, storage_capacity);
|
|
|
- Charge(mV_Charge, &mAh, 1);
|
|
|
- P_mAh = Voltage2mah(mAh, storage_capacity);
|
|
|
- P2 = chargeV2P_f(mV_Battery);
|
|
|
- kg = mV_Charge / 1000.0f;
|
|
|
- if (kg > 1.0f)
|
|
|
- {
|
|
|
- kg = 1.0f;
|
|
|
- }
|
|
|
- P2 = (1.0f - kg) * P2 + kg * P_mAh;
|
|
|
- if (P1 < P2) //过滤刚插上充电线时候的虚低
|
|
|
- {
|
|
|
- P1 = P2;
|
|
|
- }
|
|
|
- if (P1 > 100)
|
|
|
- {
|
|
|
- P1 = 99.9;
|
|
|
- }
|
|
|
- }
|
|
|
- break;
|
|
|
- }
|
|
|
- return P1;
|
|
|
-}
|
|
|
-
|
|
|
-static float adc_tp4056_power = 0;
|
|
|
-
|
|
|
-static void hal_battery_Process(void)
|
|
|
-{
|
|
|
- static int cprign=0;
|
|
|
- #if PRINTBLE
|
|
|
- char buff[256];
|
|
|
- unsigned char len = 0;
|
|
|
- #endif
|
|
|
- int16_t adcVal;
|
|
|
- int16_t CHARGMEASURE;
|
|
|
- int16_t volTemp;
|
|
|
- int16_t volTemp_CHARGMEASURE;
|
|
|
-
|
|
|
- adcVal = ADC_GetValue(PIN_ADC_BAT_CHANNEL);
|
|
|
- volTemp = ADC_RESULT_IN_MILLI_VOLTS(adcVal) * 5 / 3; // 电池电压转换计算
|
|
|
- CHARGMEASURE = ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL);
|
|
|
- volTemp_CHARGMEASURE = ADC_RESULT_IN_MILLI_VOLTS(CHARGMEASURE);// 电池电压转换计算
|
|
|
- adc_tp4056_power = Power_management((float)volTemp, (float)volTemp_CHARGMEASURE);
|
|
|
- #if PRINTBLE
|
|
|
- len = sprintf(buff, "%4d ,%4d ,%4d,%f\r\n",TIME_GetTicks(), volTemp, volTemp_CHARGMEASURE, adc_tp4056_power);
|
|
|
- send_bytes_client((unsigned char*)buff, len);
|
|
|
- #endif
|
|
|
-
|
|
|
- if(cprign%3==0){
|
|
|
- log("%4d,%4d,%4d,%2.1f,%d,%2.1f,%2.1f,%2.1f\n",TIME_GetTicks(),volTemp, volTemp_CHARGMEASURE, adc_tp4056_power,sta,P_mAh,P2,kg);
|
|
|
- logbuftemp[logbuftemp_len]=0;
|
|
|
- Except_TxError(EXCEPT_DATA_BATTERY,logbuftemp);
|
|
|
- }cprign++;
|
|
|
-// cli_process(&clirtt);
|
|
|
-// uint8_t persent = 0;
|
|
|
-// persent = (uint8_t)(adc_tp4056_power+0.5f);
|
|
|
-// DEBUG_LOG("hal_battery_Process:%d,%d\n",persent,volTemp);
|
|
|
- if(print_log)
|
|
|
- {
|
|
|
- unsigned char length=0;
|
|
|
- while(ringframe_out(&battlog,logbuftemp,&length)==0)
|
|
|
- {
|
|
|
- while(send_bytes_client((unsigned char*)logbuftemp, length)!=0);
|
|
|
- SEGGER_RTT_Write(0,logbuftemp, length);
|
|
|
- }
|
|
|
- print_log=0;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-//返回的电量范围: 0~100 表示电量百分比
|
|
|
-uint8_t GetBatteryPersent(void)
|
|
|
-{
|
|
|
- uint8_t persent = 0;
|
|
|
- persent = (uint8_t)(adc_tp4056_power+0.5f);
|
|
|
- if(persent > 100)persent =100;
|
|
|
- return persent;
|
|
|
-}
|
|
|
-void hal_battery_init(void){
|
|
|
- hal_battery_Process();
|
|
|
- BLE_Client_Rx_Regist(BLE_ERR,cb_BLE_Client_ERR);
|
|
|
- Process_Start(1000,"hal_battery_Process",hal_battery_Process);
|
|
|
-}
|
|
|
-
|