- Adapted indenation to new agreed upon system
- Added support for second generation scriptcontext based counter
This commit is contained in:
391
simev.c
391
simev.c
@ -50,220 +50,213 @@
|
||||
#include "evdriver.h"
|
||||
#include "simev.h"
|
||||
/*-----------------------------------------------------------------------*/
|
||||
typedef struct {
|
||||
float fFailure;
|
||||
float fTarget;
|
||||
float fSpeed;
|
||||
time_t tFinish;
|
||||
} SimST, *pSimST;
|
||||
typedef struct {
|
||||
float fFailure;
|
||||
float fTarget;
|
||||
float fSpeed;
|
||||
time_t tFinish;
|
||||
} SimST, *pSimST;
|
||||
/*-----------------------------------------------------------------------*/
|
||||
static float SimRandom(void)
|
||||
{
|
||||
float fVal;
|
||||
|
||||
fVal = ( (float) rand() / (float)RAND_MAX) * 100.0;
|
||||
return fVal;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int RunComplete(pEVDriver self)
|
||||
{
|
||||
pSimST pMe = NULL;
|
||||
time_t tD;
|
||||
|
||||
assert(self);
|
||||
pMe = (pSimST)self->pPrivate;
|
||||
assert(pMe);
|
||||
|
||||
if(pMe->fFailure < .0){
|
||||
return 1;
|
||||
}
|
||||
static float SimRandom(void)
|
||||
{
|
||||
float fVal;
|
||||
|
||||
if((int)time(&tD) > pMe->tFinish)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
fVal = ((float) rand() / (float) RAND_MAX) * 100.0;
|
||||
return fVal;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int RunComplete(pEVDriver self)
|
||||
{
|
||||
pSimST pMe = NULL;
|
||||
time_t tD;
|
||||
|
||||
assert(self);
|
||||
pMe = (pSimST) self->pPrivate;
|
||||
assert(pMe);
|
||||
|
||||
if (pMe->fFailure < .0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ((int) time(&tD) > pMe->tFinish) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
static int GetSimPos(pEVDriver self, float *fPos)
|
||||
{
|
||||
pSimST pMe = NULL;
|
||||
|
||||
assert(self);
|
||||
pMe = (pSimST)self->pPrivate;
|
||||
assert(pMe);
|
||||
static int GetSimPos(pEVDriver self, float *fPos)
|
||||
{
|
||||
pSimST pMe = NULL;
|
||||
|
||||
assert(self);
|
||||
pMe = (pSimST) self->pPrivate;
|
||||
assert(pMe);
|
||||
|
||||
|
||||
if (SimRandom() < pMe->fFailure) {
|
||||
*fPos = SimRandom();
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (RunComplete(self)) {
|
||||
if (pMe->fFailure < .0) {
|
||||
*fPos = pMe->fTarget;
|
||||
} else {
|
||||
*fPos = pMe->fTarget + SimRandom() / 50 - 1.0;
|
||||
}
|
||||
} else { /* simulate a mispositioned motor */
|
||||
|
||||
*fPos = pMe->fTarget - 10.;
|
||||
return 1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
if(SimRandom() < pMe->fFailure)
|
||||
{
|
||||
*fPos = SimRandom();
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(RunComplete(self))
|
||||
{
|
||||
if(pMe->fFailure < .0){
|
||||
*fPos = pMe->fTarget;
|
||||
} else {
|
||||
*fPos = pMe->fTarget + SimRandom()/50-1.0;
|
||||
}
|
||||
}
|
||||
else /* simulate a mispositioned motor */
|
||||
{
|
||||
*fPos = pMe->fTarget - 10.;
|
||||
return 1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static int SimRun(pEVDriver self, float fVal)
|
||||
{
|
||||
pSimST pMe = NULL;
|
||||
float fDiff;
|
||||
time_t tD;
|
||||
|
||||
assert(self);
|
||||
pMe = (pSimST)self->pPrivate;
|
||||
assert(pMe);
|
||||
static int SimRun(pEVDriver self, float fVal)
|
||||
{
|
||||
pSimST pMe = NULL;
|
||||
float fDiff;
|
||||
time_t tD;
|
||||
|
||||
/* calculate time for completion */
|
||||
fDiff = fVal - pMe->fTarget;
|
||||
if(fDiff < .0) fDiff = -fDiff;
|
||||
pMe->tFinish = (int)time(&tD) + (int)(fDiff/pMe->fSpeed);
|
||||
assert(self);
|
||||
pMe = (pSimST) self->pPrivate;
|
||||
assert(pMe);
|
||||
|
||||
pMe->fTarget = fVal;
|
||||
/* calculate time for completion */
|
||||
fDiff = fVal - pMe->fTarget;
|
||||
if (fDiff < .0)
|
||||
fDiff = -fDiff;
|
||||
pMe->tFinish = (int) time(&tD) + (int) (fDiff / pMe->fSpeed);
|
||||
|
||||
/* in a fifth the failures, simply die, else simply do not find pos */
|
||||
if(SimRandom() < (pMe->fFailure/5))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
/*--------------------------------------------------------------------------*/
|
||||
static int SimError(pEVDriver self, int *iCode, char *error, int iErrLen)
|
||||
{
|
||||
assert(self);
|
||||
|
||||
if(RunComplete(self))
|
||||
{
|
||||
*iCode = 56;
|
||||
strncpy(error,"ERROR: (-: Simulated environment device error :-) ",iErrLen);
|
||||
}
|
||||
else
|
||||
{
|
||||
*iCode = 12;
|
||||
strncpy(error,"Environment still creeping along",iErrLen-1);
|
||||
}
|
||||
return 1;
|
||||
pMe->fTarget = fVal;
|
||||
|
||||
/* in a fifth the failures, simply die, else simply do not find pos */
|
||||
if (SimRandom() < (pMe->fFailure / 5)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
static int SimSend(pEVDriver self, char *pCommand, char *pReply, int iLen)
|
||||
{
|
||||
pSimST pMe = NULL;
|
||||
float fDiff;
|
||||
time_t tD;
|
||||
|
||||
assert(self);
|
||||
pMe = (pSimST)self->pPrivate;
|
||||
assert(pMe);
|
||||
|
||||
if(SimRandom() < pMe->fFailure)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
strncpy(pReply,"Device gracefully accepted command",iLen);
|
||||
return 1;
|
||||
}
|
||||
static int SimError(pEVDriver self, int *iCode, char *error, int iErrLen)
|
||||
{
|
||||
assert(self);
|
||||
|
||||
if (RunComplete(self)) {
|
||||
*iCode = 56;
|
||||
strncpy(error, "ERROR: (-: Simulated environment device error :-) ",
|
||||
iErrLen);
|
||||
} else {
|
||||
*iCode = 12;
|
||||
strncpy(error, "Environment still creeping along", iErrLen - 1);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
static int SimSim(pEVDriver self)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
static int SimSend(pEVDriver self, char *pCommand, char *pReply, int iLen)
|
||||
{
|
||||
pSimST pMe = NULL;
|
||||
float fDiff;
|
||||
time_t tD;
|
||||
|
||||
assert(self);
|
||||
pMe = (pSimST) self->pPrivate;
|
||||
assert(pMe);
|
||||
|
||||
if (SimRandom() < pMe->fFailure) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
strncpy(pReply, "Device gracefully accepted command", iLen);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
static int SimSim(pEVDriver self)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int SimFix(pEVDriver self, int iError)
|
||||
{
|
||||
float fRand;
|
||||
|
||||
/* return the three values MOTREDO, MOTFAIL, MOTOK with a third
|
||||
randomness
|
||||
*/
|
||||
assert(self);
|
||||
fRand = SimRandom();
|
||||
|
||||
if(iError == 12)
|
||||
{
|
||||
return DEVREDO;
|
||||
}
|
||||
|
||||
SICSLogWrite("Simulated Environment device dying randomly",eHWError);
|
||||
if(fRand < 0.3333)
|
||||
{
|
||||
return DEVOK;
|
||||
}
|
||||
else if(fRand < 0.66666)
|
||||
{
|
||||
return DEVREDO;
|
||||
}
|
||||
else
|
||||
{
|
||||
return DEVFAULT;
|
||||
}
|
||||
static int SimFix(pEVDriver self, int iError)
|
||||
{
|
||||
float fRand;
|
||||
|
||||
/* return the three values MOTREDO, MOTFAIL, MOTOK with a third
|
||||
randomness
|
||||
*/
|
||||
assert(self);
|
||||
fRand = SimRandom();
|
||||
|
||||
if (iError == 12) {
|
||||
return DEVREDO;
|
||||
}
|
||||
|
||||
SICSLogWrite("Simulated Environment device dying randomly", eHWError);
|
||||
if (fRand < 0.3333) {
|
||||
return DEVOK;
|
||||
} else if (fRand < 0.66666) {
|
||||
return DEVREDO;
|
||||
} else {
|
||||
return DEVFAULT;
|
||||
}
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
static int SimHalt(pEVDriver *self)
|
||||
{
|
||||
assert(self);
|
||||
|
||||
return 1;
|
||||
}
|
||||
static int SimHalt(pEVDriver * self)
|
||||
{
|
||||
assert(self);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
pEVDriver CreateSIMEVDriver(int argc, char *argv[])
|
||||
{
|
||||
pEVDriver pNew = NULL;
|
||||
pSimST pSim = NULL;
|
||||
|
||||
pNew = CreateEVDriver(argc,argv);
|
||||
pSim = (pSimST)malloc(sizeof(SimST));
|
||||
if(!pNew || !pSim)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
pNew->pPrivate = pSim;
|
||||
/* This causes problems with fortify. Apparently the free does not
|
||||
get replaced properly with the proper debug malloc function.
|
||||
pEVDriver CreateSIMEVDriver(int argc, char *argv[])
|
||||
{
|
||||
pEVDriver pNew = NULL;
|
||||
pSimST pSim = NULL;
|
||||
|
||||
pNew = CreateEVDriver(argc, argv);
|
||||
pSim = (pSimST) malloc(sizeof(SimST));
|
||||
if (!pNew || !pSim) {
|
||||
return NULL;
|
||||
}
|
||||
pNew->pPrivate = pSim;
|
||||
/* This causes problems with fortify. Apparently the free does not
|
||||
get replaced properly with the proper debug malloc function.
|
||||
pNew->KillPrivate = free;
|
||||
*/
|
||||
|
||||
pNew->KillPrivate = NULL;
|
||||
|
||||
/* initalise SimST */
|
||||
pSim->fFailure = .1;
|
||||
pSim->fSpeed = 1.;
|
||||
pSim->fTarget = 0.0;
|
||||
pSim->tFinish = 0;
|
||||
|
||||
/* are there parameters which give values for SIM ? */
|
||||
if(argc > 0)
|
||||
{
|
||||
pSim->fFailure = atof(argv[0]);
|
||||
}
|
||||
if(argc > 1)
|
||||
{
|
||||
pSim->fSpeed = atof(argv[1]);
|
||||
}
|
||||
|
||||
/* initialise function pointers */
|
||||
pNew->SetValue = SimRun;
|
||||
pNew->GetValue = GetSimPos;
|
||||
pNew->Send = SimSend;
|
||||
pNew->GetError = SimError;
|
||||
pNew->TryFixIt = SimFix;
|
||||
pNew->Init = SimSim;
|
||||
pNew->Close = SimSim;
|
||||
|
||||
return pNew;
|
||||
}
|
||||
*/
|
||||
|
||||
pNew->KillPrivate = NULL;
|
||||
|
||||
/* initalise SimST */
|
||||
pSim->fFailure = .1;
|
||||
pSim->fSpeed = 1.;
|
||||
pSim->fTarget = 0.0;
|
||||
pSim->tFinish = 0;
|
||||
|
||||
/* are there parameters which give values for SIM ? */
|
||||
if (argc > 0) {
|
||||
pSim->fFailure = atof(argv[0]);
|
||||
}
|
||||
if (argc > 1) {
|
||||
pSim->fSpeed = atof(argv[1]);
|
||||
}
|
||||
|
||||
/* initialise function pointers */
|
||||
pNew->SetValue = SimRun;
|
||||
pNew->GetValue = GetSimPos;
|
||||
pNew->Send = SimSend;
|
||||
pNew->GetError = SimError;
|
||||
pNew->TryFixIt = SimFix;
|
||||
pNew->Init = SimSim;
|
||||
pNew->Close = SimSim;
|
||||
|
||||
return pNew;
|
||||
}
|
||||
|
Reference in New Issue
Block a user