Low-Energy Muon (LEM) Experiment  0.5.2
danfysik_scfe.cxx
Go to the documentation of this file.
1 /********************************************************************\
2 
3  Name: danfysik_scfe.cxx
4  Created by: Andreas Suter
5 
6  Contents: Midas Slowcontrol Frontend to handle the Danfysik
7  power supply for the Bpar magnet.
8 
9 \********************************************************************/
10 
11 #include <cstdio>
12 #include <cstdlib>
13 #include <cstring>
14 #include <cmath>
15 
16 #include "midas.h"
17 #include "mfe.h"
18 
19 #include "class/multi.h"
20 #include "danfysik.h"
21 #include "tcpip_rs232.h"
22 
23 //-- Globals -------------------------------------------------------
24 
25 #define DANFYSIK_REMOTE_CH 0
26 #define DANFYSIK_STATE_CH 1
27 #define DANFYSIK_DEMAND_CH 2
28 #define DANFYSIK_MEASURE_CH 3
29 
30 #define DANFYSIK_TIMEOUT_CHECK 30
31 // danfysik demand <-> measured check timestamp
33 
35 const char *frontend_name = "Danfysik_SC";
37 const char *frontend_file_name = __FILE__;
38 
40 BOOL frontend_call_loop = FALSE;
41 
43 INT display_period = 1000;
44 
46 INT max_event_size = 10000;
47 
49 INT max_event_size_frag = 5*1024*1024;
50 
52 INT event_buffer_size = 10*10000;
53 
54 //-- Equipment list ------------------------------------------------
55 
60 DEVICE_DRIVER danfysik_driver[] = {
61  { "Danfysik_in", danfysik_in, 5, tcpip_rs232, DF_INPUT },
62  { "Danfysik_out", danfysik_out, 4, tcpip_rs232, DF_OUTPUT },
63  { "" }
64 };
65 
75 
77 EQUIPMENT equipment[] = {
78 
79  { "Danfysik", // equipment name
80  {80, 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  30000, // read every 30 sec
89  0, // stop run after this event limit
90  0, // number of sub events
91  1, // log history every event
92  "", "", "",},
93  cd_multi_read, // readout routine
94  cd_multi, // class driver main routine
95  danfysik_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 //-- Frontend Init -------------------------------------------------
115 {
116  // de-register run-transition notifications
117  cm_deregister_transition(TR_START);
118  cm_deregister_transition(TR_STOP);
119  cm_deregister_transition(TR_PAUSE);
120  cm_deregister_transition(TR_RESUME);
121 
122  // init danfysik demand <-> measured read timer
123  danfysik_check_timestamp = ss_time();
124 
125  return CM_SUCCESS;
126 }
127 
128 //-- Frontend Exit -------------------------------------------------
134 {
135  return CM_SUCCESS;
136 }
137 
138 //-- Frontend Loop -------------------------------------------------
144 {
145  DWORD now;
146  HNDLE hDB, hKeyIn, hKeyOut;
147  int status, size;
148  float setpoint_current, measured_current, remote, state;
149  static int err_count = 0;
150  char str[128];
151 
152  // is it time to check things?
153  now = ss_time();
155  return CM_SUCCESS;
156 
157  // update timestamp
159 
160  // get experiment handle
161  cm_get_experiment_database(&hDB, NULL);
162 
163  // get danfysik input handle
164  status = db_find_key(hDB, 0, "/Equipment/Danfysik/Variables/Input", &hKeyIn);
165  if (status != DB_SUCCESS) {
166  cm_msg(MINFO, "frontend_loop", "danfysik_scfe, frontend_loop: couldn't get danfysik input key from the ODB.");
167  return CM_SUCCESS;
168  }
169  // get danfysik output handle
170  status = db_find_key(hDB, 0, "/Equipment/Danfysik/Variables/Output", &hKeyOut);
171  if (status != DB_SUCCESS) {
172  cm_msg(MINFO, "frontend_loop", "danfysik_scfe, frontend_loop: couldn't get danfysik output key from the ODB.");
173  return CM_SUCCESS;
174  }
175 
176  // get danfysik state and remote flag
177  size = sizeof(float);
178  status = db_get_data_index(hDB, hKeyOut, &remote, &size, DANFYSIK_REMOTE_CH, TID_FLOAT);
179  size = sizeof(float);
180  status = db_get_data_index(hDB, hKeyOut, &state, &size, DANFYSIK_STATE_CH, TID_FLOAT);
181 
182  // if remote and on check things
183  if ((remote != 1.0) || (state != 1.0))
184  return CM_SUCCESS;
185 
186  // get current demand
187  size = sizeof(float);
188  status = db_get_data_index(hDB, hKeyOut, &setpoint_current, &size, DANFYSIK_DEMAND_CH, TID_FLOAT);
189 
190  // get current measured
191  size = sizeof(float);
192  status = db_get_data_index(hDB, hKeyIn, &measured_current, &size, DANFYSIK_MEASURE_CH, TID_FLOAT);
193 
194  // max deviation < 10%?
195  if (fabsf(setpoint_current-measured_current)/fabsf(setpoint_current) > 0.1) {
196  err_count++;
197  } else {
198  err_count = 0;
199  }
200 
201  // too many consecutive error counts, hence issue a warning
202  if (err_count == 10) {
203  sprintf(str, "Danfysik Setpoint = %0.1f A, Measured = %0.1f A: something is fishy!", setpoint_current, measured_current);
204  al_trigger_alarm( "danfysik_current_monitoring", str, "Warning", str, AT_INTERNAL);
205  }
206 
207  return CM_SUCCESS;
208 }
209 
210 //-- Begin of Run --------------------------------------------------
215 INT begin_of_run(INT run_number, char *error)
216 {
217  return CM_SUCCESS;
218 }
219 
220 //-- End of Run ----------------------------------------------------
225 INT end_of_run(INT run_number, char *error)
226 {
227  return CM_SUCCESS;
228 }
229 
230 //-- Pause Run -----------------------------------------------------
235 INT pause_run(INT run_number, char *error)
236 {
237  return CM_SUCCESS;
238 }
239 
240 //-- Resume Run ----------------------------------------------------
245 INT resume_run(INT run_number, char *error)
246 {
247  return CM_SUCCESS;
248 }
249 
250 //------------------------------------------------------------------
#define DANFYSIK_MEASURE_CH
#define DANFYSIK_STATE_CH
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
BOOL frontend_call_loop
frontend_loop is called periodically if this variable is TRUE
Definition: vme_fe.cxx:173
INT display_period
a frontend status page is displayed with this frequency in ms
Definition: vme_fe.cxx:176
INT event_buffer_size
buffer size to hold events
Definition: vme_fe.cxx:185
INT frontend_exit()
Definition: vme_fe.cxx:565
EQUIPMENT equipment[]
Definition: vme_fe.cxx:343
INT begin_of_run(INT run_number, char *error)
Definition: vme_fe.cxx:597
INT danfysik_in(INT cmd,...)
DEVICE_DRIVER danfysik_driver[]
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
BOOL equipment_common_overwrite
Definition: vme_fe.cxx:341
INT end_of_run(INT run_number, char *error)
Definition: vme_fe.cxx:865
INT poll_event(INT source, INT count, BOOL test)
Definition: vme_fe.cxx:2088
#define DANFYSIK_TIMEOUT_CHECK
HNDLE hDB
#define DANFYSIK_REMOTE_CH
#define DANFYSIK_DEMAND_CH
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
DWORD danfysik_check_timestamp
INT danfysik_out(INT cmd,...)