update in line with nhq driver

r2590 | dcl | 2008-05-29 14:50:35 +1000 (Thu, 29 May 2008) | 2 lines
This commit is contained in:
Douglas Clowes
2008-05-29 14:50:35 +10:00
parent a87c8149db
commit 28e39b963a
2 changed files with 127 additions and 31 deletions

View File

@ -70,6 +70,7 @@ typedef struct orhvps_s {
float fUpper; /* Normal Operating Voltage */
float fLower; /* Normal Moving Voltage */
bool isLocked; /* changes no longer permitted */
bool bRunFlag; /* set by the run command */
char* name;
pAsyncUnit asyncUnit;
StateMachine fsm;
@ -166,7 +167,7 @@ const char* event_name(pEvtEvent event, char* text, int length)
snprintf(text, length, "eStateEvent");
return text;
case eTimerEvent:
snprintf(text, length, "eTimerEvent");
snprintf(text, length, "eTimerEvent (%d mSec)", event->event.tmr.timerValue);
return text;
case eMessageEvent:
snprintf(text, length, "eMessageEvent:");
@ -193,6 +194,23 @@ const char* event_name(pEvtEvent event, char* text, int length)
}
}
/**
* \brief parse the HV response for several consecutive states [bot, top]
*
* It captures the value in the first state and confirms the value in the last
* state. If any error occurs it returns -1 and resets the substate to bot.
* When the same value has been read for each substate from [bot, top] it
* returns 0 to confirm the value. As each value is confirmed it returns +1.
*
* \param sm pointer to the state machine
* \param pCmd pointer to the command and response
* \param bot first state (capture value)
* \param top last state confirm value
*
* \return -1 Error
* 0 sequence correct so far
* +1 sequence complete, can use the value
*/
static int getProcess(pStateMachine sm, pAsyncTxn pCmd, int bot, int top) {
pEVDriver driv = (pEVDriver) sm->context;
pORHVPSDriv priv = (pORHVPSDriv) driv->pPrivate;
@ -242,6 +260,7 @@ static void ORHVState_Unknown(pStateMachine sm, pEvtEvent event) {
if (priv->state_timer)
NetWatchRemoveTimer(priv->state_timer);
priv->state_timer = NULL;
priv->bRunFlag = false;
ORHV_SendCmd(priv, "vz", 2, fsm_msg_callback);
sm->mySubState = 1;
return;
@ -300,9 +319,13 @@ static void ORHVState_Idle(pStateMachine sm, pEvtEvent event){
NetWatchRegisterTimer(&priv->state_timer,
200,
fsm_tmr_callback, sm);
sm->mySubState = 1;
return;
case eTimerEvent:
priv->state_timer = NULL;
if (sm->mySubState == 6) {
if (priv->bRunFlag) {
priv->bRunFlag = false;
newVal = roundf(priv->fTarget / (priv->fMax / 63.0));
if (newVal > priv->iValue) {
fsm_change_state(sm, ORHVState_Raising);
@ -312,13 +335,47 @@ static void ORHVState_Idle(pStateMachine sm, pEvtEvent event){
fsm_change_state(sm, ORHVState_Lowering);
return;
}
NetWatchRegisterTimer(&priv->state_timer,
200,
fsm_tmr_callback, sm);
}
/*
* There was either no commanded to move,
* on no need to move,
* so start another sequence
*/
sm->mySubState = 1;
}
ORHV_SendCmd(priv, "Hz", 2, fsm_msg_callback);
return;
case eMessageEvent:
do {
pAsyncTxn pCmd = event->event.msg.cmd;
if (sm->mySubState >= 1 && sm->mySubState <= 5) {
/* HV Get Request */
int iRet;
iRet = getProcess(sm, pCmd, 1, 5);
if (iRet == 0) { /* final value OK */
sm->mySubState = 6;
NetWatchRegisterTimer(&priv->state_timer,
100, /* TODO*/
fsm_tmr_callback, sm);
return;
}
if (iRet < 0) { /* error, start again */
sm->mySubState = 1;
NetWatchRegisterTimer(&priv->state_timer,
100, /* TODO*/
fsm_tmr_callback, sm);
return;
}
/* normal, just ask again for confirmation */
ORHV_SendCmd(priv, "Hz", 2, fsm_msg_callback);
return;
}
} while (0);
sm->mySubState = 1;
NetWatchRegisterTimer(&priv->state_timer,
100, /* TODO*/
fsm_tmr_callback, sm);
return;
case eCommandEvent:
return;
case eTimeoutEvent:
@ -546,15 +603,12 @@ static int ORHVPSSetValue( pEVDriver self, float fPos) {
assert(self);
assert(self->pPrivate);
me = (pORHVPSDriv) self->pPrivate;
if (me->isLocked) {
me->iError = ORHVPS_ERR_LOCKED;
return 0;
}
if (fPos < 0.0 || fPos > me->fMax) {
me->iError = ORHVPS_ERR_RANGE;
return 0;
}
me->fTarget = fPos;
me->bRunFlag = true;
return 1;
}
static int ORHVPSSend(pEVDriver self, char *pCommand, char *pReply, int iLen) {
@ -833,6 +887,7 @@ pEVDriver CreateORHVPSDriver(int argc, char *argv[])
priv->fLower = 0.0;
priv->fUpper = 1800.0;
priv->iPeriod = get_period(priv->fMax, priv->fRate);
priv->bRunFlag = false;
fsm_change_state(&priv->fsm, ORHVState_Unknown);
@ -847,6 +902,7 @@ void ORHVPSRegister(pEVControl self, pEVDriver driv)
if (priv->name)
free(priv->name);
priv->name = strdup(self->pName);
priv->fsm.name = priv->name;
}
}
@ -922,18 +978,39 @@ int ORHVPSWrapper(SConnection *pCon, SicsInterp *pSics, void *pData,
SCWrite(pCon, cmd, eValue);
return 1;
}
if (strcasecmp("up", argv[1]) == 0) {
if (strcasecmp("up", argv[1]) == 0 || strcasecmp("down", argv[1]) == 0) {
int iRet;
if(!SCMatchRights(pCon,usUser))
return 0;
if (strcasecmp("up", argv[1]) == 0)
priv->fTarget = priv->fUpper;
return 1;
}
if (strcasecmp("down", argv[1]) == 0) {
else
priv->fTarget = priv->fLower;
return 1;
if (priv->fTarget > priv->fMax)
priv->fTarget = priv->fMax;
if (priv->fTarget < 0)
priv->fTarget = 0.0;
iRet = EVCDrive(priv->controller, pCon, priv->fTarget);
if(iRet)
SCSendOK(pCon);
return iRet;
}
if (strcasecmp("off", argv[1]) == 0) {
int iRet;
if(!SCMatchRights(pCon,usUser))
return 0;
priv->fTarget = 0.0;
iRet = EVCDrive(priv->controller, pCon, priv->fTarget);
if(iRet)
SCSendOK(pCon);
return iRet;
}
if (strcasecmp("upper", argv[1]) == 0) {
char rsp[CMDLEN];
if (argc > 2) {
if (priv->isLocked) {
if (!SCMatchRights(pCon, usUser))
return 0;
if (priv->isLocked && !SCMatchRights(pCon, usMugger)) {
SCWrite(pCon, "object is locked", eError);
return 0;
}
@ -955,7 +1032,9 @@ int ORHVPSWrapper(SConnection *pCon, SicsInterp *pSics, void *pData,
if (strcasecmp("lower", argv[1]) == 0) {
char rsp[CMDLEN];
if (argc > 2) {
if (priv->isLocked) {
if (!SCMatchRights(pCon, usUser))
return 0;
if (priv->isLocked && !SCMatchRights(pCon, usMugger)) {
SCWrite(pCon, "object is locked", eError);
return 0;
}
@ -977,6 +1056,8 @@ int ORHVPSWrapper(SConnection *pCon, SicsInterp *pSics, void *pData,
if (strcasecmp("max", argv[1]) == 0) {
char rsp[CMDLEN];
if (argc > 2) {
if (!SCMatchRights(pCon, usUser))
return 0;
if (priv->isLocked) {
SCWrite(pCon, "object is locked", eError);
return 0;
@ -997,7 +1078,7 @@ int ORHVPSWrapper(SConnection *pCon, SicsInterp *pSics, void *pData,
SCWrite(pCon, rsp, eValue);
return 1;
}
if (strcasecmp("debug", argv[1]) == 0) {
if (strcasecmp("debug", argv[1]) == 0 && SCMatchRights(pCon, usMugger)) {
char rsp[CMDLEN];
if (argc > 2) {
int debug = atoi(argv[2]);
@ -1013,7 +1094,9 @@ int ORHVPSWrapper(SConnection *pCon, SicsInterp *pSics, void *pData,
if (strcasecmp("rate", argv[1]) == 0) {
char rsp[CMDLEN];
if (argc > 2) {
if (priv->isLocked) {
if (!SCMatchRights(pCon, usUser))
return 0;
if (priv->isLocked && !SCMatchRights(pCon, usMugger)) {
SCWrite(pCon, "object is locked", eError);
return 0;
}
@ -1036,6 +1119,8 @@ int ORHVPSWrapper(SConnection *pCon, SicsInterp *pSics, void *pData,
if (strcasecmp("period", argv[1]) == 0) {
char rsp[CMDLEN];
if (argc > 2) {
if (!SCMatchRights(pCon, usUser))
return 0;
SCWrite(pCon, "cannot set period, set max and rate instead", eError);
return 0;
}
@ -1045,6 +1130,8 @@ int ORHVPSWrapper(SConnection *pCon, SicsInterp *pSics, void *pData,
}
if (strcasecmp("reset", argv[1]) == 0) {
char rsp[CMDLEN];
if (!SCMatchRights(pCon, usUser))
return 0;
fsm_change_state(&priv->fsm, ORHVState_Unknown);
snprintf(rsp, CMDLEN, "%s.reset = 1", priv->name);
SCWrite(pCon, rsp, eValue);
@ -1052,8 +1139,19 @@ int ORHVPSWrapper(SConnection *pCon, SicsInterp *pSics, void *pData,
}
if (strcasecmp("lock", argv[1]) == 0) {
char rsp[CMDLEN];
if (!SCMatchRights(pCon, usUser))
return 0;
priv->isLocked = 1;
snprintf(rsp, CMDLEN, "%s.lock = 1", priv->name);
snprintf(rsp, CMDLEN, "%s.lock = %d", priv->name, priv->isLocked);
SCWrite(pCon, rsp, eValue);
return 1;
}
if (strcasecmp("unlock", argv[1]) == 0
&& SCMatchRights(pCon, usMugger)
&& priv->fsm.debug) {
char rsp[CMDLEN];
priv->isLocked = 0;
snprintf(rsp, CMDLEN, "%s.lock = %d", priv->name, priv->isLocked);
SCWrite(pCon, rsp, eValue);
return 1;
}

View File

@ -18,9 +18,7 @@
#include <stdio.h>
#include "sics.h"
#include <obpar.h>
#include <evcontroller.h>
#include <evcontroller.i>
#include "evdriver.h"
#include "ansto_evcontroller.h"
void ORHVPSInitProtocol(SicsInterp *pSics);