Temp.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 "at32f421.h"//#include "stm32f10x.h"
  19. #include "Temp.h"
  20. #include "user.h"
  21. /******************************
  22. *
  23. * Parameter
  24. *
  25. ******************************/
  26. //b
  27. SLONG tmp_CurCalibCoef[CURCALIBNUM] ={
  28. 8008, // -20~0
  29. 30224, // 0-19 Q10
  30. 53743, // 20-39
  31. 77427, // 40-59
  32. 101477, // 60-79
  33. 125143, // 80-99
  34. 149524, // 100-119
  35. 174172, // 120-139
  36. };
  37. //value
  38. static SWORD PCB_swRTempTab[TEMPNUM] = {
  39. 10102, // 0.01kOnm IPM voltage at -20 C
  40. 3362, // 0.01kOnm IPM voltage at 0 C
  41. 1253, // 0.01kOnm IPM voltage at 20 C
  42. 530, // 0.01kOnm IPM voltage at 40 C
  43. 248, // 0.01kOnm IPM voltage at 60 C
  44. 127, // 0.01kOnm IPM voltage at 80 C
  45. 69, // 0.01kOnm IPM voltage at 100 C
  46. 40, //120C
  47. };
  48. //K
  49. static SWORD PCB_swRTempCofTab[TEMPNUM] = {
  50. -3, // Q10 -20~-1 Coef of R
  51. -10, // Q10 0-19 Coef of R
  52. -28, // Q10 20-39 Coef of R
  53. -72, // Q10 40-59 Coef of R
  54. -167, // Q10 60-79 Coef of R
  55. -349, // Q10 80-99 Coef of R
  56. -696, //Q10 100-119
  57. -1301, //Q10 120-139
  58. };
  59. /* motor NTC intercept array */
  60. SLONG q10_motor_tmp_CurCalibCoef[CURCALIBNUM] = {
  61. 113172, // 70-81 Q10
  62. 126361, // 81-91
  63. 139612, // 92-102
  64. 151296, // 103-113
  65. 166287, // 114-124
  66. 179722, // 125-135
  67. 195184, // 136-146
  68. };
  69. /* motor NTC resistance */
  70. static SWORD motor_swRTempTab[TEMPNUM] = {
  71. 223, // 0.01kOnm IPM voltage at 70 C
  72. 163, // 0.01kOnm IPM voltage at 81 C
  73. 120, // 0.01kOnm IPM voltage at 92 C
  74. 90, // 0.01kOnm IPM voltage at 103 C
  75. 69, // 0.01kOnm IPM voltage at 114 C
  76. 53, // 0.01kOnm IPM voltage at 125 C
  77. 41, //0.01kOnm IPM voltage at 136 C
  78. };
  79. /* motor NTC fitting slope array */
  80. static SWORD q10_motor_swRTempCofTab[TEMPNUM] = {
  81. -186, // Q10 70-81 Coef of R
  82. -267, // Q10 81-91 Coef of R
  83. -377, // Q10 92-102 Coef of R
  84. -525, // Q10 103-113 Coef of R
  85. -718, // Q10 114-124 Coef of R
  86. -970, //Q10 125-135
  87. -1340, //Q10 136-146
  88. };
  89. SWORD tmp_PcbTemp = 0;
  90. SWORD tmp_MotTemp = 0;
  91. /***************************************************************
  92. Function: TempInit;
  93. Description: cadence frequency get initialization
  94. Call by: functions in main loop;
  95. Input Variables: N/A
  96. Output/Return Variables: N/A
  97. Subroutine Call: N/A;
  98. Reference: N/A
  99. ****************************************************************/
  100. void TempInit(void)
  101. {
  102. #if 0
  103. UWORD CNT = 0;
  104. for (CNT = 0; CNT < (TEMPNUM - 1); CNT++)
  105. {
  106. PCB_swRTempCofTab[CNT] = ((SLONG)20 << 10) / (PCB_swRTempTab[CNT + 1] - PCB_swRTempTab[CNT]);
  107. }
  108. #endif
  109. }
  110. /***************************************************************
  111. Function: PcbTempCal;
  112. Description: Calculation of Pcb Temp using PcbR
  113. Call by: functions in main loop;
  114. Input Variables: N/A
  115. Output/Return Variables: N/A
  116. Subroutine Call: N/A;
  117. Reference: N/A
  118. ****************************************************************/
  119. #if 0
  120. void PcbTempCal(SWORD PcbR)
  121. {
  122. if (PcbR >= PCB_swRTempTab[temp_0])
  123. {
  124. tmp_PcbTemp = 0;
  125. }
  126. else if (PcbR < PCB_swRTempTab[temp_0] && PcbR >= PCB_swRTempTab[temp_20])
  127. {
  128. tmp_PcbTemp = 0 + (((PcbR - PCB_swRTempTab[temp_0]) * PCB_swRTempCofTab[temp_coef_0_20]) >> 10);
  129. }
  130. else if (PcbR < PCB_swRTempTab[temp_20] && PcbR >= PCB_swRTempTab[temp_40])
  131. {
  132. tmp_PcbTemp = 20 + (((PcbR - PCB_swRTempTab[temp_20]) * PCB_swRTempCofTab[temp_coef_20_40]) >> 10);
  133. }
  134. else if (PcbR < PCB_swRTempTab[temp_40] && PcbR >= PCB_swRTempTab[temp_60])
  135. {
  136. tmp_PcbTemp = 40 + (((PcbR - PCB_swRTempTab[temp_40]) * PCB_swRTempCofTab[temp_coef_40_60]) >> 10);
  137. }
  138. else if (PcbR < PCB_swRTempTab[temp_60] && PcbR >= PCB_swRTempTab[temp_80])
  139. {
  140. tmp_PcbTemp = 60 + (((PcbR - PCB_swRTempTab[temp_60]) * PCB_swRTempCofTab[temp_coef_60_80]) >> 10);
  141. }
  142. else if (PcbR < PCB_swRTempTab[temp_80] && PcbR >= PCB_swRTempTab[temp_100])
  143. {
  144. tmp_PcbTemp = 80 + (((PcbR - PCB_swRTempTab[temp_80]) * PCB_swRTempCofTab[temp_coef_80_100]) >> 10);
  145. }
  146. else if (PcbR < PCB_swRTempTab[temp_100])
  147. {
  148. tmp_PcbTemp = 100;
  149. }
  150. else
  151. {}
  152. }
  153. #endif
  154. /*!
  155. * @brief Fault state called in fast state machine
  156. *
  157. * @param 1)current NTC resistance 2) NTC resistance array
  158. * 3)fitting slope array 4)intercept array 5)140C correspond resistance
  159. *
  160. * @return current temperature
  161. */
  162. #if 1
  163. SWORD TempCal(SWORD PcbR, SWORD *ptemp_tab, SWORD *p_temp_coef, SLONG *p_cali_coef, SWORD temp_140)
  164. {
  165. SWORD per_temp;
  166. if (PcbR >= *(ptemp_tab+temp_0))
  167. {
  168. per_temp = 0;
  169. }
  170. else if (PcbR < *(ptemp_tab+temp_0) && PcbR >= *(ptemp_tab+temp_20))
  171. {
  172. per_temp = (PcbR * (*(p_temp_coef+temp_0)) + (*(p_cali_coef+temp_0))) >> 10;
  173. }
  174. else if (PcbR < *(ptemp_tab+temp_20) && PcbR >= *(ptemp_tab+temp_40))
  175. {
  176. per_temp = (PcbR * (*(p_temp_coef+temp_20)) + (*(p_cali_coef+temp_20))) >> 10;
  177. }
  178. else if (PcbR < *(ptemp_tab+temp_40) && PcbR >= *(ptemp_tab+temp_60))
  179. {
  180. per_temp = (PcbR * (*(p_temp_coef+temp_40)) + (*(p_cali_coef+temp_40))) >> 10;
  181. }
  182. else if (PcbR < *(ptemp_tab+temp_60) && PcbR >= *(ptemp_tab+temp_80))
  183. {
  184. per_temp = (PcbR * (*(p_temp_coef+temp_60)) + (*(p_cali_coef+temp_60))) >> 10;
  185. }
  186. else if (PcbR < *(ptemp_tab+temp_80) && PcbR >= *(ptemp_tab+temp_100))
  187. {
  188. per_temp = (PcbR * (*(p_temp_coef+temp_80)) + (*(p_cali_coef+temp_80))) >> 10;
  189. }
  190. else if (PcbR < *(ptemp_tab+temp_100) && PcbR >= *(ptemp_tab+temp_120))
  191. {
  192. per_temp = (PcbR * (*(p_temp_coef+temp_100)) + (*(p_cali_coef+temp_100))) >> 10;
  193. }
  194. else if(PcbR < *(ptemp_tab+temp_120))
  195. {
  196. per_temp = (PcbR * (*(p_temp_coef+temp_120)) + (*(p_cali_coef+temp_120))) >> 10;
  197. }
  198. else if(PcbR < temp_140)
  199. {
  200. per_temp = 150;
  201. }
  202. else
  203. {
  204. }
  205. return per_temp;
  206. }
  207. //PCB Temp
  208. SWORD PCBTempCal(UWORD PcbR, SWORD *ptemp_tab, SWORD *p_temp_coef, SLONG *p_cali_coef, SWORD temp_140)
  209. {
  210. SWORD per_temp;
  211. if (PcbR >= *(ptemp_tab+PCBtemp_N20))
  212. {
  213. per_temp = -20;
  214. }
  215. else if (PcbR < *(ptemp_tab+PCBtemp_N20) && PcbR >= *(ptemp_tab+PCBtemp_0))
  216. {
  217. per_temp = (PcbR * (*(p_temp_coef+PCBtemp_N20)) + (*(p_cali_coef+PCBtemp_N20))) >> 10; //-20~0
  218. }
  219. else if (PcbR < *(ptemp_tab+PCBtemp_0) && PcbR >= *(ptemp_tab+PCBtemp_20))
  220. {
  221. per_temp = (PcbR * (*(p_temp_coef+PCBtemp_0)) + (*(p_cali_coef+PCBtemp_0))) >> 10;//0-20
  222. }
  223. else if (PcbR < *(ptemp_tab+PCBtemp_20) && PcbR >= *(ptemp_tab+PCBtemp_40))
  224. {
  225. per_temp = (PcbR * (*(p_temp_coef+PCBtemp_20)) + (*(p_cali_coef+PCBtemp_20))) >> 10;//20-40
  226. }
  227. else if (PcbR < *(ptemp_tab+PCBtemp_40) && PcbR >= *(ptemp_tab+PCBtemp_60))
  228. {
  229. per_temp = (PcbR * (*(p_temp_coef+PCBtemp_40)) + (*(p_cali_coef+PCBtemp_40))) >> 10;//40-60
  230. }
  231. else if (PcbR < *(ptemp_tab+PCBtemp_60) && PcbR >= *(ptemp_tab+PCBtemp_80))
  232. {
  233. per_temp = (PcbR * (*(p_temp_coef+PCBtemp_60)) + (*(p_cali_coef+PCBtemp_60))) >> 10;//60-80
  234. }
  235. else if (PcbR < *(ptemp_tab+PCBtemp_80) && PcbR >= *(ptemp_tab+PCBtemp_100))
  236. {
  237. per_temp = (PcbR * (*(p_temp_coef+PCBtemp_80)) + (*(p_cali_coef+PCBtemp_80))) >> 10;// 80-100
  238. }
  239. else if (PcbR < *(ptemp_tab+PCBtemp_100) && PcbR >= *(ptemp_tab+PCBtemp_120))
  240. {
  241. per_temp = (PcbR * (*(p_temp_coef+PCBtemp_100)) + (*(p_cali_coef+PCBtemp_100))) >> 10;// 100-120
  242. }
  243. else if(PcbR < *(ptemp_tab+PCBtemp_120))
  244. {
  245. per_temp = (PcbR * (*(p_temp_coef+PCBtemp_120)) + (*(p_cali_coef+PCBtemp_120))) >> 10;
  246. }
  247. else if(PcbR < temp_140)
  248. {
  249. per_temp = 150;
  250. }
  251. else
  252. {
  253. }
  254. return per_temp;
  255. }
  256. void PcbTempCal(UWORD PcbR)
  257. {
  258. tmp_PcbTemp = PCBTempCal(PcbR, PCB_swRTempTab, PCB_swRTempCofTab, tmp_CurCalibCoef, temp_pcb_140);
  259. }
  260. void MotorTempCal(SWORD PcbR)
  261. {
  262. #if((MOTOR_ID_SEL == MOTOR_LUNGU_WELLING_HAL_POLE10_750W_500Ratio) ||(MOTOR_ID_SEL ==MOTOR_LUNGU_WELLING_HAL_POLE10_500W_500Ratio) ||(MOTOR_ID_SEL == MOTOR_LUNGU_WELLING_HAL_POLE10_1000W_500Ratio))
  263. tmp_MotTemp = 74;//
  264. #else
  265. tmp_MotTemp = TempCal(PcbR, motor_swRTempTab, q10_motor_swRTempCofTab, q10_motor_tmp_CurCalibCoef, temp_motor_150);
  266. #endif
  267. }
  268. #endif
  269. /*************************************************************************
  270. End of this File (EOF)!
  271. Do not put anything after this part!
  272. *************************************************************************/