1 | /**
|
---|
2 | * @fileOverview This file has functions related to documenting JavaScript.
|
---|
3 | * @author <a href="mailto:thomas.bretz@epfl.ch">Thomas Bretz</a>
|
---|
4 | */
|
---|
5 | 'use strict';
|
---|
6 |
|
---|
7 | //this is just the class implementation of 'Observation'
|
---|
8 | include('scripts/Observation_class.js');
|
---|
9 |
|
---|
10 | dimctrl.defineState(37, "TimeOutBeforeTakingData", "MCP took more than 5minutes to start TakingData");
|
---|
11 |
|
---|
12 |
|
---|
13 | // error handline : http://www.sitepoint.com/exceptional-exception-handling-in-javascript/
|
---|
14 | // clases: http://www.phpied.com/3-ways-to-define-a-javascript-class/
|
---|
15 | //
|
---|
16 | // Arguments: TakeFirstDrsCalib
|
---|
17 | // To be determined: How to stop the script without foreceful interruption?
|
---|
18 |
|
---|
19 | // adapt states of drivectrl
|
---|
20 |
|
---|
21 | //
|
---|
22 | // SCRIPTS:
|
---|
23 | //
|
---|
24 | // - Startup, e.g. do not open LID
|
---|
25 | //
|
---|
26 | // - Bring telescop to state 'operational', e.g. open lid,
|
---|
27 | // when first run != START is detected in the loop
|
---|
28 | //
|
---|
29 | // - Take data
|
---|
30 | //
|
---|
31 | // - Shutdown
|
---|
32 | //
|
---|
33 | // ----> Does smartfact display alarm messages already?
|
---|
34 | // ----> How to display errors in smartfact? Is that necessary at all?
|
---|
35 | // (red output? maybe just <red>...</red>?
|
---|
36 | //
|
---|
37 | // ----------------------------------------------------------------
|
---|
38 |
|
---|
39 | function currentEst(source)
|
---|
40 | {
|
---|
41 | var moon = new Moon();
|
---|
42 | if (!moon.isUp)
|
---|
43 | return 7.7;
|
---|
44 |
|
---|
45 | var dist = Sky.dist(moon, source);
|
---|
46 |
|
---|
47 | var alt = 90-moon.toLocal().zd;
|
---|
48 |
|
---|
49 | var lc = dist*alt*pow(Moon.disk(), 6)/360/360;
|
---|
50 |
|
---|
51 | var cur = 7.7+4942*lc;
|
---|
52 |
|
---|
53 | return cur;
|
---|
54 | }
|
---|
55 |
|
---|
56 | function thresholdEst(source) // relative threshold (ratio)
|
---|
57 | {
|
---|
58 | // Assumption:
|
---|
59 | // atmosphere is 70km, shower taks place after 60km, earth radius 6400km
|
---|
60 | // just using the cosine law
|
---|
61 | // This fits very well with MC results: See Roger Firpo, p.45
|
---|
62 | // "Study of the MAGIC telescope sensitivity for Large Zenith Angle observations"
|
---|
63 |
|
---|
64 | var c = Math.cos(Math.Pi-source.zd);
|
---|
65 | var ratio = (10*sqrt(409600*c*c+9009) + 6400*c - 60)/10;
|
---|
66 |
|
---|
67 | // assumption: Energy threshold increases linearily with current
|
---|
68 | // assumption: Energy threshold increases linearily with distance
|
---|
69 |
|
---|
70 | return ratio*currentEst(source)/7.7;
|
---|
71 | }
|
---|
72 |
|
---|
73 | // Ratio in rate would be (estimate, not precise calculation)
|
---|
74 | // pow(ratio, -0.7)
|
---|
75 |
|
---|
76 | // ----------------------------------------------------------------
|
---|
77 |
|
---|
78 | //DN: the name of the Subscription object 'service_con' is not really
|
---|
79 | // telling that its a subscription to FAD_CONTROL/CONNECTIONS
|
---|
80 | // fad_connections sound ok for be, since
|
---|
81 | // fad_connections.onchange() is pretty clear
|
---|
82 | // fad_connections.reconnect() is not really good, but at least it has FAD in it.
|
---|
83 | var service_con = new Subscription("FAD_CONTROL/CONNECTIONS");
|
---|
84 | /**
|
---|
85 | * call-back function of FAD_CONTROL/CONNECTIONS
|
---|
86 | * store IDs of problematic FADs
|
---|
87 | *
|
---|
88 | */
|
---|
89 | service_con.onchange = function(evt)
|
---|
90 | {
|
---|
91 | this.reset = [ ];
|
---|
92 |
|
---|
93 | for (var x=0; x<40; x++)
|
---|
94 | if (evt.obj['status'][x]!=66 && evt.obj['status'][x]!=67)
|
---|
95 | this.reset.push(x);
|
---|
96 |
|
---|
97 | if (this.reset.length==0)
|
---|
98 | return;
|
---|
99 |
|
---|
100 | dim.alarm("FAD board loss detected...");
|
---|
101 | dim.send("MCP/RESET");
|
---|
102 | dim.send("FAD_CONTROL/CLOSE_OPEN_FILES");
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * reconnect to problematic FADs
|
---|
107 | *
|
---|
108 | * Dis- and Reconnects to FADs, found to be problematic by call-back function
|
---|
109 | * onchange() to have a different CONNECTION value than 66 or 67.
|
---|
110 | *
|
---|
111 | * @returns
|
---|
112 | * a boolean is returned.
|
---|
113 | * reconnect returns true if:
|
---|
114 | * * nothing needed to be reset --> no problems found by onchange()
|
---|
115 | * * the reconnection went fine.
|
---|
116 | *
|
---|
117 | * reconnect *never returns false* so far.
|
---|
118 | *
|
---|
119 | * @example
|
---|
120 | * if (!service_con.reconnect())
|
---|
121 | * exit();
|
---|
122 | */
|
---|
123 | service_con.reconnect = function()
|
---|
124 | {
|
---|
125 | // this.reset is a list containing the IDs of FADs,
|
---|
126 | // which have neither CONNECTION==66 nor ==67, whatever this means :-)
|
---|
127 | if (this.reset.length==0)
|
---|
128 | return true;
|
---|
129 |
|
---|
130 | console.out(" Reconnect: start ["+this.reset.length+"]");
|
---|
131 |
|
---|
132 | for (var i=0; i<this.reset.length; i++)
|
---|
133 | dim.send("FAD_CONTROL/DISCONNECT", this.reset[i]);
|
---|
134 |
|
---|
135 | v8.sleep(3000);
|
---|
136 |
|
---|
137 | while (this.reset.length)
|
---|
138 | dim.send("FAD_CONTROL/CONNECT", this.reset.pop());
|
---|
139 |
|
---|
140 | v8.sleep(1000);
|
---|
141 | dim.wait("FAD_CONTROL", "Connected", 3000);
|
---|
142 |
|
---|
143 | console.out(" Reconnect: end");
|
---|
144 |
|
---|
145 | return true;
|
---|
146 | }
|
---|
147 |
|
---|
148 | function takeRun(type, count, time)
|
---|
149 | {
|
---|
150 | if (!count)
|
---|
151 | count = -1;
|
---|
152 | if (!time)
|
---|
153 | time = -1;
|
---|
154 |
|
---|
155 | console.out(" Take run N="+count+" T="+time+"s ["+type+"]");
|
---|
156 | // change rats for cal runs1!!!
|
---|
157 |
|
---|
158 | dim.send("MCP/START", time?time:-1, count?count:-1, type);
|
---|
159 |
|
---|
160 |
|
---|
161 |
|
---|
162 | // What could be a reasonable timeout here?
|
---|
163 | // FIXME: Replace by callback?
|
---|
164 | //
|
---|
165 | // DN: I believe instead of waiting for 'TakingData' one could split this
|
---|
166 | // up into two checks with an extra condition:
|
---|
167 | // if type == 'data':
|
---|
168 | // wait until ThresholdCalibration starts:
|
---|
169 | // --> this time should be pretty identical for each run
|
---|
170 | // if this takes longer than say 3s:
|
---|
171 | // there might be a problem with one/more FADs
|
---|
172 | //
|
---|
173 | // wait until "TakingData":
|
---|
174 | // --> this seems to take even some minutes sometimes...
|
---|
175 | // (might be optimized rather soon, but still in the moment...)
|
---|
176 | // if this takes way too long:
|
---|
177 | // there might be something broken,
|
---|
178 | // so maybe a very high time limit is ok here.
|
---|
179 | // I think there is not much that can go wrong,
|
---|
180 | // when the Thr-Calib has already started. Still it might be nice
|
---|
181 | // If in the future RateControl is written so to find out that
|
---|
182 | // in case the threshold finding algorithm does
|
---|
183 | // *not converge as usual*
|
---|
184 | // it can complain, and in this way give a hint, that the weather
|
---|
185 | // might be a little bit too bad.
|
---|
186 | // else:
|
---|
187 | // wait until "TakingData":
|
---|
188 | // --> in a non-data run this time should be pretty short again
|
---|
189 | // if this takes longer than say 3s:
|
---|
190 | // there might be a problem with one/more FADs
|
---|
191 | //
|
---|
192 |
|
---|
193 | if (!dim.wait("MCP", "TakingData", -300000) )
|
---|
194 | {
|
---|
195 | console.out("MCP took longer than 5 minutes to start TakingData");
|
---|
196 | console.out("maybe this idicates a problem with one of the FADs?");
|
---|
197 | dimctrl.setState(37);
|
---|
198 | dim.wait("MCP", "TakingData", 500);
|
---|
199 | }
|
---|
200 | dim.wait("MCP", "Idle");
|
---|
201 |
|
---|
202 | console.out(" Take run: end");
|
---|
203 |
|
---|
204 | // DN: currently reconnect() never returns false
|
---|
205 | // .. but it can fail of course.
|
---|
206 | if (!service_con.reconnect())
|
---|
207 | exit();
|
---|
208 |
|
---|
209 | return true;//service_con.reconnect();
|
---|
210 | }
|
---|
211 |
|
---|
212 | // ----------------------------------------------------------------
|
---|
213 |
|
---|
214 | function doDrsCalibration()
|
---|
215 | {
|
---|
216 | console.out(" DRS cal: start");
|
---|
217 | service_feedback.voltageOff();
|
---|
218 |
|
---|
219 | while (1)
|
---|
220 | {
|
---|
221 | dim.send("FAD_CONTROL/START_DRS_CALIBRATION");
|
---|
222 | if (!takeRun("drs-pedestal", 1000)) // 40 / 20s (50Hz)
|
---|
223 | continue;
|
---|
224 | if (!takeRun("drs-gain", 1000)) // 40 / 20s (50Hz)
|
---|
225 | continue;
|
---|
226 | if (!takeRun("drs-pedestal", 1000)) // 40 / 20s (50Hz)
|
---|
227 | continue;
|
---|
228 |
|
---|
229 | dim.send("FAD_CONTROL/SET_FILE_FORMAT", 2);
|
---|
230 | if (!takeRun("drs-pedestal", 1000)) // 40 / 20s (50Hz)
|
---|
231 | continue;
|
---|
232 | if (!takeRun("drs-time", 1000)) // 40 / 20s (50Hz)
|
---|
233 | continue;
|
---|
234 |
|
---|
235 | dim.send("FAD_CONTROL/RESET_SECONDARY_DRS_BASELINE");
|
---|
236 | if (!takeRun("pedestal", 1000)) // 40 / 10s (80Hz)
|
---|
237 | continue;
|
---|
238 |
|
---|
239 | dim.send("FAD_CONTROL/SET_FILE_FORMAT", 2);
|
---|
240 | if (!takeRun("pedestal", 1000)) // 40 / 10s (80Hz)
|
---|
241 | continue;
|
---|
242 | // -----------
|
---|
243 | // 4'40 / 2'00
|
---|
244 |
|
---|
245 | break;
|
---|
246 | }
|
---|
247 |
|
---|
248 | console.out(" DRS cal: end");
|
---|
249 | }
|
---|
250 |
|
---|
251 | // ----------------------------------------------------------------
|
---|
252 |
|
---|
253 | function OpenLid()
|
---|
254 | {
|
---|
255 | var horizon_parameter = "nautical";
|
---|
256 | while (Sun.horizon( horizon_parameter ).isUp)
|
---|
257 | {
|
---|
258 | var minutes_until_sunset = (Sun.horizon( horizon_parameter ).set - new Date())/60000;
|
---|
259 | console.out("Open Lid - Info: ");
|
---|
260 | console.out(" Sun is above FACT-horizon, lid cannot be opened.");
|
---|
261 | console.out(" sleeping " + minutes_until_sunset + "minutes ...");
|
---|
262 | v8.sleep(60000);
|
---|
263 | }
|
---|
264 |
|
---|
265 | console.out("Open lid: start");
|
---|
266 |
|
---|
267 | // Wait for lid to be open
|
---|
268 | if (dim.state("LID_CONTROL").name=="Closed")
|
---|
269 | dim.send("LID_CONTROL/OPEN");
|
---|
270 | dim.wait("LID_CONTROL", "Open", 30000);
|
---|
271 | console.out("Open lid: done");
|
---|
272 | }
|
---|
273 |
|
---|
274 | function CloseLid()
|
---|
275 | {
|
---|
276 | console.out("Close lid: start");
|
---|
277 |
|
---|
278 | // Wait for lid to be open
|
---|
279 | if (dim.state("LID_CONTROL").name=="Open")
|
---|
280 | dim.send("LID_CONTROL/CLOSE");
|
---|
281 | dim.wait("LID_CONTROL", "Closed", 30000);
|
---|
282 |
|
---|
283 | console.out("Close lid: end");
|
---|
284 | }
|
---|
285 |
|
---|
286 | // ----------------------------------------------------------------
|
---|
287 |
|
---|
288 | var service_feedback = new Subscription("FEEDBACK/DEVIATION");
|
---|
289 |
|
---|
290 | // DN: Why is voltageOff() implemented as
|
---|
291 | // a method of a Subscription to a specific Service
|
---|
292 | // I naively would think of voltageOff() as an unbound function.
|
---|
293 | // I seems to me it has to be a method of a Subscription object, in order
|
---|
294 | // to use the update counting method. But does it have to be
|
---|
295 | // a Subscription to FEEDBACK/DEVIATION, or could it work with other services as well?
|
---|
296 | service_feedback.voltageOff = function()
|
---|
297 | {
|
---|
298 | console.out(" Voltage off: start");
|
---|
299 |
|
---|
300 | var isOn = dim.state("BIAS_CONTROL").name=="VoltageOn";
|
---|
301 |
|
---|
302 | if (isOn)
|
---|
303 | {
|
---|
304 | console.out(" Voltage on: switch off");
|
---|
305 | dim.send("BIAS_CONTROL/SET_ZERO_VOLTAGE");
|
---|
306 | }
|
---|
307 |
|
---|
308 | dim.wait("BIAS_CONTROL", "VoltageOff", 5000);
|
---|
309 |
|
---|
310 | // FEEDBACK stays in CurrentCtrl when Voltage is off but output enabled
|
---|
311 | // dim.wait("FEEDBACK", "CurrentCtrlIdle", 1000);
|
---|
312 |
|
---|
313 | console.out(" Voltage off: end");
|
---|
314 | }
|
---|
315 |
|
---|
316 | // DN: The name of the method voltageOn() in the context of the method
|
---|
317 | // voltageOff() is a little bit misleading, since when voltageOff() returns
|
---|
318 | // the caller can be sure the voltage is off, but when voltageOn() return
|
---|
319 | // this is not the case, in the sense, that the caller can now take data.
|
---|
320 | // instead the caller of voltageOn() *must* call waitForVoltageOn() afterwards
|
---|
321 | // in order to safely take good-quality data.
|
---|
322 | // This could lead to nasty bugs in the sense, that the second call might
|
---|
323 | // be forgotten by somebody
|
---|
324 | //
|
---|
325 | // so I suggest to rename voltageOn() --> prepareVoltageOn()
|
---|
326 | // waitForVoltageOn() stays as it is
|
---|
327 | // and one creates a third method called:voltageOn() like this
|
---|
328 | /* service_feedback.voltageOn = function()
|
---|
329 | * {
|
---|
330 | * this.prepareVoltageOn();
|
---|
331 | * this.waitForVoltageOn();
|
---|
332 | * }
|
---|
333 | *
|
---|
334 | * */
|
---|
335 | // For convenience.
|
---|
336 |
|
---|
337 | service_feedback.voltageOn = function()
|
---|
338 | {
|
---|
339 | //if (Sun.horizon("FACT").isUp)
|
---|
340 | // throw new Error("Sun is above FACT-horizon, voltage cannot be switched on.");
|
---|
341 |
|
---|
342 | console.out(" Voltage on: start");
|
---|
343 |
|
---|
344 | var isOff = dim.state("BIAS_CONTROL").name=="VoltageOff";
|
---|
345 |
|
---|
346 | if (isOff)
|
---|
347 | {
|
---|
348 | console.out(" Voltage on: switch on");
|
---|
349 | console.out(JSON.stringify(dim.state("BIAS_CONTROL")));
|
---|
350 |
|
---|
351 | dim.send("BIAS_CONTROL/SET_GLOBAL_DAC", 1);
|
---|
352 | }
|
---|
353 |
|
---|
354 | // Wait until voltage on
|
---|
355 | dim.wait("BIAS_CONTROL", "VoltageOn", 5000);
|
---|
356 |
|
---|
357 | // From now on the feedback waits for a valid report from the FSC
|
---|
358 | // and than switchs to CurrentControl
|
---|
359 | dim.wait("FEEDBACK", "CurrentControl", 60000);
|
---|
360 |
|
---|
361 | if (isOff)
|
---|
362 | {
|
---|
363 | this.cnt = this.get().counter;
|
---|
364 | console.out(" Voltage on: cnt="+this.cnt);
|
---|
365 | }
|
---|
366 |
|
---|
367 | console.out(" Voltage on: end");
|
---|
368 | }
|
---|
369 |
|
---|
370 |
|
---|
371 |
|
---|
372 | service_feedback.waitForVoltageOn = function()
|
---|
373 | {
|
---|
374 | // waiting 45sec for the current control to stabilize...
|
---|
375 | // v8.sleep(45000);
|
---|
376 |
|
---|
377 | // ----- Wait for at least three updates -----
|
---|
378 | // The feedback is started as if the camera where at 0deg
|
---|
379 | // Then after the first temp update, the temperature will be set to the
|
---|
380 | // correct value (this has already happened)
|
---|
381 | // So we only have to wait for the current to get stable.
|
---|
382 | // This should happen after three to five current updates.
|
---|
383 | // So we want one recent temperature update
|
---|
384 | // and three recent current updates
|
---|
385 | console.out(" Voltage wait: start");
|
---|
386 | while (this.cnt==undefined || this.get().counter<=this.cnt+2)
|
---|
387 | v8.sleep();
|
---|
388 | console.out(" Voltage wait: end [cnt="+this.get().counter+"]");
|
---|
389 | }
|
---|
390 |
|
---|
391 | // ================================================================
|
---|
392 | // Crosscheck all states
|
---|
393 | // ================================================================
|
---|
394 |
|
---|
395 | include('scripts/Startup.js');//Startup();
|
---|
396 |
|
---|
397 | /*
|
---|
398 | include('scripts/CheckStates.js');
|
---|
399 |
|
---|
400 | var table =
|
---|
401 | [
|
---|
402 | [ "TNG_WEATHER" ],
|
---|
403 | [ "MAGIC_WEATHER" ],
|
---|
404 | [ "CHAT" ],
|
---|
405 | [ "SMART_FACT" ],
|
---|
406 | [ "FSC_CONTROL", [ "Connected" ] ],
|
---|
407 | [ "MCP", [ "Idle" ] ],
|
---|
408 | [ "TIME_CHECK", [ "Valid" ] ],
|
---|
409 | [ "PWR_CONTROL", [ "SystemOn" ] ],
|
---|
410 | [ "AGILENT_CONTROL", [ "VoltageOn" ] ],
|
---|
411 | [ "BIAS_CONTROL", [ "VoltageOff" ] ],
|
---|
412 | [ "FEEDBACK", [ "CurrentControl", "CurrentCtrlIdle", "Connected" ] ],
|
---|
413 | [ "RATE_SCAN", [ "Connected" ] ],
|
---|
414 | [ "RATE_CONTROL", [ "Connected" ] ],
|
---|
415 | [ "LID_CONTROL", [ "Open", "Closed" ] ],
|
---|
416 | [ "DRIVE_CONTROL", [ "Armed", "Tracking", "OnTrack" ] ],
|
---|
417 | [ "FTM_CONTROL", [ "Idle", "TriggerOn" ] ],
|
---|
418 | [ "FAD_CONTROL", [ "Connected", "WritingData" ] ],
|
---|
419 | [ "DATA_LOGGER", [ "NightlyFileOpen", "WaitForRun", "Logging" ] ],
|
---|
420 | ];
|
---|
421 |
|
---|
422 | console.out("Checking states.");
|
---|
423 | if (!checkStates(table, 10000))
|
---|
424 | {
|
---|
425 | console.out("Something unexpected has happened. Although the startup-",
|
---|
426 | "procedure has finished, not all servers are in the state",
|
---|
427 | "in which they ought to be. Please, try to find out what",
|
---|
428 | "happened...");
|
---|
429 | exit();
|
---|
430 | }
|
---|
431 |
|
---|
432 | console.out("Checking states: done.");
|
---|
433 | */
|
---|
434 | // ----------------------------------------------------------------
|
---|
435 |
|
---|
436 | console.out("Checking send.");
|
---|
437 | checkSend(["MCP", "DRIVE_CONTROL", "LID_CONTROL", "FAD_CONTROL", "FEEDBACK"]);
|
---|
438 | console.out("Checking send: done");
|
---|
439 |
|
---|
440 | // ----------------------------------------------------------------
|
---|
441 |
|
---|
442 | console.out("Feedback init: start.");
|
---|
443 | service_feedback.get(5000);
|
---|
444 |
|
---|
445 | dim.send("FEEDBACK/ENABLE_OUTPUT", true);
|
---|
446 | dim.send("FEEDBACK/START_CURRENT_CONTROL", 0.);
|
---|
447 |
|
---|
448 | console.out("Feedback init: end.");
|
---|
449 |
|
---|
450 | // ----------------------------------------------------------------
|
---|
451 | // ================================================================
|
---|
452 | // ----------------------------------------------------------------
|
---|
453 |
|
---|
454 | // this file just contains the definition of
|
---|
455 | // the variable observations, which builds our nightly schedule, hence the filename
|
---|
456 | include('scripts/schedule.js');
|
---|
457 |
|
---|
458 | // make Observation objects from user input and check if 'date' is increasing.
|
---|
459 | for (var i=0; i<observations.length; i++)
|
---|
460 | {
|
---|
461 | observations[i] = new Observation(observations[i]);
|
---|
462 |
|
---|
463 | // check if the start date given by the user is increasing.
|
---|
464 | if (i>0 && observations[i].start <= observations[i-1].start)
|
---|
465 | {
|
---|
466 | throw new Error("Start time '"+utc.toUTCString()+
|
---|
467 | "' in row "+i+" exceeds start time in row "+(i-1));
|
---|
468 | }
|
---|
469 | }
|
---|
470 |
|
---|
471 |
|
---|
472 |
|
---|
473 | // ----------------------------------------------------------------
|
---|
474 | // Bring the system into a well defined state
|
---|
475 | // ----------------------------------------------------------------
|
---|
476 |
|
---|
477 | console.out("Drs runs init: start.");
|
---|
478 |
|
---|
479 | // FIMXE: Double subscription is not allowed!
|
---|
480 | // also Startup needs DRS_RUNS
|
---|
481 | var service_drs = new Subscription("FAD_CONTROL/DRS_RUNS");
|
---|
482 | service_drs.get(5000, false);
|
---|
483 |
|
---|
484 | console.out("Drs runs init: end.");
|
---|
485 |
|
---|
486 | // FIXME: Check if the last DRS calibration was complete?
|
---|
487 | // ----------------------------------------------------------------
|
---|
488 |
|
---|
489 | // We have to stup a few things here which might not by set by Startup.js
|
---|
490 | dim.send("FAD_CONTROL/SET_FILE_FORMAT", 2);
|
---|
491 |
|
---|
492 | // ----------------------------------------------------------------
|
---|
493 |
|
---|
494 | console.out("Start main loop.");
|
---|
495 |
|
---|
496 | function Startup()
|
---|
497 | {
|
---|
498 | /**** dummy ****/
|
---|
499 | console.out(" => [STARTUP] called.");
|
---|
500 | }
|
---|
501 |
|
---|
502 | function Shutdown()
|
---|
503 | {
|
---|
504 | /**** dummy ****/
|
---|
505 | //console.out(" => [SHUTDOWN] called.");
|
---|
506 | console.out("Shutdown: start");
|
---|
507 | service_feedback.voltageOff();
|
---|
508 | CloseLid();
|
---|
509 | dim.send("DRIVE_CONTROL/PARK");
|
---|
510 | dim.wait("DRIVE_CONTROL", "Moving", 3000);
|
---|
511 | dim.wait("DRIVE_CONTROL", "Armed", 120000);
|
---|
512 | console.out("Shutdown: end");
|
---|
513 | }
|
---|
514 |
|
---|
515 | // Get the observation scheduled for 'now' from the table and
|
---|
516 | // return its index
|
---|
517 | function getObservation(now)
|
---|
518 | {
|
---|
519 | if (now==undefined)
|
---|
520 | now = new Date();
|
---|
521 |
|
---|
522 | if (isNaN(now.valueOf()))
|
---|
523 | throw new Error("Date argument in getObservation invalid.");
|
---|
524 |
|
---|
525 | for (var i=0; i<observations.length; i++)
|
---|
526 | if ( now<observations[i].start )
|
---|
527 | return i-1;
|
---|
528 |
|
---|
529 | return observations.length-1;
|
---|
530 | }
|
---|
531 |
|
---|
532 |
|
---|
533 | // DN: using so called magic numbers to encode certain
|
---|
534 | // states of the logic is considered pretty bad coding as far as I understand.
|
---|
535 | // the reader at this point has no idea why run is -2 ... this is the first time she
|
---|
536 | // reads about this variable and there is not a word of explanation found.
|
---|
537 | var run = -2;
|
---|
538 | var lastObs;
|
---|
539 |
|
---|
540 | while (1)
|
---|
541 | {
|
---|
542 | // Check if observation position is still valid
|
---|
543 | // If source position has changed, set run=0
|
---|
544 | var idxObs = getObservation();
|
---|
545 | if (idxObs===undefined)
|
---|
546 | exit();
|
---|
547 |
|
---|
548 | if (idxObs==-1)
|
---|
549 | {
|
---|
550 | v8.sleep(1000);
|
---|
551 | continue;
|
---|
552 | }
|
---|
553 |
|
---|
554 | var obs = observations[idxObs];
|
---|
555 | var nextObs = observations[idxObs+1];
|
---|
556 |
|
---|
557 | // Check if observation target has changed
|
---|
558 | if (lastObs!=idxObs)
|
---|
559 | {
|
---|
560 | console.out("--- "+idxObs+" ---");
|
---|
561 | console.out("Current time: "+new Date());
|
---|
562 | console.out("Current observation: "+obs);
|
---|
563 | console.out("Next observation: "+nextObs);
|
---|
564 | console.out("");
|
---|
565 |
|
---|
566 | // This is the first source, but we do not come from
|
---|
567 | // a scheduled 'START', so we have to check if the
|
---|
568 | // telescop is operational already
|
---|
569 | if (run==-2)
|
---|
570 | {
|
---|
571 | Startup(); // -> Bias On/Off?, Lid open/closed?
|
---|
572 | CloseLid();
|
---|
573 | }
|
---|
574 |
|
---|
575 | run = 0;
|
---|
576 | }
|
---|
577 | lastObs = idxObs;
|
---|
578 |
|
---|
579 | // We have performed startup or shutdown... wait for next observation
|
---|
580 | if (run==-1)
|
---|
581 | {
|
---|
582 | v8.sleep(1000);
|
---|
583 | continue;
|
---|
584 | }
|
---|
585 |
|
---|
586 | // Check if obs.task is one of the one-time-tasks
|
---|
587 | switch (obs.task)
|
---|
588 | {
|
---|
589 | case "STARTUP":
|
---|
590 | console.out(" STARTUP", "");
|
---|
591 | Startup(); // BiasOn/Off?, Lid open/close?
|
---|
592 | CloseLid();
|
---|
593 |
|
---|
594 | console.out(" Take DRS calibration.");
|
---|
595 | doDrsCalibration(); // -> VoltageOff
|
---|
596 |
|
---|
597 | service_feedback.voltageOn();
|
---|
598 | service_feedback.waitForVoltageOn();
|
---|
599 |
|
---|
600 | // Before we can switch to 3000 we have to make the right DRS calibration
|
---|
601 | console.out(" Take single p.e. run.");
|
---|
602 | while (!takeRun("pedestal", 5000));
|
---|
603 |
|
---|
604 | service_feedback.voltageOff();
|
---|
605 |
|
---|
606 | console.out(" Waiting for first scheduled observation.","");
|
---|
607 | run = -1;
|
---|
608 | continue;
|
---|
609 |
|
---|
610 | case "SHUTDOWN":
|
---|
611 | console.out(" SHUTDOWN","");
|
---|
612 | Shutdown();
|
---|
613 |
|
---|
614 | console.out(" Waiting for next startup.", "");
|
---|
615 | run = -1;
|
---|
616 | continue;
|
---|
617 |
|
---|
618 | case "RATESCAN":
|
---|
619 | console.out(" RATESCAN ");
|
---|
620 |
|
---|
621 | dim.send("DRIVE_CONTROL/STOP");
|
---|
622 | dim.wait("DRIVE_CONTROL", "Armed", 3000);
|
---|
623 |
|
---|
624 | if (obs.source != undefined)
|
---|
625 | dim.send("DRIVE_CONTROL/TRACK_ON", obs.source);
|
---|
626 | else
|
---|
627 | dim.send("DRIVE_CONTROL/TRACK", obs.ra, obs.dec);
|
---|
628 |
|
---|
629 | //OpenLid();
|
---|
630 | dim.wait("DRIVE_CONTROL", "OnTrack", 300000);
|
---|
631 |
|
---|
632 | // Checking if system is Ready for Data Taking, which is in this case
|
---|
633 | // the same as Ready for RateScan.
|
---|
634 | //
|
---|
635 | // this part might be simply wrong here, since I should be able to expect
|
---|
636 | // the system to be able for data taking. And if not, then it is not here,
|
---|
637 | // to bring the system into a better state, correct?
|
---|
638 | dim.wait("FEEDBACK", "CurrentControl", -100);
|
---|
639 | dim.wait("BIAS_CONTROL", "VoltageOn", -100);
|
---|
640 | dim.wait("FAD_CONTROL", "Connected", -100);
|
---|
641 |
|
---|
642 | dim.wait("RATE_SCAN","Connected", 5000);
|
---|
643 | dim.send("RATE_SCAN/START_THRESHOLD_SCAN", 50, 1000, -10);
|
---|
644 |
|
---|
645 | // lets wait if the Ratescan really starts .. it should be started after 10sec max.
|
---|
646 | dim.wait("RATE_SCAN", "InProgress", 10000);
|
---|
647 | dim.wait("RATE_SCAN", "Connected", 2700000);
|
---|
648 |
|
---|
649 | // this line is actually some kind of hack.
|
---|
650 | // after the Ratescan, no data is written to disk. I don't know why, but it happens all the time
|
---|
651 | // So I decided to put this line here as a kind of patchwork....
|
---|
652 | dim.send("FAD_CONTROL/SET_FILE_FORMAT", 2);
|
---|
653 |
|
---|
654 | console.out("Ratescan done.");
|
---|
655 | run = -1;
|
---|
656 | continue;
|
---|
657 | }
|
---|
658 | /*
|
---|
659 | if (Sun.horizon("FACT").isUp)
|
---|
660 | {
|
---|
661 | console.out(" SHUTDOWN","");
|
---|
662 | Shutdown();
|
---|
663 | console.out(" Exit forced due to broken schedule", "");
|
---|
664 | exit();
|
---|
665 | }
|
---|
666 | */
|
---|
667 | // Calculate remaining time for this observation in minutes
|
---|
668 | var remaining = (nextObs.start-new Date())/60000;
|
---|
669 |
|
---|
670 | // ------------------------------------------------------------
|
---|
671 |
|
---|
672 | console.out(" Checking states [mainloop]");
|
---|
673 | var table =
|
---|
674 | [
|
---|
675 | [ "TNG_WEATHER" ],
|
---|
676 | [ "MAGIC_WEATHER" ],
|
---|
677 | [ "CHAT" ],
|
---|
678 | [ "SMART_FACT" ],
|
---|
679 | [ "DATA_LOGGER", [ "NightlyFileOpen", "WaitForRun", "Logging" ] ],
|
---|
680 | [ "FSC_CONTROL", [ "Connected" ] ],
|
---|
681 | [ "MCP", [ "Idle" ] ],
|
---|
682 | [ "TIME_CHECK", [ "Valid" ] ],
|
---|
683 | [ "PWR_CONTROL", [ "SystemOn" ] ],
|
---|
684 | [ "AGILENT_CONTROL", [ "VoltageOn" ] ],
|
---|
685 | [ "BIAS_CONTROL", [ "VoltageOff", "VoltageOn" ] ],
|
---|
686 | [ "FEEDBACK", [ "CurrentCtrlIdle", "CurrentControl" ] ],
|
---|
687 | [ "RATE_SCAN", [ "Connected" ] ],
|
---|
688 | [ "RATE_CONTROL", [ "Connected", "InProgress" ] ],
|
---|
689 | [ "LID_CONTROL", [ "Open", "Closed" ] ],
|
---|
690 | [ "DRIVE_CONTROL", [ "Armed", "Tracking", "OnTrack" ] ],
|
---|
691 | [ "FTM_CONTROL", [ "Idle", "TriggerOn" ] ],
|
---|
692 | [ "FAD_CONTROL", [ "Connected", "WritingData" ] ],
|
---|
693 | ];
|
---|
694 |
|
---|
695 | if (!checkStates(table))
|
---|
696 | {
|
---|
697 | throw new Error("Something unexpected has happened. One of the servers"+
|
---|
698 | "is in a state in which it should not be. Please,"+
|
---|
699 | "try to find out what happened...");
|
---|
700 | }
|
---|
701 |
|
---|
702 | console.out(" Checking states: end.");
|
---|
703 |
|
---|
704 | // ------------------------------------------------------------
|
---|
705 |
|
---|
706 | console.out(" Run #"+run+" ("+parseInt(remaining)+"min)");
|
---|
707 |
|
---|
708 | // ----- Time since last DRS Calibration [min] ------
|
---|
709 | var runs = service_drs.get(0, false);
|
---|
710 | var diff = (new Date()-runs.time)/60000;
|
---|
711 |
|
---|
712 | // Warning: 'roi=300' is a number which is not intrisically fixed
|
---|
713 | // but can change depending on the taste of the observers
|
---|
714 | var valid = runs.data[1][2]>0 && runs.data[0]==300;
|
---|
715 |
|
---|
716 | if (valid)
|
---|
717 | console.out(" Last DRS calib: "+diff+"min ago");
|
---|
718 | else
|
---|
719 | console.out(" No valid drs calibration");
|
---|
720 |
|
---|
721 | // Changine pointing position and take calibration...
|
---|
722 | // ...every four runs (every ~20min)
|
---|
723 | // ...if at least ten minutes of observation time are left
|
---|
724 | // ...if this is the first run on the source
|
---|
725 | var point = (run%4==0 && remaining>10) || run==0;
|
---|
726 |
|
---|
727 | // Take DRS Calib...
|
---|
728 | // ...every four runs (every ~20min)
|
---|
729 | // ...at last every two hours
|
---|
730 | // ...when DRS temperature has changed by more than 2deg (?)
|
---|
731 | // ...when more than 15min of observation are left
|
---|
732 | // ...no drs calibration was done yet
|
---|
733 | var drscal = (run%4==0 && (remaining>15 && diff>70)) || !valid;
|
---|
734 |
|
---|
735 | if (point)
|
---|
736 | {
|
---|
737 | var wobble = parseInt(run/4)%2;
|
---|
738 |
|
---|
739 | //console.out(" Move telescope to '"+source+"' "+offset+" "+wobble);
|
---|
740 | console.out(" Move telescope to '"+obs.source+"' ["+wobble+"]");
|
---|
741 |
|
---|
742 | //var offset = observations[obs][2];
|
---|
743 | //var wobble = observations[obs][3 + parseInt(run/4)%2];
|
---|
744 |
|
---|
745 | //dim.send("DRIVE_CONTROL/TRACK_SOURCE", offset, wobble, source);
|
---|
746 |
|
---|
747 | dim.send("DRIVE_CONTROL/TRACK_WOBBLE", wobble+1, obs.source);
|
---|
748 |
|
---|
749 | // Do we have to check if the telescope is really moving?
|
---|
750 | // We can cross-check the SOURCE service later
|
---|
751 | }
|
---|
752 |
|
---|
753 | if (drscal)
|
---|
754 | {
|
---|
755 | console.out(" Take DRS calibration.");
|
---|
756 | doDrsCalibration(); // -> VoltageOff
|
---|
757 | }
|
---|
758 |
|
---|
759 | OpenLid();
|
---|
760 |
|
---|
761 | // voltage must be switched on after the lid is open for the
|
---|
762 | // feedback to adapt the voltage properly to the night-sky
|
---|
763 | // background light level.
|
---|
764 | service_feedback.voltageOn();
|
---|
765 |
|
---|
766 | // This is now th right time to wait for th drive to be stable
|
---|
767 | dim.wait("DRIVE_CONTROL", "OnTrack", 150000); // 110s for turning and 30s for stabilizing
|
---|
768 |
|
---|
769 | // Now we have to be prepared for data-taking:
|
---|
770 | // make sure voltage is on
|
---|
771 | service_feedback.waitForVoltageOn();
|
---|
772 |
|
---|
773 | // If pointing had changed, do calibration
|
---|
774 | if (point)
|
---|
775 | {
|
---|
776 | console.out(" Calibration.");
|
---|
777 |
|
---|
778 | // Calibration (2% of 20')
|
---|
779 | while (1)
|
---|
780 | {
|
---|
781 | if (!takeRun("pedestal", 1000)) // 80 Hz -> 10s
|
---|
782 | continue;
|
---|
783 | if (!takeRun("light-pulser-ext", 1000)) // 80 Hz -> 10s
|
---|
784 | continue;
|
---|
785 | break;
|
---|
786 | }
|
---|
787 | }
|
---|
788 |
|
---|
789 | console.out(" Taking data: start [5min]");
|
---|
790 |
|
---|
791 | var len = 300;
|
---|
792 | while (len>0)
|
---|
793 | {
|
---|
794 | var time = new Date();
|
---|
795 | if (!takeRun("data", -1, len)) // Take data (5min)
|
---|
796 | len -= parseInt((new Date()-time)/1000);
|
---|
797 | else
|
---|
798 | break;
|
---|
799 | }
|
---|
800 |
|
---|
801 | //v8.sleep(360000);
|
---|
802 | console.out(" Taking data: done");
|
---|
803 |
|
---|
804 | run++;
|
---|
805 | }
|
---|
806 |
|
---|
807 | service_drs.close();
|
---|