AlcapDAQ  1
frontend.c
Go to the documentation of this file.
1 /********************************************************************\
2 
3  Name: frontend.c
4  Created by: Stefan Ritt
5 
6  Contents: Example Slow Control Frontend program. Defines two
7  slow control equipments, one for a HV device and one
8  for a multimeter (usually a general purpose PC plug-in
9  card with A/D inputs/outputs. As a device driver,
10  the "null" driver is used which simulates a device
11  without accessing any hardware. The used class drivers
12  cd_hv and cd_multi act as a link between the ODB and
13  the equipment and contain some functionality like
14  ramping etc. To form a fully functional frontend,
15  the device driver "null" has to be replaces with
16  real device drivers.
17 
18 
19  $Log: frontend.c,v $
20  Revision 1.1.1.1 2004/10/20 18:41:18 admin
21  Trying again to import midas-1.9.5. There are NT executables, but I'm importing them as well (w/o -kb switch so watch out!)
22 
23  Revision 1.11 2004/01/08 08:40:09 midas
24  Implemented standard indentation
25 
26  Revision 1.10 2002/06/10 07:15:17 midas
27  Use new DF_xxx flags
28 
29  Revision 1.9 2002/05/29 13:35:47 midas
30  Added max_event_size_frag
31 
32  Revision 1.8 2002/05/13 22:21:08 midas
33  Fixed typo
34 
35  Revision 1.7 2002/03/13 08:39:28 midas
36  Use bus drivers in examples
37 
38  Revision 1.6 2000/08/21 10:49:11 midas
39  Added max_event_size
40 
41  Revision 1.5 2000/03/02 22:00:47 midas
42  Added number of subevents in equipment list
43 
44  Revision 1.4 1999/12/21 09:38:23 midas
45  Use new driver names
46 
47  Revision 1.3 1998/10/23 08:46:26 midas
48  Added #include "null.h"
49 
50  Revision 1.2 1998/10/12 12:18:59 midas
51  Added Log tag in header
52 
53 
54 \********************************************************************/
55 
56 #include <stdio.h>
57 #include "midas.h"
58 //#include "class/hv.h"
59 //#include "class/multi.h"
60 //#include "device/nulldev.h"
61 //#include "bus/null.h"
62 
63 /* make frontend functions callable from the C framework */
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
69 
70 HNDLE hDB;
71 HNDLE hKey;
72 int size;
74 char Alarm[128] = "/Equipment/SeparatorMonitor/Variables/Alarm_Triggered";
75 
76 INT read_separator_event(char *pevent, INT off);
77 INT pre_begin_of_run();
79 
80 /*-- Globals -------------------------------------------------------*/
81 
82 /* The frontend name (client name) as seen by other MIDAS clients */
83 char *frontend_name = "SeparatorMonitor";
84 /* The frontend file name, don't change it */
85 char *frontend_file_name = __FILE__;
86 
87 /* frontend_loop is called periodically if this variable is TRUE */
89 
90 /* a frontend status page is displayed with this frequency in ms */
91 INT display_period = 1000;
92 
93 /* maximum event size produced by this frontend */
94 INT max_event_size = 10000;
95 
96 /* maximum event size for fragmented events (EQ_FRAGMENTED) */
97 INT max_event_size_frag = 5 * 1024 * 1024;
98 
99 /* buffer size to hold events */
100 INT event_buffer_size = 10 * 10000;
101 
102 /*-- Equipment list ------------------------------------------------*/
103 
104 BANK_LIST separator_bank_list[] = {
105  { "SEPA", TID_FLOAT, 4, NULL },
106  { "" },
107 };
108 
109 typedef struct SEPARATOR_DATA_Struct {
110  float hv;
111  float current;
112  float vac;
113  float sep_stat;
115 
116 #undef USE_INT
117 
118 EQUIPMENT equipment[] = {
119  {"SeparatorMonitor", /* equipment name */
120  { 22, 0, /* event ID, trigger mask */
121  "SYSTEM", /* event buffer */
122  EQ_PERIODIC, /* equipment type */
123  0, /* event source */
124  "MIDAS", /* format */
125  TRUE, /* enabled */
126  RO_ALWAYS|RO_ODB, /* read all the time */
127  50000, /* reads spaced by this many ms */
128  0, /* stop run after this event limit */
129  0, /* number of sub events */
130  0, /* log history every event */
131  "", "", "", },
132  read_separator_event, /* readout routine */
133  NULL, NULL,
135  },
136 
137  {""}
138 };
139 
140 
141 #ifdef __cplusplus
142 }
143 #endif
144 
145 /*-- Dummy routines ------------------------------------------------*/
146 
148 {
149  return CM_SUCCESS;
150 }
151 
152 INT pre_begin_of_run()
153 {
154  return CM_SUCCESS;
155 }
156 
157 INT poll_event(INT source[], INT count, BOOL test)
158 {
159  return 1;
160 };
161 INT interrupt_configure(INT cmd, INT source[], PTYPE adr)
162 {
163  return 1;
164 };
165 
166 /*-- Frontend Init -------------------------------------------------*/
167 
169 {
170 
171  // get the LabView filename
172  cm_get_experiment_database(&hDB, NULL);
173 
174  if(db_find_key(hDB, 0, Alarm, &hKey) != SUCCESS){
175  db_create_key(hDB,0,Alarm,TID_INT);
176  }
177 
178  return CM_SUCCESS;
179 }
180 
181 /*-- Frontend Exit -------------------------------------------------*/
182 
184 {
185  return CM_SUCCESS;
186 }
187 
188 /*-- Frontend Loop -------------------------------------------------*/
189 
191 {
192  ss_sleep(100);
193  return CM_SUCCESS;
194 }
195 
196 /*-- Begin of Run --------------------------------------------------*/
197 
198 INT begin_of_run(INT run_number, char *error)
199 {
200  return CM_SUCCESS;
201 }
202 
203 /*-- End of Run ----------------------------------------------------*/
204 
205 INT end_of_run(INT run_number, char *error)
206 {
207  return CM_SUCCESS;
208 }
209 
210 /*-- Pause Run -----------------------------------------------------*/
211 
212 INT pause_run(INT run_number, char *error)
213 {
214  return CM_SUCCESS;
215 }
216 
217 /*-- Resuem Run ----------------------------------------------------*/
218 
219 INT resume_run(INT run_number, char *error)
220 {
221  return CM_SUCCESS;
222 }
223 
224 /*------------------------------------------------------------------*/
225 
226 INT read_separator_event(char *pevent, INT off){
227  static SEPARATOR_DATA separator_data;
228 
229  static char oldtimestamp[80];
230  char timestamp[80];
231  char timestring[80];
232  FILE *infile=NULL;
233  float *pdata;
234  static float lastHVs[4] = {-1., -1., -1., -1.};
235  static int initDone = 0;
236  int i;
237 
238  size = sizeof(sepDemand);
239  db_get_value(hDB,0, "/Experiment/Edit on start/Separator HV",
240  &sepDemand, &size, TID_INT, 1);
241 
242  timestamp[0]='\0';
243 
244  infile=fopen("/tmp/separator.txt","r");
245  if (infile==NULL){
246  cm_msg(MERROR,frontend_name,
247  "Could not open tempfile. separator_daemon.pl not running?");
248  separator_data.hv =-1;
249  separator_data.vac =-1;
250  separator_data.sep_stat =-1;
251  }
252  else{
253  fscanf(infile,"%s\n%f\n%f\n%f\n%f",timestamp,&separator_data.hv,
254  &separator_data.current,&separator_data.vac,
255  &separator_data.sep_stat);
256  fclose(infile);
257 
258  if(!strcmp(oldtimestamp, timestamp)){
259  separator_data.hv=-1;
260  separator_data.vac=-1;
261  separator_data.sep_stat=-1;
262  }
263  }
264 
265  if(initDone == 0){
266  for(i=0; i<4; i++) lastHVs[i] = separator_data.hv;
267  initDone = 1;
268  }
269  else{
270  for(i=0; i<3; i++) lastHVs[i] = lastHVs[i+1];
271  lastHVs[3] = separator_data.hv;
272  }
273 
274  //for(i=0; i<4; i++) printf("HV[%d]=%d\n",i,(int)lastHVs[i]);
275  //printf("Demand = %d\n", sepDemand);
276 
277  //Send to midas:
278  bk_init(pevent);
279  bk_create(pevent,"SEPA",TID_FLOAT,&pdata);
280  pdata[0] = separator_data.hv;
281  pdata[1] = separator_data.current;
282  pdata[2] = separator_data.vac;
283  pdata[3] = separator_data.sep_stat;
284  bk_close(pevent,pdata+4);
285 
286  int sepError = 1;
287 
288  for(i=0; i<4; i++){
289  if((int)lastHVs[i] == sepDemand) sepError = 0;
290  }
291 
292  if(sepError==1){
293  if(strcmp(oldtimestamp, timestamp)){
294  cm_msg(MERROR,frontend_name,"Separator high voltage not same as demand!");
295  printf("Demand = %d", sepDemand);
296  }
297  else{
298  cm_msg(MERROR,frontend_name,"Old and new timestamps identical."
299  " Refresh too high or separator_daemon.pl not running.");
300  }
301  int on = 1;
302  db_set_value(hDB, 0, Alarm, &on, sizeof(on), 1, TID_INT);
303  }
304  else{
305  int off = 0;
306  db_set_value(hDB, 0, Alarm, &off, sizeof(off), 1, TID_INT);
307  }
308 
309  strcpy(oldtimestamp,timestamp);
310 
311  return bk_size(pevent);
312 }