insert tecs_client into archive + little update

This commit is contained in:
cvs
2000-05-29 09:05:05 +00:00
parent 1a56fbc568
commit a89b3208d6
12 changed files with 1433 additions and 165 deletions

View File

@@ -25,24 +25,36 @@ static char *binDir=NULL;
static char *logDir=NULL;
typedef struct {
float temp, t1, t2; /* calc, high, low temperature */
float tMin, tMax, min1, max1, min2, max2; /* minimum and maximum temperatures since ... */
float t, min, max; /* temperatures */
int sMin, sMax; /* reading status summary */
int present; /* sensor is present */
int readStat; /* reading status */
char ch[2]; /* channels */
} SensorT;
SensorT
sens1, sens2, sens3, sens4,
*sensors[5]={NULL, &sens1, &sens2, &sens3, &sens4 },
*sensor=&sens1;
typedef struct {
SensorT *sensor1, *sensor2;
float temp; /* weighted temperature */
float tMin, tMax; /* minimum and maximum temperatures since ... */
int dirty; /* input config to be reloaded */
int try; /* trial count */
int manual; /* manual device */
int code, code1; /* device code, buffer for device code */
int nSens; /* number of sensors */
int codChanged; /* code has changed */
int codDefined; /* code is not yet confirmed */
float scale; /* scale for extreme ranges */
char ch1[2], ch2[2]; /* channels for high/low T */
char device[16]; /* device name */
char tname[16];
} Testpoint;
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 */
cryo={&sens1, &sens2 }, /* data for main sensors (on heat exchanger, or the only sensors) */
samp={&sens3, &sens4 }, /* data for extra sensors of sample stick */
*tpoints[2]={&cryo, &samp},
*tpoint=&cryo;
@@ -68,6 +80,7 @@ static int
noResp=2, /* no response */
quit, /* quit server */
controlMode=2, /* 0: control on heater, 1: control on sample, 3: 2nd loop for difference heater-sample */
int2=30, /* inegration time for controlMode 2 */
remoteMode, /* 1: local, 2: remote */
maxfld, /* last used display field */
busy, /* busy after CRVSAV */
@@ -379,13 +392,15 @@ int configInput(void) {
t++;
n=1;
if (tpoint==&samp) {
samp.nSens=0;
sens3.present=0;
sens4.present=0;
i=sscanf(t, "%12s%d%d", nam, &nn, &n);
if (i<1) ERR_MSG("missing sensor name");
ext=".s";
dispFld=2;
} else {
cryo.nSens=0;
sens1.present=0;
sens2.present=0;
tLow=0; tHigh=0;
controlMode=0;
i=sscanf(t, "%12s%d%d%d%f%d%f%f%f", nam, &n, &nn, &controlMode, &tLimit, &resist, &power, &tLow, &tHigh);
@@ -402,12 +417,13 @@ int configInput(void) {
}
str_append(nam, ext);
ERR_I(retstat=instCurve(nam, tpoint->ch1, dispFld));
ERR_I(retstat=instCurve(nam, tpoint->sensor1->ch, dispFld));
tpoint->sensor1->present=1;
if (n==2) {
str_append(nam, "l");
ERR_I(retstat=instCurve(nam, tpoint->ch2, dispFld+2));
ERR_I(retstat=instCurve(nam, tpoint->sensor2->ch, dispFld+2));
tpoint->sensor2->present=1;
}
tpoint->nSens=n;
return(0);
OnError: return(retstat);
}
@@ -452,73 +468,84 @@ int loadCache(void) {
return(-1);
}
float WeightedAverage(int n, float tH, float tL) {
float WeightedAverage(int presentH, int presentL, float tH, float tL) {
float p,q;
if (n==0) {
return(0.0);
} else if (n<2) {
return(tH);
} else {
if (tL<tLow) {
return(tL);
} else if (tH<tHigh) {
p=tHigh-tH;
q=tL-tLow;
if (p==0.0 && q==0.0) { p=1; q=1; } /* should not be the case */
return((tL*p*p+tH*q*q)/(p*p+q*q));
} else {
return(tH);
if (presentH) {
if (presentL) {
if (tL<tLow) {
return(tL);
} else if (tH<tHigh) {
p=tHigh-tH;
q=tL-tLow;
if (p==0.0 && q==0.0) { p=1; q=1; } /* should not be the case */
return((tL*p*p+tH*q*q)/(p*p+q*q));
}
}
return(tH);
} else if (presentL) {
return(tL);
}
return(0.0);
}
int LogMinMax(int new) {
char buf[256];
int i, j, logIt;
char buf[256], bufs[256];
int i, j, l, ls, logIt, stat;
float tol, tmin[2], tmax[2];
SensorT *s1, *s2;
buf[0]='\0';
if (cryo.nSens>0) {
str_append(buf, "MDAT?[cryo.ch1]>cryo.min1,cryo.max1;");
if (cryo.nSens>1) {
str_append(buf, "MDAT?[cryo.ch2]>cryo.min2,cryo.max2;");
} else {
cryo.t2=0;
l=0;
ls=0;
for (i=1; i<=4; i++) {
sensor=sensors[i];
sensor->sMin=0;
sensor->sMax=0;
if (sensor->present) {
assert(l<128);
sprintf(buf+l, "MDAT?[sens%d.ch]>sens%d.min,sens%d.max;", i, i, i);
l=strlen(buf);
assert(ls<128);
sprintf(bufs+ls, "MDATST?[sens%d.ch]>sens%d.sMin,sens%d.sMax;", i, i, i);
ls=strlen(bufs);
}
} else {
cryo.t1=0;
cryo.t2=0;
}
if (samp.nSens>0) {
str_append(buf, "MDAT?[samp.ch1]>samp.min1,samp.max1;");
if (samp.nSens>1) {
str_append(buf, "MDAT?[samp.ch2]>samp.min2,samp.max2;");
} else {
samp.t2=0;
}
} else {
samp.t1=0;
samp.t2=0;
}
i=strlen(buf);
if (i>0) {
if (ls>0) {
bufs[ls-1]='\0'; /* strip off ';' */
ERR_P(LscCmd(ser, bufs));
str_append(buf, "MNMXRST");
ERR_P(LscCmd(ser, buf));
}
/* check for reading errors */
for (i=1; i<=4; i++) {
sensor=sensors[i];
stat=sensor->sMin | sensor->sMax;
if (stat != sensor->readStat) {
sensor->readStat=stat;
if (stat & 1) logfileOut(LOG_MAIN, "invalid reading %s\n", sensor->ch);
if (stat & 2) logfileOut(LOG_MAIN, "old reading %s\n", sensor->ch);
if (stat & 12) logfileOut(LOG_MAIN, "unknown reading status %s\n", sensor->ch);
if (stat & 16) logfileOut(LOG_MAIN, "temp underrange %s\n", sensor->ch);
if (stat & 32) logfileOut(LOG_MAIN, "temp overrange %s\n", sensor->ch);
if (stat & 64) logfileOut(LOG_MAIN, "units zero %s\n", sensor->ch);
if (stat &128) logfileOut(LOG_MAIN, "units overrange %s\n", sensor->ch);
if (stat==0) logfileOut(LOG_MAIN, "reading o.k. %s\n", sensor->ch);
}
}
logIt=0;
for (i=0; i<2; i++) {
tpoint=tpoints[i];
if (tpoint->nSens>0) {
tpoint->tMin = WeightedAverage(tpoint->nSens, tpoint->min1, tpoint->min2) * tpoint->scale;
tpoint->tMax = WeightedAverage(tpoint->nSens, tpoint->max1, tpoint->max2) * tpoint->scale;
}
s1=tpoint->sensor1;
s2=tpoint->sensor2;
tpoint->tMin = WeightedAverage(s1->present, s2->present, s1->min, s2->min) * tpoint->scale;
tpoint->tMax = WeightedAverage(s1->present, s2->present, s1->max, s2->max) * tpoint->scale;
}
sprintf(buf, "@%.3f < T < %.3f K", cryo.tMin, cryo.tMax);
if (samp.nSens>0) {
if (samp.tMax>0.0) {
sprintf(buf1, "(reg), %.3f < T < %.3f K (samp)", samp.tMin, samp.tMax);
str_append(buf, buf1);
}
@@ -542,13 +569,13 @@ int SetTemp(int switchOn) {
logfileOut(LOG_MAIN, "set %.3f\n", tempC);
}
scale=cryo.scale;
ch=cryo.ch1;
if (cryo.nSens>1 && tempC<(tLow+tHigh)/2) ch=cryo.ch2;
if (samp.nSens>0) {
ch=sens1.ch;
if (sens2.present && tempC<(tLow+tHigh)/2) ch=sens2.ch;
if (sens3.present) {
if (controlMode==1) { /* control directly on sample sensor */
tShift=0;
ch=samp.ch1;
if (cryo.nSens>1 && tempC<(tLow+tHigh)/2) ch=samp.ch2;
ch=sens3.ch;
if (sens2.present && tempC<(tLow+tHigh)/2) ch=sens4.ch;
scale=samp.scale;
} else if (controlMode!=2) {
tShift=0;
@@ -581,42 +608,29 @@ int SetTemp(int switchOn) {
int ReadTemp(void) {
char buf[256];
int i;
int i, l;
SensorT *sensor;
readTemp=0;
buf[0]='\0';
if (cryo.nSens>0) {
str_append(buf, "KRDG?[cryo.ch1]>cryo.t1;");
if (cryo.nSens>1) {
str_append(buf, "KRDG?[cryo.ch2]>cryo.t2;");
l=0;
for (i=1; i<=4; i++) {
sensor=sensors[i];
if (sensor->present) {
assert(l<128);
sprintf(buf+l, "KRDG?[sens%d.ch]>sens%d.t;", i, i);
l=strlen(buf);
} else {
cryo.t2=0;
sensor->t=0.0;
}
} else {
cryo.t1=0;
cryo.t2=0;
}
if (samp.nSens>0) {
str_append(buf, "KRDG?[samp.ch1]>samp.t1;");
if (samp.nSens>1) {
str_append(buf, "KRDG?[samp.ch2]>samp.t2;");
} else {
samp.t2=0;
}
} else {
samp.t1=0;
samp.t2=0;
}
i=strlen(buf);
if (i>0) {
buf[i-1]='\0'; /* strip off ';' */
if (l>0) {
buf[l-1]='\0'; /* strip off ';' */
ERR_P(LscCmd(ser, buf));
}
cryo.temp=WeightedAverage(cryo.nSens, cryo.t1, cryo.t2)*cryo.scale;
samp.temp=WeightedAverage(samp.nSens, samp.t1, samp.t2)*samp.scale;
cryo.temp=WeightedAverage(sens1.present, sens2.present, sens1.t, sens2.t)*cryo.scale;
samp.temp=WeightedAverage(sens3.present, sens4.present, sens3.t, sens4.t)*samp.scale;
if (samp.temp==0.0) samp.temp=cryo.temp;
if (!deviceFlag
&& !samp.dirty && samp.codDefined && !samp.codChanged
@@ -635,7 +649,7 @@ int PeriodicTask(void) {
char *next;
int i, k;
time_t putTim;
float t3[3], p, d, w;
float t3[3], p, d, w, t;
ERR_P(LscCmd(ser, "DIOST?>cod1,out1;DOUT 3,29;HTR?>htr;HTRST?>htrst;BUSY?>busy"));
if (cryo.codDefined && samp.codDefined) {
@@ -644,6 +658,7 @@ int PeriodicTask(void) {
}
if (htrst!=htrst0) {
ERR_I(LogMinMax(0));
if (htrst<0 || htrst>6) {
sprintf(buf, "heater status %d\n", htrst);
logfileOut(LOG_MAIN, buf);
@@ -715,18 +730,18 @@ int PeriodicTask(void) {
if (tim>=logTime) {
i=0;
if (cryo.nSens>0) {
if (sens1.present) {
t3[0]=cryo.temp;
i=1;
} else {
t3[0]=undef;
}
if (samp.nSens>0) {
if (sens3.present) {
t3[1]=samp.temp;
i=2;
} else {
if (cryo.nSens>1) {
t3[1]=cryo.t2;
if (sens2.present) {
t3[1]=sens2.t;
i=2;
} else {
t3[1]=undef;
@@ -743,11 +758,14 @@ int PeriodicTask(void) {
logTime=(putTim/logPeriod+1)*logPeriod;
if (tim>mmTime) ERR_I(LogMinMax(0));
}
if (samp.nSens>0 && cryo.nSens>0 && controlMode==2 && tempC!=0) {
d=(tempH-cryo.temp)/cryo.temp-1.0; /* relative difference */
if (sens1.present && sens3.present && controlMode==2 && tempC!=0) {
t=sens1.t;
if (sens2.present && tempC<(tLow+tHigh)/2) t=sens2.t;
d=(tempH-t)/t-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/per) tInt+=w; /* increase integral time until 30 sec. */
if (int2<1) int2=1;
if (tInt<int2*1000/per) tInt+=w; /* increase integral time until int2 sec. */
if (tInt>w) {
p=w/tInt;
} else {
@@ -847,7 +865,8 @@ int inputSettings(Testpoint *this) {
}
}
if (tpoint->dirty>0) tpoint->try=0;
tpoint->nSens=0;
tpoint->sensor1->present=0;
tpoint->sensor2->present=0;
if (!tpoint->manual) { tpoint->device[0]='\0'; concatDevice(); }
tpoint->dirty=configInput();
if (tpoint->dirty<0) {
@@ -896,52 +915,50 @@ int SetPower(void) {
}
int Display(void) {
char flds[5], fmt[5], disp[32], buf[256];
int i,j,k;
char flds[6], fmt[6], buf[256];
int i,k,l;
SensorT *s;
maxfld=0;
for (i=1; i<=4; i++) { flds[i]=' '; fmt[i]=' '; }
for (j=0; j<2; j++) { /* fill in kelvin display fields */
tpoint=tpoints[j];
if (tpoint->nSens>0) {
k=1+j;
flds[k]=tpoint->ch1[0]; fmt[k]='1'; if (k>maxfld) maxfld=k;
if (tpoint->nSens>1) {
k=3+j;
flds[k]=tpoint->ch2[0]; fmt[k]='1'; if (k>maxfld) maxfld=k;
k=1;
flds[0]='*';
flds[5]='\0';
for (i=1; i<=4; i++) { /* fill in kelvin fields */
s=sensors[i];
if (s->present) {
flds[k]=s->ch[0];
fmt[k]='1';
if (k>maxfld) maxfld=k;
} else {
flds[k]='\0';
}
k=k+2; if (k>4) k=2;
}
for (i=1; i<=4; i++) { /* fill in raw fields */
s=sensors[i];
if (s->present) {
k=strlen(flds); /* find next free field */
if (k<=4) {
if (k>maxfld) maxfld=k;
flds[k]=s->ch[0];
fmt[k]='3';
}
}
}
for (j=0; j<2; j++) { /* fill raw display fields */
tpoint=tpoints[j];
if (tpoint->nSens>0) {
k=2-j; /* try first right (or left) of the kelvin field */
if (flds[k]!=' ') k=3+j; /* then the field below */
if (flds[k]!=' ') k=4-j; /* then below right */
if (flds[k]==' ') {
if (k>maxfld) maxfld=k;
flds[k]=tpoint->ch1[0]; fmt[k]='3';
}
if (tpoint->nSens>1) {
k=4-j; /* try right (or left) of the kelvin field */
if (flds[k]==' ') {
if (k>maxfld) maxfld=k;
flds[k]=tpoint->ch2[0]; fmt[k]='3';
}
}
}
}
/* fields 5-8 standard raw data */
ERR_P(LscCmd(ser, "DISPFLD 5,A,3;DISPFLD 6,C,3;DISPFLD 7,B,3;DISPFLD 8,D,3"));
if (maxfld==0) { /* show raw data */
ERR_P(LscCmd(ser, "DISPFLD 1,A,3;DISPFLD 2,C,3;DISPFLD 3,B,3;DISPFLD 4,D,3;DISPLAY:4"));
} else {
buf[0]='\0';
for (i=1; i<=maxfld; i++) {
if (flds[i]!=' ') {
sprintf(disp, "DISPFLD %d,%c,%c;", i, flds[i], fmt[i]);
l=0;
for (k=1; k<=maxfld; k++) {
if (flds[k]!='\0') {
assert(l<128);
sprintf(buf+l, "DISPFLD %d,%c,%c;", k, flds[k], fmt[k]);
l=strlen(buf);
}
str_append(buf, disp);
}
str_append(buf, "DISPLAY:[maxfld]");
ERR_P(LscCmd(ser, buf));
@@ -952,6 +969,7 @@ int Display(void) {
int Settings(void) {
char nbuf[256], buf[256], *cfg, *p;
char alarms[3];
cfg=NULL;
if (cryo.dirty && cryo.codDefined || samp.dirty && samp.codDefined) {
@@ -959,17 +977,30 @@ int Settings(void) {
ERR_I(inputSettings(&cryo));
ERR_I(inputSettings(&samp));
if (cryo.nSens>0) {
ERR_P(LscCmd(ser, "ALARM A:0;ALARM B:0;ALARM C:0;ALARM D:0"));
alarms[0]='\0';
alarms[1]='\0';
alarms[2]='\0';
if (sens1.present) {
ERR_I(SetPower());
ERR_P(LscCmd(ser, "ALARM [cryo.ch1]:1,1,[tLimit],0,0,1;ALARM [cryo.ch2]:0;RELAY 1:1;BEEP:0"));
if (samp.nSens>0) {
ERR_P(LscCmd(ser, "ALARM [samp.ch1]:1,1,[tLimit],0,0,1;ALARM [samp.ch2]:0"));
} else {
ERR_P(LscCmd(ser, "ALARM [samp.ch1]:0;ALARM [samp.ch2]:0"));
str_copy(buf, "ALARM [sens1.ch]:1,1,[tLimit],0,0,1;RELAY 1:1;BEEP:0");
alarms[0]=sens1.ch[0];
if (sens3.present) {
str_append(buf, ";ALARM [sens3.ch]:1,1,[tLimit],0,0,1");
alarms[1]=sens3.ch[0];
}
} else {
ERR_P(LscCmd(ser, "ALARM [cryo.ch1]:0;ALARM [cryo.ch2]:0;ALARM [samp.ch1]:0;ALARM [samp.ch2]:0"));
ERR_P(LscCmd(ser, buf));
}
/* switch of unused channels */
buf[0]='\0';
if (NULL==strchr(alarms, 'A')) str_append(buf, ";ALARM A:0");
if (NULL==strchr(alarms, 'B')) str_append(buf, ";ALARM B:0");
if (NULL==strchr(alarms, 'C')) str_append(buf, ";ALARM C:0");
if (NULL==strchr(alarms, 'D')) str_append(buf, ";ALARM D:0");
if (buf[0]!='\0') ERR_P(LscCmd(ser, buf+1)); /* send without leading semicolon */
ERR_I(Display());
str_copy(nbuf, binDir);
@@ -1012,7 +1043,7 @@ int ExecuteRequest(void) {
}
if (setFlag) {
setFlag=0;
if (cryo.nSens>0) {
if (sens1.present) {
tInt=0; /* reset integral time */
ERR_I(SetTemp(1));
}
@@ -1123,11 +1154,11 @@ int main(int argc, char *argv[])
int port, msecTmo;
str_copy(cryo.tname,"main");
str_copy(cryo.ch1,"A");
str_copy(cryo.ch2,"B");
str_copy(sens1.ch,"A");
str_copy(sens2.ch,"B");
str_copy(samp.tname,"sample stick");
str_copy(samp.ch1,"C");
str_copy(samp.ch2,"D");
str_copy(sens3.ch,"C");
str_copy(sens4.ch,"D");
cryo.codChanged=1;
cryo.scale=1.0;
samp.codChanged=1;
@@ -1203,18 +1234,23 @@ int main(int argc, char *argv[])
CocDefPtr(tpoint, Testpoint);
CocFltFld(Testpoint, temp, CocRD);
CocFltFld(Testpoint, t1, CocRD);
CocFltFld(Testpoint, t2, CocRD);
CocFltFld(Testpoint, scale, CocRD);
CocFltFld(Testpoint, min1, CocRD);
CocFltFld(Testpoint, min2, CocRD);
CocFltFld(Testpoint, max1, CocRD);
CocFltFld(Testpoint, max2, CocRD);
CocFltFld(Testpoint, tMin, CocRD);
CocFltFld(Testpoint, tMax, CocRD);
CocStrFld(Testpoint, ch1, CocRD);
CocStrFld(Testpoint, ch2, CocRD);
CocDefStruct(sens1, SensorT);
CocDefStruct(sens2, SensorT);
CocDefStruct(sens3, SensorT);
CocDefStruct(sens4, SensorT);
CocDefPtr(sensor, SensorT);
CocFltFld(SensorT, t, CocRD);
CocFltFld(SensorT, min, CocRD);
CocFltFld(SensorT, max, CocRD);
CocIntFld(SensorT, readStat, CocRD);
CocIntFld(SensorT, sMin, CocRD);
CocIntFld(SensorT, sMax, CocRD);
CocStrFld(SensorT, ch, CocRD);
CocDefFlt(htr, CocRD);
CocDefFlt(power, powerFlag);
@@ -1256,6 +1292,7 @@ int main(int argc, char *argv[])
CocDefInt(logPeriod, CocWR);
CocDefInt(readTemp, CocWR);
CocDefInt(controlMode, CocWR);
CocDefInt(int2, CocWR);
CocDefInt(busy, CocRD);
CocDefInt(serialNo, CocRD);
CocDefInt(configuring, CocRD);
@@ -1263,6 +1300,12 @@ int main(int argc, char *argv[])
CocAlias(tempX,cryo.temp);
CocAlias(tempP,samp.temp);
CocAlias(tX,cryo.temp);
CocAlias(tS,samp.temp);
CocAlias(t1,sens1.t);
CocAlias(t2,sens2.t);
CocAlias(t3,sens3.t);
CocAlias(t4,sens4.t);
CocAlias(set,tempC);
CocAlias(int,integ);