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

Last change on this file since 16475 was 16384, checked in by tbretz, 12 years ago
Some improvements switching on the drive cabinet.
File size: 8.7 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(handleFeedbackConnected); // Feedback needs FAD to be Connected
92loop.add(handleRatectrlConnected);
93loop.add(handleFadConnected);
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 can safely try to connect the FAD boards.
132// -----------------------------------------------------------------
133/*
134 include("scripts/handleFadConnected.js");
135
136// If FADs already connected
137
138checkSend(["FAD_CONTROL"]);
139
140loop = new Handler("ConnectFad");
141loop.add(handleFadConnected);
142loop.run();
143
144var failed = false;
145dim.onchange["FAD_CONTROL"] = function(arg)
146{
147 if (this.rc && arg.name!="Connected")
148 failed = true;
149}
150
151console.out("FADs connected.");
152console.out("");
153
154console.out(dim.state("FAD_CONTROL").name);
155console.out(dim.state("MCP").name);
156*/
157
158// ================================================================
159// Underflow check
160// ================================================================
161// Is it necessary to check for the so called 'underflow-problem'?
162// (This is necessary after each power cycle)
163// ----------------------------------------------------------------
164
165include('scripts/CheckUnderflow.js');
166
167// Now it is time to check the connection of the FADs
168// it might hav thrown an exception already anyway
169
170
171// ================================================================
172// Power on drive system if power is off (do it hre to make sure not
173// everything is switchd on at the same time)
174// ================================================================
175
176//console.out("PWR: "+(dim.state("PWR_CONTROL").index&16));
177
178if ((dim.state("PWR_CONTROL").index&16)==0)
179{
180 console.out("Drive cabinet not power... Switching on.");
181 dim.send("PWR_CONTROL/TOGGLE_DRIVE");
182 v8.timeout(5000, function() { if (dim.state("PWR_CONTROL").index&16) return true; });
183}
184
185include("scripts/handleDriveArmed.js");
186
187checkSend(["DRIVE_CONTROL"]);
188
189loop = new Handler("ArmDrive");
190loop.add(handleDriveArmed);
191loop.run();
192
193
194// ================================================================
195// Bias crate calibration
196// ================================================================
197// Bias crate calibration if necessary (it is aftr 4pm (local tome)
198// and the last calibration was more than eight hours ago.
199// -----------------------------------------------------------------
200
201// At this point we know that:
202// 1) The lid is closed
203// 2) The feedback is stopped
204// 3) The voltage is off
205function makeCurrentCalibration()
206{
207 dim.send("BIAS_CONTROL/SET_GLOBAL_DAC", 1);
208 dim.wait("BIAS_CONTROL", "VoltageOn", 3000);
209
210 var now = new Date();
211 dim.send("FEEDBACK/CALIBRATE_CURRENTS");
212
213 // FIXME: Timeout!
214 console.out("Wait for calibration to start");
215 dim.wait("FEEDBACK", "Calibrating", 5000);
216
217 console.out("Wait for calibration to end");
218 dim.wait("FEEDBACK", "Connected", 60000);
219
220 console.out("Calibration took "+(new Date()-now)+"ms");
221
222 console.out("Wait for voltage to be off");
223 dim.send("BIAS_CONTROL/SET_ZERO_VOLTAGE");
224 dim.wait("BIAS_CONTROL", "VoltageOff", 5000);
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
237// !data_calibration.data: FEEDBACK might just be freshly
238// started and will not yet serve this service.
239if (data_calibration.data==null || (diff>8 && now.getHours()>16))
240{
241 if (data_calibration.data==null)
242 console.out("No BIAS crate calibration avaliable: New calibration needed.");
243 else
244 console.out("Last BIAS crate calibration taken at "+age.toUTCString()+": New calibration needed.");
245
246 makeCurrentCalibration();
247}
248
249service_calibration.close();
250
251// ================================================================
252// Crosscheck all states
253// ================================================================
254
255var table =
256[
257 [ "TNG_WEATHER" ],
258 [ "MAGIC_WEATHER" ],
259 [ "CHAT" ],
260 [ "SMART_FACT" ],
261 [ "TEMPERATURE" ],
262 [ "DATA_LOGGER", [ "NightlyFileOpen", "WaitForRun", "Logging" ] ],
263 [ "FSC_CONTROL", [ "Connected" ] ],
264 [ "MCP", [ "Idle" ] ],
265 [ "TIME_CHECK", [ "Valid" ] ],
266 [ "PWR_CONTROL", [ "SystemOn" ] ],
267 [ "AGILENT_CONTROL", [ "VoltageOn" ] ],
268 [ "BIAS_CONTROL", [ "VoltageOn", "VoltageOff" ] ],
269 [ "FEEDBACK", [ "Connected" ] ],
270 [ "RATE_SCAN", [ "Connected" ] ],
271 [ "RATE_CONTROL", [ "Connected" ] ],
272 [ "LID_CONTROL", [ "Open", "Closed" ] ],
273 [ "DRIVE_CONTROL", [ "Armed", "Tracking", "OnTrack", "Locked" ] ],
274 [ "FTM_CONTROL", [ "Idle", "TriggerOn" ] ],
275 [ "FAD_CONTROL", [ "Connected", "WritingData" ] ],
276];
277
278if (!checkStates(table))
279{
280 throw new Error("Something unexpected has happened. Although the startup-"+
281 "procedure has finished, not all servers are in the state "+
282 "in which they ought to be. Please, try to find out what "+
283 "happened...");
284}
285
286
Note: See TracBrowser for help on using the repository browser.