- various enhancements

This commit is contained in:
zolliker
2007-02-23 12:25:36 +00:00
parent a3bcd7586b
commit 284c955411
4 changed files with 43 additions and 18 deletions

33
ease.c
View File

@ -89,6 +89,7 @@ void EaseWriteError(EaseBase *eab) {
} }
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
void EaseWrite(EaseBase *eab, char *cmd) { void EaseWrite(EaseBase *eab, char *cmd) {
/* cmd==NULL: repeat the same command */
int iRet; int iRet;
char trash[64]; char trash[64];
int l; int l;
@ -100,7 +101,9 @@ void EaseWrite(EaseBase *eab, char *cmd) {
if (iRet < 0) break; if (iRet < 0) break;
ParPrintf(eab, -2, "trash: %s\n", trash); ParPrintf(eab, -2, "trash: %s\n", trash);
} }
snprintf(eab->cmd, sizeof(eab->cmd), "%s%s", cmd, eab->ser->sendTerminator); if (cmd) {
snprintf(eab->cmd, sizeof(eab->cmd), "%s%s", cmd, eab->ser->sendTerminator);
}
iRet = writeRS232(eab->ser, eab->cmd, strlen(eab->cmd)); iRet = writeRS232(eab->ser, eab->cmd, strlen(eab->cmd));
if (iRet < 0) { if (iRet < 0) {
FsmStop(eab->task, eab->idle); FsmStop(eab->task, eab->idle);
@ -112,25 +115,36 @@ void EaseWrite(EaseBase *eab, char *cmd) {
eab->errCode = iRet; eab->errCode = iRet;
return; return;
} }
ParPrintf(eab, -2, "cmd: %s", cmd); if (cmd) {
ParPrintf(eab, -2, "cmd: %s", cmd);
} else {
ParPrintf(eab, -2, "cmd: %s", eab->cmd);
}
eab->state = EASE_expect; eab->state = EASE_expect;
eab->cmdtime = time(NULL); eab->cmdtime = time(NULL);
} }
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
int EaseWaitRead(EaseBase *eab) { int EaseWaitRead(EaseBase *eab) {
int cnt=0; int cnt=0;
time_t start;
if (eab->state < EASE_idle) { if (eab->state < EASE_idle) {
return NOTCONNECTED; return NOTCONNECTED;
} }
FsmPause(eab->task, 1); FsmPause(eab->task, 1);
time(&start);
while (eab->state == EASE_expect) { while (eab->state == EASE_expect) {
/* TaskYield(pServ->pTasker); does not work (pardef is not recursive) */ /* TaskYield(pServ->pTasker); does not work (pardef is not recursive) */
FsmTaskHandler(eab->task); FsmTaskHandler(eab->task);
cnt++; cnt++;
if (time(NULL) > start+5) {
eab->ans[0]='\0';
eab->state = EASE_read;
return TIMEOUT;
}
} }
FsmPause(eab->task, 0); FsmPause(eab->task, 0);
/* if (cnt>1) printf("TaskYield %d\n", cnt); */ /* if (cnt>1) ParPrintf(eab, eError, "TaskYield %d\n", cnt); */
return 1; return 1;
} }
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
@ -408,8 +422,9 @@ void EaseSetUpdate(void *object, int flag, int state) {
static long EaseRun(void *obj, SConnection *pCon, float fVal) { static long EaseRun(void *obj, SConnection *pCon, float fVal) {
EaseBase *eab = EaseBaseCast(obj); EaseBase *eab = EaseBaseCast(obj);
EaseDriv *ead = EaseDrivCast(obj); EaseDriv *ead = EaseDrivCast(obj);
int canc;
assert(ead );
assert(ead);
ParSaveConn(eab, pCon); ParSaveConn(eab, pCon);
if (! eab->doit) { if (! eab->doit) {
@ -421,7 +436,10 @@ static long EaseRun(void *obj, SConnection *pCon, float fVal) {
return 0; return 0;
} }
if (FsmStop(eab->task, eab->doit)) { if (FsmStop(eab->task, eab->doit)) {
canc = 1;
ParPrintf(ead, eWarning, "running %s cancelled", eab->p.name); ParPrintf(ead, eWarning, "running %s cancelled", eab->p.name);
} else {
canc = 0;
} }
if (eab->todo) { if (eab->todo) {
ParPrintf(ead, eError, "ERROR: %s busy", eab->p.name); ParPrintf(ead, eError, "ERROR: %s busy", eab->p.name);
@ -442,6 +460,9 @@ static long EaseRun(void *obj, SConnection *pCon, float fVal) {
ead->finish = 0; ead->finish = 0;
ead->usedSettle = 0; ead->usedSettle = 0;
ead->tolState = EASE_outOfTolerance; ead->tolState = EASE_outOfTolerance;
if (canc) {
ParPrintf(ead, eWarning, "rerun %s", eab->p.name);
}
return 1; return 1;
} }
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
@ -633,7 +654,7 @@ static int EaseInit(SConnection *pCon, EaseBase *eab, int argc, char *argv[],
eab->task = NULL; eab->task = NULL;
if (EaseRestart(eab) < 0) return -1; if (EaseRestart(eab) < 0) return -1;
eab->task = FsmStartTask(eab, eab->handler, eab->idle); eab->task = FsmStartTask(eab, eab->handler, eab->idle, eab->p.name);
TaskRegister(pServ->pTasker, (TaskFunc)FsmTaskHandler, NULL, FsmKill, TaskRegister(pServ->pTasker, (TaskFunc)FsmTaskHandler, NULL, FsmKill,
eab->task, 0); eab->task, 0);

13
fsm.c
View File

@ -6,6 +6,7 @@ a finite state machine within sics
M. Zolliker, Aug 2004 M. Zolliker, Aug 2004
---------------------------------------------------------------------------- */ ---------------------------------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include <time.h> #include <time.h>
@ -28,12 +29,12 @@ struct Fsm {
int pause; int pause;
int sp; int sp;
StackItem stack[MAXSTACK]; StackItem stack[MAXSTACK];
Statistics *stat;
}; };
static Fsm *fsm = NULL; static Fsm *fsm = NULL;
static FsmFunc callFunc = NULL; static FsmFunc callFunc = NULL;
static Statistics *taskStat = NULL;
void FsmWait(long delay) { void FsmWait(long delay) {
assert(fsm); assert(fsm);
@ -49,10 +50,7 @@ int FsmTaskHandler(Fsm *task) {
long line; long line;
Statistics *old; Statistics *old;
if (taskStat == NULL) { old = StatisticsBegin(task->stat);
taskStat=StatisticsNew("<fsm>");
}
old = StatisticsBegin(taskStat);
if (task->pause) { if (task->pause) {
task->handler(task->obj); task->handler(task->obj);
StatisticsEnd(old); StatisticsEnd(old);
@ -110,12 +108,15 @@ void FsmRestartTask(Fsm *task, FsmFunc func) {
task->till = 0; task->till = 0;
} }
Fsm *FsmStartTask(void *obj, FsmHandler handler, FsmFunc func) { Fsm *FsmStartTask(void *obj, FsmHandler handler, FsmFunc func, char *name) {
Fsm *task; Fsm *task;
char fsmName[80];
task=malloc(sizeof *task); task=malloc(sizeof *task);
task->obj = obj; task->obj = obj;
task->handler = handler; task->handler = handler;
snprintf(fsmName, sizeof fsmName, "fsm %s", name);
task->stat = StatisticsNew(fsmName);
FsmRestartTask(task, func); FsmRestartTask(task, func);
return task; return task;
} }

View File

@ -368,6 +368,7 @@ static long IpsChangeField(long pc, void *object) {
ParPrintf(drv, -1, "IPS: wait %d sec to open switch", delay); ParPrintf(drv, -1, "IPS: wait %d sec to open switch", delay);
start_ramp: start_ramp:
ParLog(drv);
FsmWait(1); FsmWait(1);
return __LINE__; case __LINE__: /**********************************/ return __LINE__; case __LINE__: /**********************************/
EaseWrite(eab, "X"); EaseWrite(eab, "X");
@ -390,6 +391,7 @@ static long IpsChangeField(long pc, void *object) {
ParPrintf(drv, -1, "IPS: ramp to %f Tesla", drv->d.targetValue); ParPrintf(drv, -1, "IPS: ramp to %f Tesla", drv->d.targetValue);
ramping: ramping:
ParLog(drv);
FsmWait(1); FsmWait(1);
return __LINE__; case __LINE__: /**********************************/ return __LINE__; case __LINE__: /**********************************/
EaseWrite(eab, "R7"); /* read "current" in Tesla */ EaseWrite(eab, "R7"); /* read "current" in Tesla */
@ -413,7 +415,6 @@ static long IpsChangeField(long pc, void *object) {
} }
OxiSet(eab, "J", fld, 3); OxiSet(eab, "J", fld, 3);
return __LINE__; case __LINE__: /**********************************/ return __LINE__; case __LINE__: /**********************************/
ParLog(drv);
goto ramping; goto ramping;
target_reached: target_reached:
@ -423,6 +424,7 @@ static long IpsChangeField(long pc, void *object) {
/* but we continue in the background */ /* but we continue in the background */
drv->tim = time(NULL); drv->tim = time(NULL);
stab3: stab3:
ParLog(drv);
FsmWait(1); FsmWait(1);
return __LINE__; case __LINE__: /**********************************/ return __LINE__; case __LINE__: /**********************************/
EaseWrite(eab, "X"); EaseWrite(eab, "X");
@ -441,6 +443,7 @@ static long IpsChangeField(long pc, void *object) {
ParPrintf(drv, -1, "IPS: wait 30 sec to close switch"); ParPrintf(drv, -1, "IPS: wait 30 sec to close switch");
wait_closed: wait_closed:
ParLog(drv);
FsmWait(1); FsmWait(1);
return __LINE__; case __LINE__: /**********************************/ return __LINE__; case __LINE__: /**********************************/
EaseWrite(eab, "R18"); /* read persistent field in Tesla */ EaseWrite(eab, "R18"); /* read persistent field in Tesla */

View File

@ -502,21 +502,21 @@ static long ItcRead(long pc, void *object) {
skiphtr: skiphtr:
if (EaseGetUpdate(drv, ITC_PROP)) goto skipprop; if (EaseGetUpdate(drv, ITC_PROP)) goto skipprop;
EaseWrite(eab, "R9"); /* read prop */ EaseWrite(eab, "R8"); /* read prop */
return __LINE__; case __LINE__: /**********************************/ return __LINE__; case __LINE__: /**********************************/
if (EaseGetUpdate(drv, ITC_PROP)) goto skipprop; if (EaseGetUpdate(drv, ITC_PROP)) goto skipprop;
drv->prop = OxiGet(eab, 1, NULL, drv->prop); drv->prop = OxiGet(eab, 1, NULL, drv->prop);
skipprop: skipprop:
if (EaseGetUpdate(drv, ITC_INTEG)) goto skipint; if (EaseGetUpdate(drv, ITC_INTEG)) goto skipint;
EaseWrite(eab, "R10"); /* read int */ EaseWrite(eab, "R9"); /* read int */
return __LINE__; case __LINE__: /**********************************/ return __LINE__; case __LINE__: /**********************************/
if (EaseGetUpdate(drv, ITC_INTEG)) goto skipint; if (EaseGetUpdate(drv, ITC_INTEG)) goto skipint;
drv->integ = OxiGet(eab, 1, NULL, drv->integ); drv->integ = OxiGet(eab, 1, NULL, drv->integ);
skipint: skipint:
if (EaseGetUpdate(drv, ITC_DERIV)) goto skipderiv; if (EaseGetUpdate(drv, ITC_DERIV)) goto skipderiv;
EaseWrite(eab, "R11"); /* read deriv */ EaseWrite(eab, "R10"); /* read deriv */
return __LINE__; case __LINE__: /**********************************/ return __LINE__; case __LINE__: /**********************************/
if (EaseGetUpdate(drv, ITC_DERIV)) goto skipderiv; if (EaseGetUpdate(drv, ITC_DERIV)) goto skipderiv;
drv->deriv = OxiGet(eab, 1, NULL, drv->deriv); drv->deriv = OxiGet(eab, 1, NULL, drv->deriv);
@ -696,8 +696,8 @@ static long ItcSet(long pc, void *object) {
} }
goto loop; goto loop;
case ITC_PROP: OxiSet(eab, "P", drv->prop, 1); goto loop; case ITC_PROP: OxiSet(eab, "P", drv->prop, 1); goto loop;
case ITC_INTEG: OxiSet(eab, "I", drv->prop, 1); goto loop; case ITC_INTEG: OxiSet(eab, "I", drv->integ, 1); goto loop;
case ITC_DERIV: OxiSet(eab, "D", drv->prop, 1); goto loop; case ITC_DERIV: OxiSet(eab, "D", drv->deriv, 1); goto loop;
default: break; default: break;
} }
} }