123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- #include "tf_sm.h"
- #include "tf.h"
- #include "tf_comm.h"
- #include "plt.h"
- static struct state_t tongfei_states[] = {
- {SMST_LAUNCH, "launch"},
- {SMST_READY, "ready"},
- {SMST_ERR, "err"},
- };
- static struct err_t tongfei_errs[] = {
- {TFERR_NONE, "none"},
- // launch
- {TFERR_LAUNCH_COMMERR, "launch, comm err"},
- // err
- {TFERR_ERR_COMMERR, "err comm err"},
- // ready
- {TFERR_READY_COMMERR, "ready comm err"},
- };
- int tongfei_sm_init(int idx)
- {
- struct statemachine_t *sm = &tf[idx].sm;
- sm_reset_timing(sm, 1, 1);
- sm->states = tongfei_states;
- sm->state_nbr = sizeof(tongfei_states) / sizeof(struct state_t);
- sm->errs = tongfei_errs;
- sm->err_nbr = sizeof(tongfei_errs) / sizeof(struct err_t);
- sm_set_state(sm, SMST_LAUNCH, MC0051GSERR_NONE);
- return 0;
- }
- static void tongfei_sm_launch(int idx)
- {
- struct tongfei_t *dev = &tf[idx];
- struct comm_t *comm = &dev->comm;
- struct statemachine_t *sm = &dev->sm;
- if (sm_get_step(sm) == 0)
- { // entry
- log_dbg("%s, idx:%d, state:%s, step:%d, entry", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- tongfei_reset_cmd(idx);
- tongfei_comm_reset(idx);
- sm_set_step(sm, 20);
- }
- else if (sm_get_step(sm) == 20)
- { // chk comm state
- if (comm_get_state(comm) == COMMST_NORMAL)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, comm ok, goto ready",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- tongfei_set_dev_ctlmode(idx,0);
- //tongfei_set_dev_runmode(idx,4);
- tongfei_set_dev_stop(idx); // 进入 ready 自启动
- sm_set_state(sm, SMST_READY, MC0051GSERR_NONE);
- }
- else
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, comm err, goto err",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_ERR, MC0051GSERR_LAUNCH_COMMERR);
- }
- }
- }
- static void tongfei_sm_err(int idx)
- {
- struct tongfei_t *dev = &tf[idx];
- struct comm_t *comm = &dev->comm;
- struct statemachine_t *sm = &dev->sm;
- static double ts_last_try;
- double ts;
- if (sm_get_step(sm) == 0)
- { // entry
- tongfei_reset_cmd(idx);
- sm_set_step(sm, 10);
- ts_last_try = sm_get_timeofday();
- }
- else if (sm_get_step(sm) == 10)
- { // wait cmd
- ts = sm_get_timeofday();
- if (tongfei_get_cmd(idx) == CMD_SM_READY)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, get ready cmd, try to comm",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- tongfei_reset_cmd(idx);
- tongfei_comm_reset(idx);
- sm_set_step(sm, 20);
- }
- else if (ts - ts_last_try > 60000)
- { // 60s
- ts_last_try = ts;
- tongfei_comm_reset(idx);
- sm_set_step(sm, 20);
- }
- }
- else if (sm_get_step(sm) == 20)
- { // chk comm state
- if (comm_get_state(comm) == COMMST_NORMAL)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, comm ok, goto ready", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_READY, MC0051GSERR_NONE);
- }
- else
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, comm err, stay err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_ERR, MC0051GSERR_ERR_COMMERR);
- }
- }
- }
- static void tongfei_sm_ready(int idx)
- {
- struct tongfei_t *dev = &tf[idx];
- struct comm_t *comm = &dev->comm;
- struct statemachine_t *sm = &dev->sm;
- /* chk comm state */
- if (comm_get_state(comm) != COMMST_NORMAL)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, comm err detected, goto err",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_ERR, MC0051GSERR_READY_COMMERR);
- return;
- }
- if (sm_get_step(sm) == 0)
- {
- if(pack_get_celltmax()>=dev->cool_start_temp)
- {
- tongfei_set_dev_runmode(idx,1);
- tongfei_set_dev_start(idx);
- sm_set_step(sm, 10);
- }
- else if(pack_get_celltmin()<=dev->heat_start_temp)
- {
- tongfei_set_dev_runmode(idx,2);
- tongfei_set_dev_start(idx);
- sm_set_step(sm, 11);
- }
- }
- else if(sm_get_step(sm) == 10)//制冷
- {
- if(pack_get_celltmax()<=dev->cool_stop_temp)
- {
- tongfei_set_dev_stop(idx);
- sm_set_step(sm, 0);
- }
- }
- else if(sm_get_step(sm) == 11)//加热
- {
- if(pack_get_celltmin()>=dev->heat_stop_temp)
- {
- tongfei_set_dev_stop(idx);
- sm_set_step(sm, 0);
- }
- }
- else
- {
- sm_set_step(sm, 0);
- }
- }
- void *tongfei_sm(int idx)
- {
- struct tongfei_t *dev = &tf[idx];
- struct statemachine_t *sm = &dev->sm;
- sm_cal_timing(sm);
- switch (sm_get_state(sm))
- {
- case SMST_LAUNCH:
- tongfei_sm_launch(idx);
- break;
- case SMST_READY:
- tongfei_sm_ready(idx);
- break;
- case SMST_ERR:
- tongfei_sm_err(idx);
- break;
- default:
- log_dbg("%s unknown state : %d", __func__, sm_get_state(sm));
- break;
- }
- }
|