OutputTask.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * OutputTask.c
  3. *
  4. * Created on: 2020年2月25日
  5. * Author: 7564
  6. */
  7. #include "OutputTask.h"
  8. bool isOpen;
  9. int InitComPort()
  10. {
  11. int fd;
  12. struct termios tios;
  13. fd = open(priPortName, O_RDWR);
  14. if(fd<=0)
  15. {
  16. #ifdef SystemLogMessage
  17. DEBUG_ERROR("open 407 Communication port NG \n");
  18. #endif
  19. return -1;
  20. }
  21. ioctl (fd, TCGETS, &tios);
  22. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  23. tios.c_lflag = 0;
  24. tios.c_iflag = 0;
  25. tios.c_oflag = 0;
  26. tios.c_cc[VMIN]=0;
  27. tios.c_cc[VTIME]=(unsigned char)1;
  28. tios.c_lflag=0;
  29. tcflush(fd, TCIFLUSH);
  30. ioctl (fd, TCSETS, &tios);
  31. return fd;
  32. }
  33. void GetClockTime(struct timespec* _now_time, void* null)
  34. {
  35. clock_gettime(CLOCK_MONOTONIC, _now_time);
  36. }
  37. unsigned long GetTimeoutValue(struct timespec _start_time)
  38. {
  39. struct timespec ts_end;
  40. unsigned long ret = 0;
  41. clock_gettime(CLOCK_MONOTONIC, &ts_end);
  42. ret = ((unsigned long)(ts_end.tv_sec - _start_time.tv_sec) * 1000000) + ((unsigned long)((ts_end.tv_nsec / 1000) - (_start_time.tv_nsec / 1000)));
  43. return ret;
  44. }
  45. void ShowMainMsg()
  46. {
  47. printf("Max Vol : %f, Max Cur : %d, POW : %d \n", UnSafeDataInfo->PSU_VOLTAGE,
  48. UnSafeDataInfo->PSU_CURRENT, UnSafeDataInfo->PSU_POWER);
  49. printf("=> ");
  50. }
  51. void ChkButtonStatus()
  52. {
  53. if (Button1 == PRESS && !leftBtnPush)
  54. {
  55. if(!leftBtnPush)
  56. {
  57. leftBtnPush = true;
  58. if (_charging_mode == CHARGING_MODE_STOP)
  59. {
  60. _charging_mode = CHARGING_MODE_START;
  61. printf("****************** Switch to Charging Mode ******************\n");
  62. }
  63. }
  64. else if (Button1 == RELEASE)
  65. {
  66. if(leftBtnPush)
  67. {
  68. leftBtnPush = false;
  69. }
  70. }
  71. }
  72. if (Button2 == PRESS && !rightBtnPush)
  73. {
  74. if(!rightBtnPush)
  75. {
  76. rightBtnPush = true;
  77. if (_charging_mode == CHARGING_MODE_START)
  78. {
  79. _charging_mode = CHARGING_MODE_TERMINATING;
  80. printf("****************** Switch to Stop Mode ******************\n");
  81. }
  82. }
  83. else if (Button2 == RELEASE)
  84. {
  85. if(rightBtnPush)
  86. {
  87. rightBtnPush = false;
  88. }
  89. }
  90. }
  91. }
  92. void GetModuleCountCallback(byte group, byte count)
  93. {
  94. printf("group = %d, count = %d \n", group, count);
  95. if (group == SYSTEM_CMD)
  96. UnSafeDataInfo->PSU_COUNT = count;
  97. }
  98. void GetAvailableCapCallback(byte address, short maxVol, short minVol, short maxCur, short totalPow)
  99. {
  100. int _groupPower = 0, _groupCurrent = 0;
  101. UnSafeDataInfo->PsuModule[address].PSU_VOLTAGE_INFO = maxVol;
  102. UnSafeDataInfo->PsuModule[address].PSU_CURRENT_INFO = maxCur;
  103. UnSafeDataInfo->PsuModule[address].PSU_POWER_INFO = totalPow;
  104. for (byte index = 0; index < UnSafeDataInfo->PSU_COUNT; index++)
  105. {
  106. _groupCurrent += UnSafeDataInfo->PsuModule[address].PSU_CURRENT_INFO;
  107. _groupPower += UnSafeDataInfo->PsuModule[address].PSU_POWER_INFO;
  108. }
  109. UnSafeDataInfo->PSU_VOLTAGE = maxVol;
  110. UnSafeDataInfo->PSU_CURRENT = _groupCurrent;
  111. UnSafeDataInfo->PSU_POWER = _groupPower;
  112. }
  113. void GetStatusCallback(byte group, byte address, byte temp, int alarm)
  114. {
  115. printf("alarm = %d \n", alarm);
  116. }
  117. void GetInputVoltageCallback(byte address, unsigned short vol1, unsigned short vol2, unsigned short vol3)
  118. {
  119. printf("vol1 = %d, vol2 = %d, vol3 = %d \n", vol1, vol2, vol3);
  120. }
  121. int CreateShareMemory()
  122. {
  123. int MeterSMId;
  124. if ((MeterSMId = shmget(ShmTestKey, sizeof(struct UnSafeData), IPC_CREAT | 0777)) < 0)
  125. {
  126. return 0;
  127. }
  128. else if ((UnSafeDataInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  129. {
  130. return 0;
  131. }
  132. memset(UnSafeDataInfo, 0, sizeof(struct UnSafeData));
  133. return 1;
  134. }
  135. static void get_char(char *word)
  136. {
  137. fd_set rfds;
  138. struct timeval tv;
  139. FD_ZERO(&rfds);
  140. FD_SET(0, &rfds);
  141. tv.tv_sec = 0;
  142. tv.tv_usec = 10; //wait input timout time
  143. //if input
  144. if (select(1, &rfds, NULL, NULL, &tv) > 0)
  145. {
  146. fgets(word, 128, stdin);
  147. }
  148. }
  149. void GetInputString()
  150. {
  151. char word[128];
  152. char newString[7][10];
  153. int i, j, ctr;
  154. get_char(word);
  155. if (strlen(word) == 0)
  156. return;
  157. //fgets(word, sizeof(word), stdin);
  158. j = 0;
  159. ctr = 0;
  160. for (i = 0; i <= (strlen(word)); i++) {
  161. if (word[i] == ' ' || word[i] == '\0' || word[i] == 10) {
  162. newString[ctr][j] = '\0';
  163. ctr++;
  164. j = 0;
  165. } else {
  166. newString[ctr][j] = word[i];
  167. j++;
  168. }
  169. }
  170. VOLTAGE = atof(newString[0]);
  171. CURRENT = atof(newString[1]);
  172. if (VOLTAGE <= UnSafeDataInfo->PSU_VOLTAGE && CURRENT <= UnSafeDataInfo->PSU_CURRENT)
  173. {
  174. //printf("OutputVol = %f, OutputCur = %f \n", VOLTAGE, CURRENT);
  175. }
  176. else
  177. {
  178. ShowMainMsg();
  179. }
  180. }
  181. void GetIavailableCallback(byte address, unsigned short Iavail, unsigned short Vext)
  182. {
  183. //printf("address = %d, Iavail = %d, Vext = %d \n", address, Iavail, Vext);
  184. }
  185. void GetOutputAndTempCallback(byte address, unsigned short outputVol,
  186. unsigned short outputCur, unsigned short outputPower, unsigned char Temperature)
  187. {
  188. //printf("***Output Value and Temp*** address = %d, Vol = %d, Cur = %d, Pow = %d, Temp = %d \n",
  189. // address, outputVol, outputCur, outputPower, Temperature);
  190. }
  191. void GetModuleStatusCallback(byte address, unsigned char isErr, unsigned char status,
  192. unsigned char err1, unsigned char err2, unsigned char err3, unsigned char err4)
  193. {
  194. //int alarm = (err2 << 24) | (err3 << 16) | (err4 << 8);
  195. // err2 == state 2
  196. // err3 == state 1
  197. // err4 == state 0
  198. //printf("***Status*** address = %d, alarm = %d \n", address, alarm);
  199. // printf("***Status*** address = %d, err1 = %d, err2 = %d, err3 = %d, err4 = %d \n",
  200. // address, err1,err2,err3,err4);
  201. }
  202. void GetModuleInputCallback(byte address, unsigned short inputR,
  203. unsigned short inputS, unsigned short inputT)
  204. {
  205. }
  206. int main(void)
  207. {
  208. isOpen =false;
  209. if(CreateShareMemory() == 0)
  210. {
  211. printf("CreateShareMemory fail. \n");
  212. return 0;
  213. }
  214. RefreshModuleCount(&GetModuleCountCallback);
  215. RefreshAvailableCap(&GetAvailableCapCallback);
  216. RefreshStatus(&GetStatusCallback);
  217. RefreshInputVol(&GetInputVoltageCallback);
  218. RefreshIavailable(&GetIavailableCallback);
  219. AutoMode_RefreshOutputAndTemp(&GetOutputAndTempCallback);
  220. AutoMode_RefreshModuleStatus(&GetModuleStatusCallback);
  221. AutoMode_RefreshModuleInput(&GetModuleInputCallback);
  222. Uart1Fd = InitComPort();
  223. libInitialize = InitialCommunication();
  224. if (Uart1Fd < 0 || !libInitialize)
  225. {
  226. printf("Initial port fail. \n");
  227. return 0;
  228. }
  229. sleep(5);
  230. GetClockTime(&_cmdSubPriority_time, NULL);
  231. VOLTAGE = 0.0;
  232. CURRENT = 0.0;
  233. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  234. // while (1)
  235. // {
  236. // printf("++++++++++++++2++++++++++++++++++++++++++++++++++++++\n");
  237. // SetWalkInConfig(0, YES, 0);
  238. // SetWalkInConfig(1, NO, 0);
  239. // printf("++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
  240. // sleep(1);
  241. // }
  242. //
  243. // sleep(1);
  244. // printf("++++++++++++++2++++++++++++++++++++++++++++++++++++++\n");
  245. // SetWalkInConfig(SYSTEM_CMD, NO, 0);
  246. // printf("++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
  247. // return 0;
  248. while (1)
  249. {
  250. GetInputGpioStatus();
  251. //ChkButtonStatus();
  252. // 切換 Walk-in mode (default 5s -> 2s)
  253. SetWalkInConfig(SYSTEM_CMD, NO, 0);
  254. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  255. while(isGetCount == YES)
  256. {
  257. if (_charging_mode == CHARGING_MODE_START)
  258. {
  259. // 取得模塊輸出額定電流能力
  260. GetModuleIavailable(0);
  261. }
  262. GetInputString();
  263. if (VOLTAGE > 150 && CURRENT >= 0)
  264. _charging_mode = CHARGING_MODE_START;
  265. else
  266. _charging_mode = CHARGING_MODE_TERMINATING;
  267. //printf("_charging_mode = %d \n", _charging_mode);
  268. switch(_charging_mode)
  269. {
  270. case CHARGING_MODE_START:
  271. {
  272. //if (!isOpen)
  273. {
  274. //SwitchPower(SYSTEM_CMD, PSU_POWER_ON);
  275. //FlashLed(SYSTEM_CMD, PSU_FLASH_ON);
  276. SetDirModulePresentOutput(0,
  277. VOLTAGE * 10,
  278. CURRENT * 10,
  279. 0x01,
  280. 0x01);
  281. }
  282. //PresentOutputVol(SYSTEM_CMD, VOLTAGE * 10, CURRENT * 10);
  283. }
  284. break;
  285. case CHARGING_MODE_TERMINATING:
  286. {
  287. //if (isOpen)
  288. {
  289. SetDirModulePresentOutput(0,
  290. VOLTAGE * 10,
  291. CURRENT * 10,
  292. 0x00,
  293. 0x01);
  294. //SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  295. //FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  296. }
  297. }
  298. break;
  299. }
  300. //GetStatus(0);
  301. //GetModuleInput(0);
  302. sleep(1);
  303. }
  304. if (UnSafeDataInfo->PSU_COUNT <= 0)
  305. {
  306. if (time > 1000)
  307. {
  308. printf("Step 1 : GetModuleCount...... \n");
  309. GetModuleCount(SYSTEM_CMD);
  310. GetClockTime(&_cmdSubPriority_time, NULL);
  311. }
  312. }
  313. else if (time < 5000)
  314. {
  315. printf("Step 2 : GetModuleCap...... \n");
  316. GetModuleCap(0);
  317. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  318. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  319. }
  320. else
  321. {
  322. ShowMainMsg();
  323. isGetCount = YES;
  324. }
  325. sleep(1);
  326. }
  327. return 0;
  328. }