hal_battery_NoPowerEnPin.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*********************************************************************
  2. * INCLUDES
  3. */
  4. #include "usr_config.h"
  5. #include "hal_battery.h"
  6. #include "cli.h"
  7. #define PRINTBLE 0
  8. #include "ble_comm.h"
  9. #include "exception.h"
  10. #include "ringframe.h"
  11. RINGFRAME_DEF(battlog, ringframe_size_4096);
  12. char logbuftemp[50];
  13. int logbuftemp_len = 0;
  14. #define log(...) {logbuftemp_len = sprintf(logbuftemp,__VA_ARGS__); while(ringframe_in(&battlog,logbuftemp,logbuftemp_len)!=0){ringframe_throw(&battlog);}}
  15. #include "hal_ble_client.h"
  16. char print_log = 0;
  17. void cb_BLE_Client_ERR(void* handle)
  18. {
  19. // BLE_Client_Rx_t* target = handle;
  20. DEBUG_LOG("cb_BLE_Client_ERR:%d,%d\n", 1, 1);
  21. print_log = 1;
  22. }
  23. int16_t ADC_GetValue(uint32_t channel)
  24. {
  25. int16_t temp = 0;
  26. ADC_Read(channel, &temp);
  27. return temp;
  28. }
  29. //typedef struct
  30. //{
  31. // void * p_buffer;
  32. // short size;
  33. // short index;
  34. // float sum;
  35. //} average_filter_t;
  36. //#define AVERAGE_FILTER_DEF( _name, _size ) \
  37. // float _name##_average_filter_buffer[(_size)]; \
  38. // average_filter_t _name= \
  39. // { \
  40. // .index = 0, \
  41. // .sum = 0.0f, \
  42. // .p_buffer = _name##_average_filter_buffer, \
  43. // .size = (_size), \
  44. // }
  45. //float average_filter_init(average_filter_t * p_avgf)
  46. //{
  47. // short i=0;
  48. // for(i=1;i<p_avgf->size;i++)
  49. // {
  50. // p_avgf->p_buffer[i]=0.0f;
  51. // }
  52. // p_avgf->index=0;
  53. // p_avgf->sum=0.0f;
  54. //}
  55. //float average_filter(average_filter_t * p_avgf,float value_in)
  56. //{
  57. // p_avgf->sum=0.0f;
  58. // p_avgf->p_buffer[p_avgf->index++]=value_in;
  59. //}
  60. battercb_t * battercb=NULL;
  61. void printbatter_cb(battercb_t *c,unsigned char *buff)
  62. {
  63. char bytes[256];
  64. int len=sprintf(bytes,"%f,%f,%f,%f,%f,%f,%f,%f,%d,%d,%d,%d\r\n",
  65. c->preBestResult_Voltage2power ,
  66. c->preBestResult_chargeV2P_f ,
  67. c->P_mAh,
  68. c->kg,
  69. c->P2 ,
  70. c->P1,
  71. c->Battery_capacity_mAh,
  72. c->adc_tp4056_power,
  73. c->init,
  74. c->sta ,
  75. c->chargeV2P_f_init,
  76. c->Voltage2power_init);
  77. SEGGER_RTT_Write(0,bytes, len);
  78. for(int i=0;i<sizeof(battercb_t);i++)
  79. {
  80. DEBUG_LOG("%X ",buff[i]);
  81. }
  82. DEBUG_LOG("\r\n");
  83. }
  84. //static __attribute__((at(0x2000FFB0))) unsigned char ram;
  85. void cb_init(void)
  86. {
  87. battercb = Except_Get_Battery_Record_Buff();
  88. if(battercb->init != 3)
  89. {
  90. battercb->init = 3;
  91. battercb->P1=100.0f;
  92. battercb->adc_tp4056_power=0;
  93. battercb->Battery_capacity_mAh=0;
  94. battercb->kg=1;
  95. battercb->P2=0;
  96. battercb->preBestResult_chargeV2P_f=0;
  97. battercb->preBestResult_Voltage2power=0;
  98. battercb->sta=0;
  99. battercb->P_mAh=0;
  100. battercb->Voltage2power_init=1;
  101. battercb->chargeV2P_f_init =1;
  102. }
  103. }
  104. //返回5V信号,有5V的话返回1,没有的话返回0
  105. static char charge_in(void)
  106. {
  107. uint32_t ch = nrf_gpio_pin_read(PIN_CHARGING);
  108. if (ch)
  109. {
  110. return 1;
  111. }
  112. else
  113. {
  114. return 0;
  115. }
  116. }
  117. static float filter(float value, float kg, float* preBestResult)
  118. {
  119. float new_v = value;
  120. new_v = *preBestResult * (1.0f - kg) + value * kg;
  121. *preBestResult = new_v;
  122. return new_v;
  123. }
  124. //鞋子ADC
  125. 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};
  126. static float interp1(float x)
  127. {
  128. int absx = (int)x - 320;
  129. float max = poo1o[absx + 1];
  130. float min = poo1o[absx];
  131. float temp = x - (float)absx - 320.0f;
  132. return (max - min) * temp + min;
  133. }
  134. //返回电压百分比
  135. static float Voltage2power(float mV)
  136. {
  137. float rev = 0;
  138. float k = 0;
  139. if (battercb->Voltage2power_init)
  140. {
  141. battercb->preBestResult_Voltage2power = mV / 10;
  142. battercb->Voltage2power_init = 0;
  143. }
  144. k = filter(mV / 10, 0.01, &battercb->preBestResult_Voltage2power);
  145. if (k < 320.0f)
  146. {
  147. rev = 0;
  148. }
  149. else if (k > 420.0f)
  150. {
  151. rev = 100;
  152. }
  153. else
  154. {
  155. rev = interp1(k);
  156. }
  157. return rev;
  158. }
  159. 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};
  160. static float interp1_chargeV2P(float x)
  161. {
  162. int absx = (int)x - 350;
  163. float max = chargeV2P[absx + 1];
  164. float min = chargeV2P[absx];
  165. float temp = x - (float)absx - 350.0f;
  166. return (max - min) * temp + min;
  167. }
  168. //返回电压百分比
  169. static float chargeV2P_f(float mV)
  170. {
  171. float rev = 0;
  172. float k = 0;
  173. if (battercb->chargeV2P_f_init)
  174. {
  175. battercb->preBestResult_chargeV2P_f = mV / 10;
  176. battercb->chargeV2P_f_init = 0;
  177. }
  178. k = filter(mV / 10, 0.05, &battercb->preBestResult_chargeV2P_f);
  179. if (k < 350.0f)
  180. {
  181. rev = 0;
  182. }
  183. else if (k > 415.0f)
  184. {
  185. rev = 100;
  186. }
  187. else
  188. {
  189. rev = interp1_chargeV2P(k);
  190. }
  191. return rev;
  192. }
  193. //返回电量百分比
  194. static float Voltage2mah(float mah, float storage_capacity)
  195. {
  196. return mah / storage_capacity * 100.0f;
  197. }
  198. //返回电压剩余绝对容量
  199. static float mah2Voltage(float P, float storage_capacity)
  200. {
  201. return storage_capacity * P / 100.0f;
  202. }
  203. static void Charge(float mV, float* mAh, float interval_s)
  204. {
  205. float A = mV / 3000.0f * 1.1f;
  206. float dmAh = A * 1000.0f * interval_s / 3600.0f;
  207. *mAh = *mAh + dmAh;
  208. }
  209. static float Power_management(float mV_Battery, float mV_Charge)
  210. {
  211. float storage_capacity = 350;
  212. switch (battercb->sta)
  213. {
  214. case 0:
  215. if (mV_Charge > 20)
  216. {
  217. battercb->sta = 2; //充电过程
  218. battercb->P1 = chargeV2P_f(mV_Battery);
  219. battercb->Battery_capacity_mAh=mah2Voltage(battercb->P1, storage_capacity);
  220. }
  221. else
  222. {
  223. battercb->P1 = Voltage2power(mV_Battery);
  224. battercb->sta = 1; //放电过程
  225. }
  226. break;
  227. case 1://放电
  228. if (mV_Charge > 20)
  229. {
  230. battercb->sta = 2; //充电过程
  231. battercb->kg = 0;
  232. // //解决插上充电没接电池状态下突然接上电池时电量不衔接的问题
  233. // if((mV_Battery<4000)&&(P1>95.0f))
  234. // {
  235. // P1=chargeV2P_f(mV_Battery);
  236. // }
  237. battercb->Battery_capacity_mAh=mah2Voltage(battercb->P1, storage_capacity);
  238. }
  239. else
  240. {
  241. battercb->P2 = Voltage2power(mV_Battery);
  242. if (battercb->P1 > battercb->P2) //过滤刚拔掉充电线时候的虚高
  243. {
  244. battercb->P1 = battercb->P2;
  245. }
  246. //解决充满电后充电器不拔出来的情况显示不到100%的情况
  247. if (charge_in() && (battercb->P1 > 95.0f))
  248. {
  249. battercb->P1 = 100.0f;
  250. }
  251. }
  252. break;
  253. case 2://充电
  254. if (mV_Charge < 20)
  255. {
  256. battercb->sta = 1; //放电过程
  257. //初始化滤波器波器
  258. battercb->preBestResult_Voltage2power = mV_Battery / 10;
  259. if (battercb->P1 > 99.1f)
  260. {
  261. battercb->P1 = 100.0f;
  262. }
  263. }
  264. else
  265. {
  266. //---------------------------------------------------
  267. Charge(mV_Charge, &battercb->Battery_capacity_mAh, 1);
  268. battercb->P_mAh = Voltage2mah(battercb->Battery_capacity_mAh, storage_capacity);
  269. //---------------------------------------------------
  270. battercb->P2 = chargeV2P_f(mV_Battery);
  271. // log("%f\n",P2);
  272. // SEGGER_RTT_Write(0,logbuftemp, logbuftemp_len);
  273. battercb->kg = mV_Charge / 1000.0f;
  274. if (battercb->kg > 1.0f)
  275. {
  276. battercb->kg = 1.0f;
  277. }
  278. battercb->P2 = (1.0f - battercb->kg) * battercb->P2 + battercb->kg * battercb->P_mAh;
  279. if (battercb->P1 < battercb->P2) //过滤刚插上充电线时候的虚低
  280. {
  281. battercb->P1 = battercb->P2;
  282. }
  283. if (battercb->P1 > 100)
  284. {
  285. battercb->P1 = 99.9;
  286. }
  287. }
  288. break;
  289. }
  290. return battercb->P1;
  291. }
  292. int16_t hal_GetBatttery_Adc(void){
  293. int16_t adcVal;
  294. adcVal = ADC_GetValue(PIN_ADC_BAT_CHANNEL);
  295. adcVal = ADC_RESULT_IN_MILLI_VOLTS(adcVal) * 5 / 3;
  296. return adcVal;
  297. }
  298. static void hal_battery_Process(void)
  299. {
  300. static int cprign = 0;
  301. #if PRINTBLE
  302. char buff[256];
  303. unsigned char len = 0;
  304. #endif
  305. int16_t adcVal;
  306. int16_t CHARGMEASURE;
  307. int16_t volTemp;
  308. int16_t volTemp_CHARGMEASURE;
  309. adcVal = ADC_GetValue(PIN_ADC_BAT_CHANNEL);
  310. volTemp = ADC_RESULT_IN_MILLI_VOLTS(adcVal) * 5 / 3; // 电池电压转换计算
  311. CHARGMEASURE = ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL);
  312. volTemp_CHARGMEASURE = ADC_RESULT_IN_MILLI_VOLTS(CHARGMEASURE);// 电池电压转换计算
  313. battercb->adc_tp4056_power = Power_management((float)volTemp, (float)volTemp_CHARGMEASURE);
  314. #if PRINTBLE
  315. len = sprintf(buff, "%4d ,%4d ,%4d,%f\r\n", TIME_GetTicks(), volTemp, volTemp_CHARGMEASURE, battercb->adc_tp4056_power);
  316. send_bytes_client((unsigned char*)buff, len);
  317. #endif
  318. if (cprign % 600 == 0)
  319. {
  320. log("%4d,%4d,%4d,%2.1f,%d,%2.1f,%2.1f,%2.1f\n", TIME_GetTicks(), volTemp, volTemp_CHARGMEASURE, battercb->adc_tp4056_power, battercb->sta, battercb->P_mAh, battercb->P2, battercb->kg);
  321. SEGGER_RTT_Write(0,logbuftemp, logbuftemp_len);
  322. logbuftemp[logbuftemp_len] = 0;
  323. Except_TxError(EXCEPT_DATA_BATTERY, logbuftemp);
  324. }
  325. cprign++;
  326. // cli_process(&clirtt);
  327. // uint8_t persent = 0;
  328. // persent = (uint8_t)(adc_tp4056_power+0.5f);
  329. // DEBUG_LOG("hal_battery_Process:%d,%d\n",persent,volTemp);
  330. if(print_log==1)
  331. {
  332. log("%4d,%4d,%4d,%2.1f,%d,%2.1f,%2.1f,%2.1f\n", TIME_GetTicks(), volTemp, volTemp_CHARGMEASURE, battercb->adc_tp4056_power, battercb->sta, battercb->P_mAh, battercb->P2, battercb->kg);
  333. SEGGER_RTT_Write(0,logbuftemp, logbuftemp_len);
  334. logbuftemp[logbuftemp_len] = 0;
  335. Except_TxError(EXCEPT_DATA_BATTERY, logbuftemp);
  336. print_log=2;
  337. }
  338. if (print_log==2)
  339. {
  340. unsigned char length = 0;
  341. while (ringframe_peek(&battlog, logbuftemp, &length) == 0)
  342. {
  343. if (send_bytes_client((unsigned char*)logbuftemp, length) != 0)
  344. {
  345. return;
  346. }
  347. ringframe_throw(&battlog);
  348. SEGGER_RTT_Write(0, logbuftemp, length);
  349. }
  350. print_log = 0;
  351. }
  352. }
  353. //返回的电量范围: 0~100 表示电量百分比
  354. uint8_t GetBatteryPersent(void)
  355. {
  356. uint8_t persent = 0;
  357. persent = (uint8_t)(battercb->adc_tp4056_power + 0.5f);
  358. // if(persent>95)persent=100;
  359. // else if((persent>85)&&(persent<=95))persent=90;
  360. // else if((persent>75)&&(persent<=85))persent=80;
  361. // else if((persent>65)&&(persent<=75))persent=70;
  362. // else if((persent>55)&&(persent<=65))persent=60;
  363. // else if((persent>45)&&(persent<=55))persent=50;
  364. // else if((persent>35)&&(persent<=45))persent=40;
  365. // else if((persent>25)&&(persent<=35))persent=30;
  366. // else if((persent>15)&&(persent<=25))persent=20;
  367. // else if((persent>10)&&(persent<=15))persent=10;
  368. if(persent>10)
  369. {
  370. persent=persent/10;
  371. persent=persent*10;
  372. }
  373. if (persent > 100)
  374. {
  375. persent = 100;
  376. }
  377. else if(persent<=0)
  378. {
  379. persent = 0;
  380. }
  381. return persent;
  382. }
  383. void hal_battery_init(void)
  384. {
  385. cb_init();
  386. hal_battery_Process();
  387. BLE_Client_Rx_Regist(BLE_ERR, cb_BLE_Client_ERR);
  388. Process_Start(1000, "hal_battery_Process", hal_battery_Process);
  389. }