tf_sm.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include "tf_sm.h"
  2. #include "tf.h"
  3. #include "tf_comm.h"
  4. #include "plt.h"
  5. static struct state_t tongfei_states[] = {
  6. {SMST_LAUNCH, "launch"},
  7. {SMST_READY, "ready"},
  8. {SMST_ERR, "err"},
  9. };
  10. static struct err_t tongfei_errs[] = {
  11. {TFERR_NONE, "none"},
  12. // launch
  13. {TFERR_LAUNCH_COMMERR, "launch, comm err"},
  14. // err
  15. {TFERR_ERR_COMMERR, "err comm err"},
  16. // ready
  17. {TFERR_READY_COMMERR, "ready comm err"},
  18. };
  19. int tongfei_sm_init(int idx)
  20. {
  21. struct statemachine_t *sm = &tf[idx].sm;
  22. sm_reset_timing(sm, 1, 1);
  23. sm->states = tongfei_states;
  24. sm->state_nbr = sizeof(tongfei_states) / sizeof(struct state_t);
  25. sm->errs = tongfei_errs;
  26. sm->err_nbr = sizeof(tongfei_errs) / sizeof(struct err_t);
  27. sm_set_state(sm, SMST_LAUNCH, MC0051GSERR_NONE);
  28. return 0;
  29. }
  30. static void tongfei_sm_launch(int idx)
  31. {
  32. struct tongfei_t *dev = &tf[idx];
  33. struct comm_t *comm = &dev->comm;
  34. struct statemachine_t *sm = &dev->sm;
  35. if (sm_get_step(sm) == 0)
  36. { // entry
  37. log_dbg("%s, idx:%d, state:%s, step:%d, entry", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
  38. tongfei_reset_cmd(idx);
  39. tongfei_comm_reset(idx);
  40. sm_set_step(sm, 20);
  41. }
  42. else if (sm_get_step(sm) == 20)
  43. { // chk comm state
  44. if (comm_get_state(comm) == COMMST_NORMAL)
  45. {
  46. log_dbg("%s, idx:%d, state:%s, step:%d, comm ok, goto ready",
  47. __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
  48. tongfei_set_dev_ctlmode(idx,0);
  49. //tongfei_set_dev_runmode(idx,4);
  50. tongfei_set_dev_stop(idx); // 进入 ready 自启动
  51. sm_set_state(sm, SMST_READY, MC0051GSERR_NONE);
  52. }
  53. else
  54. {
  55. log_dbg("%s, idx:%d, state:%s, step:%d, comm err, goto err",
  56. __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
  57. sm_set_state(sm, SMST_ERR, MC0051GSERR_LAUNCH_COMMERR);
  58. }
  59. }
  60. }
  61. static void tongfei_sm_err(int idx)
  62. {
  63. struct tongfei_t *dev = &tf[idx];
  64. struct comm_t *comm = &dev->comm;
  65. struct statemachine_t *sm = &dev->sm;
  66. static double ts_last_try;
  67. double ts;
  68. if (sm_get_step(sm) == 0)
  69. { // entry
  70. tongfei_reset_cmd(idx);
  71. sm_set_step(sm, 10);
  72. ts_last_try = sm_get_timeofday();
  73. }
  74. else if (sm_get_step(sm) == 10)
  75. { // wait cmd
  76. ts = sm_get_timeofday();
  77. if (tongfei_get_cmd(idx) == CMD_SM_READY)
  78. {
  79. log_dbg("%s, idx:%d, state:%s, step:%d, get ready cmd, try to comm",
  80. __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
  81. tongfei_reset_cmd(idx);
  82. tongfei_comm_reset(idx);
  83. sm_set_step(sm, 20);
  84. }
  85. else if (ts - ts_last_try > 60000)
  86. { // 60s
  87. ts_last_try = ts;
  88. tongfei_comm_reset(idx);
  89. sm_set_step(sm, 20);
  90. }
  91. }
  92. else if (sm_get_step(sm) == 20)
  93. { // chk comm state
  94. if (comm_get_state(comm) == COMMST_NORMAL)
  95. {
  96. log_dbg("%s, idx:%d, state:%s, step:%d, comm ok, goto ready", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
  97. sm_set_state(sm, SMST_READY, MC0051GSERR_NONE);
  98. }
  99. else
  100. {
  101. log_dbg("%s, idx:%d, state:%s, step:%d, comm err, stay err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
  102. sm_set_state(sm, SMST_ERR, MC0051GSERR_ERR_COMMERR);
  103. }
  104. }
  105. }
  106. static void tongfei_sm_ready(int idx)
  107. {
  108. struct tongfei_t *dev = &tf[idx];
  109. struct comm_t *comm = &dev->comm;
  110. struct statemachine_t *sm = &dev->sm;
  111. /* chk comm state */
  112. if (comm_get_state(comm) != COMMST_NORMAL)
  113. {
  114. log_dbg("%s, idx:%d, state:%s, step:%d, comm err detected, goto err",
  115. __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
  116. sm_set_state(sm, SMST_ERR, MC0051GSERR_READY_COMMERR);
  117. return;
  118. }
  119. if (sm_get_step(sm) == 0)
  120. {
  121. if(pack_get_celltmax()>=dev->cool_start_temp)
  122. {
  123. tongfei_set_dev_runmode(idx,1);
  124. tongfei_set_dev_start(idx);
  125. sm_set_step(sm, 10);
  126. }
  127. else if(pack_get_celltmin()<=dev->heat_start_temp)
  128. {
  129. tongfei_set_dev_runmode(idx,2);
  130. tongfei_set_dev_start(idx);
  131. sm_set_step(sm, 11);
  132. }
  133. }
  134. else if(sm_get_step(sm) == 10)//制冷
  135. {
  136. if(pack_get_celltmax()<=dev->cool_stop_temp)
  137. {
  138. tongfei_set_dev_stop(idx);
  139. sm_set_step(sm, 0);
  140. }
  141. }
  142. else if(sm_get_step(sm) == 11)//加热
  143. {
  144. if(pack_get_celltmin()>=dev->heat_stop_temp)
  145. {
  146. tongfei_set_dev_stop(idx);
  147. sm_set_step(sm, 0);
  148. }
  149. }
  150. else
  151. {
  152. sm_set_step(sm, 0);
  153. }
  154. }
  155. void *tongfei_sm(int idx)
  156. {
  157. struct tongfei_t *dev = &tf[idx];
  158. struct statemachine_t *sm = &dev->sm;
  159. sm_cal_timing(sm);
  160. switch (sm_get_state(sm))
  161. {
  162. case SMST_LAUNCH:
  163. tongfei_sm_launch(idx);
  164. break;
  165. case SMST_READY:
  166. tongfei_sm_ready(idx);
  167. break;
  168. case SMST_ERR:
  169. tongfei_sm_err(idx);
  170. break;
  171. default:
  172. log_dbg("%s unknown state : %d", __func__, sm_get_state(sm));
  173. break;
  174. }
  175. }