123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885 |
- #include "plt.h"
- static struct state_t n9_states[] = {
- {SMST_LAUNCH, "launch"},
- {SMST_STDBY, "stdby"},
- {SMST_STOP, "stop"},
- {SMST_READY, "ready"},
- {SMST_DHG, "dhg"},
- {SMST_CHG, "chg"},
- {SMST_OFFGRID, "offgrid"},
- {SMST_ERR, "err"},
- };
- static struct err_t n9_errs[] = {
- {N9ERR_NONE, "none"},
- // launch IES1000ERR_LAUNCH_COMMERR
- {N9ERR_LAUNCH_COMMERR, "launch, comm err"},
- // err
- {N9ERR_ERR_COMMERR, "err, comm err"},
- {N9ERR_ERR_PWRUP, "err, pwrup"},
- {N9ERR_ERR_PWROFF, "err, power off"},
- // stdby
- {N9ERR_STDBY_COMMERR, "stdby, comm err"},
- {N9ERR_STDBY_CHK_DEVAPS0_FAIL_AFTER_SET, "stdby, chk aps = 0 fail"},
- {N9ERR_STDBY_WAIT_STOP_TIMEOUT, "stdby, chk stop fail"},
- // stop
- {N9ERR_STOP_COMMERR, "stop, comm err"},
- {N9ERR_STOP_NONE_STOP_DETECTED, "stop, none stop run state detected"},
- {N9ERR_STOP_CHK_DEVAPS0_FAIL_AFTER_SET, "stop, chk dev aps = 0 fail"},
- {N9ERR_STOP_WAIT_START_TIMEOUT, "stop, wait start timeout"},
- // ready
- {N9ERR_READY_COMMERR, "ready, comm err"},
- {N9ERR_READY_NOSTART_DETECTED, "ready, no start detected"},
- {N9ERR_READY_WAIT_STOP_TIMEOUT, "ready, wait stop timeout"},
- {N9ERR_READY_WAIT_AP_OVER_0_TIMEOUT, "ready, wait ap over zero time out"},
- {N9ERR_READY_WAIT_AP_UNDER_0_TIMEOUT, "ready, wait ap under zero time out"},
- // dhg
- {N9ERR_DHG_COMMERR, "dhg, comm err"},
- {N9ERR_DHG_NONDHG_DETECTED, "dhg, none dhg detected"},
- {N9ERR_DHG_WAIT_DEVAPS0_FOR_READYCMD_TIMEOUT, "dhg, wait set dev aps = 0 time out"},
- {N9ERR_DHG_WAIT_AP0_FOR_READYCMD_TIMEOUT, "dhg, wait ap = 0 for ready cmd time out"},
- {N9ERR_DHG_TICK_TIMEOUT_DETECTED, "dhg, tick timeout"},
- // chg
- {N9ERR_CHG_COMMERR, "chg, comm err"},
- {N9ERR_CHG_NONCHG_DETECTED, "chg, none dhg detected"},
- {N9ERR_CHG_WAIT_DEVAPS0_FOR_READYCMD_TIMEOUT, "chg, wait set dev aps = 0 time out"},
- {N9ERR_CHG_WAIT_AP0_FOR_READYCMD_TIMEOUT, "chg, wait ap = 0 for ready cmd time out"},
- {N9ERR_CHG_TICK_TIMEOUT_DETECTED, "chg, tick timeout"},
- // offgrid
- {N9ERR_OFFGRID_ERRSTAT_DETECTED, "offgrid, err state detected"},
- {N9ERR_OFFGRID_NON_DHGCHG_DETECTED, "offgrid, non dhg chg state detected"},
- };
- int n9_sm_init(int idx)
- {
- struct statemachine_t *sm = &n9[idx].sm;
- sm_reset_timing(sm, 10, 10);
- sm->states = n9_states;
- sm->state_nbr = sizeof(n9_states) / sizeof(struct state_t);
- sm->errs = n9_errs;
- sm->err_nbr = sizeof(n9_errs) / sizeof(struct err_t);
- sm_set_state(sm, SMST_LAUNCH, N9ERR_NONE);
- return 0;
- }
- static void n9_sm_launch(int idx)
- {
- struct n9_t *dev = &n9[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));
- n9_reset_cmd(idx);
- n9_comm_reset(idx);
- sm_set_step(sm, 20);
- }
- else if (sm_get_step(sm) == 20)
- {
- if (comm_get_state(comm) == COMMST_NORMAL)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, comm ok, goto stdby",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_STDBY, N9ERR_NONE);
- }
- else
- {
- 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, N9ERR_LAUNCH_COMMERR);
- }
- }
- }
- static void n9_sm_err(int idx)
- {
- struct n9_t *dev = &n9[idx];
- struct comm_t *comm = &n9[idx].comm;
- struct statemachine_t *sm = &n9[idx].sm;
- static double ts_last_try;
- double ts;
- 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));
- n9_reset_cmd(idx);
- n9_set_dev_stopcmd(idx); // if err, stop device immediately
- 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 (n9_get_cmd(idx) == CMD_SM_STDBY)
- { // stdby cmd
- log_dbg("%s, idx:%d, state:%s, step:%d, get stdby cmd, reset comm then chk",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- n9_reset_cmd(idx);
- n9_comm_reset(idx);
- sm_set_step(sm, 20);
- }
- else if (comm_get_state(comm) != COMMST_NORMAL && (ts - ts_last_try > 60000))
- { // 60s
- log_dbg("%s, idx:%d, state:%s, step:%d,comm:%s,reset comm",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), comm_get_state_str(comm));
- ts_last_try = ts;
- n9_comm_reset(idx);
- }
- }
- 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 stdby",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_STDBY, N9ERR_NONE);
- }
- else
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, comm err detected, stay err",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_ERR, N9ERR_ERR_COMMERR);
- }
- }
- }
- static void n9_sm_stdby(int idx)
- {
- struct n9_t *dev = &n9[idx];
- struct comm_t *comm = &n9[idx].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, N9ERR_STDBY_COMMERR);
- return;
- }
- if (sm_get_step(sm) == 0)
- { /* entry */
- n9_reset_cmd(idx);
- // n9_set_dev_resetcmd(idx); // cyx : stdby : 各种设备保持原样
- // n9_set_dev_stopcmd(idx);
- sm_set_step(sm, 10);
- }
- else if (sm_get_step(sm) == 10)
- { /* wait cmd */
- if (n9_get_cmd(idx) == CMD_SM_STOP)
- { /* stop cmd */
- log_dbg("%s, idx:%d, state:%s, step:%d, get stop cmd, chk devaps = 0 ?", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- n9_reset_cmd(idx);
- sm_set_step(sm, 20);
- }
- }
- else if (sm_get_step(sm) == 20)
- { /* chk devaps = 0 ? */
- if (n9_get_dev_aps(idx) != 0)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, devaps != 0 detected, set it to 0, wait and chk", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- n9_set_dev_aps(idx, 0);
- sm_set_step(sm, 30);
- }
- else
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, devaps = 0 detected, chk stop state", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_step(sm, 40);
- }
- }
- else if (sm_get_step(sm) == 30)
- { /* wait and chk devaps = 0 */
- if (n9_get_dev_aps(idx) == 0)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, devaps = 0 detected, chk stop state", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_step(sm, 40);
- }
- else
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, chk devaps = 0 fail after set, goto err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_ERR, N9ERR_STDBY_CHK_DEVAPS0_FAIL_AFTER_SET);
- }
- }
- else if (sm_get_step(sm) == 40)
- { /* chk runstate = stop ? */
- if (N9_RUNSTAT_IDLE != n9_get_runstat(idx))
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, non-stop run state detected, send stop cmd, wait and chk", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- n9_set_dev_stopcmd(idx); // when stdby->stop, stop device
- sm_set_step(sm, 50);
- sm_set_count(sm, 0);
- }
- else
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, stop run state detected, goto stop",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_STOP, N9ERR_NONE);
- }
- }
- else if (sm_get_step(sm) == 50)
- { /* wait run state = stop after stopcmd*/
- if (N9_RUNSTAT_IDLE == n9_get_runstat(idx))
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, stop run state detected, goto stop", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_STOP, N9ERR_NONE);
- }
- else
- {
- sm_inc_count(sm);
- if (sm_get_count(sm) >= 100)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, wait stop run state timeout, goto err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_ERR, N9ERR_STDBY_WAIT_STOP_TIMEOUT);
- }
- else
- {
- if (sm_get_count(sm) % 50 == 0)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, waiting stop run state, count:%d", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm));
- }
- }
- }
- }
- }
- static void n9_sm_offgrid(int idx)
- {
- // struct ies1000_t* dev = &ies1000[idx];
- // struct comm_t* comm = &ies1000[idx].comm;
- // struct statemachine_t* sm = &ies1000[idx].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, IES1000ERR_STOP_COMMERR);
- // return;
- // }
- // if( sm_get_step(sm) == 0 ){ // entry
- // ies1000_reset_cmd(idx);
- // sm_set_step(sm, 10);
- // }else if( sm_get_step(sm) == 10 ){ // wait cmd and chk
- // if( ies1000_get_cmd(idx) == CMD_SM_READY ){ // ready cmd
- // log_dbg("%s, idx:%d, state:%s, step:%d, get ready cmd, set dev idlecmd, goto ready",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- // ies1000_reset_cmd(idx);
- // ies1000_set_dev_idlecmd(idx);
- // sm_set_state(sm, SMST_READY, IES1000ERR_NONE);
- // }else if( ies1000_get_errstat(idx) != 0){
- // log_dbg("%s, idx:%d, state:%s, step:%d, err state detected, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- // sm_set_state(sm, SMST_ERR, IES1000ERR_OFFGRID_ERRSTAT_DETECTED);
- // }else if( ies1000_get_runstat(idx) != IES1000_RUNSTAT_CHG && ies1000_get_runstat(idx) != IES1000_RUNSTAT_DHG){
- // log_dbg("%s, idx:%d, state:%s, step:%d, non dhg chg run state detected, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- // sm_set_state(sm, SMST_ERR, IES1000ERR_OFFGRID_NON_DHGCHG_DETECTED);
- // }else{
- // }
- // }
- }
- static void n9_sm_stop(int idx)
- {
- struct n9_t *dev = &n9[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, N9ERR_STOP_COMMERR);
- return;
- }
- if (sm_get_step(sm) == 0)
- { // entry
- n9_reset_cmd(idx);
- n9_set_aps(idx, 0);
- sm_set_step(sm, 10);
- }
- else if (sm_get_step(sm) == 10)
- { // wait cmd and chk
- if (n9_get_cmd(idx) == CMD_SM_STDBY)
- { // stdby cmd
- log_dbg("%s, idx:%d, state:%s, step:%d, get stdby cmd, goto stdby", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- n9_reset_cmd(idx);
- sm_set_state(sm, SMST_STDBY, N9ERR_NONE);
- }
- else if (n9_get_cmd(idx) == CMD_SM_READY)
- { // ready cmd
- log_dbg("%s, idx:%d, state:%s, step:%d, get ready cmd, set dev resetcmd, chk dev aps", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- n9_reset_cmd(idx);
- sm_set_step(sm, 20);
- n9_set_dev_resetcmd(idx);
- }
- else
- { /* no cmd, do chking */
- if (N9_RUNSTAT_IDLE != n9_get_runstat(idx))
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, run state != stop detected, goto err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_ERR, N9ERR_STOP_NONE_STOP_DETECTED);
- }
- }
- }
- else if (sm_get_step(sm) == 20)
- { /* chk devaps = 0 ? */
- if (n9_get_dev_aps(idx) != 0)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, dev aps != 0 detected, set it to 0, then chk", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- n9_set_dev_aps(idx, 0);
- sm_set_step(sm, 30);
- }
- else
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, dev aps = 0 detected, goto set pcs start cmd", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_step(sm, 40);
- }
- }
- else if (sm_get_step(sm) == 30)
- { /* wait and chk devaps = 0 */
- if (n9_get_dev_aps(idx) == 0)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, dev aps = 0 detected,got set pcs start cmd", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_step(sm, 40);
- }
- else
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, chk dev aps = 0 fail, goto err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_ERR, N9ERR_STOP_CHK_DEVAPS0_FAIL_AFTER_SET);
- }
- }
- else if (sm_get_step(sm) == 40)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, set pcs start cmd and goto wait pcs start", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- n9_set_dev_startcmd(idx);
- sm_set_step(sm, 50);
- }
- else if (sm_get_step(sm) == 50)
- { // wait pcs idle
- if (N9_RUNSTAT_RUN == n9_get_runstat(idx))
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, chk pcs start ok, goto ready", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_READY, N9ERR_NONE);
- }
- else
- {
- sm_inc_count(sm);
- if (sm_get_count(sm) >= 200)
- { /* should over 10s */
- sm_set_state(sm, SMST_ERR, N9ERR_STOP_WAIT_START_TIMEOUT);
- log_dbg("%s, idx:%d, state:%s, step:%d, wait pcs start timeout, goto err",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- }
- else
- {
- if (sm_get_count(sm) % 50 == 0)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, waiting wait pcs start, count:%d",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm));
- }
- }
- }
- }
- }
- static void n9_sm_ready(int idx)
- {
- struct n9_t *dev = &n9[idx];
- struct comm_t *comm = &dev->comm;
- struct statemachine_t *sm = &dev->sm;
- int aps = n9_get_aps(idx);
- int ap = n9_get_ap(idx);
- /* 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, N9ERR_READY_COMMERR);
- return;
- }
- 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));
- n9_reset_cmd(idx);
- n9_reset_aps(idx);
- sm_set_step(sm, 10);
- }
- else if (sm_get_step(sm) == 10)
- { // wait and chk
- if (n9_get_cmd(idx) == CMD_SM_STOP)
- { // stop cmd
- log_dbg("%s, idx:%d, state:%s, step:%d, get stop cmd, dev stop cmd sent, wait and chk", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- n9_reset_cmd(idx);
- n9_set_dev_stopcmd(idx);
- sm_set_step(sm, 20);
- sm_set_count(sm, 0);
- }
- else if (aps > 0)
- { // new aps, prepare to dhg
- log_dbg("%s, idx:%d, state:%s, step:%d, aps > 0 detected, %d, set dev aps, wait and chk",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), aps);
- n9_set_dev_aps(idx, aps);
- sm_set_step(sm, 30);
- sm_set_count(sm, 0);
- }
- else if (aps < 0)
- { // new aps, prepare to chg
- log_dbg("%s, idx:%d, state:%s, step:%d, aps < 0 detected, %d, set dev aps, wait and chk",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), aps);
- n9_set_dev_aps(idx, aps);
- sm_set_step(sm, 40);
- sm_set_count(sm, 0);
- } /*else if( ies1000_get_cmd(idx) == CMD_SM_OFFGRID ){ // offgrid cmd
- log_dbg("%s, idx:%d, state:%s, step:%d, get offgrid cmd, set dev run mode = offgrid, wait and chk",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- ies1000_reset_cmd(idx);
- ies1000_set_dev_runmod(idx, IES1000_RUNMOD_OFFGRID);
- sm_set_step(sm, 500);
- sm_set_count(sm, 0);
- }*/
- else if (n9_get_runstat(idx) != N9_RUNSTAT_RUN)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, run state != idle detected, %d, goto err",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), n9_get_runstat(idx));
- sm_set_state(sm, SMST_ERR, N9ERR_READY_NOSTART_DETECTED);
- }
- }
- else if (sm_get_step(sm) == 20)
- { /* wait and chk run state = stop */
- if (n9_get_runstat(idx) == N9_RUNSTAT_IDLE)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, dev run state = stop detected, goto stop",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_STOP, N9ERR_NONE);
- }
- else
- {
- sm_inc_count(sm);
- if (sm_get_count(sm) >= 100)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, wait dev run state = stop timeout, goto err",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_ERR, N9ERR_READY_WAIT_STOP_TIMEOUT);
- }
- else
- {
- if (sm_get_count(sm) % 50 == 0)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, waiting dev run state = stop, count:%d",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm));
- }
- }
- }
- }
- else if (sm_get_step(sm) == 30)
- { // chk ap > 0
- if (ap > 0)
- { /* chk ok */
- log_dbg("%s, idx:%d, state:%s, step:%d, ap > 0 detected, %d, goto dhg",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), ap);
- sm_set_state(sm, SMST_DHG, N9ERR_NONE);
- }
- else
- {
- sm_inc_count(sm);
- if (sm_get_count(sm) >= 100)
- {
- sm_set_state(sm, SMST_ERR, N9ERR_READY_WAIT_AP_OVER_0_TIMEOUT);
- log_dbg("%s, idx:%d, state:%s, step:%d, wait ap > 0 for dhg timeout, goto err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- }
- else
- {
- if (sm_get_count(sm) % 50 == 0)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, waiting ap > 0 for dhg, count:%d",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm));
- }
- }
- }
- }
- else if (sm_get_step(sm) == 40)
- { // chk ap for chg
- if (ap < 0)
- { /* chk ok */
- log_dbg("%s, idx:%d, state:%s, step:%d, ap = %d < 0 detected, goto chg",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), ap);
- sm_set_state(sm, SMST_CHG, N9ERR_NONE);
- }
- else
- {
- sm_inc_count(sm);
- if (sm_get_count(sm) >= 100)
- {
- sm_set_state(sm, SMST_ERR, N9ERR_READY_WAIT_AP_UNDER_0_TIMEOUT);
- log_dbg("%s, idx:%d, state:%s, step:%d, wait ap < 0 for chg timeout, goto err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- }
- else
- {
- if (sm_get_count(sm) % 50 == 0)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, waiting ap < 0 for chg, count:%d",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm));
- }
- }
- }
- } /*else if( sm_get_step(sm) == 500 ){ // wait run mode = offgrid for offgrid
- if( IES1000_RUNMOD_OFFGRID == ies1000_get_runmod(idx) ){
- log_dbg("%s, idx:%d, state:%s, step:%d, chk run mode = offgrid ok, set dev startcmd, wait and chk voltage",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- ies1000_set_dev_startcmd(idx);
- sm_set_step(sm, 501);
- sm_set_count(sm, 0);
- }else{
- sm_inc_count(sm);
- if( sm_get_count(sm) >= 100 ){ // 5s
- sm_set_state(sm, SMST_ERR, IES1000ERR_READY_WAIT_RUNMOD_OFFGRID_FOR_OFFGRID_TIMEOUT);
- log_dbg("%s, idx:%d, state:%s, step:%d, wait run mode = offgrid timeout, goto err",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- }else{
- log_dbg("%s, idx:%d, state:%s, step:%d, waiting run mode = offgrid, count:%d",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm));
- }
- }
- }else if( sm_get_step(sm) == 501 ){ // chk voltage for offgrid
- if( ies1000_get_ua(idx) > 340.0 && ies1000_get_ua(idx) < 420.0){
- log_dbg("%s, idx:%d, state:%s, step:%d, chk voltage ok, %.1f, goto offgrid",__func__, idx, sm_get_szstate(sm), sm_get_step(sm), ies1000_get_ua(idx));
- sm_set_state(sm, SMST_OFFGRID, IES1000ERR_NONE);
- }else{
- sm_inc_count(sm);
- if( sm_get_count(sm) >= 300 ){ // 15s
- log_dbg("%s, idx:%d, state:%s, step:%d, wait voltage to 380 for offgrid timeout, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_ERR, IES1000ERR_READY_WAIT_TARGET_VOLTAGE_FOR_OFFGRID_TIMEOUT);
- }else{
- log_dbg("%s, idx:%d, state:%s, step:%d, wait target voltage for offgrid, count:%d",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm));
- }
- }
- }*/
- }
- static void n9_sm_dhg(int idx)
- {
- struct n9_t *dev = &n9[idx];
- struct comm_t *comm = &dev->comm;
- struct statemachine_t *sm = &dev->sm;
- int aps = n9_get_aps(idx);
- int last_aps = n9_get_last_aps(idx);
- int ap = n9_get_ap(idx);
- /* 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, N9ERR_DHG_COMMERR);
- return;
- }
- 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));
- n9_reset_cmd(idx);
- sm_set_step(sm, 10);
- n9_reset_bsytikchk(idx);
- gt_reset_bsytikchk(idx);
- }
- else if (sm_get_step(sm) == 10)
- { // wait and chk
- if (n9_get_cmd(idx) == CMD_SM_READY)
- { // ready cmd
- log_dbg("%s, idx:%d, state:%s, step:%d, got ready cmd, set dev aps = 0, wait and chk",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- n9_reset_cmd(idx);
- n9_set_dev_aps(idx, 0);
- sm_set_step(sm, 30);
- sm_set_count(sm, 0);
- }
- else if (n9_get_runstat(idx) != N9_RUNSTAT_RUN)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, run state != dhg detected, set dev aps to 0, goto err",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- n9_set_dev_aps(idx, 0);
- sm_set_state(sm, SMST_ERR, N9ERR_DHG_NONDHG_DETECTED);
- }
- else if (aps < 0)
- { // aps <= 0
- log_dbg("%s, idx:%d, state:%s, step:%d, aps <= 0 detected, %d, set dev aps = 0, wait and chk",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), aps);
- n9_set_dev_aps(idx, 0);
- sm_set_step(sm, 30);
- sm_set_count(sm, 0);
- }
- else if (n9_is_aps_changed(idx))
- { // new aps, set devaps and chk
- log_dbg("%s, idx:%d, state:%s, step:%d, new aps detected, %d -> %d, set dev aps",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), last_aps, aps);
- n9_set_aps(idx, aps); // update last aps
- n9_set_dev_aps(idx, aps);
- }
- else if (n9_is_bsytikchk_timeout(idx))
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, bsytik timeout detected, set dev aps to 0",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- n9_set_aps(idx, 0);
- }
- }
- else if (sm_get_step(sm) == 30)
- { // chk dev aps = 0 for readycmd
- if (n9_get_dev_aps(idx) == 0)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, chk dev aps = 0 ok, wait and chk ap",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_step(sm, 31);
- sm_set_count(sm, 0);
- }
- else
- {
- sm_inc_count(sm);
- if (sm_get_count(sm) >= 100)
- {
- sm_set_state(sm, SMST_ERR, N9ERR_DHG_WAIT_DEVAPS0_FOR_READYCMD_TIMEOUT);
- log_dbg("%s, idx:%d, state:%s, step:%d, wait dev aps = 0 for readycmd timeout, goto err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- }
- else
- {
- if (sm_get_count(sm) % 50 == 0)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, waiting dev aps = 0 for readycmd, count:%d",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm));
- }
- }
- }
- }
- else if (sm_get_step(sm) == 31)
- { // chk ap = 0 for readycmd
- if (ap < 2 && ap > -2)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, chk ap ok for readycmd, ap now: %d, goto ready",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), ap);
- if (n9_is_bsytikchk_timeout(idx))
- {
- sm_set_state(sm, SMST_ERR, N9ERR_DHG_TICK_TIMEOUT_DETECTED);
- }
- else
- {
- sm_set_state(sm, SMST_READY, N9ERR_NONE);
- }
- }
- else
- {
- sm_inc_count(sm);
- if (sm_get_count(sm) >= 100)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, wait ap = 0 for readycmd timeout, ap still: %d, goto err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), ap);
- sm_set_state(sm, SMST_ERR, N9ERR_DHG_WAIT_AP0_FOR_READYCMD_TIMEOUT);
- }
- else
- {
- if (sm_get_count(sm) % 50 == 0)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, waiting ap = 0 for readycmd, count:%d, ap still:%d",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm), ap);
- }
- }
- }
- }
- }
- static void n9_sm_chg(int idx)
- {
- struct n9_t *dev = &n9[idx];
- struct comm_t *comm = &dev->comm;
- struct statemachine_t *sm = &dev->sm;
- int aps = n9_get_aps(idx);
- int last_aps = n9_get_last_aps(idx);
- int ap = n9_get_ap(idx);
- /* 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, N9ERR_CHG_COMMERR);
- return;
- }
- 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));
- n9_reset_cmd(idx);
- sm_set_step(sm, 10);
- n9_reset_bsytikchk(idx);
- gt_reset_bsytikchk(idx);
- }
- else if (sm_get_step(sm) == 10)
- { // wait and chk
- if (n9_get_cmd(idx) == CMD_SM_READY)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, got ready cmd, set dev aps = 0",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- n9_reset_cmd(idx);
- n9_set_dev_aps(idx, 0);
- sm_set_step(sm, 30);
- sm_set_count(sm, 0);
- }
- else if (n9_get_runstat(idx) != N9_RUNSTAT_RUN)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, run state != chg detected, set dev aps to 0, goto err",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- n9_set_dev_aps(idx, 0);
- sm_set_state(sm, SMST_ERR, N9ERR_CHG_NONCHG_DETECTED);
- }
- else if (aps > 0)
- { // aps => 0
- log_dbg("%s, idx:%d, state:%s, step:%d, aps >= 0 detected, %d, set dev aps = 0, wait and chk",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), aps);
- n9_set_dev_aps(idx, 0);
- sm_set_step(sm, 30);
- sm_set_count(sm, 0);
- }
- else if (n9_is_aps_changed(idx))
- { /* new aps, set devaps and chk */
- log_dbg("%s, idx:%d, state:%s, step:%d, new aps detected, %d -> %d, set dev aps",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), last_aps, aps);
- n9_set_aps(idx, aps); // update last aps
- n9_set_dev_aps(idx, aps);
- }
- else if (n9_is_bsytikchk_timeout(idx))
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, bsytik timeout detected, set dev aps to 0",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- n9_set_aps(idx, 0);
- }
- }
- else if (sm_get_step(sm) == 30)
- { // chk dev aps = 0 for readycmd
- if (n9_get_dev_aps(idx) == 0)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, chk dev aps = 0 ok, wait and chk ap",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_step(sm, 31);
- sm_set_count(sm, 0);
- }
- else
- {
- sm_inc_count(sm);
- if (sm_get_count(sm) >= 100)
- {
- sm_set_state(sm, SMST_ERR, N9ERR_CHG_WAIT_DEVAPS0_FOR_READYCMD_TIMEOUT);
- log_dbg("%s, idx:%d, state:%s, step:%d, wait dev aps = 0 for readycmd timeout, goto err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- }
- else
- {
- if (sm_get_count(sm) % 50 == 0)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, waiting dev aps = 0 for readycmd, count:%d",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm));
- }
- }
- }
- }
- else if (sm_get_step(sm) == 31)
- { // chk ap = 0 for readycmd
- if (ap < 2 && ap > -2)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, chk ap ok for readycmd, ap now: %d, goto ready",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), ap);
- if (n9_is_bsytikchk_timeout(idx))
- {
- sm_set_state(sm, SMST_ERR, N9ERR_CHG_TICK_TIMEOUT_DETECTED);
- }
- else
- {
- sm_set_state(sm, SMST_READY, N9ERR_NONE);
- }
- }
- else
- {
- sm_inc_count(sm);
- if (sm_get_count(sm) >= 100)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, wait ap = 0 for readycmd timeout, ap still :%d, goto err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), ap);
- sm_set_state(sm, SMST_ERR, N9ERR_CHG_WAIT_AP0_FOR_READYCMD_TIMEOUT);
- }
- else
- {
- if (sm_get_count(sm) % 50 == 0)
- {
- log_dbg("%s, idx:%d, state:%s, step:%d, waiting ap = 0 for readycmd, count:%d, ap still :%d",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm), ap);
- }
- }
- }
- }
- }
- void n9_sm(int idx)
- {
- struct n9_t *dev = &n9[idx];
- struct statemachine_t *sm = &dev->sm;
- sm_cal_timing(&dev->sm);
- if (device_power_on == 0)
- {
- n9_set_dev_aps(idx, 0);
- n9_set_dev_stopcmd(idx);
- sm_set_state(sm, SMST_ERR, N9ERR_ERR_PWROFF);
- }
- switch (sm_get_state(sm))
- {
- case SMST_LAUNCH:
- n9_sm_launch(idx);
- break;
- case SMST_STDBY:
- n9_sm_stdby(idx);
- break;
- case SMST_STOP:
- n9_sm_stop(idx);
- break;
- case SMST_OFFGRID:
- n9_sm_offgrid(idx);
- break;
- case SMST_READY:
- n9_sm_ready(idx);
- break;
- case SMST_DHG:
- n9_sm_dhg(idx);
- break;
- case SMST_CHG:
- n9_sm_chg(idx);
- break;
- case SMST_ERR:
- n9_sm_err(idx);
- break;
- default:
- log_dbg("%s, unknown state, %d", __func__, n9_get_state(idx));
- break;
- }
- }
|