/*============================================================================= Filename: analogIn.c Abs: This file contains subroutines supporting analog conversion Rem: All functions called by the subroutine record get passed one argument: Functions: Description ------------ ---------------------------- InitAnalogCalc General Initialization AnalogCalc Analog converion based on signal type Spear Functions: -------------- AnalogTcRefTemp Calc thermocouple ref temp AnalogTcTemp Calc thermocouple type "T" temp and iso-thermal plate voltage contribution Local Functions: --------------- PolyInt Simple parabolic interpolation between the indicated data points. Side: When adding or removing functions, modify analogSub.dbd created: 02-Sep-2009 -------------------------------------------------------------- revision history: 08-Jul-2017, K. Luchini remove functions ToTcTemp and AnalogTcVplate and combine into AnalogTcRefTemp function 31-May-2017, K. Luchini modify AnalogTcTemp to use for both ref and other type "t" thermocouple channels. remove AnalogTcTemp function 07-Feb-2017, K. Luchini added header, and functions AnalogTcTemp, AnalogTcTemp2,AnalogTcVplate,PolyInt 02-Sep-2009 Sonya Hoobler initial version ==============================================================*/ #include "copyright_SLAC.h" #include #include /* for pow(), log() */ #include #include /* for sprintf */ #include "epicsTypes.h" /* epicsUInt16, etc. */ #include "dbAccess.h" /* S_db_badField */ #include "errlog.h" /* for recGblRecordError */ #include "alarm.h" /* READ_ALARM,MINOR_ALARM */ #include "recGbl.h" /* for recGblSetSevr */ #include #include #include #define MIN(x,y) ((x>y) ? y : x) #define MAX(x,y) ((x>y) ? x : y) /* Status Returns */ #define OK 0 #define ERROR -1 #define TC_NPTS 20 static const const double tc_vdata_a[TC_NPTS] = {-0.006259,-0.005928,-0.005492,-0.005131,-0.004004, -0.002575,-0.000872, 0 , 0.000789, 0.001611, 0.002035, 0.002467, 0.003357, 0.004277, 0.006702, 0.009286, 0.012011, 0.013860, 0.017816, 0.020869}; static const const double tc_tdata_a[TC_NPTS] = {-273.15, -223.15, -193.15, -173.15, -123.15, 173.15, -23.15, 0 , 20 , 50 , 60 , 80, 100 , 150 , 200 , 250 , 300 , 350}; #define TCTEMP_BAD_DATA_MSG "%s: Corrupt SAM data ref volts=%f for %s\n" #define TCTEMP_RERR_MSG "%s: resistance is zero, %s\n" #define TCTEMP_RNEG_MSG "%s: resistance is negative %s\n" /* Local prototypes */ static double PolyInt(double xin, epicsUInt16 npts, double const * const ydata_a, double const * const xdata_a); /*============================================================================= Name: InitAnalogCalc Abs: Initialization function for analog conversion routines Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read/write pointer to subroutine record Side: None Ret: long OK - Successful (always) ==============================================================================*/ static long InitAnalogCalc( subRecord *psub ) { /* * General purpose initialization required since all subroutine records * require a non-NULL init routine even if no initialization is required. * Note that most subroutines in this file use this routine as an init * routine. If init logic is needed for a specific subroutine, create a * new routine for it - don't modify this one. */ return 0; } /*============================================================================= Name: AnalogCalc Abs: Subroutine to perform calculation to convert raw dat (INPB for an anlog input signal. Calculation is depends on type (A) of signal. Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read/write pointer to subroutine record Rem: Init Subroutine for AnalogCalc() Input: -------- A = type, where 1=linear scaling, 2=log scaling 3=phase mixer If type=1, perform linear scaling, write result to val If type=2, perform log scaling, write result to val If type 3, perform phase mixer conversion, write result to val B = raw signal C = offs for linear, coef for log D = coef for linear, coef of exponent for log E = val of associated device (phase mixer only) F = sign (1 or -1) (phase mixer only) Output: -------- VAL = converted value Side: None Ret: int OK - Successful (always) ==============================================================================*/ static long AnalogCalc( subRecord *psub ) { int type; float t; float b; float c; float d; float e; float f; if( psub == NULL ) return 0; type = psub->a; b = psub->b; c = psub->c; d = psub->d; e = psub->e; f = psub->f; if(type == 1) /* linear scaling */ psub->val = c + d*b; if(type == 2) /* log scaling */ psub->val = c * (pow(10,d*b)); if(type == 3) /* phase mixer */ { t = MIN(MAX(d*0.01745329*b,-1.2),1.2); psub->val = c + 57.29577*(t + 0.1667*pow(t,5)) + f*e; } return 0; } /*============================================================================= Name: AnalogTcRefTemp Abs: This function is a voltage vs temperature data for "T" copper-constrantan type thermocouples. The voltages are in volts, C and presssure in 0 degC. "ice point" reference. The temperatures C are in degree C. This routine convets the voltage across a standard 2252 Ohm @ 25C Thermistor to temperatures. The electric circuit is as follows: <10.240 Volts>------/\/\/\--------/\/\/\------+ 5100 Ohms 1.000 M-Ohms | | < + >------------------------+ SAM Input | < - >--+ / | \ 2252 Ohm | / Thermistor | \ | | +---------------------+ | <10.240 Return>-------------------------------+ The expected resistance of the thermistor is between 42 Ohms and 1.7 M-Ohms. The current thorugh the thermistor is (10.240 - voltage-drop) / (sum of resistances) and the resistance is: voltage-drop / current From solid state physics (and a bit of numerical analysis) we know the absolute (Kelvin) temperature of a semiconductor is T = 1 / ( A + B Log(resistance) + C Log(resistance)**3 ) for our favorite thermistor, A, B, anc C are defined in the array "data_a". Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read/write pointer to subroutine record Input: -------- A = ref volts RefTempVRaw i.e. SAM ref chan (15,31) B - lower limit range of raw data C - upper limit range of raw data Outputs: --------- F = current G = resistance H = ref temperature in kelvin I - ref temperature in celsius J - This is the voltage error due to the temperature of the referance plane VAL = ref temperature in celsius Rem: Side: Determine if the input channel is the reference voltage based on naming convention. All reference voltage channels have "RefTemp" in the name. Ret: long OK - Successful ERROR - Failed Auth: dd-mmm-yyyy, Clemens Wermelskirchen (wermelsk) ------------------------------------------------------------------------------ Mod: 09-Feb-2017, Kristi Luchini (luchini) converted from VMS fortran ==============================================================================*/ static long AnalogTcRefTemp( subRecord *psub ) { long status = OK; /* return status */ double tbar,alogr; static const epicsUInt16 npts = TC_NPTS; static const double illegal_val = -573.0; /* un-physical value */ static const double thermistor_a[3] = {1.4616E-4,2.39427E-4,9.59358E-8}; /* initialize output values */ psub->f=0; /* current */ psub->g=0; /* resistance */ psub->h=0; /* ref temp in kelvin */ psub->i=0; /* ref temp in celsius */ psub->j=0; /* voltage error */ /* * Test if the voltage is in the legitimate range * of 42 < r < 1.7E6 for thermistor and sensor. */ if ((psub->ab) || (psub->a>psub->c)) { psub->val = illegal_val; /* set to un-physical value */ psub->j = illegal_val; return(status); } /* calcualte the current */ psub->f = (10.24 - psub->a)/1005100.0; if (psub->f!=0.0) { /* calcualte the resistance, must be greater than 0. */ psub->g = psub->a/psub->f; if (psub->g>0.0) { /* Calcuate reference temperature in kelvin */ alogr = log(psub->g); tbar = thermistor_a[0] + thermistor_a[1]*alogr + thermistor_a[2]*pow(alogr,3); if (tbar != abs(tc_tdata_a[0])) { psub->h = 1.0/tbar; /* ref temp in Kelvin */ psub->i = psub->h + tc_tdata_a[0]; /* convert to celsius */ /* * Compute the Iso-Thermal Plate Voltage Contribution. This is the * voltage error due to the temperature of the reference plane. * if the plane were at 0C, then VPLATE woudl be 0. */ psub->j = PolyInt(psub->i,npts,tc_vdata_a,tc_tdata_a); /* * Calculate the temperature of the ref channel. * Note that if the Iso-Thermo Plane is near room * temperature, voltages (as read by the SAM) or -8 mV to22mV * represent the full range of legitimate temperatures of type "T" * thermocouples. */ psub->val = PolyInt(psub->j,npts,tc_tdata_a,tc_vdata_a); } } } return(status); } /*============================================================================= Name: AnalogTcTemp Abs: Calculate the temperature of the thermocouple in this block of SAM channels. The voltage vs temperature data are for for "T" copper-constrantan type thermocouples. The voltages are in volts, C and presssure are in 0 degC. "ice point" reference. The temperatures is calcualted in degree C. This routine converts the voltage across a standard 2252 Ohm @ 25C Thermistor to temperatures. The electric circuit is as follows: <10.240 Volts>------/\/\/\--------/\/\/\------+ 5100 Ohms 1.000 M-Ohms | | < + >------------------------+ SAM Input | < - >--+ / | \ 2252 Ohm | / Thermistor | \ | | +---------------------+ | <10.240 Return>-------------------------------+ The expected resistance of the thermistor is between 42 Ohms and 1.7 M-Ohms. The current thorugh the thermistor is (10.240 - voltage-drop) / (sum of resistances) and the resistance is: voltage-drop / current From solid state physics (and a bit of numerical analysis) we know the absolute (Kelvin) temperature of a semiconductor is T = 1 / ( A + B Log(resistance) + C Log(resistance)**3 ) for our favorite thermistor, A, B, anc C are defined in the array "thermistor_a". Note that if the iso-thermal plane is near room temperature, voltages (as read by the SAM) of -8mV to +22mV represent the full range of legitimate temperature of type "T" thermocouples. Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read/write pointer to subroutine record Input: -------- A - raw volts from SAM Channel 0-14,16-30, Channel 15 and 31 are ref temperatures. B - lower limit range of raw data C - upper limit range of raw data D = ref voltage Vplate. This is the voltage error due to the temperature of the referance plane iso-thermal plane. (ref volt is SAM chan 15 or 30) Output: -------- VAL = thermocouple temperature Rem: Side: Determine if the raw voltage is within a valid range. Ret: long OK - Successful (always) Auth: dd-mmm-yyyy, Clemens Wermelskirchen (wermelsk) ------------------------------------------------------------------------------ Mod: 09-Feb-2017, Kristi Luchini (luchini) converted from VMS fortran ==============================================================================*/ static long AnalogTcTemp( subRecord *psub ) { long status = OK; /* return status */ double vabs; static const epicsUInt16 npts = TC_NPTS; static const double illegal_val = -573.0; /* un-physical value */ /* * Check that raw data is within the valid range. * Otherwise, consider it an illegal, and set * an un-physical value and return an error */ if (((psub->ab) || (psub->a>psub->c)) || (psub->d==illegal_val)) psub->val = illegal_val; /* indicate raw data is an illegal value */ else { vabs = psub->a + psub->d; psub->val = PolyInt(vabs,npts,tc_tdata_a,tc_vdata_a); } return(status); } /*============================================================================= Name: PolyInt Abs: This functin is a simple parabolic interpolation between the indicated data points. The X data array is presumed to be monotonically increasing in value. Args: Type Name Access Description ----------- ----------- ---------- ---------------------------- double xin read only input data epicsUInt16 npts read only number of data pts in array double * ydata_a read only y data array double * xdata_a read only x data array Side: None Ret: float - interpolated data. Auth: dd-mmm-yyyy, Clemens Wermelskirchen (wermelsk) ------------------------------------------------------------------------------ Mod: 09-Feb-2017, Kristi Luchini (luchini) converted from VMS fortran polyint() ==============================================================================*/ static double PolyInt(double xin, epicsUInt16 npts, double const * const ydata_a, double const * const xdata_a) { double x1,x2,x3,x,xout; double y1,y2,y3; double bb=0,cc=0; epicsUInt16 i; for (i=1; i=npts) i=npts-1; x1 = xdata_a[i-1]; y1 = ydata_a[i-1]; x2 = xdata_a[i] - x1; y2 = ydata_a[i] - y1; x3 = xdata_a[i+1] - x1; y3 = ydata_a[i+1] - y1; /* solve for polynomial (parabolic) */ cc = (x2*y3/x3-y2)/(x2*(x3-x2)); bb = (y2-cc*x2*x2)/x2; x = xin-x1; xout = bb*x + cc*x*x + y1; return(xout); } epicsRegisterFunction(InitAnalogCalc); epicsRegisterFunction(AnalogCalc); epicsRegisterFunction(AnalogTcRefTemp); epicsRegisterFunction(AnalogTcTemp);