MIDAS
Loading...
Searching...
No Matches
frontend.cxx
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 for equipment control
7 through EPICS channel access.
8
9 $Id$
10
11\********************************************************************/
12
13#include <stdio.h>
14#include <math.h>
15#include "midas.h"
16#include "class/generic.h"
17#include "device/epics_ca.h"
18#include "mfe.h"
19
20/*-- Globals -------------------------------------------------------*/
21
22/* The frontend name (client name) as seen by other MIDAS clients */
23const char *frontend_name = "feEpics";
24/* The frontend file name, don't change it */
25const char *frontend_file_name = __FILE__;
26
27/* frontend_loop is called periodically if this variable is TRUE */
29
30/* a frontend status page is displayed with this frequency in ms */
32
33/* maximum event size for fragmented events (EQ_FRAGMENTED) */
34INT max_event_size_frag = 5 * 1024 * 1024;
35
36/* maximum event size produced by this frontend */
38
39/* buffer size to hold events */
41
43
44/*-- Equipment list ------------------------------------------------*/
45
46/*
47The following statement allocates 20 channels for the beamline
48control through the epics channel access device driver. The
49EPICS channel names are stored under
50
51 /Equipment/Beamline/Settings/Devices/Beamline
52
53while the channel names as the midas slow control sees them are
54under
55
56 /Equipment/Beamline/Settings/Names
57
58An example set of channel names is saved in the triumf.odb file
59in this directory and can be loaded into the ODB with the odbedit
60command
61
62 load beamline.xml
63
64before the frontend is started. The CMD_SET_LABEL statement
65actually defines who determines the label name. If this flag is
66set, the CMD_SET_LABEL command in the device driver is disabled,
67therefore the label is taken from EPICS, otherwise the label is
68taken from MIDAS and set in EPICS.
69
70The same can be done with the demand values. If the command
71CMD_SET_DEMAND is disabled, the demand value is always determied
72by EPICS.
73*/
74
75/* device driver list */
77 {"Beamline", epics_ca, 20, NULL}, /* disable CMD_SET_LABEL */
78 {""}
79};
80
82
83 {"Beamline", /* equipment name */
84 { 3, 0, /* event ID, trigger mask */
85 "SYSTEM", /* event buffer */
86 EQ_SLOW, /* equipment type */
87 0, /* event source */
88 "FIXED", /* format */
89 TRUE, /* enabled */
90 RO_RUNNING | RO_TRANSITIONS, /* read when running and on transitions */
91 60000, /* read every 60 sec */
92 0, /* stop run after this event limit */
93 0, /* number of sub events */
94 1, /* log history every event */
95 "", "", "" },
96 cd_gen_read, /* readout routine */
97 cd_gen, /* class driver main routine */
98 epics_driver, /* device driver list */
99 NULL, /* init string */
100 },
101
102 {""}
103};
104
105/*-- Dummy routines ------------------------------------------------*/
106
108{
109 return 1;
110};
112{
113 return 1;
114};
115
116/*-- Frontend Init -------------------------------------------------*/
117
119{
120 /* enable/disable certain command in device driver */
121
122 // default number of channels is set in the epics_driver struct
123 // at the top of this file. If ODB has more channels, we do not want
124 // them truncated. Set number of channels to what we see in ODB. K.O.
125
126 int status;
127 HNDLE hkey;
128 std::string odb_path;
129 odb_path += "/Equipment/";
130 odb_path += equipment[0].name;
131 odb_path += "/Variables/Measured";
132
133 status = db_find_key(hDB, 0, odb_path.c_str(), &hkey);
134 if (hkey) {
135 KEY key;
136 status = db_get_key(hDB, hkey, &key);
137 if (status == DB_SUCCESS) {
138 int default_values = epics_driver[0].channels;
139
140 //printf("key [%s] size %d default %d\n", key.name, key.num_values, default_values);
141
142 if (key.num_values > default_values) {
144 }
145 }
146 }
147
148 return CM_SUCCESS;
149}
150
151/*-- Frontend Exit -------------------------------------------------*/
152
154{
155 return CM_SUCCESS;
156}
157
158/*-- Frontend Loop -------------------------------------------------*/
159/* Issue a watchdog counter every second for the Epics world
160 for R/W access control.
161 This counter will appear in the measured variable under index 19.
162*/
164{
165 int status, size;
166 static HNDLE hDB, hWatch=0, hRespond;
167 static DWORD watchdog_time=0;
168 static float dog=0.f;
169
170 /* slow down frontend not to eat all CPU cycles */
171 /* ss_sleep(50); */
172 status = cm_yield(50); /* 15Feb05 */
173
174 //if (status != SS_TIMEOUT)
175 // cm_msg(MERROR, "frontend_loop", "cm_yield status %d", status);
176
177 if (status == RPC_SHUTDOWN || status == SS_ABORT) {
178 return status;
179 }
180
181 if (ss_time() - watchdog_time > 10)
182 {
183 //const float wraparound = 100;
184 watchdog_time = ss_time();
185 if (!hWatch)
186 {
188 status = db_find_key(hDB, 0, "/equipment/Beamline/variables/demand", &hWatch);
189 status = db_find_key(hDB, 0, "/equipment/Beamline/variables/measured", &hRespond);
190 if (status != DB_SUCCESS) {
191 cm_msg(MERROR, "frontend_loop", "key not found");
192 return FE_ERR_HW;
193 }
194 }
195 if (hWatch) {
196 /* Check if Epics alive */
197 float cat=0.f;
198 size = sizeof(float);
199 db_get_data_index(hDB, hRespond, &cat, &size, 19, TID_FLOAT);
200 if (fabs(dog - cat) > 10) {
201 cm_msg(MINFO,"feEpics","Epics R/W access watchdog failure, wrote %f, read %f!", dog, cat);
202 }
203
204 db_set_data_index(hDB, hWatch, &dog, sizeof(float), 19, TID_FLOAT);
205 }
206 dog += 1;
207 // watchdog mismatch check above does not know how to check for wraparound. K.O.
208 //if (dog > wraparound)
209 // dog = 0;
210 }
211 return CM_SUCCESS;
212}
213
214/*-- Begin of Run --------------------------------------------------*/
215
217{
218 return CM_SUCCESS;
219}
220
221/*-- End of Run ----------------------------------------------------*/
222
224{
225 return CM_SUCCESS;
226}
227
228/*-- Pause Run -----------------------------------------------------*/
229
231{
232 return CM_SUCCESS;
233}
234
235/*-- Resume Run ----------------------------------------------------*/
236
238{
239 return CM_SUCCESS;
240}
241
242/*------------------------------------------------------------------*/
#define FALSE
Definition cfortran.h:309
BOOL frontend_call_loop
Definition frontend.cxx:28
const char * frontend_file_name
Definition frontend.cxx:25
INT max_event_size
Definition frontend.cxx:37
INT frontend_exit()
Frontend exit.
Definition frontend.cxx:153
INT frontend_init()
Frontend initialization.
Definition frontend.cxx:118
INT event_buffer_size
Definition frontend.cxx:40
INT max_event_size_frag
Definition frontend.cxx:34
INT interrupt_configure(INT cmd, INT source, PTYPE adr)
Definition frontend.cxx:111
BOOL equipment_common_overwrite
Definition frontend.cxx:42
INT poll_event(INT source, INT count, BOOL test)
Definition frontend.cxx:107
EQUIPMENT equipment[]
Definition frontend.cxx:81
const char * frontend_name
Definition frontend.cxx:23
INT display_period
Definition frontend.cxx:31
DEVICE_DRIVER epics_driver[]
Definition frontend.cxx:76
INT begin_of_run(INT run_number, char *error)
Begin of Run.
Definition frontend.cxx:216
INT frontend_loop()
Frontend loop.
Definition frontend.cxx:163
INT end_of_run(INT run_number, char *error)
End of Run.
Definition frontend.cxx:223
INT cm_yield(INT millisec)
Definition midas.cxx:5642
INT cm_get_experiment_database(HNDLE *hDB, HNDLE *hKeyClient)
Definition midas.cxx:3011
#define CM_SUCCESS
Definition midas.h:582
#define DB_SUCCESS
Definition midas.h:631
#define SS_ABORT
Definition midas.h:677
#define RPC_SHUTDOWN
Definition midas.h:707
#define FE_ERR_HW
Definition midas.h:719
unsigned int DWORD
Definition mcstd.h:51
#define MINFO
Definition midas.h:560
#define MERROR
Definition midas.h:559
#define RO_TRANSITIONS
Definition midas.h:434
#define TID_FLOAT
Definition midas.h:341
#define RO_RUNNING
Definition midas.h:426
#define EQ_SLOW
Definition midas.h:418
DWORD ss_time()
Definition system.cxx:3462
INT cm_msg(INT message_type, const char *filename, INT line, const char *routine, const char *format,...)
Definition midas.cxx:915
INT db_get_data_index(HNDLE hDB, HNDLE hKey, void *data, INT *buf_size, INT idx, DWORD type)
Definition odb.cxx:6893
INT db_get_key(HNDLE hDB, HNDLE hKey, KEY *key)
Definition odb.cxx:6019
INT db_set_data_index(HNDLE hDB, HNDLE hKey, const void *data, INT data_size, INT idx, DWORD type)
Definition odb.cxx:7648
INT db_find_key(HNDLE hDB, HNDLE hKey, const char *key_name, HNDLE *subhKey)
Definition odb.cxx:4079
INT run_number[2]
Definition mana.cxx:246
HNDLE hDB
main ODB handle
Definition mana.cxx:207
double count
Definition mdump.cxx:33
KEY key
Definition mdump.cxx:34
INT HNDLE
Definition midas.h:132
DWORD BOOL
Definition midas.h:105
int INT
Definition midas.h:129
#define PTYPE
Definition midas.h:170
#define TRUE
Definition midas.h:182
#define resume_run
#define pause_run
program test
Definition miniana.f:6
DWORD status
Definition odbhist.cxx:39
Definition midas.h:1026
INT num_values
Definition midas.h:1028
char name[NAME_LENGTH]
Definition midas.h:1173