Low-Energy Muon (LEM) Experiment  0.5.2
beamline_scfe.cxx
Go to the documentation of this file.
1 /********************************************************************\
2 
3  Name: beamline_scfe.cxx
4  Created by: Andreas Suter, 2013/04/10
5  Thomas Prokscha, 2013/06/07: added checking of beamline devices
6 
7  Contents: Midas frontend to operate the muE4 beamline via EPICS
8 
9 \********************************************************************/
10 
11 #include <cstdio>
12 #include <cstring>
13 #include <cmath>
14 
15 #include "midas.h"
16 #include "mfe.h"
17 #include "msystem.h"
18 
19 #include "bus/tcpip.h"
20 #include "class/generic.h"
21 #include "lem_epics.h"
22 #include "experim.h"
23 
24 #define BL_CHANNELS 40
25 #define BL_TOLERANCE 0.01
26 #define BL_CHECK_MAX_COUNT 5
27 
28 //-- Globals -------------------------------------------------------
29 
31 const char *frontend_name = "BeamLine SC";
33 const char *frontend_file_name = __FILE__;
34 
36 BOOL frontend_call_loop = FALSE;
37 
39 INT display_period = 1000;
40 
42 INT max_event_size = 10000;
43 
45 INT max_event_size_frag = 5*1024*1024;
46 
48 INT event_buffer_size = 10*10000;
49 
56 
57 //-- Equipment list ------------------------------------------------
58 
60 DEVICE_DRIVER bl_driver[] = {
61 // { "Beamline", psi_epics, BL_CHANNELS, tcpip, DF_PRIO_DEVICE },
62  { "Beamline", lem_epics, BL_CHANNELS, tcpip, DF_PRIO_DEVICE },
63  { "" }
64 };
65 
75 
77 EQUIPMENT equipment[] = {
78 
79  { "Beamline", // equipment name
80  {99, 0, // event ID, trigger mask
81  "SYSTEM", // event buffer
82  EQ_SLOW, // equipment type
83  0, // event source
84  "FIXED", // format
85  TRUE, // enabled
86  RO_RUNNING |
87  RO_TRANSITIONS, // read when running and on transitions
88  60000, // read every 60 sec
89  0, // stop run after this event limit
90  0, // number of sub events
91  1, // log history every event
92  "", "", "",},
93  cd_gen_read, // readout routine
94  cd_gen, // class driver main routine
95  bl_driver, // device driver list
96  NULL, // init string
97  },
98 
99  { "" }
100 };
101 
102 
103 
104 //-- Dummy routines ------------------------------------------------
105 
106 INT poll_event(INT source, INT count, BOOL test) {return 1;};
107 INT interrupt_configure(INT cmd, INT source, POINTER_T adr) {return 1;};
108 
109 
110 /*-- Beamline checking routines -------------------------------------*/
111 /*
112  * <p>Hot-link routine; shows the devices which are being checked
113  *
114  */
115 void bl_check_info(INT hDB, INT hKey, void *dummy)
116 {
117  INT i;
118 
119  if (!beamline_monitoring.bl_checking){
120  cm_msg(MINFO,"bl_check_info", "Checking of beamline devices disabled.");
121  return;
122  }
123 
124  if (beamline_monitoring.bl_checking!=0)
125  cm_msg(MINFO,"bl_check_info", "Checking of beamline devices enabled.");
126 
127  if (beamline_monitoring.bl_checking==2)
128  cm_msg(MINFO,"bl_check_info", "Automatic switch-on of beamline devices enabled.");
129 
130  if (beamline_monitoring.bl_checking!=2)
131  cm_msg(MINFO,"bl_check_info", "Automatic switch-on of beamline devices disabled.");
132 
133  for (i=0; i<BL_CHANNELS; i++){
134  if (!beamline_monitoring.bl_channel_checking[i]) continue;
135  cm_msg(MINFO,"bl_check_info", "Checking beamline device %s", beamline_settings.names[i]);
136  }
137  return;
138 }
139 
140 
141 /*-- Beamline checking routine -------------------------------------*/
142 /*
143  * <p>Hot-link routine; check beamline magnet supplies: compare Demand
144  * with Measured values to detect if there is a problem with one of the
145  * power supplies;
146  * Send an error message, if error occurs BL_CHECK_MAX_COUNT times successivley.
147  * It can be disabled by setting
148  * /Equipment/Beamline/Monitoring/bl_checking = 0
149  *
150  */
151 void bl_check_devices(INT hDB, INT hKey, void *dummy)
152 {
153  INT i, status;
154  float bl_deviation;
155  char alarm_message[128];
156  char cmd[256];
157 
158  if (!beamline_monitoring.bl_checking)
159  return;
160 
161  for ( i=0; i<BL_CHANNELS; i++){
162 
163  if (!beamline_monitoring.bl_channel_checking[i]) continue;
164 
165  bl_deviation = fabs(beamline_event.demand[i] - beamline_event.measured[i]);
166  if (beamline_event.demand[i] != 0.0){
167  if ( bl_deviation > BL_TOLERANCE*fabs(beamline_event.demand[i]) ||
168  bl_deviation > BL_TOLERANCE*fabs(beamline_event.measured[i]))
169  bl_check_counter[i]++;
170  else
171  bl_check_counter[i] = 0; //everything ok
172  }
173  else
174  bl_check_counter[i] = 0;
175 
177  sprintf(alarm_message,"Problem with beamline device %s: Demand = %8f, Measured = %8.4f",
178  beamline_settings.names[i], beamline_event.demand[i], beamline_event.measured[i]);
179  al_trigger_alarm( "bl_check_devices", alarm_message,
180  "Warning", "BeamLine Magnets have to be checked!", AT_INTERNAL);
181  if ((i<18 || i == 30) && beamline_monitoring.bl_checking==2) {//beam line magnet device, try to switch it on
182  sprintf(cmd, "caput %s:COM:1 1", beamline_settings.names[i]);
183  status = system(cmd);
184  cm_msg(MINFO,"bl_check_devices","Try to turn on magnet device: cmd = %s",cmd);
185 // cm_msg(MINFO,"frontend_init","Return status of cmd: %d", status);
186  ss_sleep(2000);
187  }
188  }
189  }// end loop over magnets
190 }
191 //-- Frontend Init -------------------------------------------------
197 {
198  HNDLE hDB, hKey;
199  INT size, i;
200 // INT status;
201 // char cmd[256];
202 
203  // de-register run-transition notifications
204  cm_deregister_transition(TR_START);
205  cm_deregister_transition(TR_STOP);
206  cm_deregister_transition(TR_PAUSE);
207  cm_deregister_transition(TR_RESUME);
208 
209  cm_get_experiment_database(&hDB, NULL);
210 
211  // get beamline settings record with device names
212  size = sizeof(beamline_settings);
213  db_find_key(hDB, 0, "/Equipment/Beamline/Settings", &hKey);
214 
215  if (db_get_record(hDB, hKey, &beamline_settings, &size, 0) != DB_SUCCESS){
216  cm_msg(MERROR, "beamline_frontend_init", "Cannot get \"/Equipment/Beamline/Settings\" record in ODB");
217  return CM_SUCCESS;
218  }
219 
220  // get beamline monitoring record
221  size = sizeof(beamline_monitoring);
222  db_find_key(hDB, 0, "/Equipment/Beamline/Monitoring", &hKey);
223 
224  if (db_open_record(hDB, hKey, &beamline_monitoring, sizeof(beamline_monitoring),
225  MODE_READ, bl_check_info,NULL) != DB_SUCCESS){
226  cm_msg(MERROR, "beamline_frontend_init", "Cannot get \"/Equipment/Beamline/Monitoring\" record in ODB");
227  return CM_SUCCESS;
228  }
229 
230  // open ODB record hot-link to beamline device Demand and Measured values
231  db_find_key(hDB, 0, "/Equipment/Beamline/Variables", &hKey);
232 
233  if (db_open_record(hDB, hKey, &beamline_event, sizeof(beamline_event),
234  MODE_READ, bl_check_devices, NULL) != DB_SUCCESS){
235  cm_msg(MERROR, "beamline_frontend_init", "Cannot open \"/Equipment/Beamline/Variables\" record in ODB");
236  return CM_SUCCESS;
237  }
238 
239  for (i=0; i<BL_CHANNELS; i++)
240  bl_check_counter[i] = 0;
241 
242  if (beamline_monitoring.bl_checking!=0)
243  cm_msg(MINFO,"beamline_frontend_init", "Checking of beamline devices enabled.");
244  else{
245  cm_msg(MINFO,"beamline_frontend_init", "Checking of beamline devices disabled.");
246  return CM_SUCCESS;
247  }
248  if (beamline_monitoring.bl_checking==2)
249  cm_msg(MINFO,"beamline_frontend_init", "Automatic switch-on of beamline devices enabled.");
250  if (beamline_monitoring.bl_checking!=2)
251  cm_msg(MINFO,"bl_check_info", "Automatic switch-on of beamline devices disabled.");
252 
253 /*
254  sprintf(cmd,"caput %s:COM:1 1", beamline_settings.names[0]);
255  status = system(cmd);
256  cm_msg(MINFO,"frontend_init","Try to turn on magnet device: cmd = %s",cmd);
257  cm_msg(MINFO,"frontend_init","Return status of cmd: %d", status);
258  ss_sleep(1000);
259 */
260  return CM_SUCCESS;
261 }
262 
263 //-- Frontend Exit -------------------------------------------------
269 {
270  return CM_SUCCESS;
271 }
272 
273 //-- Frontend Loop -------------------------------------------------
279 {
280  return CM_SUCCESS;
281 }
282 
283 //-- Begin of Run --------------------------------------------------
288 INT begin_of_run(INT run_number, char *error)
289 {
290  return CM_SUCCESS;
291 }
292 
293 //-- End of Run ----------------------------------------------------
298 INT end_of_run(INT run_number, char *error)
299 {
300  return CM_SUCCESS;
301 }
302 
303 //-- Pause Run -----------------------------------------------------
308 INT pause_run(INT run_number, char *error)
309 {
310  return CM_SUCCESS;
311 }
312 
313 //-- Resume Run ----------------------------------------------------
318 INT resume_run(INT run_number, char *error)
319 {
320  return CM_SUCCESS;
321 }
322 
323 //------------------------------------------------------------------
const char * frontend_name
&lt; created by ODBedit, command &quot;make&quot;
Definition: vme_fe.cxx:167
INT max_event_size
maximum event size produced by this frontend
Definition: vme_fe.cxx:179
float demand[40]
Definition: experim.h:1421
DEVICE_DRIVER bl_driver[]
device driver list
BOOL frontend_call_loop
frontend_loop is called periodically if this variable is TRUE
Definition: vme_fe.cxx:173
INT bl_check_counter[BL_CHANNELS]
counter needed in the beamline check
INT display_period
a frontend status page is displayed with this frequency in ms
Definition: vme_fe.cxx:176
BEAMLINE_MONITORING beamline_monitoring
char names[40][32]
Definition: experim.h:1527
INT event_buffer_size
buffer size to hold events
Definition: vme_fe.cxx:185
#define BL_CHANNELS
number for muE4 beamline epics channels
void bl_check_devices(INT hDB, INT hKey, void *dummy)
BEAMLINE_SETTINGS beamline_settings
INT frontend_exit()
Definition: vme_fe.cxx:565
EQUIPMENT equipment[]
Definition: vme_fe.cxx:343
INT lem_epics(INT cmd,...)
INT begin_of_run(INT run_number, char *error)
Definition: vme_fe.cxx:597
INT interrupt_configure(INT cmd, INT source, PTYPE adr)
Definition: vme_fe.cxx:2105
INT frontend_loop()
Definition: vme_fe.cxx:957
INT resume_run(INT run_number, char *error)
Definition: vme_fe.cxx:900
INT max_event_size_frag
maximum event size for fragmented events (EQ_FRAGMENTED)
Definition: vme_fe.cxx:182
INT frontend_init()
Definition: vme_fe.cxx:429
#define BL_TOLERANCE
when checking devices allow 1% difference between Demand/Measured values
BEAMLINE_EVENT beamline_event
for slow control parameters
Definition: vme_fe.cxx:204
HNDLE hKey
BOOL equipment_common_overwrite
Definition: vme_fe.cxx:341
INT end_of_run(INT run_number, char *error)
Definition: vme_fe.cxx:865
INT32 bl_channel_checking[40]
Definition: experim.h:1838
#define BL_CHECK_MAX_COUNT
INT poll_event(INT source, INT count, BOOL test)
Definition: vme_fe.cxx:2088
float measured[40]
Definition: experim.h:1422
HNDLE hDB
INT pause_run(INT run_number, char *error)
Definition: vme_fe.cxx:893
const char * frontend_file_name
The frontend file name, don&#39;t change it.
Definition: vme_fe.cxx:170
void bl_check_info(INT hDB, INT hKey, void *dummy)