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