hi!
This commit is contained in:
159
tecs/tecs.c
159
tecs/tecs.c
@@ -12,8 +12,9 @@
|
||||
#include "tecs_lsc.h"
|
||||
#include "tecs_dlog.h"
|
||||
|
||||
int ftime (struct timeb *__timeptr); /* for some reason not defined in timeb.h with flag -std1 */
|
||||
|
||||
#define TABLE_FILE "lsci.tab"
|
||||
#define NO_CODE 999
|
||||
#define Progress(I) if (configuring) { configuring+=I; }
|
||||
|
||||
static SerChannel *ser=NULL;
|
||||
@@ -40,13 +41,13 @@ typedef struct {
|
||||
Testpoint /* C standard guarantees initialization to zero */
|
||||
cryo, /* data for main sensors (on heat exchanger, or the only sensors) */
|
||||
samp, /* data for extra sensors of sample stick */
|
||||
*testpoints[2]={&cryo, &samp},
|
||||
*testpoint=&cryo;
|
||||
*tpoints[2]={&cryo, &samp},
|
||||
*tpoint=&cryo;
|
||||
|
||||
static float
|
||||
tempC, /* set T */
|
||||
tempH, /* set T on heater */
|
||||
htr, /* heat power */
|
||||
tempC, /* set T (for sample) */
|
||||
tempH, /* set T on heat exchanger */
|
||||
htr, /* heat power percentage */
|
||||
tLimit, power, /* heater parameters */
|
||||
tLow=0, tHigh=0, /* lower limit of high-T sensor, upper limit of low-T sensor */
|
||||
tShift=0, /* setpoint shift */
|
||||
@@ -57,12 +58,11 @@ static int
|
||||
period=5000, /* default read interval (msec.) */
|
||||
logTime, /* next logging time */
|
||||
setFlag, /* temperature to be set */
|
||||
remoteFlag, /* to be set to remote mode */
|
||||
saveTime, /* time for a CRVSAV command */
|
||||
noResp=1, /* no response */
|
||||
quit, /* quit server */
|
||||
controlMode=2, /* 0: control on heater, 1: control on sample, 3: 2nd loop for difference heater-sample */
|
||||
mode, /* 0: local, 2: remote */
|
||||
mode=2, /* 0: local, 2: remote */
|
||||
maxfld, /* last used display field */
|
||||
busy, /* busy after CRVSAV */
|
||||
deviceFlag, /* device given via net */
|
||||
@@ -106,7 +106,7 @@ void idleHdl(int tmo, int fd) {
|
||||
if (iRet<0) logfileShowErr("CocHandleRequests");
|
||||
}
|
||||
|
||||
void concatDevice() {
|
||||
void concatDevice(void) {
|
||||
str_copy(device, cryo.device);
|
||||
if (0!=strcmp(cryo.device, samp.device) && (samp.device[0]!='\0' || samp.manual)) {
|
||||
str_append(device, "/");
|
||||
@@ -292,7 +292,7 @@ int instCurve(char *nam, char *channel) {
|
||||
return(retstat);
|
||||
}
|
||||
|
||||
int configInput() {
|
||||
int configInput(void) {
|
||||
char *t;
|
||||
char buf[80], nam[16], nbuf[256];
|
||||
int i, n, nn;
|
||||
@@ -300,11 +300,11 @@ int configInput() {
|
||||
char *ext;
|
||||
|
||||
retstat=-2; /* errors in following section are severe */
|
||||
if (testpoint->manual) {
|
||||
sprintf(buf, "'%s'", testpoint->device);
|
||||
if (tpoint->manual) {
|
||||
sprintf(buf, "'%s'", tpoint->device);
|
||||
} else {
|
||||
sprintf(buf, "%+d ", testpoint->code);
|
||||
if (testpoint->code==0) return(0);
|
||||
sprintf(buf, "%+d ", tpoint->code);
|
||||
if (tpoint->code==0) return(0);
|
||||
}
|
||||
if (table!=NULL && tim>tableTime+60) { my_free(table); table=NULL; }; /* clear old table */
|
||||
if (table==NULL) { /* read table */
|
||||
@@ -321,7 +321,7 @@ int configInput() {
|
||||
if (t==NULL) ERR_MSG("missing ' in table file");
|
||||
t++;
|
||||
n=1;
|
||||
if (testpoint==&samp) {
|
||||
if (tpoint==&samp) {
|
||||
samp.nSens=0;
|
||||
i=sscanf(t, "%12s%d%d", nam, &nn, &n);
|
||||
if (i<1) ERR_MSG("missing sensor name");
|
||||
@@ -337,23 +337,23 @@ int configInput() {
|
||||
if (n<0 || n>2) ERR_MSG("illegal value for nsensor");
|
||||
if (n==0) return(0);
|
||||
nam[strlen(nam)-1]='\0'; /* strip off quote */
|
||||
if (!testpoint->manual) { /* set device name */
|
||||
str_copy(testpoint->device, nam);
|
||||
if (!tpoint->manual) { /* set device name */
|
||||
str_copy(tpoint->device, nam);
|
||||
concatDevice();
|
||||
}
|
||||
str_append(nam, ext);
|
||||
|
||||
ERR_I(retstat=instCurve(nam, testpoint->ch1));
|
||||
ERR_I(retstat=instCurve(nam, tpoint->ch1));
|
||||
if (n==2) {
|
||||
str_append(nam, "l");
|
||||
ERR_I(retstat=instCurve(nam, testpoint->ch2));
|
||||
ERR_I(retstat=instCurve(nam, tpoint->ch2));
|
||||
}
|
||||
testpoint->nSens=n;
|
||||
tpoint->nSens=n;
|
||||
return(0);
|
||||
OnError: return(retstat);
|
||||
}
|
||||
|
||||
int loadCache() {
|
||||
int loadCache(void) {
|
||||
int i, j, k;
|
||||
char *res;
|
||||
char buf[256], nbuf[256], lbuf[16];
|
||||
@@ -418,11 +418,9 @@ float WeightedAverage(int n, float tH, float tL) {
|
||||
|
||||
int SetTemp(int switchOn) {
|
||||
char *ch;
|
||||
float scale;
|
||||
|
||||
if (tempC==0) {
|
||||
ERR_P(LscCmd(ser, "RANGE:0;SETP 1:0"));
|
||||
return(0);
|
||||
}
|
||||
scale=cryo.scale;
|
||||
ch=cryo.ch1;
|
||||
if (cryo.nSens>1 && tempC<(tLow+tHigh)/2) ch=cryo.ch2;
|
||||
if (samp.nSens>0) {
|
||||
@@ -430,6 +428,7 @@ int SetTemp(int switchOn) {
|
||||
tShift=0;
|
||||
ch=samp.ch1;
|
||||
if (cryo.nSens>1 && tempC<(tLow+tHigh)/2) ch=samp.ch2;
|
||||
scale=samp.scale;
|
||||
} else if (controlMode!=2) {
|
||||
tShift=0;
|
||||
}
|
||||
@@ -437,7 +436,14 @@ int SetTemp(int switchOn) {
|
||||
tShift=0;
|
||||
}
|
||||
str_copy(chan, ch);
|
||||
tempH=tempC+tShift;
|
||||
if (scale!=1.0) {
|
||||
ERR_P(LscCmd(ser, "LINEAR C,1,0,1,1,[tempC]"));
|
||||
}
|
||||
if (tempC==0) {
|
||||
ERR_P(LscCmd(ser, "RANGE:0;SETP 1:0"));
|
||||
return(0);
|
||||
}
|
||||
tempH=(tempC+tShift)/scale;
|
||||
if (switchOn) {
|
||||
ERR_P(LscCmd(ser, "CSET 1:[chan],1,1,0;RANGE:[iRange];SETP 1:[tempH]"));
|
||||
} else {
|
||||
@@ -447,7 +453,7 @@ int SetTemp(int switchOn) {
|
||||
OnError: return(-1);
|
||||
}
|
||||
|
||||
int ReadTemp() {
|
||||
int ReadTemp(void) {
|
||||
char buf[256];
|
||||
int i;
|
||||
|
||||
@@ -498,11 +504,11 @@ int ReadTemp() {
|
||||
OnError: return(-1);
|
||||
}
|
||||
|
||||
int PeriodicTask() {
|
||||
int PeriodicTask(void) {
|
||||
char buf[256], lbuf[16];
|
||||
char *res;
|
||||
int i, k;
|
||||
float t2[2], p, d;
|
||||
float t2[2], p, d, w;
|
||||
|
||||
ERR_P(LscCmd(ser, "DIOST?>cod1,out1;DOUT 3,29;HTR?>htr;BUSY?>busy"));
|
||||
if (cryo.codDefined) {
|
||||
@@ -520,6 +526,7 @@ int PeriodicTask() {
|
||||
str_copy(status, "controller connected");
|
||||
}
|
||||
configuring++;
|
||||
tempC=0;
|
||||
if (cryo.manual || cryo.code!=0) { cryo.dirty=1; }
|
||||
if (samp.manual || samp.code!=0) { samp.dirty=1; }
|
||||
ERR_P(LscCmd(ser, "RANGE:0")); /* switch off heater */
|
||||
@@ -549,10 +556,12 @@ int PeriodicTask() {
|
||||
logTime=(tim/logPeriod+1)*logPeriod;
|
||||
}
|
||||
if (samp.nSens>0 && cryo.nSens>0 && controlMode==2 && tempC!=0) {
|
||||
d=tempH-cryo.temp; d=exp(-d*d); /* if d is small, we are far from setpoint */
|
||||
if (tInt<60000/period) tInt+=d; /* increase integral time until 60 sec. */
|
||||
if (tInt>d) {
|
||||
p=d/tInt;
|
||||
d=(tempH-cryo.temp)/cryo.temp-1.0; /* relative difference */
|
||||
w=exp(-d*d*230); /* gaussian */
|
||||
if (w<0.1) tInt=0; /* reset when far from setpoint (more than 10 %) */
|
||||
if (tInt<30000/period) tInt+=w; /* increase integral time until 30 sec. */
|
||||
if (tInt>w) {
|
||||
p=w/tInt;
|
||||
} else {
|
||||
p=1.0;
|
||||
}
|
||||
@@ -577,26 +586,27 @@ int PeriodicTask() {
|
||||
cryo.code1=3*decod[cod2 % 8] ^ 2*decod[cod1 % 8]; /* ^ is exclusive OR */
|
||||
samp.code1=-(3*decod[cod2 / 8] ^ 2*decod[cod1 / 8]);
|
||||
for (i=0; i<2; i++) {
|
||||
testpoint=testpoints[i];
|
||||
if (testpoint->code1!=testpoint->code) {
|
||||
testpoint->code=testpoint->code1;
|
||||
testpoint->codChanged=1;
|
||||
tpoint=tpoints[i];
|
||||
if (tpoint->code1!=tpoint->code) {
|
||||
tpoint->code=tpoint->code1;
|
||||
tpoint->codChanged=1;
|
||||
} else {
|
||||
if (testpoint->codChanged) {
|
||||
testpoint->codChanged=0;
|
||||
if (tpoint->codChanged) {
|
||||
tpoint->codChanged=0;
|
||||
Progress(1);
|
||||
if (testpoint->code1==0) {
|
||||
logfileOut(LOG_MAIN, "%s unplugged\n", testpoint->tname);
|
||||
if (tpoint->code1==0) {
|
||||
logfileOut(LOG_MAIN, "%s unplugged\n", tpoint->tname);
|
||||
} else {
|
||||
logfileOut(LOG_MAIN, "plugged %d on %s\n", testpoint->code1, testpoint->tname);
|
||||
logfileOut(LOG_MAIN, "plugged %d on %s\n", tpoint->code1, tpoint->tname);
|
||||
}
|
||||
if (testpoint->codDefined) {
|
||||
testpoint->manual=0;
|
||||
if (tpoint->codDefined) {
|
||||
tpoint->manual=0;
|
||||
}
|
||||
testpoint->dirty=1;
|
||||
tempC=0;
|
||||
tpoint->dirty=1;
|
||||
if (!configuring) configuring=1;
|
||||
}
|
||||
testpoint->codDefined=1;
|
||||
tpoint->codDefined=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -619,32 +629,32 @@ int PeriodicTask() {
|
||||
|
||||
int inputSettings(Testpoint *this) {
|
||||
|
||||
testpoint=this;
|
||||
if (testpoint->dirty && samp.codDefined) {
|
||||
if (busy==0 || cryo.dirty>=0 && testpoint->dirty>=0) { /* do not enter when busy and cryo.dirty/P indicates error on last time */
|
||||
if (testpoint->manual) {
|
||||
logfileOut(LOG_MAIN ,"configure %s inputs for %s\n", testpoint->tname, testpoint->device);
|
||||
tpoint=this;
|
||||
if (tpoint->dirty && samp.codDefined) {
|
||||
if (busy==0 || cryo.dirty>=0 && tpoint->dirty>=0) { /* do not enter when busy and cryo.dirty/P indicates error on last time */
|
||||
if (tpoint->manual) {
|
||||
logfileOut(LOG_MAIN ,"configure %s inputs for %s\n", tpoint->tname, tpoint->device);
|
||||
} else {
|
||||
if (testpoint->code==0) {
|
||||
logfileOut(LOG_MAIN ,"reset %s inputs\n", testpoint->tname);
|
||||
if (tpoint->code==0) {
|
||||
logfileOut(LOG_MAIN ,"reset %s inputs\n", tpoint->tname);
|
||||
} else {
|
||||
logfileOut(LOG_MAIN ,"configure %s inputs for code %+d\n", testpoint->tname, testpoint->code);
|
||||
logfileOut(LOG_MAIN ,"configure %s inputs for code %+d\n", tpoint->tname, tpoint->code);
|
||||
}
|
||||
}
|
||||
if (testpoint->dirty>0) testpoint->try=0;
|
||||
testpoint->nSens=0;
|
||||
if (!testpoint->manual) { testpoint->device[0]='\0'; concatDevice(); }
|
||||
testpoint->dirty=configInput();
|
||||
if (testpoint->dirty<0) {
|
||||
testpoint->try++;
|
||||
if (testpoint->dirty!=-1 || testpoint->try>3) {
|
||||
if (tpoint->dirty>0) tpoint->try=0;
|
||||
tpoint->nSens=0;
|
||||
if (!tpoint->manual) { tpoint->device[0]='\0'; concatDevice(); }
|
||||
tpoint->dirty=configInput();
|
||||
if (tpoint->dirty<0) {
|
||||
tpoint->try++;
|
||||
if (tpoint->dirty!=-1 || tpoint->try>3) {
|
||||
logfileShowErr("fatal error");
|
||||
testpoint->dirty=0; testpoint->device[0]='\0'; concatDevice();
|
||||
tpoint->dirty=0; tpoint->device[0]='\0'; concatDevice();
|
||||
} else {
|
||||
logfileShowErr("try again");
|
||||
}
|
||||
} else {
|
||||
ERR_P(LscCmd(ser, "ALARM [testpoint.ch1]:[testpoint.nSens],1,[tLimit],0,0,1;ALARM [testpoint.ch2]:0;RELAY 1:1;BEEP:0"));
|
||||
ERR_P(LscCmd(ser, "ALARM [tpoint.ch1]:[tpoint.nSens],1,[tLimit],0,0,1;ALARM [tpoint.ch2]:0;RELAY 1:1;BEEP:0"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -652,7 +662,7 @@ int inputSettings(Testpoint *this) {
|
||||
OnError: return(-1);
|
||||
}
|
||||
|
||||
int Settings() {
|
||||
int Settings(void) {
|
||||
int i, j, k;
|
||||
float pa, pr, pw, dif;
|
||||
char nbuf[256], buf[256], *cfg, *p;
|
||||
@@ -661,9 +671,9 @@ int Settings() {
|
||||
if (cryo.dirty && cryo.codDefined || samp.dirty && samp.codDefined) {
|
||||
|
||||
for (i=0; i<2; i++) {
|
||||
testpoint=testpoints[i];
|
||||
if (testpoint->dirty) {
|
||||
ERR_P(LscCmd(ser, "DISPFLD 2,[testpoint.ch1],1;DISPFLD 4,[testpoint.ch2],1"));
|
||||
tpoint=tpoints[i];
|
||||
if (tpoint->dirty) {
|
||||
ERR_P(LscCmd(ser, "DISPFLD 2,[tpoint.ch1],1;DISPFLD 4,[tpoint.ch2],1"));
|
||||
}
|
||||
}
|
||||
inputSettings(&cryo);
|
||||
@@ -692,9 +702,7 @@ int Settings() {
|
||||
power=pw;
|
||||
logfileOut(LOG_INFO, "power %f\n", power, iAmp, iRange);
|
||||
ERR_P(LscCmd(ser, "CLIMIT 1:[tLimit],0,0,[iAmp],[iRange]"));
|
||||
tempC=0.001; tShift=0;
|
||||
ERR_I(SetTemp(1));
|
||||
tempC=0;
|
||||
}
|
||||
if (samp.nSens>=cryo.nSens) {
|
||||
maxfld=2*samp.nSens;
|
||||
@@ -740,7 +748,7 @@ int Settings() {
|
||||
return(-1);
|
||||
}
|
||||
|
||||
int ExecuteRequest() {
|
||||
int ExecuteRequest(void) {
|
||||
char *t, *res;
|
||||
struct CocClient *client;
|
||||
|
||||
@@ -765,6 +773,7 @@ int ExecuteRequest() {
|
||||
client->cmd[0]='\0';
|
||||
}
|
||||
if (deviceFlag) {
|
||||
tempC=0;
|
||||
if (!configuring) {
|
||||
str_copy(status, "configuring");
|
||||
configuring=1;
|
||||
@@ -860,7 +869,7 @@ int main(int argc, char *argv[])
|
||||
binDir="bin/";
|
||||
logDir="log/";
|
||||
serverId="tecs";
|
||||
host="lnsp26";
|
||||
host="lnsp26:4000/0";
|
||||
port=0;
|
||||
msecTmo=0;
|
||||
logfileOut(LOG_INFO ,"%s ", argv[0]);
|
||||
@@ -919,22 +928,25 @@ int main(int argc, char *argv[])
|
||||
|
||||
CocDefStruct(cryo, Testpoint);
|
||||
CocDefStruct(samp, Testpoint);
|
||||
CocDefPtr(testpoint, Testpoint);
|
||||
CocDefPtr(tpoint, Testpoint);
|
||||
|
||||
CocFltFld(Testpoint, temp, CocRD);
|
||||
CocFltFld(Testpoint, t1, CocRD);
|
||||
CocFltFld(Testpoint, t2, CocRD);
|
||||
CocFltFld(Testpoint, scale, CocRD);
|
||||
|
||||
CocStrFld(Testpoint, ch1, CocRD);
|
||||
CocStrFld(Testpoint, ch2, CocRD);
|
||||
|
||||
CocDefFlt(htr, CocRD);
|
||||
CocDefFlt(power, CocRD);
|
||||
CocDefFlt(tempC, setFlag);
|
||||
CocDefFlt(tempH, CocRD);
|
||||
CocDefFlt(tLimit, CocRD);
|
||||
|
||||
CocAlias(tempX,cryo.temp);
|
||||
CocAlias(tempP,samp.temp);
|
||||
CocAlias(set,tempC);
|
||||
|
||||
CocDefStr(device, deviceFlag);
|
||||
|
||||
@@ -982,6 +994,7 @@ int main(int argc, char *argv[])
|
||||
logfileOut(LOG_INFO, "opened\n");
|
||||
}
|
||||
logfileWrite(logMask);
|
||||
ERR_P(LscCmd(ser, "MODE:[mode]"));
|
||||
per=period;
|
||||
cntError=0;
|
||||
while (!quit) {
|
||||
@@ -994,6 +1007,8 @@ int main(int argc, char *argv[])
|
||||
if (cntError>0) cntError--;
|
||||
}
|
||||
}
|
||||
ERR_P(LscCmd(ser, "MODE:1"));
|
||||
logfileWrite(logMask);
|
||||
ERR_MSG("got quit command");
|
||||
OnError:
|
||||
logfileShowErr("exit TecsServer");
|
||||
|
||||
Reference in New Issue
Block a user