123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850 |
- #include "plt.h"
- static struct state_t pws1_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 pws1_errs[] = {
- {PWS1ERR_NONE, "none"},
- // launch PWS1ERR_LAUNCH_COMMERR
- {PWS1ERR_LAUNCH_COMMERR, "launch, comm err"},
- // err
- {PWS1ERR_ERR_COMMERR, "err, comm err"},
- {PWS1ERR_ERR_PWRUP, "err, pwrup"},
-
- // stdby
- {PWS1ERR_STDBY_COMMERR, "stdby, comm err"},
- {PWS1ERR_STDBY_CHK_DEVAPS0_FAIL_AFTER_SET, "stdby, chk aps = 0 fail"},
- {PWS1ERR_STDBY_CHK_STOP_FAIL_AFTER_SET, "stdby, chk stop fail"},
- {PWS1ERR_STDBY_WAIT_STOP_TIMEOUT, "stdby, wait stop timeout"},
- {PWS1ERR_STDBY_WAIT_RUN_TIMEOUT, "stdby, wait run state timeout"},
- {PWS1ERR_STDBY_UNKOWN_RUNSTAT, "stdby, unknown run state"},
- // stop
- {PWS1ERR_STOP_COMMERR, "stop, comm err"},
- {PWS1ERR_STOP_NONE_STOP_DETECTED, "stop, none stop run state detected"},
- {PWS1ERR_STOP_CHK_DEVAPS0_FAIL_AFTER_SET, "stop, chk dev aps = 0 fail"},
- {PWS1ERR_STOP_CHK_ONGRID_FAIL_AFTER_SET, "stop, chk on grid fail after set"},
- {PWS1ERR_STOP_CHK_DISPATCH_AC_FAIL_AFTER_SET, "stop, chk dispatch ac fail after set"},
- {PWS1ERR_STOP_CHK_AC_CONTROL_MODEL_FIX_AC_POWER_FIAL_AFTER_SET, "stop, chk ac control fix power fail fater set"},
- {PWS1ERR_STOP_CHK_STATE_RUNNING_FAIL_AFTER_SET, "stop, chk state running fail after set"},
-
- // ready
- {PWS1ERR_READY_COMMERR, "ready, comm err"},
- {PWS1ERR_READY_WAIT_STOP_TIMEOUT, "ready, wait stop timeout"},
- {PWS1ERR_READY_WAIT_AP4DHG_TIMEOUT, "ready, wait ap for dhg timeout"},
- {PWS1ERR_READY_WAIT_DHG_STATE_TIME_OUT, "ready, wait dhg state timeout"},
- {PWS1ERR_READY_WAIT_AP4CHG_TIMEOUT, "ready, wait ap for chg timeout"},
- {PWS1ERR_READY_WAIT_CHG_STATE_TIME_OUT, "ready, wait chg state timeout"},
- {PWS1ERR_READY_WAIT_RUNSTATE_STOP_TIMEOUT_AFTER_SET, "ready, wait run state stop time out after set"},
- {PWS1ERR_READY_WAIT_RUNMODE_OFFGRID_TIMEOUT_AFTER_SET, "stop, wait run mode offgrid time out after set"},
- {PWS1ERR_READY_WAIT_RUNSTATE_RUNING_TIMEOUT_AFTER_SET, "stop, wait run state running time out after set"},
- {PWS1ERR_READY_WAIT_TARGET_VOLTAGE_FOR_OFFGRID_TIMEOUT, "stop, wait target voltage for offgrid timeout"},
-
-
- // dhg
- {PWS1ERR_DHG_COMMERR, "dhg, comm err"},
- {PWS1ERR_DHG_STATE_NOT_RUN_DETECTED, "dhg, not run detected"},
- {PWS1ERR_DHG_STATE_NOT_DHG_DETECTED, "dhg, non-dhg detected"},
- {PWS1ERR_DHG_WAIT_DEVAPS0_FOR_READYCMD_TIMEOUT, "dhg, wait ap = 0 timeout"},
- {PWS1ERR_DHG_WAIT_DEV_NO_DHG_TIME_OUT, "dhg, wait no dhg timeout"},
- // chg
- {PWS1ERR_CHG_COMMERR, "chg, comm err"},
- {PWS1ERR_CHG_STATE_NOT_RUN_DETECTED, "chg, not run detected"},
- {PWS1ERR_CHG_STATE_NOT_CHG_DETECTED, "chg, non-chg detected"},
- {PWS1ERR_CHG_WAIT_DEVAPS0_FOR_READYCMD_TIMEOUT, "chg, wait ap = 0 timeout"},
- {PWS1ERR_CHG_WAIT_DEV_NO_CHG_TIME_OUT, "chg, wait no chg timeout"},
- // offgrid
- {PWS1ERR_OFFGRID_ERRSTAT_DETECTED, "offgrid, err state detected"},
- {PWS1ERR_OFFGRID_NOT_RUNNING_DETECTED, "offgrid, not run detected"},
- {PWS1ERR_OFFGRID_WAIT_STOP_TIME_OUT, "offgrid, wait stop time out"},
- {PWS1ERR_OFFGRID_WAIT_RUN_MODE_ON_GIRD_TIME_OUT, "offgrid, wait run mode on grid time out"},
- };
- int pws1_sm_init(int idx)
- {
- struct statemachine_t* sm = &pws1[idx].sm;
- sm_reset_timing(sm, 10, 10);
- sm->states = pws1_states;
- sm->state_nbr = sizeof(pws1_states)/sizeof(struct state_t);
- sm->errs = pws1_errs;
- sm->err_nbr = sizeof(pws1_errs)/sizeof(struct err_t);
- sm_set_state( sm, SMST_LAUNCH, PWS1ERR_NONE );
-
- return 0;
- }
- static void pws1_sm_launch( int idx )
- {
- struct pws1_t* dev = &pws1[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));
- pws1_reset_cmd(idx);
- pws1_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 err detected, goto err",__func__, idx,sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_ERR, PWS1ERR_LAUNCH_COMMERR);
- }else if(pws1_get_runstat(idx) != PWS1_RUNSTAT_PCS_INIT && pws1_get_runstat(idx) != PWS1_RUNSTAT_UNKOWNING){
- log_dbg("%s, idx:%d, state:%s, step:%d, comm ok and pcs init finished,goto stdby!!",__func__, idx,sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_STDBY, PWS1ERR_NONE);
- }
- }
- }
- static void pws1_sm_err( int idx )
- {
- struct pws1_t* dev = &pws1[idx];
- struct comm_t* comm = &pws1[idx].comm;
- struct statemachine_t* sm = &pws1[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));
- pws1_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( pws1_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));
- pws1_reset_cmd(idx);
- pws1_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;
- pws1_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, PWS1ERR_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, PWS1ERR_ERR_COMMERR);
- }
- }
- }
- static void pws1_sm_stdby( int idx )
- {
- struct pws1_t* dev = &pws1[idx];
- struct comm_t* comm = &pws1[idx].comm;
- struct statemachine_t* sm = &pws1[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, PWS1ERR_STDBY_COMMERR);
- return;
- }
- if( sm_get_step(sm) == 0 ){ /* entry */
- pws1_reset_cmd(idx);
- sm_set_step(sm, 10);
- }else if( sm_get_step(sm) == 10 ){ /* wait cmd */
- if( pws1_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));
- pws1_reset_cmd(idx);
- sm_set_step(sm, 20);
- }
- }else if( sm_get_step(sm) == 20 ){ /* chk devaps = 0 ? */
- if( pws1_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));
- pws1_comm_set_active_power(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( pws1_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, PWS1ERR_STDBY_CHK_DEVAPS0_FAIL_AFTER_SET);
- }
- }else if( sm_get_step(sm) == 40 ){ /* chk runstate = stop ? */
- if( PWS1_RUNSTAT_STOP != pws1_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));
- pws1_comm_set_dev_stopcmd(idx);
- 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, PWS1ERR_NONE);
- }
- }else if( sm_get_step(sm) == 50 ){ /* wait run state = stop after stopcmd*/
- if( PWS1_RUNSTAT_STOP == pws1_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, PWS1ERR_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, PWS1ERR_STDBY_WAIT_STOP_TIMEOUT);
- }else if( sm_get_count(sm) % 10 == 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 pws1_sm_offgrid( int idx )
- {
- struct pws1_t* dev = &pws1[idx];
- struct comm_t* comm = &pws1[idx].comm;
- struct statemachine_t* sm = &pws1[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, PWS1ERR_STOP_COMMERR);
- return;
- }
- if( sm_get_step(sm) == 0 ){ // entry
- pws1_reset_cmd(idx);
- sm_set_step(sm, 10);
- }else if( sm_get_step(sm) == 10 ){ // wait cmd and chk
- if( pws1_get_cmd(idx) == CMD_SM_READY ){ // ready cmd
- log_dbg("%s, idx:%d, state:%s, step:%d, get ready cmd, set dev stopcmd, goto wait pcs stop",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- pws1_reset_cmd(idx);
- pws1_comm_set_dev_stopcmd(idx);
- sm_set_step(sm,20);
- sm_set_count(sm,0);
- }else if( pws1_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, PWS1ERR_OFFGRID_ERRSTAT_DETECTED);
- }else if( pws1_get_runstat(idx) != PWS1_RUNSTAT_CHG || pws1_get_runstat(idx) != PWS1_RUNSTAT_DHG || pws1_get_runstat(idx) != PWS1_RUNSTAT_IDLE){
- log_dbg("%s, idx:%d, state:%s, step:%d, runstate != DHG||CHG||IDLE,goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_ERR, PWS1ERR_OFFGRID_NOT_RUNNING_DETECTED);
- }else{
- }
- }else if( sm_get_step(sm) == 20){
- if(pws1_get_runstat(idx) == PWS1_RUNSTAT_STOP){
- log_dbg("%s, idx:%d, state:%s, step:%d,chk pcs run state stop ok,set pws1 on grid mode",__func__,idx,sm_get_szstate(sm),sm_get_step(sm));
- pws1_comm_set_grid_mode(idx,0);
- sm_set_step(sm,21);
- sm_set_count(sm,0);
- }else{
- sm_inc_count(sm);
- if(sm_get_count(sm) > 50){
- log_dbg("%s, idx:%d, state:%s, step:%d,wait pcs stop timeout,goto err",__func__,idx,sm_get_szstate(sm),sm_get_step(sm));
- sm_set_state(sm,SMST_ERR,PWS1ERR_OFFGRID_WAIT_STOP_TIME_OUT);
- }else if(sm_get_count(sm) % 10 == 0){
- log_dbg("%s, idx:%d, state:%s, step:%d,wait pcs goto stop run mode",__func__,idx,sm_get_szstate(sm),sm_get_step(sm));
- }
- }
- }else if( sm_get_step(sm) == 21){
- if(pws1_get_runmod(idx) == 0){
- log_dbg("%s, idx:%d, state:%s, step:%d,chk pcs run mode == ongride",__func__,idx,sm_get_szstate(sm),sm_get_step(sm));
- if(pws1_get_runstat(idx) == PWS1_RUNSTAT_IDLE){
- log_dbg("%s, idx:%d, state:%s, step:%d,chk pcs runstate == idle,goto ready",__func__,idx,sm_get_szstate(sm),sm_get_step(sm));
- sm_set_state(sm, SMST_READY, PWS1ERR_NONE);
- }else{
- log_dbg("%s, idx:%d, state:%s, step:%d,chk pcs runstate != idle,goto err",__func__,idx,sm_get_szstate(sm),sm_get_step(sm));
- sm_set_state(sm,SMST_ERR,PWS1ERR_OFFGRID_CHK_RUNSTATE_NOT_IDLE_AFTER_SET_ONGRIDE);
- }
- }else{
- sm_inc_count(sm);
- if(sm_get_count(sm) > 50){
- log_dbg("%s, idx:%d, state:%s, step:%d,wait pcs goto on gride mode time out,goto err",__func__,idx,sm_get_szstate(sm),sm_get_step(sm));
- sm_set_state(sm,SMST_ERR,PWS1ERR_OFFGRID_WAIT_RUN_MODE_ON_GIRD_TIME_OUT);
- }else if(sm_get_count(sm) % 10 == 0){
- log_dbg("%s, idx:%d, state:%s, step:%d,wait pcs goto on gride mode",__func__,idx,sm_get_szstate(sm),sm_get_step(sm));
- }
- }
- }
- }
- static void pws1_sm_stop( int idx )
- {
- struct pws1_t* dev = &pws1[idx];
- struct comm_t* comm = &pws1[idx].comm;
- struct statemachine_t* sm = &pws1[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, PWS1ERR_STOP_COMMERR);
- return;
- }
- if( sm_get_step(sm) == 0 ){ // entry
- pws1_reset_cmd(idx);
- pws1_set_aps(idx, 0);
- sm_set_step(sm, 10);
- }else if( sm_get_step(sm) == 10 ){ // wait cmd and chk
- if( pws1_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));
- pws1_reset_cmd(idx);
- sm_set_state(sm, SMST_STDBY, PWS1ERR_NONE);
- }else if( pws1_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));
- pws1_reset_cmd(idx);
- sm_set_step(sm, 20);
- pws1_comm_set_dev_resetcmd(idx);
- }else{ /* no cmd, do chking */
- if( PWS1_RUNSTAT_STOP != pws1_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, PWS1ERR_STOP_NONE_STOP_DETECTED);
- }
- }
- }else if( sm_get_step(sm) == 20 ){ /* chk devaps = 0 ? */
- if( pws1_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));
- pws1_comm_set_active_power(idx, 0);
- sm_set_step(sm, 21);
- }else{
- log_dbg("%s, idx:%d, state:%s, step:%d, dev aps = 0 detected, goto check runmode",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_step(sm, 30);
- }
- }else if( sm_get_step(sm) == 21 ){ /* wait and chk devaps = 0 */
- if( pws1_get_dev_aps(idx) == 0 ){
- log_dbg("%s, idx:%d, state:%s, step:%d, dev aps = 0 detected,goto check runmode",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_step(sm, 30);
- }else{
- sm_inc_count(sm);
- if(sm_get_count(sm) > 50){
- log_dbg("%s, idx:%d, state:%s, step:%d, chk wait aps=0 time out, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_ERR, PWS1ERR_STOP_CHK_DEVAPS0_FAIL_AFTER_SET);
- }else if(sm_get_count(sm) % 10 == 0){
- log_dbg("%s, idx:%d, state:%s, step:%d, chk waiting aps=0",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- }else{
- //wait
- }
- }
- }else if( sm_get_step(sm) == 30 ){ // chk pcs runmod == ongrid
- if( PWS1_RUNMOD_ONGRID == pws1_get_runmod(idx) ){
- log_dbg("%s, idx:%d, state:%s, step:%d, chk pcs runmod == ongrid ok, goto check dispatch mode",__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 pcs runmod != ongrid, set pcs ongrid mode and got check",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- pws1_comm_set_grid_mode(idx,0);
- sm_set_step(sm,31);
- sm_set_count(sm,0);
- }
- }else if( sm_get_step(sm) == 31){
- if( pws1_get_runmod(idx) == PWS1_RUNMOD_ONGRID){
- log_dbg("%s, idx:%d, state:%s, step:%d, chk pcs runmod == ongrid ok, goto check dispatch mode",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_step(sm, 40);
- }else{
- sm_inc_count(sm);
- if(sm_get_count(sm) > 50){
- log_dbg("%s, idx:%d, state:%s, step:%d, wait runmod == ongrid time out, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_ERR, PWS1ERR_STOP_CHK_ONGRID_FAIL_AFTER_SET);
- }else if(sm_get_count(sm) % 10 == 0){
- log_dbg("%s, idx:%d, state:%s, step:%d, waiting runmod == ongrid",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- }else{
- //wait
- }
- }
- }else if( sm_get_step(sm) == 40 ){ // chk pcs dispatching mode == ac dispatching
- if( PWS1_DISPATCH_MODE_AC == pws1_get_dispatch_mode(idx) ){
- log_dbg("%s, idx:%d, state:%s, step:%d, chk pcs dispatch mode == AC, goto chk power control mode",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_step(sm,50);
- }else{
- log_dbg("%s, idx:%d, state:%s, step:%d, set pcs dispatch mode = ac and goto check",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- pws1_comm_set_dispatching_mode(idx,0);
- sm_set_step(sm,41);
- sm_set_count(sm,0);
- }
- }else if( sm_get_step(sm) == 41){
- if( PWS1_DISPATCH_MODE_AC == pws1_get_dispatch_mode(idx) ){
- log_dbg("%s, idx:%d, state:%s, step:%d, chk pcs dispatch mode == AC, goto chk power control mode",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_step(sm,50);
- }else{
- sm_inc_count(sm);
- if( sm_get_count(sm) >= 50 ){ /* 2.5s */
- sm_set_state(sm, SMST_ERR, PWS1ERR_STOP_CHK_DISPATCH_AC_FAIL_AFTER_SET);
- log_dbg("%s, idx:%d, state:%s, step:%d, wait dispatch mode == ac time out goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- }else{
- log_dbg("%s, idx:%d, state:%s, step:%d, waiting dispatch mode == ac count:%d",__func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm));
- }
- }
- }else if( sm_get_step(sm) == 50 ){ // chk pcs dispatching mode == ac dispatching
- if( PWS1_AC_POWER_CONTROL_MODE_FIX_AC_POWER == pws1_get_active_power_control_mode(idx) ){
- log_dbg("%s, idx:%d, state:%s, step:%d, chk pcs ac power_control == fix ac power, goto chk run state",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_step(sm,60);
- }else{
- log_dbg("%s, idx:%d, state:%s, step:%d, set pcs ac power_control == fix ac power and goto check",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- pws1_comm_set_active_power_control_mode(idx,0);
- sm_set_step(sm,51);
- sm_set_count(sm,0);
- }
- }else if( sm_get_step(sm) == 51){
- if( PWS1_AC_POWER_CONTROL_MODE_FIX_AC_POWER == pws1_get_active_power_control_mode(idx) ){
- log_dbg("%s, idx:%d, state:%s, step:%d, chk pcs ac power_control == fix ac power, goto chk run state",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_step(sm,60);
- }else{
- sm_inc_count(sm);
- if( sm_get_count(sm) >= 50 ){ /* 2.5s */
- sm_set_state(sm, SMST_ERR, PWS1ERR_STOP_CHK_AC_CONTROL_MODEL_FIX_AC_POWER_FIAL_AFTER_SET);
- log_dbg("%s, idx:%d, state:%s, step:%d, wait ac power_control == fix ac power time out goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- }else{
- log_dbg("%s, idx:%d, state:%s, step:%d, waiting ac power_control == fix ac power count:%d",__func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm));
- }
- }
- }else if( sm_get_step(sm) == 60){
- if(pws1_get_runstat(idx) == PWS1_RUNSTAT_IDLE){
- log_dbg("%s, idx:%d, state:%s, step:%d, set pcs running state == idle and goto ready",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_READY, PWS1ERR_NONE);
- }else{
- log_dbg("%s, idx:%d, state:%s, step:%d, set pcs running state,and goto chk",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- pws1_comm_set_dev_startcmd(idx);
- sm_set_step(sm,61);
- sm_set_count(sm,0);
- }
- }else if( sm_get_step(sm) == 61){
- if(pws1_get_runstat(idx) == PWS1_RUNSTAT_IDLE){
- log_dbg("%s, idx:%d, state:%s, step:%d, set pcs running state == idle and goto ready",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_READY, PWS1ERR_NONE);
- }else{
- sm_inc_count(sm);
- if( sm_get_count(sm) >= 50 ){ /* 2.5s */
- sm_set_state(sm, SMST_ERR, PWS1ERR_STOP_CHK_STATE_RUNNING_FAIL_AFTER_SET);
- log_dbg("%s, idx:%d, state:%s, step:%d, wait runstate == idle time out goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- }else{
- log_dbg("%s, idx:%d, state:%s, step:%d, waiting runstate == idle count:%d",__func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm));
- }
- }
- }
- }
- static void pws1_sm_ready( int idx )
- {
- struct pws1_t* dev = &pws1[idx];
- struct comm_t* comm = &pws1[idx].comm;
- struct statemachine_t* sm = &pws1[idx].sm;
- int aps = pws1_get_aps(idx);
- int ap = pws1_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, PWS1ERR_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));
- pws1_reset_cmd(idx);
- pws1_reset_aps(idx);
- sm_set_step(sm, 10);
- }else if( sm_get_step(sm) == 10 ){ // wait and chk
- if( pws1_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));
- pws1_reset_cmd(idx);
- pws1_comm_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);
- pws1_comm_set_active_power(idx,(double)(-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);
- pws1_comm_set_active_power(idx,(double)(-aps));
- sm_set_step(sm, 40);
- sm_set_count(sm, 0);
- }else if( pws1_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));
- pws1_reset_cmd(idx);
- pws1_comm_set_dev_stopcmd(idx);
- sm_set_step(sm, 500);
- sm_set_count(sm, 0);
- }else if( pws1_get_runstat(idx) != PWS1_RUNSTAT_IDLE){
- log_dbg("%s, idx:%d, state:%s, step:%d, run state != running detected, %d, goto err",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), pws1_get_runstat(idx));
- sm_set_state(sm, SMST_ERR, PWS1ERR_READY_RUNSTATE_NOT_RUN_DETECTED);
- }
- }else if( sm_get_step(sm) == 20){ /* wait and chk run state = stop */
- if( pws1_get_runstat(idx) == PWS1_RUNSTAT_STOP ){
- 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, PWS1ERR_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, PWS1ERR_READY_WAIT_STOP_TIMEOUT);
- }else{
- 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 = %d > 0 detected, goto check run state",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), ap);
- 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, PWS1ERR_READY_WAIT_AP4DHG_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)%10 == 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) == 31){
- if( pws1_get_runstat(idx) == PWS1_RUNSTAT_DHG){
- log_dbg("%s, idx:%d, state:%s, step:%d,check dhg state ok,goto dhg",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_DHG, PWS1ERR_NONE);
- }else{
- sm_inc_count(sm);
- if( sm_get_count(sm) >= 100 ){
- sm_set_state(sm, SMST_ERR, PWS1ERR_READY_WAIT_DHG_STATE_TIME_OUT);
- log_dbg("%s, idx:%d, state:%s, step:%d, wait dhg state time out, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- }else if(sm_get_count(sm)%10 == 0){
-
- log_dbg("%s, idx:%d, state:%s, step:%d, waiting dhg state time out, 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 chk run state",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), ap);
- sm_set_step(sm,41);
- sm_set_count(sm,0);
- }else{
- sm_inc_count(sm);
- if( sm_get_count(sm) >= 100 ){
- sm_set_state(sm, SMST_ERR, PWS1ERR_READY_WAIT_AP4CHG_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{
- 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) == 41){
- if( pws1_get_runstat(idx) == PWS1_RUNSTAT_CHG){
- log_dbg("%s, idx:%d, state:%s, step:%d,check chg state ok,goto chg",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_CHG, PWS1ERR_NONE);
- }else{
- sm_inc_count(sm);
- if( sm_get_count(sm) >= 100 ){
- sm_set_state(sm, SMST_ERR, PWS1ERR_READY_WAIT_CHG_STATE_TIME_OUT);
- log_dbg("%s, idx:%d, state:%s, step:%d, wait chg state time out, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- }else if(sm_get_count(sm)%10 == 0){
-
- log_dbg("%s, idx:%d, state:%s, step:%d, waiting chg state time out, 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( PWS1_RUNSTAT_STOP == pws1_get_runstat(idx) ){
- log_dbg("%s, idx:%d, state:%s, step:%d, chk run state = stop ok ,set offgrid mode and goto chk",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- pws1_comm_set_grid_mode(idx,1);
- 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, PWS1ERR_READY_WAIT_RUNSTATE_STOP_TIMEOUT_AFTER_SET);
- log_dbg("%s, idx:%d, state:%s, step:%d, wait run state = stop timeout, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- }else if( sm_get_count(sm) % 10 == 0){
- log_dbg("%s, idx:%d, state:%s, step:%d, waiting run state = stop, count:%d",__func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm));
- }else{
- //wait
- }
- }
- }else if( sm_get_step(sm) == 501 ){
- if( PWS1_RUNMOD_OFFGRID == pws1_get_runmod(idx) ){
- log_dbg("%s, idx:%d, state:%s, step:%d, chk run mode = offgrid ok ,set pcs start and goto chk",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- pws1_comm_set_dev_startcmd(1);
- sm_set_step(sm, 502);
- sm_set_count(sm, 0);
- }else{
- sm_inc_count(sm);
- if( sm_get_count(sm) >= 100 ){ // 5s
- sm_set_state(sm, SMST_ERR, PWS1ERR_READY_WAIT_RUNMODE_OFFGRID_TIMEOUT_AFTER_SET);
- 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 if( sm_get_count(sm) % 10 == 0){
- 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{
- //wait
- }
- }
- }else if( sm_get_step(sm) == 502 ){
- if( PWS1_RUNSTAT_IDLE == pws1_get_runstat(idx) || PWS1_RUNSTAT_CHG == pws1_get_runstat(idx) || PWS1_RUNSTAT_DHG == pws1_get_runstat(idx)){
- log_dbg("%s, idx:%d, state:%s, step:%d, chk run state = idl||chg||dhg ok ,goto chk",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_step(sm, 503);
- sm_set_count(sm, 0);
- }else{
- sm_inc_count(sm);
- if( sm_get_count(sm) >= 100 ){ // 5s
- sm_set_state(sm, SMST_ERR, PWS1ERR_READY_WAIT_RUNSTATE_RUNING_TIMEOUT_AFTER_SET);
- log_dbg("%s, idx:%d, state:%s, step:%d, wait run state = idl||chg||dhg timeout, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- }else if( sm_get_count(sm) % 10 == 0){
- log_dbg("%s, idx:%d, state:%s, step:%d, waiting run state = idl||chg||dhg, count:%d",__func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm));
- }else{
- //wait
- }
- }
- }else if( sm_get_step(sm) == 503){ // chk voltage for offgrid
- if( pws1_get_uab(idx) > 340.0 && pws1_get_uab(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), pws1_get_uab(idx));
- sm_set_state(sm, SMST_OFFGRID, PWS1ERR_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, PWS1ERR_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 pws1_sm_dhg( int idx )
- {
- struct pws1_t* dev = &pws1[idx];
- struct comm_t* comm = &pws1[idx].comm;
- struct statemachine_t* sm = &pws1[idx].sm;
- int aps = pws1_get_aps(idx);
- int last_aps = pws1_get_last_aps(idx);
- int ap = pws1_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, PWS1ERR_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));
- sm_set_step(sm, 10);
- pws1_reset_bsytikchk(idx);
- }else if( sm_get_step(sm) == 10 ){ // wait and chk
- if( pws1_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));
- pws1_reset_cmd(idx);
- pws1_comm_set_active_power(idx, 0);
- sm_set_step(sm, 30);
- sm_set_count(sm, 0);
- }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);
- pws1_comm_set_active_power(idx, 0);
- sm_set_step(sm, 30);
- sm_set_count(sm, 0);
- }else if( pws1_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);
- pws1_set_aps(idx, aps); // update last aps
- pws1_comm_set_active_power(idx, (double)(-aps));
- }else if( pws1_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));
- pws1_set_aps(idx, 0);
- }else if( pws1_get_runstat(idx) != PWS1_RUNSTAT_DHG ){
- 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));
- pws1_comm_set_active_power(idx, 0);
- sm_set_state(sm, SMST_ERR, PWS1ERR_DHG_STATE_NOT_RUN_DETECTED);
- }
- }else if( sm_get_step(sm) == 30 ){ // chk dev aps = 0 for readycmd
- if( pws1_get_dev_aps(idx) == 0 ){
- log_dbg("%s, idx:%d, state:%s, step:%d, chk dev aps = 0 ok, goto chk dhg state",
- __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, PWS1ERR_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{
- 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){
- if(pws1_get_runstat(idx) == PWS1_RUNSTAT_IDLE){
- log_dbg("%s, idx:%d, state:%s, step:%d, chk in idle state, goto ready",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm,SMST_READY,PWS1ERR_NONE);
- }else{
- sm_inc_count(sm);
- if( sm_get_count(sm) >= 100 ){
- sm_set_state(sm, SMST_ERR, PWS1ERR_DHG_WAIT_DEV_NO_DHG_TIME_OUT);
- log_dbg("%s, idx:%d, state:%s, step:%d, wait dev no dhg timeout, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- }else if(sm_get_count(sm) % 10 == 0){
- log_dbg("%s, idx:%d, state:%s, step:%d, waiting no dhg for readycmd, count: %d",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm));
- }
- }
- }
- }
- static void pws1_sm_chg( int idx )
- {
- struct pws1_t* dev = &pws1[idx];
- struct comm_t* comm = &pws1[idx].comm;
- struct statemachine_t* sm = &pws1[idx].sm;
- int aps = pws1_get_aps(idx);
- int last_aps = pws1_get_last_aps(idx);
- int ap = pws1_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, PWS1ERR_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));
- sm_set_step(sm, 10);
- pws1_reset_bsytikchk(idx);
- }else if( sm_get_step(sm) == 10 ){ // wait and chk
- if( pws1_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));
- pws1_reset_cmd(idx);
- pws1_comm_set_active_power(idx, 0);
- sm_set_step(sm, 30);
- sm_set_count(sm, 0);
- }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);
- pws1_comm_set_active_power(idx, 0);
- sm_set_step(sm, 30);
- sm_set_count(sm, 0);
- }else if( pws1_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);
- pws1_set_aps(idx, aps); // update last aps
- pws1_comm_set_active_power(idx, (-aps));
- }else if( pws1_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));
- pws1_set_aps(idx, 0);
- }else if( pws1_get_runstat(idx) != PWS1_RUNSTAT_CHG ){
- 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));
- pws1_comm_set_active_power(idx, 0);
- sm_set_state(sm, SMST_ERR, PWS1ERR_CHG_STATE_NOT_RUN_DETECTED);
- }
- }else if( sm_get_step(sm) == 30 ){ // chk dev aps = 0 for readycmd
- if( pws1_get_dev_aps(idx) == 0 ){
- log_dbg("%s, idx:%d, state:%s, step:%d, chk dev aps = 0 ok, goto chk chg state",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_step(sm,31);
- }else{
- sm_inc_count(sm);
- if( sm_get_count(sm) >= 100 ){
- sm_set_state(sm, SMST_ERR, PWS1ERR_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{
- 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){
- if(pws1_get_runstat(idx) == PWS1_RUNSTAT_IDLE){
- log_dbg("%s, idx:%d, state:%s, step:%d, chk in idle state, goto ready",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm,SMST_READY,PWS1ERR_NONE);
- }else{
- sm_inc_count(sm);
- if( sm_get_count(sm) >= 100 ){
- sm_set_state(sm, SMST_ERR, PWS1ERR_CHG_WAIT_DEV_NO_CHG_TIME_OUT);
- log_dbg("%s, idx:%d, state:%s, step:%d, wait dev no chg timeout, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- }else if(sm_get_count(sm) % 10 == 0){
- log_dbg("%s, idx:%d, state:%s, step:%d, waiting no chg for readycmd, count: %d",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm));
- }
- }
- }
- }
- void pws1_sm( int idx )
- {
- struct pws1_t* dev = &pws1[idx];
- struct statemachine_t* sm = &dev->sm;
- sm_cal_timing(&dev->sm);
- switch( sm_get_state( sm ) ){
- case SMST_LAUNCH:
- pws1_sm_launch( idx );
- break;
- case SMST_STDBY:
- pws1_sm_stdby( idx );
- break;
- case SMST_STOP:
- pws1_sm_stop( idx );
- break;
- case SMST_OFFGRID:
- pws1_sm_offgrid( idx );
- break;
- case SMST_READY:
- pws1_sm_ready( idx );
- break;
- case SMST_DHG:
- pws1_sm_dhg( idx );
- break;
- case SMST_CHG:
- pws1_sm_chg( idx );
- break;
- case SMST_ERR:
- pws1_sm_err( idx );
- break;
- default:
- log_dbg("%s, unknown state, %d",__func__, pws1_get_state( idx ));
- break;
- }
- }
|