Temp.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /**
  2. * @file Power.c
  3. * @author Wang, Zhiyu(wangzy49@midea.com)
  4. * @brief Power of ebike
  5. * @version 0.1
  6. * @date 2021-09-29
  7. *
  8. * @copyright Copyright (c) 2021
  9. *
  10. */
  11. /************************************************************************
  12. Beginning of File, do not put anything above here except notes
  13. Compiler Directives:
  14. *************************************************************************/
  15. #include "syspar.h"
  16. #include "typedefine.h"
  17. #include "mathtool.h"
  18. #include "Temp.h"
  19. /******************************
  20. *
  21. * Parameter
  22. *
  23. ******************************/
  24. //b
  25. SLONG tmp_CurCalibCoef[CURCALIBNUM] ={
  26. 10017, // -20~0
  27. 32891, // 0-19 Q10
  28. 55994, // 20-39
  29. 79315, // 40-59
  30. 102904, // 60-79
  31. 126058, // 80-99
  32. 151406, // 100-119
  33. 179931, // 120-139
  34. };
  35. //value
  36. static SWORD PCB_swRTempTab[TEMPNUM] = {
  37. 10102, // 0.01kOnm IPM voltage at -20 C
  38. 3318, // 0.01kOnm IPM voltage at 0 C
  39. 1252, // 0.01kOnm IPM voltage at 20 C
  40. 530, // 0.01kOnm IPM voltage at 40 C
  41. 247, // 0.01kOnm IPM voltage at 60 C
  42. 125, // 0.01kOnm IPM voltage at 80 C
  43. 67, // 0.01kOnm IPM voltage at 100 C
  44. 39, //120C , 25 at 140 C
  45. };
  46. //K
  47. static SWORD PCB_swRTempCofTab[TEMPNUM] = {
  48. -3, // Q10 -20~-1 Coef of R
  49. -10, // Q10 0-19 Coef of R
  50. -28, // Q10 20-39 Coef of R
  51. -72, // Q10 40-59 Coef of R
  52. -168, // Q10 60-79 Coef of R
  53. -353, // Q10 80-99 Coef of R
  54. -731, //Q10 100-119
  55. -1463, //Q10 120-139
  56. };
  57. /* motor NTC intercept array */
  58. SLONG q10_motor_tmp_CurCalibCoef[CURCALIBNUM] = {
  59. 108660, // 70-81 Q10
  60. 120804, // 81-91
  61. 135836, // 92-102
  62. 146552, // 103-113
  63. 162816, // 114-124
  64. 182711, // 125-135
  65. 185344, // 136-146
  66. };
  67. /* motor NTC resistance */
  68. static SWORD motor_swRTempTab[TEMPNUM] = {
  69. 174, // 0.01kOnm IPM voltage at 70 C
  70. 121, // 0.01kOnm IPM voltage at 81 C
  71. 85, // 0.01kOnm IPM voltage at 92 C
  72. 62, // 0.01kOnm IPM voltage at 103 C
  73. 45, // 0.01kOnm IPM voltage at 114 C
  74. 34, // 0.01kOnm IPM voltage at 125 C
  75. 27, //0.01kOnm IPM voltage at 136 C, 21 at 146 C
  76. };
  77. /* motor NTC fitting slope array */
  78. static SWORD q10_motor_swRTempCofTab[TEMPNUM] = {
  79. -213, // Q10 70-81 Coef of R
  80. -313, // Q10 81-91 Coef of R
  81. -490, // Q10 92-102 Coef of R
  82. -663, // Q10 103-113 Coef of R
  83. -1024, // Q10 114-124 Coef of R
  84. -1609, //Q10 125-135
  85. -1707, //Q10 136-146
  86. };
  87. SWORD tmp_PcbTemp = 0;
  88. SWORD tmp_MotTemp = 0;
  89. /***************************************************************
  90. Function: TempInit;
  91. Description: cadence frequency get initialization
  92. Call by: functions in main loop;
  93. Input Variables: N/A
  94. Output/Return Variables: N/A
  95. Subroutine Call: N/A;
  96. Reference: N/A
  97. ****************************************************************/
  98. void TempInit(void)
  99. {
  100. #if 0
  101. UWORD CNT = 0;
  102. for (CNT = 0; CNT < (TEMPNUM - 1); CNT++)
  103. {
  104. PCB_swRTempCofTab[CNT] = ((SLONG)20 << 10) / (PCB_swRTempTab[CNT + 1] - PCB_swRTempTab[CNT]);
  105. }
  106. #endif
  107. }
  108. /***************************************************************
  109. Function: PcbTempCal;
  110. Description: Calculation of Pcb Temp using PcbR
  111. Call by: functions in main loop;
  112. Input Variables: N/A
  113. Output/Return Variables: N/A
  114. Subroutine Call: N/A;
  115. Reference: N/A
  116. ****************************************************************/
  117. #if 0
  118. void PcbTempCal(SWORD PcbR)
  119. {
  120. if (PcbR >= PCB_swRTempTab[temp_0])
  121. {
  122. tmp_PcbTemp = 0;
  123. }
  124. else if (PcbR < PCB_swRTempTab[temp_0] && PcbR >= PCB_swRTempTab[temp_20])
  125. {
  126. tmp_PcbTemp = 0 + (((PcbR - PCB_swRTempTab[temp_0]) * PCB_swRTempCofTab[temp_coef_0_20]) >> 10);
  127. }
  128. else if (PcbR < PCB_swRTempTab[temp_20] && PcbR >= PCB_swRTempTab[temp_40])
  129. {
  130. tmp_PcbTemp = 20 + (((PcbR - PCB_swRTempTab[temp_20]) * PCB_swRTempCofTab[temp_coef_20_40]) >> 10);
  131. }
  132. else if (PcbR < PCB_swRTempTab[temp_40] && PcbR >= PCB_swRTempTab[temp_60])
  133. {
  134. tmp_PcbTemp = 40 + (((PcbR - PCB_swRTempTab[temp_40]) * PCB_swRTempCofTab[temp_coef_40_60]) >> 10);
  135. }
  136. else if (PcbR < PCB_swRTempTab[temp_60] && PcbR >= PCB_swRTempTab[temp_80])
  137. {
  138. tmp_PcbTemp = 60 + (((PcbR - PCB_swRTempTab[temp_60]) * PCB_swRTempCofTab[temp_coef_60_80]) >> 10);
  139. }
  140. else if (PcbR < PCB_swRTempTab[temp_80] && PcbR >= PCB_swRTempTab[temp_100])
  141. {
  142. tmp_PcbTemp = 80 + (((PcbR - PCB_swRTempTab[temp_80]) * PCB_swRTempCofTab[temp_coef_80_100]) >> 10);
  143. }
  144. else if (PcbR < PCB_swRTempTab[temp_100])
  145. {
  146. tmp_PcbTemp = 100;
  147. }
  148. else
  149. {}
  150. }
  151. #endif
  152. /*!
  153. * @brief Fault state called in fast state machine
  154. *
  155. * @param 1)current NTC resistance 2) NTC resistance array
  156. * 3)fitting slope array 4)intercept array 5)140C correspond resistance
  157. *
  158. * @return current temperature
  159. */
  160. #if 1
  161. SWORD TempCal(SWORD PcbR, SWORD *ptemp_tab, SWORD *p_temp_coef, SLONG *p_cali_coef, SWORD temp_140)
  162. {
  163. SWORD per_temp;
  164. if (PcbR >= *(ptemp_tab+temp_0))
  165. {
  166. per_temp = 70;
  167. }
  168. else if (PcbR < *(ptemp_tab+temp_0) && PcbR >= *(ptemp_tab+temp_20))
  169. {
  170. per_temp = (PcbR * (*(p_temp_coef+temp_0)) + (*(p_cali_coef+temp_0))) >> 10;
  171. }
  172. else if (PcbR < *(ptemp_tab+temp_20) && PcbR >= *(ptemp_tab+temp_40))
  173. {
  174. per_temp = (PcbR * (*(p_temp_coef+temp_20)) + (*(p_cali_coef+temp_20))) >> 10;
  175. }
  176. else if (PcbR < *(ptemp_tab+temp_40) && PcbR >= *(ptemp_tab+temp_60))
  177. {
  178. per_temp = (PcbR * (*(p_temp_coef+temp_40)) + (*(p_cali_coef+temp_40))) >> 10;
  179. }
  180. else if (PcbR < *(ptemp_tab+temp_60) && PcbR >= *(ptemp_tab+temp_80))
  181. {
  182. per_temp = (PcbR * (*(p_temp_coef+temp_60)) + (*(p_cali_coef+temp_60))) >> 10;
  183. }
  184. else if (PcbR < *(ptemp_tab+temp_80) && PcbR >= *(ptemp_tab+temp_100))
  185. {
  186. per_temp = (PcbR * (*(p_temp_coef+temp_80)) + (*(p_cali_coef+temp_80))) >> 10;
  187. }
  188. else if (PcbR < *(ptemp_tab+temp_100) && PcbR >= *(ptemp_tab+temp_120))
  189. {
  190. per_temp = (PcbR * (*(p_temp_coef+temp_100)) + (*(p_cali_coef+temp_100))) >> 10;
  191. }
  192. else if(PcbR < *(ptemp_tab+temp_120))
  193. {
  194. per_temp = (PcbR * (*(p_temp_coef+temp_120)) + (*(p_cali_coef+temp_120))) >> 10;
  195. }
  196. else if(PcbR < temp_140)
  197. {
  198. per_temp = 150;
  199. }
  200. else
  201. {
  202. }
  203. return per_temp;
  204. }
  205. //PCB Temp
  206. SWORD PCBTempCal(UWORD PcbR, SWORD *ptemp_tab, SWORD *p_temp_coef, SLONG *p_cali_coef, SWORD temp_140)
  207. {
  208. SWORD per_temp;
  209. if (PcbR >= *(ptemp_tab+PCBtemp_N20))
  210. {
  211. per_temp = -20;
  212. }
  213. else if (PcbR < *(ptemp_tab+PCBtemp_N20) && PcbR >= *(ptemp_tab+PCBtemp_0))
  214. {
  215. per_temp = (PcbR * (*(p_temp_coef+PCBtemp_N20)) + (*(p_cali_coef+PCBtemp_N20))) >> 10; //-20~0
  216. }
  217. else if (PcbR < *(ptemp_tab+PCBtemp_0) && PcbR >= *(ptemp_tab+PCBtemp_20))
  218. {
  219. per_temp = (PcbR * (*(p_temp_coef+PCBtemp_0)) + (*(p_cali_coef+PCBtemp_0))) >> 10;//0-20
  220. }
  221. else if (PcbR < *(ptemp_tab+PCBtemp_20) && PcbR >= *(ptemp_tab+PCBtemp_40))
  222. {
  223. per_temp = (PcbR * (*(p_temp_coef+PCBtemp_20)) + (*(p_cali_coef+PCBtemp_20))) >> 10;//20-40
  224. }
  225. else if (PcbR < *(ptemp_tab+PCBtemp_40) && PcbR >= *(ptemp_tab+PCBtemp_60))
  226. {
  227. per_temp = (PcbR * (*(p_temp_coef+PCBtemp_40)) + (*(p_cali_coef+PCBtemp_40))) >> 10;//40-60
  228. }
  229. else if (PcbR < *(ptemp_tab+PCBtemp_60) && PcbR >= *(ptemp_tab+PCBtemp_80))
  230. {
  231. per_temp = (PcbR * (*(p_temp_coef+PCBtemp_60)) + (*(p_cali_coef+PCBtemp_60))) >> 10;//60-80
  232. }
  233. else if (PcbR < *(ptemp_tab+PCBtemp_80) && PcbR >= *(ptemp_tab+PCBtemp_100))
  234. {
  235. per_temp = (PcbR * (*(p_temp_coef+PCBtemp_80)) + (*(p_cali_coef+PCBtemp_80))) >> 10;// 80-100
  236. }
  237. else if (PcbR < *(ptemp_tab+PCBtemp_100) && PcbR >= *(ptemp_tab+PCBtemp_120))
  238. {
  239. per_temp = (PcbR * (*(p_temp_coef+PCBtemp_100)) + (*(p_cali_coef+PCBtemp_100))) >> 10;// 100-120
  240. }
  241. else if(PcbR < *(ptemp_tab+PCBtemp_120))
  242. {
  243. per_temp = (PcbR * (*(p_temp_coef+PCBtemp_120)) + (*(p_cali_coef+PCBtemp_120))) >> 10;
  244. }
  245. else if(PcbR < temp_140)
  246. {
  247. per_temp = 150;
  248. }
  249. else
  250. {
  251. }
  252. return per_temp;
  253. }
  254. void PcbTempCal(UWORD PcbR)
  255. {
  256. tmp_PcbTemp = PCBTempCal(PcbR, PCB_swRTempTab, PCB_swRTempCofTab, tmp_CurCalibCoef, temp_pcb_140);
  257. }
  258. void MotorTempCal(SWORD PcbR)
  259. {
  260. tmp_MotTemp = TempCal(PcbR, motor_swRTempTab, q10_motor_swRTempCofTab, q10_motor_tmp_CurCalibCoef, temp_motor_150);
  261. }
  262. #endif
  263. /*************************************************************************
  264. End of this File (EOF)!
  265. Do not put anything after this part!
  266. *************************************************************************/