allow parameter change while oscillating

This commit is contained in:
Douglas Clowes
2012-11-29 12:24:22 +11:00
parent e073e3c4a8
commit 9a43c3f0d4

View File

@ -13,19 +13,25 @@
#include "task.h" #include "task.h"
#include "commandlog.h" #include "commandlog.h"
#include "oscillate.h" #include "oscillate.h"
#include "status.h"
#define ABS(x) (x < 0 ? -(x) : (x)) #define ABS(x) (x < 0 ? -(x) : (x))
/*================== real work =========================================*/ /*================== real work =========================================*/
static void StopOscillation(pOscillator self) static void StopOscillation(pOscillator self)
{ {
int savedStatus;
assert(self != NULL); assert(self != NULL);
if (self->taskID > 0) { if (self->taskID > 0) {
self->pMot->pDriver->Halt(self->pMot->pDriver); self->pMot->pDriver->Halt(self->pMot->pDriver);
self->stopFlag = 1; self->stopFlag = 1;
self->taskID = -1; self->taskID = -1;
} }
savedStatus = GetStatus(); /* fool status check in ObParSet (avoid "Cannot change parameter while running" message */
SetStatus(eEager);
MotorSetPar(self->pMot, self->pCon, "accesscode", usUser); MotorSetPar(self->pMot, self->pCon, "accesscode", usUser);
SetStatus(savedStatus);
if (self->debug > 0) { if (self->debug > 0) {
WriteToCommandLog("oscillator>> ", "Stopping"); WriteToCommandLog("oscillator>> ", "Stopping");
} }
@ -68,6 +74,7 @@ static int OscillationTask(void *data)
assert(self); assert(self);
if (self->stopFlag == 1) { if (self->stopFlag == 1) {
finishDriving(self->pMot, self->pCon);
return 0; return 0;
} }
@ -104,10 +111,16 @@ static int OscillationTask(void *data)
case HWBusy: case HWBusy:
break; break;
case HWIdle: case HWIdle:
if (self->cycles > 0 && self->currcycle == self->cycles) {
StopOscillation(self);
return 1;
}
pos = getNextPos(self); pos = getNextPos(self);
status = MotorRun(self->pMot, self->pCon, pos); status = MotorRun(self->pMot, self->pCon, pos);
if (status == OKOK) { if (status == OKOK) {
self->pMot->pDrivInt->iErrorCount = 0; self->pMot->pDrivInt->iErrorCount = 0;
if (self->cycles > 0)
(self->currcycle)++;
} }
if (self->debug > 0) { if (self->debug > 0) {
snprintf(message, 131, "Started oscillation to %f, ret code = %d", snprintf(message, 131, "Started oscillation to %f, ret code = %d",
@ -122,7 +135,7 @@ static int OscillationTask(void *data)
static int StartOscillation(pOscillator self, SConnection * pCon) static int StartOscillation(pOscillator self, SConnection * pCon)
{ {
float fval; float fval;
int status; int status, savedStatus;
char error[80], pBueffel[255]; char error[80], pBueffel[255];
assert(self); assert(self);
@ -138,7 +151,10 @@ static int StartOscillation(pOscillator self, SConnection * pCon)
self->lowerLimit += .5; self->lowerLimit += .5;
MotorGetPar(self->pMot, "softupperlim", &self->upperLimit); MotorGetPar(self->pMot, "softupperlim", &self->upperLimit);
self->upperLimit -= .5; self->upperLimit -= .5;
savedStatus = GetStatus(); /* fool status check in ObParSet (avoid "Cannot change parameter while running" message */
SetStatus(eEager);
MotorSetPar(self->pMot, self->pCon, "accesscode", (float) usInternal); MotorSetPar(self->pMot, self->pCon, "accesscode", (float) usInternal);
SetStatus(savedStatus);
self->nextTargetFlag = 0; self->nextTargetFlag = 0;
self->errorCount = 0; self->errorCount = 0;
self->stopFlag = 0; self->stopFlag = 0;
@ -221,6 +237,8 @@ int MakeOscillator(SConnection * pCon, SicsInterp * pSics, void *pData,
memset(pNew, 0, sizeof(Oscillator)); memset(pNew, 0, sizeof(Oscillator));
pNew->pDes = CreateDescriptor("Oscillator"); pNew->pDes = CreateDescriptor("Oscillator");
pNew->pMot = pMot; pNew->pMot = pMot;
pNew->cycles = 0;
pNew->currcycle = 0;
pNew->pCon = SCCreateDummyConnection(pSics); pNew->pCon = SCCreateDummyConnection(pSics);
if (!pNew->pDes || !pNew->pCon) { if (!pNew->pDes || !pNew->pCon) {
SCWrite(pCon, "ERROR: out of memory creating oscillator", eError); SCWrite(pCon, "ERROR: out of memory creating oscillator", eError);
@ -259,6 +277,12 @@ int OscillatorWrapper(SConnection * pCon, SicsInterp * pSics, void *pData,
} }
strtolower(argv[1]); strtolower(argv[1]);
if (argc == 3) {
self->cycles = 2 * atoi(argv[2]);
} else {
self->cycles = 0;
}
self->currcycle = 0;
if (strcmp(argv[1], "start") == 0) { if (strcmp(argv[1], "start") == 0) {
return StartOscillation(self, pCon); return StartOscillation(self, pCon);
} else if (strcmp(argv[1], "stop") == 0) { } else if (strcmp(argv[1], "stop") == 0) {