source: branches/FACT++_scripts_refactoring/Startup.js@ 18233

Last change on this file since 18233 was 18229, checked in by dneise, 9 years ago
use new libraries CheckFTU and CheckUnderflow
File size: 10.3 KB
Line 
1'use strict';
2
3include("scripts/CheckFTU.js");
4include("scripts/CheckUnderflow.js");
5// To de done:
6// - CheckLID status (should be open or closed)
7// - Is it necessary to switch the bias-voltage off?
8// - Get reasonable timeouts for all steps (wait, get, run)
9// - Improve order to accelerate execution
10//
11// =================================================================
12
13/*
14var table =
15[
16 [ "AGILENT_CONTROL" ],
17 [ "BIAS_CONTROL" ],
18 [ "CHAT" ],
19 [ "DATA_LOGGER" ],
20 [ "DRIVE_CONTROL" ],
21 [ "FEEDBACK" ],
22 [ "FAD_CONTROL" ],
23 [ "FSC_CONTROL" ],
24 [ "FTM_CONTROL" ],
25 [ "LID_CONTROL" ],
26 [ "MAGIC_WEATHER" ],
27 [ "MCP" ],
28 [ "PWR_CONTROL" ],
29 [ "RATE_CONTROL" ],
30 [ "RATE_SCAN" ],
31 [ "SMART_FACT" ],
32 [ "TIME_CHECK" ],
33 [ "TNG_WEATHER" ],
34];
35
36if (dim.state("DRIVE_CONTROL").name=="Locked")
37{
38 throw new Error("Drivectrl still locked... needs UNLOCK first.");
39 //while (!dim.send("DRIVE_CONTROL"))
40 // v8.sleep();
41 //dim.send("DRIVE_CONTROL/UNLOCK");
42 //dim.wait("DRIVE_CONTROL", "Armed", 1000);
43}
44
45*/
46
47console.out("");
48dim.alarm();
49
50var loop;
51include("scripts/Handler.js");
52include("scripts/CheckStates.js");
53
54// -----------------------------------------------------------------
55// Make sure camera electronics is switched on and has power
56// -----------------------------------------------------------------
57
58include("scripts/handleAgilentPowerOn24V.js");
59include("scripts/handleAgilentPowerOn50V.js");
60include("scripts/handleAgilentPowerOn80V.js");
61include("scripts/handlePwrCameraOn.js");
62
63checkSend(["AGILENT_CONTROL_24V","AGILENT_CONTROL_50V","AGILENT_CONTROL_80V","PWR_CONTROL"]);
64
65loop = new Handler("PowerOn");
66//loop.add(handleAgilentPowerOn24V);
67//loop.add(handleAgilentPowerOn50V);
68//loop.add(handleAgilentPowerOn80V);
69loop.add(handlePwrCameraOn);
70loop.run();
71console.out("");
72
73// If power was switched on: wait for a few seconds
74
75// -----------------------------------------------------------------
76// Now take care that the bias control, the ftm and the fsc are
77// properly connected and are in a reasonable state (e.g. the
78// trigger is switched off)
79// -----------------------------------------------------------------
80
81include("scripts/handleBiasVoltageOff.js");
82include("scripts/handleFtmIdle.js");
83include("scripts/handleFscConnected.js");
84include("scripts/handleFeedbackConnected.js");
85include("scripts/handleRatectrlConnected.js");
86include("scripts/handleLidClosed.js");
87include("scripts/handleFadConnected.js");
88
89checkSend(["BIAS_CONTROL","FAD_CONTROL","FTM_CONTROL", "FSC_CONTROL", "FEEDBACK", "RATE_CONTROL", "MCP"]);
90
91dim.send("MCP/RESET");
92
93loop = new Handler("SystemSetup");
94loop.add(handleBiasVoltageOff);
95loop.add(handleFtmIdle);
96loop.add(handleFscConnected);
97loop.add(handleFadConnected);
98loop.add(handleFeedbackConnected); // Feedback needs FAD to be Connected
99loop.add(handleRatectrlConnected);
100loop.add(handleLidClosed);
101loop.run();
102
103console.out("biasctrl: "+dim.state("BIAS_CONTROL").name);
104console.out("ftmctrl: "+dim.state("FTM_CONTROL").name);
105console.out("fscctrl: "+dim.state("FSC_CONTROL").name);
106console.out("feedback: "+dim.state("FEEDBACK").name);
107console.out("ratecontrol: "+dim.state("RATE_CONTROL").name);
108console.out("fadctrl: "+dim.state("FAD_CONTROL").name);
109console.out("mcp: "+dim.state("MCP").name);
110console.out("");
111
112console.out("Enable all FTU");
113dim.send("FTM_CONTROL/ENABLE_FTU", -1, true);
114
115// -----------------------------------------------------------------
116// Now we check the FTU connection
117// -----------------------------------------------------------------
118CheckFTU.checkFTU();
119
120
121// -----------------------------------------------------------------
122// Now we check the clock conditioner
123// -----------------------------------------------------------------
124
125var sub_counter = new Subscription("FTM_CONTROL/COUNTER");
126var counter = sub_counter.get(3000, false).counter;
127dim.send("FTM_CONTROL/REQUEST_STATIC_DATA");
128v8.timeout(3000, function() { if (sub_counter.get(0, false).counter>counter) return true; });
129if (sub_counter.get(0, false).qos&0x100==0)
130 throw new Error("Clock conditioner not locked.");
131sub_counter.close();
132
133// -----------------------------------------------------------------
134// Now we can safely try to connect the FAD boards.
135// -----------------------------------------------------------------
136/*
137 include("scripts/handleFadConnected.js");
138
139// If FADs already connected
140
141checkSend(["FAD_CONTROL"]);
142
143loop = new Handler("ConnectFad");
144loop.add(handleFadConnected);
145loop.run();
146
147var failed = false;
148dim.onchange["FAD_CONTROL"] = function(arg)
149{
150 if (this.rc && arg.name!="Connected")
151 failed = true;
152}
153
154console.out("FADs connected.");
155console.out("");
156
157console.out(dim.state("FAD_CONTROL").name);
158console.out(dim.state("MCP").name);
159*/
160
161// ================================================================
162// Underflow check
163// ================================================================
164// Is it necessary to check for the so called 'underflow-problem'?
165// (This is necessary after each power cycle)
166// ----------------------------------------------------------------
167CheckUnderflow.checkUnderflow();
168
169// Now it is time to check the connection of the FADs
170// it might hav thrown an exception already anyway
171
172
173// ================================================================
174// Power on drive system if power is off (do it hre to make sure not
175// everything is switchd on at the same time)
176// ================================================================
177
178//console.out("PWR: "+(dim.state("PWR_CONTROL").index&16));
179
180if ((dim.state("PWR_CONTROL").index&16)==0)
181{
182 console.out("Drive cabinet not powered... Switching on.");
183 dim.send("PWR_CONTROL/TOGGLE_DRIVE");
184 v8.timeout(5000, function() { if (dim.state("PWR_CONTROL").index&16) return true; });
185}
186
187include("scripts/handleDriveArmed.js");
188
189checkSend(["DRIVE_CONTROL"]);
190
191loop = new Handler("ArmDrive");
192loop.add(handleDriveArmed);
193loop.run();
194
195
196// ================================================================
197// Bias crate calibration
198// ================================================================
199// Bias crate calibration if necessary (it is aftr 4pm (local tome)
200// and the last calibration was more than eight hours ago.
201// -----------------------------------------------------------------
202
203// At this point we know that:
204// 1) The lid is closed
205// 2) The feedback is stopped
206// 3) The voltage is off
207function makeCurrentCalibration()
208{
209 dim.send("BIAS_CONTROL/SET_ZERO_VOLTAGE");
210 dim.wait("BIAS_CONTROL", "VoltageOff", 30000); // waS: 15000
211
212 var now = new Date();
213 dim.send("FEEDBACK/CALIBRATE");
214
215 console.out("Wait for calibration to start");
216 dim.wait("FEEDBACK", "Calibrating", 5000);
217
218 console.out("Wait for calibration to end");
219 dim.wait("FEEDBACK", "Calibrated", 90000);
220
221 console.out("Calibration finished ["+(new Date()-now)+"ms]");
222
223 console.out("Wait for voltage to be off");
224 dim.wait("BIAS_CONTROL", "VoltageOff", 30000); // was: 15000
225}
226
227// Check age of calibration
228var service_calibration = new Subscription("FEEDBACK/CALIBRATION");
229
230var data_calibration = service_calibration.get(3000, false);
231
232var age = data_calibration.time;
233var now = new Date();
234
235var diff = (now-age)/3600000;
236
237var fb_state = dim.state("FEEDBACK").index;
238
239// !data_calibration.data: FEEDBACK might just be freshly
240// started and will not yet serve this service.
241if (fb_state<5 || (diff>8 && now.getHours()>16))
242{
243 if (fb_state<5)
244 console.out("No BIAS crate calibration available: New calibration needed.");
245 else
246 console.out("Last BIAS crate calibration taken at "+age.toUTCString()+": New calibration needed.");
247
248 makeCurrentCalibration();
249}
250
251service_calibration.close();
252
253// ================================================================
254// Setup GPS control and wait for the satellites to be locked
255// ================================================================
256
257checkSend(["GPS_CONTROL"]);
258
259if (dim.state("GPS_CONTROL").name=="Disconnected")
260 dim.send("GPS_CONTROL/RECONNECT");
261
262// Wait for being connectes
263v8.timeout(5000, function() { if (dim.state("GPS_CONTROL").name!="Disconnected") return true; });
264
265// Wait for status available
266v8.timeout(5000, function() { if (dim.state("GPS_CONTROL").name!="Connected") return true; });
267
268if (dim.state("GPS_CONTROL").name=="Disabled")
269 dim.send("GPS_CONTROL/ENABLE");
270
271// Wait for gps to be enabled and locked
272dim.wait("GPS_CONTROL", "Locked", 15000);
273
274// ================================================================
275// Crosscheck all states
276// ================================================================
277
278// FIXME: Check if there is a startup scheduled, if not do not force
279// drive to be switched on
280
281var table =
282[
283 [ "TNG_WEATHER" ],
284 [ "MAGIC_WEATHER" ],
285 [ "CHAT" ],
286 [ "SMART_FACT" ],
287 [ "TEMPERATURE" ],
288 [ "EVENT_SERVER", [ "Running", "Standby" ] ],
289 [ "DATA_LOGGER", [ "NightlyFileOpen", "WaitForRun", "Logging" ] ],
290 [ "FSC_CONTROL", [ "Connected" ] ],
291 [ "MCP", [ "Idle" ] ],
292 [ "TIME_CHECK", [ "Valid" ] ],
293 [ "PWR_CONTROL", [ "SystemOn" ] ],
294 [ "AGILENT_CONTROL_24V", [ "VoltageOn" ] ],
295 [ "AGILENT_CONTROL_50V", [ "VoltageOn" ] ],
296 [ "AGILENT_CONTROL_80V", [ "VoltageOn" ] ],
297 [ "BIAS_CONTROL", [ "VoltageOff" ] ],
298 [ "FEEDBACK", [ "Calibrated" ] ],
299 [ "RATE_SCAN", [ "Connected" ] ],
300 [ "RATE_CONTROL", [ "Connected" ] ],
301 [ "DRIVE_CONTROL", [ "Armed", "Tracking", "OnTrack", "Locked" ] ],
302 [ "LID_CONTROL", [ "Open", "Closed" ] ],
303 [ "FTM_CONTROL", [ "Valid", "TriggerOn" ] ],
304 [ "FAD_CONTROL", [ "Connected", "WritingData" ] ],
305 [ "GPS_CONTROL", [ "Locked" ] ],
306 [ "SQM_CONTROL", [ "Valid" ] ],
307 [ "PFMINI_CONTROL", [ "Receiving" ] ],
308];
309
310
311
312if (!checkStates(table))
313{
314 throw new Error("Something unexpected has happened. Although the startup-"+
315 "procedure has finished, not all servers are in the state "+
316 "in which they ought to be. Please, try to find out what "+
317 "happened...");
318}
Note: See TracBrowser for help on using the repository browser.