This is Unofficial EPICS BASE Doxygen Site
aoRecord.c
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2008 UChicago Argonne LLC, as Operator of Argonne
3 * National Laboratory.
4 * Copyright (c) 2002 The Regents of the University of California, as
5 * Operator of Los Alamos National Laboratory.
6 * EPICS BASE is distributed subject to a Software License Agreement found
7 * in file LICENSE that is included with this distribution.
8 \*************************************************************************/
9 
10 /* aoRecord.c - Record Support Routines for Analog Output records */
11 /*
12  * Original Author: Bob Dalesio
13  * Date: 7-14-89
14  *
15  */
16 
17 #include <stddef.h>
18 #include <stdlib.h>
19 #include <stdarg.h>
20 #include <stdio.h>
21 #include <string.h>
22 
23 #include "dbDefs.h"
24 #include "epicsPrint.h"
25 #include "epicsMath.h"
26 #include "alarm.h"
27 #include "callback.h"
28 #include "cvtTable.h"
29 #include "dbAccess.h"
30 #include "dbEvent.h"
31 #include "dbFldTypes.h"
32 #include "devSup.h"
33 #include "errMdef.h"
34 #include "special.h"
35 #include "recSup.h"
36 #include "recGbl.h"
37 #include "menuConvert.h"
38 #include "menuOmsl.h"
39 #include "menuYesNo.h"
40 #include "menuIvoa.h"
41 
42 #define GEN_SIZE_OFFSET
43 #include "aoRecord.h"
44 #undef GEN_SIZE_OFFSET
45 #include "epicsExport.h"
46 
47 /* Create RSET - Record Support Entry Table*/
48 #define report NULL
49 #define initialize NULL
50 static long init_record(struct dbCommon *, int);
51 static long process(struct dbCommon *);
52 static long special(DBADDR *, int);
53 #define get_value NULL
54 #define cvt_dbaddr NULL
55 #define get_array_info NULL
56 #define put_array_info NULL
57 static long get_units(DBADDR *, char *);
58 static long get_precision(const DBADDR *, long *);
59 #define get_enum_str NULL
60 #define get_enum_strs NULL
61 #define put_enum_str NULL
62 static long get_graphic_double(DBADDR *, struct dbr_grDouble *);
63 static long get_control_double(DBADDR *, struct dbr_ctrlDouble *);
64 static long get_alarm_double(DBADDR *, struct dbr_alDouble *);
65 
67  RSETNUMBER,
68  report,
69  initialize,
71  process,
72  special,
73  get_value,
74  cvt_dbaddr,
77  get_units,
85 };
86 epicsExportAddress(rset,aoRSET);
87 
88 static void checkAlarms(aoRecord *);
89 static long fetch_value(aoRecord *, double *);
90 static void convert(aoRecord *, double);
91 static void monitor(aoRecord *);
92 static long writeValue(aoRecord *);
93 
94 static long init_record(struct dbCommon *pcommon, int pass)
95 {
96  struct aoRecord *prec = (struct aoRecord *)pcommon;
97  aodset *pdset;
98  double eoff = prec->eoff, eslo = prec->eslo;
99  double value;
100  long status = 0;
101 
102  if (pass == 0) return 0;
103 
104  recGblInitSimm(pcommon, &prec->sscn, &prec->oldsimm, &prec->simm, &prec->siml);
105 
106  if(!(pdset = (aodset *)(prec->dset))) {
107  recGblRecordError(S_dev_noDSET,(void *)prec,"ao: init_record");
108  return(S_dev_noDSET);
109  }
110  /* get the initial value if dol is a constant*/
111  if (recGblInitConstantLink(&prec->dol,DBF_DOUBLE,&prec->val))
112  prec->udf = isnan(prec->val);
113 
114  /* must have write_ao function defined */
115  if ((pdset->common.number < 6) || (pdset->write_ao ==NULL)) {
116  recGblRecordError(S_dev_missingSup,(void *)prec,"ao: init_record");
117  return(S_dev_missingSup);
118  }
119  prec->init = TRUE;
120  /*The following is for old device support that doesnt know about eoff*/
121  if ((prec->eslo==1.0) && (prec->eoff==0.0)) {
122  prec->eoff = prec->egul;
123  }
124 
125  if (pdset->common.init_record) {
126  status = pdset->common.init_record(pcommon);
127  if (prec->linr == menuConvertSLOPE) {
128  prec->eoff = eoff;
129  prec->eslo = eslo;
130  }
131  switch(status){
132  case(0): /* convert */
133  value = (double)prec->rval + (double)prec->roff;
134  if(prec->aslo!=0.0) value *= prec->aslo;
135  value += prec->aoff;
136  if (prec->linr == menuConvertNO_CONVERSION){
137  ; /*do nothing*/
138  } else if ((prec->linr == menuConvertLINEAR) ||
139  (prec->linr == menuConvertSLOPE)) {
140  value = value*prec->eslo + prec->eoff;
141  }else{
142  if(cvtRawToEngBpt(&value,prec->linr,prec->init,
143  (void *)&prec->pbrk,&prec->lbrk)!=0) break;
144  }
145  prec->val = value;
146  prec->udf = isnan(value);
147  break;
148  case(2): /* no convert */
149  break;
150  default:
151  recGblRecordError(S_dev_badInitRet,(void *)prec,"ao: init_record");
152  return(S_dev_badInitRet);
153  }
154  }
155  prec->oval = prec->pval = prec->val;
156  prec->mlst = prec->val;
157  prec->alst = prec->val;
158  prec->lalm = prec->val;
159  prec->oraw = prec->rval;
160  prec->orbv = prec->rbv;
161  return(0);
162 }
163 
164 static long process(struct dbCommon *pcommon)
165 {
166  struct aoRecord *prec = (struct aoRecord *)pcommon;
167  aodset *pdset = (aodset *)(prec->dset);
168  long status=0;
169  unsigned char pact=prec->pact;
170  double value;
171 
172  if ((pdset==NULL) || (pdset->write_ao==NULL)) {
173  prec->pact=TRUE;
174  recGblRecordError(S_dev_missingSup,(void *)prec,"write_ao");
175  return(S_dev_missingSup);
176  }
177 
178  /* fetch value and convert*/
179  if (prec->pact == FALSE) {
180  if (!dbLinkIsConstant(&prec->dol) &&
181  prec->omsl == menuOmslclosed_loop) {
182  status = fetch_value(prec, &value);
183  }
184  else {
185  value = prec->val;
186  }
187  if(!status) convert(prec, value);
188  prec->udf = isnan(prec->val);
189  }
190 
191  /* check for alarms */
192  checkAlarms(prec);
193 
194  if (prec->nsev < INVALID_ALARM )
195  status=writeValue(prec); /* write the new value */
196  else {
197  switch (prec->ivoa) {
198  case (menuIvoaContinue_normally) :
199  status=writeValue(prec); /* write the new value */
200  break;
201  case (menuIvoaDon_t_drive_outputs) :
202  break;
203  case (menuIvoaSet_output_to_IVOV) :
204  if(prec->pact == FALSE){
205  prec->val=prec->ivov;
206  value=prec->ivov;
207  convert(prec,value);
208  }
209  status=writeValue(prec); /* write the new value */
210  break;
211  default :
212  status=-1;
213  recGblRecordError(S_db_badField,(void *)prec,
214  "ao:process Illegal IVOA field");
215  }
216  }
217 
218  /* check if device support set pact */
219  if ( !pact && prec->pact ) return(0);
220  prec->pact = TRUE;
221 
222  recGblGetTimeStampSimm(prec, prec->simm, NULL);
223 
224  /* check event list */
225  monitor(prec);
226 
227  /* process the forward scan link record */
228  recGblFwdLink(prec);
229 
230  prec->init=FALSE;
231  prec->pact=FALSE;
232  return(status);
233 }
234 
235 static long special(DBADDR *paddr, int after)
236 {
237  aoRecord *prec = (aoRecord *)(paddr->precord);
238  aodset *pdset = (aodset *) (prec->dset);
239  int special_type = paddr->special;
240 
241  switch(special_type) {
242  case(SPC_LINCONV):
243  if(pdset->common.number<6 ) {
244  recGblDbaddrError(S_db_noMod,paddr,"ao: special");
245  return(S_db_noMod);
246  }
247  prec->init=TRUE;
248  if ((prec->linr == menuConvertLINEAR) && pdset->special_linconv) {
249  double eoff = prec->eoff;
250  double eslo = prec->eslo;
251  long status;
252  prec->eoff = prec->egul;
253  status = (*pdset->special_linconv)(prec,after);
254  if (eoff != prec->eoff)
255  db_post_events(prec, &prec->eoff, DBE_VALUE|DBE_LOG);
256  if (eslo != prec->eslo)
257  db_post_events(prec, &prec->eslo, DBE_VALUE|DBE_LOG);
258  return (status);
259  }
260  return (0);
261  case(SPC_MOD):
262  if (dbGetFieldIndex(paddr) == aoRecordSIMM) {
263  if (!after)
264  recGblSaveSimm(prec->sscn, &prec->oldsimm, prec->simm);
265  else
266  recGblCheckSimm((dbCommon *)prec, &prec->sscn, prec->oldsimm, prec->simm);
267  return(0);
268  }
269  default:
270  recGblDbaddrError(S_db_badChoice,paddr,"ao: special");
271  return(S_db_badChoice);
272  }
273 }
274 
275 #define indexof(field) aoRecord##field
276 
277 static long get_units(DBADDR * paddr,char *units)
278 {
279  aoRecord *prec=(aoRecord *)paddr->precord;
280 
281  if(paddr->pfldDes->field_type == DBF_DOUBLE) {
282  switch (dbGetFieldIndex(paddr)) {
283  case indexof(ASLO):
284  case indexof(AOFF):
285  break;
286  default:
287  strncpy(units,prec->egu,DB_UNITS_SIZE);
288  }
289  }
290  return(0);
291 }
292 
293 static long get_precision(const DBADDR *paddr,long *precision)
294 {
295  aoRecord *prec=(aoRecord *)paddr->precord;
296 
297  *precision = prec->prec;
298  switch (dbGetFieldIndex(paddr)) {
299  case indexof(VAL):
300  case indexof(OVAL):
301  case indexof(PVAL):
302  break;
303  default:
304  recGblGetPrec(paddr,precision);
305  }
306  return(0);
307 }
308 
309 static long get_graphic_double(DBADDR *paddr,struct dbr_grDouble *pgd)
310 {
311  aoRecord *prec=(aoRecord *)paddr->precord;
312 
313  switch (dbGetFieldIndex(paddr)) {
314  case indexof(VAL):
315  case indexof(OVAL):
316  case indexof(PVAL):
317  case indexof(HIHI):
318  case indexof(HIGH):
319  case indexof(LOW):
320  case indexof(LOLO):
321  case indexof(LALM):
322  case indexof(ALST):
323  case indexof(MLST):
324  case indexof(IVOV):
325  pgd->upper_disp_limit = prec->hopr;
326  pgd->lower_disp_limit = prec->lopr;
327  break;
328  default:
329  recGblGetGraphicDouble(paddr,pgd);
330  }
331  return(0);
332 }
333 
334 static long get_control_double(DBADDR *paddr, struct dbr_ctrlDouble *pcd)
335 {
336  aoRecord *prec=(aoRecord *)paddr->precord;
337 
338  switch (dbGetFieldIndex(paddr)) {
339  case indexof(VAL):
340  case indexof(OVAL):
341  case indexof(PVAL):
342  case indexof(HIHI):
343  case indexof(HIGH):
344  case indexof(LOW):
345  case indexof(LOLO):
346  case indexof(LALM):
347  case indexof(ALST):
348  case indexof(MLST):
349  pcd->upper_ctrl_limit = prec->drvh;
350  pcd->lower_ctrl_limit = prec->drvl;
351  break;
352  default:
353  recGblGetControlDouble(paddr,pcd);
354  }
355  return(0);
356 }
357 static long get_alarm_double(DBADDR *paddr, struct dbr_alDouble *pad)
358 {
359  aoRecord *prec=(aoRecord *)paddr->precord;
360 
361  if(dbGetFieldIndex(paddr) == indexof(VAL)){
362  pad->upper_alarm_limit = prec->hhsv ? prec->hihi : epicsNAN;
363  pad->upper_warning_limit = prec->hsv ? prec->high : epicsNAN;
364  pad->lower_warning_limit = prec->lsv ? prec->low : epicsNAN;
365  pad->lower_alarm_limit = prec->llsv ? prec->lolo : epicsNAN;
366  } else recGblGetAlarmDouble(paddr,pad);
367  return(0);
368 }
369 
370 static void checkAlarms(aoRecord *prec)
371 {
372  double val, hyst, lalm;
373  double alev;
374  epicsEnum16 asev;
375 
376  if (prec->udf) {
377  recGblSetSevr(prec, UDF_ALARM, prec->udfs);
378  return;
379  }
380 
381  val = prec->val;
382  hyst = prec->hyst;
383  lalm = prec->lalm;
384 
385  /* alarm condition hihi */
386  asev = prec->hhsv;
387  alev = prec->hihi;
388  if (asev && (val >= alev || ((lalm == alev) && (val >= alev - hyst)))) {
389  if (recGblSetSevr(prec, HIHI_ALARM, asev))
390  prec->lalm = alev;
391  return;
392  }
393 
394  /* alarm condition lolo */
395  asev = prec->llsv;
396  alev = prec->lolo;
397  if (asev && (val <= alev || ((lalm == alev) && (val <= alev + hyst)))) {
398  if (recGblSetSevr(prec, LOLO_ALARM, asev))
399  prec->lalm = alev;
400  return;
401  }
402 
403  /* alarm condition high */
404  asev = prec->hsv;
405  alev = prec->high;
406  if (asev && (val >= alev || ((lalm == alev) && (val >= alev - hyst)))) {
407  if (recGblSetSevr(prec, HIGH_ALARM, asev))
408  prec->lalm = alev;
409  return;
410  }
411 
412  /* alarm condition low */
413  asev = prec->lsv;
414  alev = prec->low;
415  if (asev && (val <= alev || ((lalm == alev) && (val <= alev + hyst)))) {
416  if (recGblSetSevr(prec, LOW_ALARM, asev))
417  prec->lalm = alev;
418  return;
419  }
420 
421  /* we get here only if val is out of alarm by at least hyst */
422  prec->lalm = val;
423  return;
424 }
425 
426 static long fetch_value(aoRecord *prec,double *pvalue)
427 {
428  short save_pact;
429  long status;
430 
431  save_pact = prec->pact;
432  prec->pact = TRUE;
433 
434  /* don't allow dbputs to val field */
435  prec->val=prec->pval;
436 
437  status = dbGetLink(&prec->dol,DBR_DOUBLE,pvalue,0,0);
438  prec->pact = save_pact;
439 
440  if (status) {
441  recGblSetSevr(prec,LINK_ALARM,INVALID_ALARM);
442  return(status);
443  }
444 
445  if (prec->oif == aoOIF_Incremental)
446  *pvalue += prec->val;
447 
448  return(0);
449 }
450 
451 static void convert(aoRecord *prec, double value)
452 {
453  /* check drive limits */
454  if (prec->drvh > prec->drvl) {
455  if (value > prec->drvh)
456  value = prec->drvh;
457  else if (value < prec->drvl)
458  value = prec->drvl;
459  }
460  prec->val = value;
461  prec->pval = value;
462 
463  /* now set value equal to desired output value */
464  /* apply the output rate of change */
465  if (prec->oroc != 0){/*must be defined and >0*/
466  double diff;
467 
468  diff = value - prec->oval;
469  if (diff < 0) {
470  if (prec->oroc < -diff)
471  value = prec->oval - prec->oroc;
472  } else if (prec->oroc < diff)
473  value = prec->oval + prec->oroc;
474  }
475  prec->omod = (prec->oval!=value);
476  prec->oval = value;
477 
478  /* convert */
479  switch (prec->linr) {
480  case menuConvertNO_CONVERSION:
481  break; /* do nothing*/
482  case menuConvertLINEAR:
483  case menuConvertSLOPE:
484  if (prec->eslo == 0.0) value = 0;
485  else value = (value - prec->eoff) / prec->eslo;
486  break;
487  default:
488  if (cvtEngToRawBpt(&value, prec->linr, prec->init,
489  (void *)&prec->pbrk, &prec->lbrk) != 0) {
490  recGblSetSevr(prec, SOFT_ALARM, MAJOR_ALARM);
491  return;
492  }
493  }
494  value -= prec->aoff;
495  if (prec->aslo != 0) value /= prec->aslo;
496 
497  /* Apply raw offset and limits, round to 32-bit integer */
498  value -= prec->roff;
499  if (value >= 0.0) {
500  if (value >= (0x7fffffff - 0.5))
501  prec->rval = 0x7fffffff;
502  else
503  prec->rval = (epicsInt32)(value + 0.5);
504  } else {
505  if (value > (0.5 - 0x80000000))
506  prec->rval = (epicsInt32)(value - 0.5);
507  else
508  prec->rval = 0x80000000;
509  }
510 }
511 
512 
513 static void monitor(aoRecord *prec)
514 {
515  unsigned monitor_mask = recGblResetAlarms(prec);
516 
517  /* check for value change */
518  recGblCheckDeadband(&prec->mlst, prec->val, prec->mdel, &monitor_mask, DBE_VALUE);
519 
520  /* check for archive change */
521  recGblCheckDeadband(&prec->alst, prec->val, prec->adel, &monitor_mask, DBE_ARCHIVE);
522 
523  /* send out monitors connected to the value field */
524  if (monitor_mask){
525  db_post_events(prec,&prec->val,monitor_mask);
526  }
527 
528  if(prec->omod) monitor_mask |= (DBE_VALUE|DBE_LOG);
529  if(monitor_mask) {
530  prec->omod = FALSE;
531  db_post_events(prec,&prec->oval,monitor_mask);
532  if(prec->oraw != prec->rval) {
533  db_post_events(prec,&prec->rval,
534  monitor_mask|DBE_VALUE|DBE_LOG);
535  prec->oraw = prec->rval;
536  }
537  if(prec->orbv != prec->rbv) {
538  db_post_events(prec,&prec->rbv,
539  monitor_mask|DBE_VALUE|DBE_LOG);
540  prec->orbv = prec->rbv;
541  }
542  }
543  return;
544 }
545 
546 static long writeValue(aoRecord *prec)
547 {
548  aodset *pdset = (aodset *) prec->dset;
549  long status = 0;
550 
551  if (!prec->pact) {
552  status = recGblGetSimm((dbCommon *)prec, &prec->sscn, &prec->oldsimm, &prec->simm, &prec->siml);
553  if (status) return status;
554  }
555 
556  switch (prec->simm) {
557  case menuYesNoNO:
558  status = pdset->write_ao(prec);
559  break;
560 
561  case menuYesNoYES: {
562  recGblSetSevr(prec, SIMM_ALARM, prec->sims);
563  if (prec->pact || (prec->sdly < 0.)) {
564  status = dbPutLink(&prec->siol, DBR_DOUBLE, &prec->oval, 1);
565  prec->pact = FALSE;
566  } else { /* !prec->pact && delay >= 0. */
567  epicsCallback *pvt = prec->simpvt;
568  if (!pvt) {
569  pvt = calloc(1, sizeof(epicsCallback)); /* very lazy allocation of callback structure */
570  prec->simpvt = pvt;
571  }
572  if (pvt) callbackRequestProcessCallbackDelayed(pvt, prec->prio, prec, prec->sdly);
573  prec->pact = TRUE;
574  }
575  break;
576  }
577 
578  default:
579  recGblSetSevr(prec, SOFT_ALARM, INVALID_ALARM);
580  status = -1;
581  }
582 
583  return status;
584 }
#define put_array_info
Definition: aoRecord.c:56
#define HIHI_ALARM
Definition: alarm.h:94
#define RSETNUMBER
Definition: recSup.h:92
#define cvt_dbaddr
Definition: aoRecord.c:54
Definition: link.h:174
#define FALSE
Definition: dbDefs.h:32
rset aoRSET
Definition: aoRecord.c:66
pvd::Status status
#define MAJOR_ALARM
Definition: alarm.h:52
#define indexof(field)
Definition: aoRecord.c:275
#define init_record
#define S_dev_missingSup
Definition: devSup.h:170
#define SPC_LINCONV
Definition: special.h:37
#define SOFT_ALARM
Definition: alarm.h:106
#define NULL
Definition: catime.c:38
Miscellaneous macro definitions.
#define DBE_ARCHIVE
Definition: caeventmask.h:39
#define get_value
Definition: aoRecord.c:53
#define get_control_double
Definition: biRecord.c:58
epicsShareFunc long cvtEngToRawBpt(double *pval, short linr, short init, void **ppbrk, short *plbrk)
#define DBE_VALUE
Definition: caeventmask.h:38
#define HIGH_ALARM
Definition: alarm.h:95
#define initialize
Definition: aoRecord.c:49
Device support routines.
#define isnan(x)
Definition: epicsMath.h:21
#define DBE_LOG
Definition: caeventmask.h:40
#define get_units
Definition: biRecord.c:52
#define SIMM_ALARM
Definition: alarm.h:110
#define get_array_info
Definition: aoRecord.c:55
#define DBR_DOUBLE
Definition: db_access.h:76
epicsShareFunc long cvtRawToEngBpt(double *pval, short linr, short init, void **ppbrk, short *plbrk)
#define LINK_ALARM
Definition: alarm.h:105
float epicsNAN
Definition: epicsMath.cpp:35
#define S_dev_badInitRet
Definition: devSup.h:173
epicsUInt16 epicsEnum16
Definition: epicsTypes.h:47
#define get_precision
Definition: biRecord.c:53
#define TRUE
Definition: dbDefs.h:27
#define report
Definition: aoRecord.c:48
if(yy_init)
Definition: scan.c:972
Definition: recSup.h:67
#define SPC_MOD
Definition: special.h:33
#define get_enum_strs
Definition: aoRecord.c:60
int prec
Definition: reader.c:29
#define INVALID_ALARM
Definition: alarm.h:53
#define LOLO_ALARM
Definition: alarm.h:96
#define get_enum_str
Definition: aoRecord.c:59
#define LOW_ALARM
Definition: alarm.h:97
#define get_graphic_double
Definition: biRecord.c:57
epicsExportAddress(rset, aoRSET)
#define special
Definition: dfanoutRecord.c:50
int epicsInt32
Definition: epicsTypes.h:42
#define S_dev_noDSET
Definition: devSup.h:169
#define get_alarm_double
Definition: aaiRecord.c:69
#define UDF_ALARM
Definition: alarm.h:108
#define put_enum_str
Definition: aoRecord.c:61
Exporting IOC objects.