FanBoard.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #include <stdio.h> /*標準輸入輸出定義*/
  2. #include <stdlib.h> /*標準函數庫定義*/
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <time.h>
  6. #include <sys/time.h>
  7. #include <sys/timeb.h>
  8. #include "../ShareMemory/shmMem.h"
  9. #include "../Config.h"
  10. #include "Module_InternalComm.h"
  11. #include "internalComm.h"
  12. //------------------------------------------------------------------------------
  13. static struct SysConfigData *pSysConfig = NULL;
  14. static struct SysInfoData *pSysInfo = NULL;
  15. static struct AlarmCodeData *pAlarmCode = NULL;
  16. static struct FanModuleData *ShmFanModuleData = NULL;
  17. static struct PsuData *ShmPsuData = NULL;
  18. static int Uart5Fd = 0;
  19. static struct timeval gFanBoardRunTimer;
  20. //------------------------------------------------------------------------------
  21. static void SetFanModuleSpeed(void)
  22. {
  23. {
  24. FanSpeed _fanSpeed = {0};
  25. _setFanSpeed += fanSpeedSmoothValue;
  26. if (_setFanSpeed >= ShmFanModuleData->SetFan1Speed) {
  27. _setFanSpeed = ShmFanModuleData->SetFan1Speed;
  28. }
  29. //printf("_setFanSpeed = %d \n", _setFanSpeed);
  30. _fanSpeed.speed[0] = _setFanSpeed;
  31. _fanSpeed.speed[1] = _setFanSpeed;
  32. _fanSpeed.speed[2] = _setFanSpeed;
  33. _fanSpeed.speed[3] = _setFanSpeed;
  34. if (Config_Fan_Speed(Uart5Fd, ADDR_FAN, &_fanSpeed) == PASS) {
  35. //log_info("successfully Fan\n");
  36. }
  37. }
  38. }
  39. // 風扇速度
  40. static void GetFanSpeed(void)
  41. {
  42. //log_info("Get fan board speed \n");
  43. if (Query_Fan_Speed(Uart5Fd, ADDR_FAN, &fanSpeed) == PASS) {
  44. ShmFanModuleData->PresentFan1Speed = fanSpeed.speed[0];
  45. ShmFanModuleData->PresentFan2Speed = fanSpeed.speed[1];
  46. ShmFanModuleData->PresentFan3Speed = fanSpeed.speed[2];
  47. ShmFanModuleData->PresentFan4Speed = fanSpeed.speed[3];
  48. // log_info("SystemFanRotaSpeed_1 = %d \n", fanSpeed.speed[0]);
  49. // log_info("SystemFanRotaSpeed_2 = %d \n", fanSpeed.speed[1]);
  50. // log_info("SystemFanRotaSpeed_3 = %d \n", fanSpeed.speed[2]);
  51. // log_info("SystemFanRotaSpeed_4 = %d \n", fanSpeed.speed[3]);
  52. // Config_Fan_Speed(Uart5Fd, ADDR_FAN, &fanSpeed[0]);
  53. //SysInfoData (SystemFanRotaSpeed)
  54. }
  55. }
  56. static void GetFanSpeedByFunction(void)
  57. {
  58. if (pSysConfig->SwitchDebugFlag == YES) {
  59. return;
  60. }
  61. // 風控修改 :
  62. // ******************************************************* //
  63. //
  64. // 當前PSU輸出總 KW PSU Temp
  65. // 30 x -------------------- x ---------- + 14 x (PSU Temp - 45)
  66. // 當前樁最大功率 KW 45
  67. //
  68. // ******************************************************* //
  69. // 當前樁最大功率 KW : ShmPsuData->SystemAvailablePower
  70. uint32_t _maxPower = ShmPsuData->SystemAvailablePower;
  71. // 當前PSU輸出總 KW & PSU Temp :
  72. uint8_t temp = 0;
  73. uint8_t index = 0;
  74. uint8_t count = 0;
  75. uint8_t gunIndex = 0;
  76. uint8_t _temp_diff = 0;
  77. float power = 0;
  78. double _pw_rate = 0;
  79. double _temp_rate = 0;
  80. for (index = 0; index < ShmPsuData->GroupCount; index++) {
  81. for (count = 0; count < ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity; count++) {
  82. if (temp < ShmPsuData->PsuGroup[index].PsuModule[count].ExletTemp) {
  83. temp = ShmPsuData->PsuGroup[index].PsuModule[count].ExletTemp;
  84. }
  85. }
  86. }
  87. for (gunIndex = 0; gunIndex < gunCount; gunIndex++) {
  88. power += (_chargingData[gunIndex]->PresentChargingPower * 10);
  89. }
  90. if (_maxPower > 0) {
  91. _pw_rate = power / (double)_maxPower;
  92. }
  93. if (temp > 0) {
  94. _temp_rate = (double)temp / 50;
  95. }
  96. if (temp > 45) {
  97. _temp_diff = temp - 70;
  98. }
  99. ShmFanModuleData->TestFanSpeed = (((50 * _pw_rate * _temp_rate) + (0.5 * _temp_diff)) / 100) * MAX_FAN_SPEED;
  100. if (ShmFanModuleData->TestFanSpeed > MAX_FAN_SPEED) {
  101. ShmFanModuleData->TestFanSpeed = MAX_FAN_SPEED;
  102. }
  103. if (ShmFanModuleData->TestFanSpeed < 0) {
  104. ShmFanModuleData->TestFanSpeed = 0;
  105. }
  106. //
  107. // printf("power = %f \n", power);
  108. // printf("_maxPower = %d \n", _maxPower);
  109. // printf("temp = %d \n", temp);
  110. //
  111. // printf("_pw_rate = %f \n", _pw_rate);
  112. // printf("_temp_rate = %f \n", _temp_rate);
  113. // printf("_temp_diff = %d \n", _temp_diff);
  114. // printf("fan rate = %f \n", (30 * _pw_rate * _temp_rate + 14 * _temp_diff));
  115. // printf("ShmFanModuleData->TestFanSpeed = %d \n", ShmFanModuleData->TestFanSpeed);
  116. }
  117. static void SetRtcData_Fan(void)
  118. {
  119. struct timeb csuTime;
  120. struct tm *tmCSU;
  121. RTC rtc = {0};
  122. ftime(&csuTime);
  123. tmCSU = localtime(&csuTime.time);
  124. // log_info("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  125. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  126. // tmCSU->tm_sec);
  127. rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
  128. rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
  129. rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
  130. rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
  131. rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
  132. rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
  133. rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
  134. rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
  135. rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
  136. rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
  137. rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
  138. rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
  139. rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
  140. rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
  141. if (Config_Rtc_Data(Uart5Fd, ADDR_FAN, &rtc) == PASS) {
  142. //log_info("SetRtc (FB) sucessfully. \n");
  143. }
  144. }
  145. static void SetModelName_Fan(void)
  146. {
  147. if (Config_Model_Name(Uart5Fd, ADDR_FAN, pSysConfig->ModelName) == PASS) {
  148. log_info("Set Model name PASS = %s \n", pSysConfig->ModelName);
  149. }
  150. }
  151. static void GetFwAndHwVersion_Fan(void)
  152. {
  153. Ver ver = {0};
  154. if (Query_FW_Ver(Uart5Fd, ADDR_FAN, &ver) == PASS) {
  155. // FanModuleData
  156. strcpy((char *)ShmFanModuleData->version, ver.Version_FW);
  157. // SystemInfo
  158. strcpy((char *)pSysInfo->FanModuleFwRev, ver.Version_FW);
  159. //log_info("GetFwAndHwVersion_Fan s1 = %s \n", ver.Version_FW);
  160. }
  161. if (Query_HW_Ver(Uart5Fd, ADDR_FAN, &ver) == PASS) {
  162. // SystemInfo
  163. strcpy((char *)pSysInfo->FanModuleHwRev, ver.Version_FW);
  164. //log_info("GetFwAndHwVersion_Fan s2 = %s \n", ver.Version_HW);
  165. }
  166. }
  167. static void fanBoardPorcess(void)
  168. {
  169. if (ShmFanModuleData->SelfTest_Comp == NO) {
  170. return;
  171. }
  172. if (ShmFanModuleData->SelfTest_Comp == YES ||
  173. strlen((char *)ShmSysConfigAndInfo->SysInfo.FanModuleFwRev) != 0 ||
  174. pSysInfo->FanModuleFwRev[0] != '\0') {
  175. ShmFanModuleData->SelfTest_Comp = YES;
  176. if (GetTimeoutValue(gFanBoardRunTimer) / 1000 >= 1000) {
  177. //GetPsuTempForFanSpeed();
  178. GetFanSpeedByFunction();
  179. GetFanSpeed();
  180. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = _setFanSpeed;
  181. gettimeofday(&gFanBoardRunTimer, NULL);
  182. ShmFanModuleData->SetFan1Speed = ShmFanModuleData->TestFanSpeed;
  183. ShmFanModuleData->SetFan2Speed = ShmFanModuleData->TestFanSpeed;
  184. ShmFanModuleData->SetFan3Speed = ShmFanModuleData->TestFanSpeed;
  185. ShmFanModuleData->SetFan4Speed = ShmFanModuleData->TestFanSpeed;
  186. //log_info("set fan = %d \n", ShmFanModuleData->SetFan1Speed);
  187. SetFanModuleSpeed();
  188. }
  189. }
  190. }
  191. static void fanBoardSelfTest(void)
  192. {
  193. if (ShmFanModuleData->SelfTest_Comp == YES) {
  194. return;
  195. }
  196. GetFwAndHwVersion_Fan();
  197. SetModelName_Fan();
  198. SetRtcData_Fan();
  199. sleep(1);
  200. gettimeofday(&gFanBoardRunTimer, NULL);
  201. }
  202. void FanBoardTask(int uartFD)
  203. {
  204. #if defined NO_FAN_BOARD || defined DD360ComBox
  205. #endif //defined NO_FAN_BOARD || defined DD360ComBox
  206. pid_t pid = fork();
  207. if (pid == 0) {
  208. int isContinue = 1;
  209. if (CreateAllCsuShareMemory() == FAIL) {
  210. log_error("Fan Board create share memory failed\r\n");
  211. }
  212. MappingGunChargingInfo("Fan Board Task");
  213. //share memory mapping
  214. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  215. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  216. pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  217. ShmFanModuleData = (struct FanModuleData *)GetShmFanModuleData();
  218. ShmPsuData = (struct PsuData *)GetShmPsuData();
  219. Uart5Fd = uartFD;
  220. while (isContinue) {
  221. fanBoardSelfTest();
  222. fanBoardPorcess();
  223. }
  224. }
  225. }