source: trunk/FACT++/scripts/Startup.js@ 17667

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