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 | dim.log("Start: "+__FILE__+" ["+__DATE__+"]");
|
---|
8 |
|
---|
9 | // This should be set in dimctrl.rc as JavaScript.schedule-database.
|
---|
10 | // It is sent together with the script to the dimserver.
|
---|
11 | // If started directly, it has to be set after the command:
|
---|
12 | //
|
---|
13 | // .js scripts/Main.js schedule-database=...
|
---|
14 | //
|
---|
15 | if (!$['schedule-database'])
|
---|
16 | throw new Error("Environment 'schedule-database' not set!");
|
---|
17 |
|
---|
18 | //dimctrl.defineState(37, "TimeOutBeforeTakingData", "MCP took more than 5minutes to start TakingData");
|
---|
19 |
|
---|
20 | // ================================================================
|
---|
21 | // Code related to the schedule
|
---|
22 | // ================================================================
|
---|
23 |
|
---|
24 | //this is just the class implementation of 'Observation'
|
---|
25 | include('scripts/Observation_class.js');
|
---|
26 | include('scripts/getSchedule.js');
|
---|
27 |
|
---|
28 | var observations = [ ];
|
---|
29 |
|
---|
30 | // Get the observation scheduled for 'now' from the table and
|
---|
31 | // return its index
|
---|
32 | function getObservation(now)
|
---|
33 | {
|
---|
34 | if (now==undefined)
|
---|
35 | now = new Date();
|
---|
36 |
|
---|
37 | if (isNaN(now.valueOf()))
|
---|
38 | throw new Error("Date argument in getObservation invalid.");
|
---|
39 |
|
---|
40 | observations = getSchedule();
|
---|
41 |
|
---|
42 | for (var i=0; i<observations.length; i++)
|
---|
43 | if (now<observations[i].start)
|
---|
44 | return i-1;
|
---|
45 |
|
---|
46 | return observations.length-1;
|
---|
47 | }
|
---|
48 |
|
---|
49 | // ================================================================
|
---|
50 | // Code to check whether observation is allowed
|
---|
51 | // ================================================================
|
---|
52 | /*
|
---|
53 | function currentEst(source)
|
---|
54 | {
|
---|
55 | var moon = new Moon();
|
---|
56 | if (!moon.isUp)
|
---|
57 | return 7.7;
|
---|
58 |
|
---|
59 | var dist = Sky.dist(moon, source);
|
---|
60 |
|
---|
61 | var alt = 90-moon.toLocal().zd;
|
---|
62 |
|
---|
63 | var lc = dist*alt*pow(Moon.disk(), 6)/360/360;
|
---|
64 |
|
---|
65 | var cur = 7.7+4942*lc;
|
---|
66 |
|
---|
67 | return cur;
|
---|
68 | }
|
---|
69 |
|
---|
70 | function thresholdEst(source) // relative threshold (ratio)
|
---|
71 | {
|
---|
72 | // Assumption:
|
---|
73 | // atmosphere is 70km, shower taks place after 60km, earth radius 6400km
|
---|
74 | // just using the cosine law
|
---|
75 | // This fits very well with MC results: See Roger Firpo, p.45
|
---|
76 | // "Study of the MAGIC telescope sensitivity for Large Zenith Angle observations"
|
---|
77 |
|
---|
78 | var c = Math.cos(Math.Pi-source.zd);
|
---|
79 | var ratio = (10*sqrt(409600*c*c+9009) + 6400*c - 60)/10;
|
---|
80 |
|
---|
81 | // assumption: Energy threshold increases linearily with current
|
---|
82 | // assumption: Energy threshold increases linearily with distance
|
---|
83 |
|
---|
84 | return ratio*currentEst(source)/7.7;
|
---|
85 | }
|
---|
86 | */
|
---|
87 |
|
---|
88 | // ================================================================
|
---|
89 | // Code to perform the DRS calib sequence
|
---|
90 | // ================================================================
|
---|
91 |
|
---|
92 | function doDrsCalibration(where)
|
---|
93 | {
|
---|
94 | console.out(" Take DRS calibration ["+where+"]");
|
---|
95 |
|
---|
96 | service_feedback.voltageOff();
|
---|
97 |
|
---|
98 | var tm = new Date();
|
---|
99 |
|
---|
100 | while (1)
|
---|
101 | {
|
---|
102 | dim.send("FAD_CONTROL/START_DRS_CALIBRATION");
|
---|
103 | if (!takeRun("drs-pedestal", 1000)) // 40 / 20s (50Hz)
|
---|
104 | continue;
|
---|
105 |
|
---|
106 | if (!takeRun("drs-gain", 1000)) // 40 / 20s (50Hz)
|
---|
107 | continue;
|
---|
108 |
|
---|
109 | if (!takeRun("drs-pedestal", 1000)) // 40 / 20s (50Hz)
|
---|
110 | continue;
|
---|
111 |
|
---|
112 | break;
|
---|
113 | }
|
---|
114 |
|
---|
115 | dim.send("FAD_CONTROL/SET_FILE_FORMAT", 2);
|
---|
116 |
|
---|
117 | while (!takeRun("drs-pedestal", 1000)); // 40 / 20s (50Hz)
|
---|
118 | while (!takeRun("drs-time", 1000)); // 40 / 20s (50Hz)
|
---|
119 |
|
---|
120 | while (1)
|
---|
121 | {
|
---|
122 | dim.send("FAD_CONTROL/RESET_SECONDARY_DRS_BASELINE");
|
---|
123 | if (takeRun("pedestal", 1000)) // 40 / 10s (80Hz)
|
---|
124 | break;
|
---|
125 | }
|
---|
126 |
|
---|
127 | dim.send("FAD_CONTROL/SET_FILE_FORMAT", 2);
|
---|
128 |
|
---|
129 | while (!takeRun("pedestal", 1000)); // 40 / 10s (80Hz)
|
---|
130 |
|
---|
131 | // -----------
|
---|
132 | // 4'40 / 2'00
|
---|
133 |
|
---|
134 | console.out(" DRS calibration done [%.1f]".$((new Date()-tm)/1000));
|
---|
135 | }
|
---|
136 |
|
---|
137 | // ================================================================
|
---|
138 | // Code related to the lid
|
---|
139 | // ================================================================
|
---|
140 |
|
---|
141 | function OpenLid()
|
---|
142 | {
|
---|
143 | /*
|
---|
144 | while (Sun.horizon(-13).isUp)
|
---|
145 | {
|
---|
146 | var now = new Date();
|
---|
147 | var minutes_until_sunset = (Sun.horizon(-13).set - now)/60000;
|
---|
148 | console.out(now.toUTCString()+": Sun above FACT-horizon, lid cannot be opened: sleeping 1min, remaining %.1fmin".$(minutes_until_sunset));
|
---|
149 | v8.sleep(60000);
|
---|
150 | }*/
|
---|
151 |
|
---|
152 | var isClosed = dim.state("LID_CONTROL").name=="Closed";
|
---|
153 |
|
---|
154 | var tm = new Date();
|
---|
155 |
|
---|
156 | // Wait for lid to be open
|
---|
157 | if (isClosed)
|
---|
158 | {
|
---|
159 | console.out(" Open lid: start");
|
---|
160 | dim.send("LID_CONTROL/OPEN");
|
---|
161 | }
|
---|
162 | dim.wait("LID_CONTROL", "Open", 30000);
|
---|
163 |
|
---|
164 | if (isClosed)
|
---|
165 | console.out(" Open lid: done [%.1fs]".$((new Date()-tm)/1000));
|
---|
166 | }
|
---|
167 |
|
---|
168 | function CloseLid()
|
---|
169 | {
|
---|
170 | var isOpen = dim.state("LID_CONTROL").name=="Open";
|
---|
171 |
|
---|
172 | var tm = new Date();
|
---|
173 |
|
---|
174 | // Wait for lid to be open
|
---|
175 | if (isOpen)
|
---|
176 | {
|
---|
177 | console.out(" Close lid: start");
|
---|
178 | dim.send("LID_CONTROL/CLOSE");
|
---|
179 | }
|
---|
180 | dim.wait("LID_CONTROL", "Closed", 30000);
|
---|
181 |
|
---|
182 | if (isOpen)
|
---|
183 | console.out(" Close lid: end [%.1fs]".$((new Date()-tm)/1000));
|
---|
184 | }
|
---|
185 |
|
---|
186 | // ================================================================
|
---|
187 | // Code related to switching bias voltage on and off
|
---|
188 | // ================================================================
|
---|
189 |
|
---|
190 | var service_feedback = new Subscription("FEEDBACK/DEVIATION");
|
---|
191 |
|
---|
192 | service_feedback.onchange = function(evt)
|
---|
193 | {
|
---|
194 | if (this.cnt && evt.counter>this.cnt+12)
|
---|
195 | return;
|
---|
196 |
|
---|
197 | if (!evt.data)
|
---|
198 | return;
|
---|
199 |
|
---|
200 | var delta = evt.obj['DeltaBias'];
|
---|
201 |
|
---|
202 | // It seems this can happen when the feedback was newly started. Why?
|
---|
203 | // What is the correct solution?
|
---|
204 | if (!delta)
|
---|
205 | return;
|
---|
206 |
|
---|
207 | var avg = 0;
|
---|
208 | for (var i=0; i<320; i++)
|
---|
209 | avg += delta[i];
|
---|
210 | avg /= 320;
|
---|
211 |
|
---|
212 | if (this.previous)
|
---|
213 | this.voltageStep = Math.abs(avg-this.previous);
|
---|
214 |
|
---|
215 | this.previous = avg;
|
---|
216 |
|
---|
217 | console.out(" DeltaV=%.3f".$(this.voltageStep));
|
---|
218 | }
|
---|
219 |
|
---|
220 | // DN: Why is voltageOff() implemented as
|
---|
221 | // a method of a Subscription to a specific Service
|
---|
222 | // I naively would think of voltageOff() as an unbound function.
|
---|
223 | // I seems to me it has to be a method of a Subscription object, in order
|
---|
224 | // to use the update counting method. But does it have to be
|
---|
225 | // a Subscription to FEEDBACK/DEVIATION, or could it work with other services as well?
|
---|
226 | service_feedback.voltageOff = function()
|
---|
227 | {
|
---|
228 | var state = dim.state("BIAS_CONTROL").name;
|
---|
229 |
|
---|
230 | if (state=="Disconnected")
|
---|
231 | {
|
---|
232 | console.out(" Voltage off: bias crate disconnected!");
|
---|
233 | return;
|
---|
234 | }
|
---|
235 |
|
---|
236 | // check of feedback has to be switched on
|
---|
237 | var isOn = state=="VoltageOn" || state=="Ramping";
|
---|
238 | if (isOn)
|
---|
239 | {
|
---|
240 | console.out(" Voltage off: start");
|
---|
241 |
|
---|
242 | // Supress the possibility that the bias control is
|
---|
243 | // ramping and will reject the command to switch the
|
---|
244 | // voltage off
|
---|
245 | var isControl = dim.state("FEEDBACK").name=="CurrentControl";
|
---|
246 | if (isControl)
|
---|
247 | {
|
---|
248 | console.out(" Suspending feedback.");
|
---|
249 | dim.send("FEEDBACK/ENABLE_OUTPUT", false);
|
---|
250 | dim.wait("FEEDBACK", "CurrentCtrlIdle", 3000);
|
---|
251 | }
|
---|
252 |
|
---|
253 | // Switch voltage off
|
---|
254 | console.out(" "+new Date().toUTCString()+": Voltage off: switch off");
|
---|
255 | dim.send("BIAS_CONTROL/SET_ZERO_VOLTAGE");
|
---|
256 |
|
---|
257 | // If the feedback was enabled, re-enable it
|
---|
258 | if (isControl)
|
---|
259 | {
|
---|
260 | console.out(" Resuming feedback.");
|
---|
261 | dim.send("FEEDBACK/ENABLE_OUTPUT", true);
|
---|
262 | dim.wait("FEEDBACK", "CurrentControl", 3000);
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | dim.wait("BIAS_CONTROL", "VoltageOff", 5000);
|
---|
267 |
|
---|
268 | // FEEDBACK stays in CurrentCtrl when Voltage is off but output enabled
|
---|
269 | // dim.wait("FEEDBACK", "CurrentCtrlIdle", 1000);
|
---|
270 |
|
---|
271 | if (isOn)
|
---|
272 | console.out(" Voltage off: end");
|
---|
273 | }
|
---|
274 |
|
---|
275 | // DN: The name of the method voltageOn() in the context of the method
|
---|
276 | // voltageOff() is a little bit misleading, since when voltageOff() returns
|
---|
277 | // the caller can be sure the voltage is off, but when voltageOn() return
|
---|
278 | // this is not the case, in the sense, that the caller can now take data.
|
---|
279 | // instead the caller of voltageOn() *must* call waitForVoltageOn() afterwards
|
---|
280 | // in order to safely take good-quality data.
|
---|
281 | // This could lead to nasty bugs in the sense, that the second call might
|
---|
282 | // be forgotten by somebody
|
---|
283 | //
|
---|
284 | // so I suggest to rename voltageOn() --> prepareVoltageOn()
|
---|
285 | // waitForVoltageOn() stays as it is
|
---|
286 | // and one creates a third method called:voltageOn() like this
|
---|
287 | /* service_feedback.voltageOn = function()
|
---|
288 | * {
|
---|
289 | * this.prepareVoltageOn();
|
---|
290 | * this.waitForVoltageOn();
|
---|
291 | * }
|
---|
292 | *
|
---|
293 | * */
|
---|
294 | // For convenience.
|
---|
295 |
|
---|
296 | service_feedback.voltageOn = function()
|
---|
297 | {
|
---|
298 | //if (Sun.horizon("FACT").isUp)
|
---|
299 | // throw new Error("Sun is above FACT-horizon, voltage cannot be switched on.");
|
---|
300 |
|
---|
301 | var isOff = dim.state("BIAS_CONTROL").name=="VoltageOff";
|
---|
302 | if (isOff)
|
---|
303 | {
|
---|
304 | console.out(" "+new Date().toUTCString()+": Voltage on: switch on");
|
---|
305 | //console.out(JSON.stringify(dim.state("BIAS_CONTROL")));
|
---|
306 |
|
---|
307 | dim.send("BIAS_CONTROL/SET_GLOBAL_DAC", 1);
|
---|
308 | }
|
---|
309 |
|
---|
310 | // Wait until voltage on
|
---|
311 | dim.wait("BIAS_CONTROL", "VoltageOn", 5000);
|
---|
312 |
|
---|
313 | // From now on the feedback waits for a valid report from the FSC
|
---|
314 | // and than switchs to CurrentControl
|
---|
315 | dim.wait("FEEDBACK", "CurrentControl", 60000);
|
---|
316 |
|
---|
317 | if (isOff)
|
---|
318 | {
|
---|
319 | console.out(" Voltage on: cnt="+this.cnt);
|
---|
320 |
|
---|
321 | this.previous = undefined;
|
---|
322 | this.cnt = this.get().counter;
|
---|
323 | this.voltageStep = undefined;
|
---|
324 | }
|
---|
325 | }
|
---|
326 |
|
---|
327 | service_feedback.waitForVoltageOn = function()
|
---|
328 | {
|
---|
329 | // waiting 45sec for the current control to stabilize...
|
---|
330 | // v8.sleep(45000);
|
---|
331 |
|
---|
332 | // ----- Wait for at least three updates -----
|
---|
333 | // The feedback is started as if the camera where at 0deg
|
---|
334 | // Then after the first temp update, the temperature will be set to the
|
---|
335 | // correct value (this has already happened)
|
---|
336 | // So we only have to wait for the current to get stable.
|
---|
337 | // This should happen after three to five current updates.
|
---|
338 | // So we want one recent temperature update
|
---|
339 | // and three recent current updates
|
---|
340 |
|
---|
341 | // Avoid output if condition is already fulfilled
|
---|
342 | if (this.cnt && this.get().counter>this.cnt+10)
|
---|
343 | return;
|
---|
344 |
|
---|
345 | // FIXME: timeout missing
|
---|
346 | console.out(" Feedback wait: start");
|
---|
347 |
|
---|
348 | function func()
|
---|
349 | {
|
---|
350 | if ((this.cnt!=undefined && this.get().counter>this.cnt+10) ||
|
---|
351 | (this.voltageStep && this.voltageStep<0.02))
|
---|
352 | return true;
|
---|
353 | }
|
---|
354 |
|
---|
355 | var now = new Date();
|
---|
356 |
|
---|
357 | v8.timeout(4*60000, func, this);
|
---|
358 |
|
---|
359 | console.out(" Feedback wait: end [dV=%.3f, cnt=%d, %.2fs]".$(this.voltageStep, this.get().counter, (new Date()-now)/1000));
|
---|
360 | }
|
---|
361 |
|
---|
362 | // ================================================================
|
---|
363 | // Function to shutdown the system
|
---|
364 | // ================================================================
|
---|
365 |
|
---|
366 | function Shutdown()
|
---|
367 | {
|
---|
368 | console.out("Shutdown: start");
|
---|
369 |
|
---|
370 | service_feedback.voltageOff();
|
---|
371 | CloseLid();
|
---|
372 | dim.send("DRIVE_CONTROL/PARK");
|
---|
373 |
|
---|
374 | console.out("Waiting for telescope to park. This may take a while.");
|
---|
375 |
|
---|
376 | // FIXME: This might not work is the drive is already close to park position
|
---|
377 | dim.wait("DRIVE_CONTROL", "Locked", 3000);
|
---|
378 |
|
---|
379 | var sub = new Subscription("DRIVE_CONTROL/POINTING_POSITION");
|
---|
380 | sub.get(5000); // FIXME: Proper error message in case of failure
|
---|
381 |
|
---|
382 | function func()
|
---|
383 | {
|
---|
384 | var report = sub.get();
|
---|
385 |
|
---|
386 | var zd = report.obj['Zd'];
|
---|
387 | var az = report.obj['Az'];
|
---|
388 |
|
---|
389 | if (zd>100 && Math.abs(az)<1)
|
---|
390 | return true;
|
---|
391 |
|
---|
392 | return undefined;
|
---|
393 | }
|
---|
394 |
|
---|
395 | var now = new Date();
|
---|
396 | v8.timeout(150000, func);
|
---|
397 |
|
---|
398 | //dim.send("FEEDBACK/STOP");
|
---|
399 | dim.send("FEEDBACK/ENABLE_OUTPUT", false);
|
---|
400 | dim.send("FTM_CONTROL/STOP_TRIGGER");
|
---|
401 | dim.send("BIAS_CONTROL/DISCONNECT");
|
---|
402 |
|
---|
403 | dim.wait("FTM_CONTROL", "Idle", 3000);
|
---|
404 | dim.wait("BIAS_CONTROL", "Disconnected", 3000);
|
---|
405 | dim.wait("FEEDBACK", "Connected", 3000);
|
---|
406 |
|
---|
407 | var report = sub.get();
|
---|
408 |
|
---|
409 | console.out("");
|
---|
410 | console.out("Shutdown procedure seems to be finished...");
|
---|
411 | console.out(" "+new Date().toUTCString());
|
---|
412 | console.out(" Telescope at Zd=%.1fdeg Az=%.1fdeg".$(report.obj['Zd'], report.obj['Az']));
|
---|
413 | console.out(" Please make sure the park position was reached");
|
---|
414 | console.out(" and the telescope is not moving anymore.");
|
---|
415 | console.out(" Please check that the lid is closed and the voltage switched off.", "");
|
---|
416 | console.out(" DRIVE_CONTROL: "+dim.state("DRIVE_CONTROL").name);
|
---|
417 | console.out(" FEEDBACK: "+dim.state("FEEDBACK").name);
|
---|
418 | console.out(" FTM_CONTROL: "+dim.state("FTM_CONTROL").name);
|
---|
419 | console.out(" BIAS_CONTROL: "+dim.state("BIAS_CONTROL").name);
|
---|
420 | console.out("");
|
---|
421 | console.out("Shutdown: end ["+(new Date()-now)/1000+"s]");
|
---|
422 |
|
---|
423 | sub.close();
|
---|
424 | }
|
---|
425 |
|
---|
426 | // ================================================================
|
---|
427 | // Check datalogger subscriptions
|
---|
428 | // ================================================================
|
---|
429 |
|
---|
430 | var datalogger_subscriptions = new Subscription("DATA_LOGGER/SUBSCRIPTIONS");
|
---|
431 | datalogger_subscriptions.get(3000, false);
|
---|
432 |
|
---|
433 | datalogger_subscriptions.check = function()
|
---|
434 | {
|
---|
435 | var obj = this.get();
|
---|
436 | if (!obj.data)
|
---|
437 | throw new Error("DATA_LOGGER/SUBSCRIPTIONS not available.");
|
---|
438 |
|
---|
439 | var expected =
|
---|
440 | [
|
---|
441 | "BIAS_CONTROL/CURRENT",
|
---|
442 | "BIAS_CONTROL/DAC",
|
---|
443 | "BIAS_CONTROL/NOMINAL",
|
---|
444 | "BIAS_CONTROL/VOLTAGE",
|
---|
445 | "DRIVE_CONTROL/POINTING_POSITION",
|
---|
446 | "DRIVE_CONTROL/SOURCE_POSITION",
|
---|
447 | "DRIVE_CONTROL/STATUS",
|
---|
448 | "DRIVE_CONTROL/TRACKING_POSITION",
|
---|
449 | "FAD_CONTROL/CONNECTIONS",
|
---|
450 | "FAD_CONTROL/DAC",
|
---|
451 | "FAD_CONTROL/DNA",
|
---|
452 | "FAD_CONTROL/DRS_RUNS",
|
---|
453 | "FAD_CONTROL/EVENTS",
|
---|
454 | "FAD_CONTROL/FEEDBACK_DATA",
|
---|
455 | "FAD_CONTROL/FILE_FORMAT",
|
---|
456 | "FAD_CONTROL/FIRMWARE_VERSION",
|
---|
457 | "FAD_CONTROL/INCOMPLETE",
|
---|
458 | "FAD_CONTROL/PRESCALER",
|
---|
459 | "FAD_CONTROL/REFERENCE_CLOCK",
|
---|
460 | "FAD_CONTROL/REGION_OF_INTEREST",
|
---|
461 | "FAD_CONTROL/RUNS",
|
---|
462 | "FAD_CONTROL/RUN_NUMBER",
|
---|
463 | "FAD_CONTROL/START_RUN",
|
---|
464 | "FAD_CONTROL/STATISTICS1",
|
---|
465 | // "FAD_CONTROL/STATISTICS2",
|
---|
466 | "FAD_CONTROL/STATS",
|
---|
467 | "FAD_CONTROL/STATUS",
|
---|
468 | "FAD_CONTROL/TEMPERATURE",
|
---|
469 | "FEEDBACK/CALIBRATED_CURRENTS",
|
---|
470 | "FEEDBACK/CALIBRATION",
|
---|
471 | "FEEDBACK/DEVIATION",
|
---|
472 | "FEEDBACK/REFERENCE",
|
---|
473 | "FSC_CONTROL/CURRENT",
|
---|
474 | "FSC_CONTROL/HUMIDITY",
|
---|
475 | "FSC_CONTROL/TEMPERATURE",
|
---|
476 | "FSC_CONTROL/VOLTAGE",
|
---|
477 | "FTM_CONTROL/COUNTER",
|
---|
478 | "FTM_CONTROL/DYNAMIC_DATA",
|
---|
479 | "FTM_CONTROL/ERROR",
|
---|
480 | "FTM_CONTROL/FTU_LIST",
|
---|
481 | "FTM_CONTROL/PASSPORT",
|
---|
482 | "FTM_CONTROL/STATIC_DATA",
|
---|
483 | "FTM_CONTROL/TRIGGER_RATES",
|
---|
484 | "LID_CONTROL/DATA",
|
---|
485 | "MAGIC_LIDAR/DATA",
|
---|
486 | "MAGIC_WEATHER/DATA",
|
---|
487 | "MCP/CONFIGURATION",
|
---|
488 | "PWR_CONTROL/DATA",
|
---|
489 | "RATE_CONTROL/THRESHOLD",
|
---|
490 | "RATE_SCAN/DATA",
|
---|
491 | "RATE_SCAN/PROCESS_DATA",
|
---|
492 | "TEMPERATURE/DATA",
|
---|
493 | "TIME_CHECK/OFFSET",
|
---|
494 | "TNG_WEATHER/DATA",
|
---|
495 | "TNG_WEATHER/DUST",
|
---|
496 | ];
|
---|
497 |
|
---|
498 | function map(entry)
|
---|
499 | {
|
---|
500 | if (entry.length==0)
|
---|
501 | return undefined;
|
---|
502 |
|
---|
503 | var rc = entry.split(',');
|
---|
504 | if (rc.length!=2)
|
---|
505 | throw new Error("Subscription list entry '"+entry+"' has wrong number of elements.");
|
---|
506 | return rc;
|
---|
507 | }
|
---|
508 |
|
---|
509 | var list = obj.data.split('\n').map(map);
|
---|
510 |
|
---|
511 | function check(name)
|
---|
512 | {
|
---|
513 | if (list.every(function(el){return el==undefined || el[0]!=name;}))
|
---|
514 | throw new Error("Subscription to '"+name+"' not available.");
|
---|
515 | }
|
---|
516 |
|
---|
517 | expected.forEach(check);
|
---|
518 | }
|
---|
519 |
|
---|
520 |
|
---|
521 |
|
---|
522 | // ================================================================
|
---|
523 | // Crosscheck all states
|
---|
524 | // ================================================================
|
---|
525 |
|
---|
526 | // ----------------------------------------------------------------
|
---|
527 | // Do a standard startup to bring the system in into a well
|
---|
528 | // defined state
|
---|
529 | // ----------------------------------------------------------------
|
---|
530 | include('scripts/Startup.js');
|
---|
531 |
|
---|
532 | // ================================================================
|
---|
533 | // Code related to monitoring the fad system
|
---|
534 | // ================================================================
|
---|
535 |
|
---|
536 | // This code is here, because scripts/Startup.js needs the
|
---|
537 | // same subscriptions... to be revised.
|
---|
538 | var sub_incomplete = new Subscription("FAD_CONTROL/INCOMPLETE");
|
---|
539 | var sub_connections = new Subscription("FAD_CONTROL/CONNECTIONS");
|
---|
540 | var sub_startrun = new Subscription("FAD_CONTROL/START_RUN");
|
---|
541 | sub_startrun.get(5000);
|
---|
542 |
|
---|
543 | include('scripts/takeRun.js');
|
---|
544 |
|
---|
545 | // ----------------------------------------------------------------
|
---|
546 | // Check that everything we need is availabel to receive commands
|
---|
547 | // (FIXME: Should that go to the general CheckState?)
|
---|
548 | // ----------------------------------------------------------------
|
---|
549 | console.out("Checking send.");
|
---|
550 | checkSend(["MCP", "DRIVE_CONTROL", "LID_CONTROL", "FAD_CONTROL", "FEEDBACK"]);
|
---|
551 | console.out("Checking send: done");
|
---|
552 |
|
---|
553 | // ----------------------------------------------------------------
|
---|
554 | // Bring feedback into the correct operational state
|
---|
555 | // ----------------------------------------------------------------
|
---|
556 | console.out("Feedback init: start.");
|
---|
557 | service_feedback.get(5000);
|
---|
558 |
|
---|
559 | dim.send("FEEDBACK/ENABLE_OUTPUT", true);
|
---|
560 | dim.send("FEEDBACK/START_CURRENT_CONTROL", 0.);
|
---|
561 |
|
---|
562 | v8.timeout(3000, function() { var n = dim.state("FEEDBACK").name; if (n=="CurrentCtrlIdle" || n=="CurrentControl") return true; });
|
---|
563 |
|
---|
564 | // ----------------------------------------------------------------
|
---|
565 | // Connect to the DRS_RUNS service
|
---|
566 | // ----------------------------------------------------------------
|
---|
567 | console.out("Drs runs init: start.");
|
---|
568 |
|
---|
569 | var sub_drsruns = new Subscription("FAD_CONTROL/DRS_RUNS");
|
---|
570 | sub_drsruns.get(5000);
|
---|
571 | // FIXME: Check if the last DRS calibration was complete?
|
---|
572 |
|
---|
573 | function getTimeSinceLastDrsCalib()
|
---|
574 | {
|
---|
575 | // ----- Time since last DRS Calibration [min] ------
|
---|
576 | var runs = sub_drsruns.get(0);
|
---|
577 | var diff = (new Date()-runs.time)/60000;
|
---|
578 |
|
---|
579 | // Warning: 'roi=300' is a number which is not intrisically fixed
|
---|
580 | // but can change depending on the taste of the observers
|
---|
581 | var valid = runs.obj['run'][2]>0 && runs.obj['roi']==300;
|
---|
582 |
|
---|
583 | if (valid)
|
---|
584 | console.out(" Last DRS calib: %.1fmin ago".$(diff));
|
---|
585 | else
|
---|
586 | console.out(" No valid drs calibration available");
|
---|
587 |
|
---|
588 | return valid ? diff : null;
|
---|
589 | }
|
---|
590 |
|
---|
591 | // ----------------------------------------------------------------
|
---|
592 | // Make sure we will write files
|
---|
593 | // ----------------------------------------------------------------
|
---|
594 | dim.send("FAD_CONTROL/SET_FILE_FORMAT", 2);
|
---|
595 |
|
---|
596 | // ----------------------------------------------------------------
|
---|
597 | // Print some information for the user about the
|
---|
598 | // expected first oberservation
|
---|
599 | // ----------------------------------------------------------------
|
---|
600 | var test = getObservation();
|
---|
601 | if (test!=undefined)
|
---|
602 | {
|
---|
603 | var n = new Date();
|
---|
604 | if (observations.length>0 && test==-1)
|
---|
605 | console.out(n.toUTCString()+": First observation scheduled for "+observations[0].start.toUTCString());
|
---|
606 | if (test>=0 && test<observations.length)
|
---|
607 | console.out(n.toUTCString()+": First observation should start immediately.");
|
---|
608 | if (observations.length>0 && observations[0].start>n+12*3600*1000)
|
---|
609 | console.out(n.toUTCString()+": No observations scheduled for the next 12 hours!");
|
---|
610 | if (observations.length==0)
|
---|
611 | console.out(n.toUTCString()+": No observations scheduled!");
|
---|
612 | }
|
---|
613 |
|
---|
614 | // ----------------------------------------------------------------
|
---|
615 | // Start main loop
|
---|
616 | // ----------------------------------------------------------------
|
---|
617 | console.out("Start main loop.");
|
---|
618 |
|
---|
619 | var run = -2; // getObservation never called
|
---|
620 | var sub;
|
---|
621 | var lastId;
|
---|
622 | var sun = Sun.horizon(-12);
|
---|
623 | var system_on; // undefined
|
---|
624 |
|
---|
625 | while (1)
|
---|
626 | {
|
---|
627 | // Check if observation position is still valid
|
---|
628 | // If source position has changed, set run=0
|
---|
629 | var idxObs = getObservation();
|
---|
630 | if (idxObs===undefined)
|
---|
631 | break;
|
---|
632 |
|
---|
633 | // we are still waiting for the first observation in the schedule
|
---|
634 | if (idxObs==-1)
|
---|
635 | {
|
---|
636 | // flag that the first observation will be in the future
|
---|
637 | run = -1;
|
---|
638 | v8.sleep(1000);
|
---|
639 | continue;
|
---|
640 | }
|
---|
641 |
|
---|
642 | // Check if we have to take action do to sun-rise
|
---|
643 | var was_up = sun.isUp;
|
---|
644 | sun = Sun.horizon(-12);
|
---|
645 | if (!was_up && sun.isUp)
|
---|
646 | {
|
---|
647 | console.out("", "Sun rise detected.... automatic shutdown initiated!");
|
---|
648 | // FIXME: State check?
|
---|
649 | Shutdown();
|
---|
650 | system_on = false;
|
---|
651 | continue;
|
---|
652 | }
|
---|
653 |
|
---|
654 | // Current and next observation target
|
---|
655 | var obs = observations[idxObs];
|
---|
656 | var nextObs = observations[idxObs+1];
|
---|
657 |
|
---|
658 | // Check if observation target has changed
|
---|
659 | if (lastId!=obs.id) // !Object.isEqual(obs, nextObs)
|
---|
660 | {
|
---|
661 | console.out("--- "+obs.id+" ---");
|
---|
662 | console.out("Current time: "+new Date().toUTCString());
|
---|
663 | console.out("Current observation: "+obs.start.toUTCString());
|
---|
664 | if (nextObs!=undefined)
|
---|
665 | console.out("Next observation: "+nextObs.start.toUTCString());
|
---|
666 | console.out("");
|
---|
667 |
|
---|
668 | // This is the first source, but we do not come from
|
---|
669 | // a scheduled 'START', so we have to check if the
|
---|
670 | // telescop is operational already
|
---|
671 | sub = 0;
|
---|
672 | if (run<0)
|
---|
673 | {
|
---|
674 | //Startup(); // -> Bias On/Off?, Lid open/closed?
|
---|
675 | //CloseLid();
|
---|
676 | }
|
---|
677 |
|
---|
678 | // The first observation had a start-time in the past...
|
---|
679 | // In this particular case start with the last entry
|
---|
680 | // in the list of measurements
|
---|
681 | if (run==-2)
|
---|
682 | sub = obs.length-1;
|
---|
683 |
|
---|
684 | run = 0;
|
---|
685 | }
|
---|
686 | lastId = obs.id;
|
---|
687 |
|
---|
688 | //if (nextObs==undefined && obs[obs.length-1].task!="SHUTDOWN")
|
---|
689 | // throw Error("Last scheduled measurement must be a shutdown.");
|
---|
690 |
|
---|
691 | // We are done with all measurement slots for this
|
---|
692 | // observation... wait for next observation
|
---|
693 | if (sub>=obs.length)
|
---|
694 | {
|
---|
695 | v8.sleep(1000);
|
---|
696 | continue;
|
---|
697 | }
|
---|
698 |
|
---|
699 | if (system_on===false && obs[sub].task!="STARTUP")
|
---|
700 | {
|
---|
701 | v8.sleep(1000);
|
---|
702 | continue;
|
---|
703 | }
|
---|
704 |
|
---|
705 | // Check if sun is still up... only DATA and RATESCAN must be suppressed
|
---|
706 | if ((obs[sub].task=="DATA" || obs[sub].task=="RATESCAN") && sun.isUp)
|
---|
707 | {
|
---|
708 | var now = new Date();
|
---|
709 | var remaining = (sun.set - now)/60000;
|
---|
710 | console.out(now.toUTCString()+" - "+obs[sub].task+": Sun above FACT-horizon: sleeping 1min, remaining %.1fmin".$(remaining));
|
---|
711 | v8.sleep(60000);
|
---|
712 | continue;
|
---|
713 | }
|
---|
714 |
|
---|
715 | console.out("\n"+(new Date()).toUTCString()+": Current measurement: "+obs[sub]);
|
---|
716 |
|
---|
717 | // FIXME: Maybe print a warning if Drive is on during day time!
|
---|
718 |
|
---|
719 | // It is not ideal that we allow the drive to be on during day time, but
|
---|
720 | // otherwise it is difficult to allow e.g. the STARTUP at the beginning of the night
|
---|
721 | var power_states = sun.isUp || system_on===false ? [ "DriveOff", "SystemOn" ] : [ "SystemOn" ];
|
---|
722 | var drive_states = sun.isUp || system_on===false ? undefined : [ "Armed", "Tracking", "OnTrack" ];
|
---|
723 |
|
---|
724 | // A scheduled task was found, lets check if all servers are
|
---|
725 | // still only and in reasonable states. If this is not the case,
|
---|
726 | // something unexpected must have happend and the script is aborted.
|
---|
727 | //console.out(" Checking states [general]");
|
---|
728 | var table =
|
---|
729 | [
|
---|
730 | [ "TNG_WEATHER" ],
|
---|
731 | [ "MAGIC_WEATHER" ],
|
---|
732 | [ "CHAT" ],
|
---|
733 | [ "SMART_FACT" ],
|
---|
734 | [ "TEMPERATURE" ],
|
---|
735 | [ "DATA_LOGGER", [ "NightlyFileOpen", "WaitForRun", "Logging" ] ],
|
---|
736 | [ "FSC_CONTROL", [ "Connected" ] ],
|
---|
737 | [ "MCP", [ "Idle" ] ],
|
---|
738 | [ "TIME_CHECK", [ "Valid" ] ],
|
---|
739 | [ "PWR_CONTROL", power_states/*[ "SystemOn" ]*/ ],
|
---|
740 | [ "AGILENT_CONTROL", [ "VoltageOn" ] ],
|
---|
741 | [ "BIAS_CONTROL", [ "VoltageOff", "VoltageOn", "Ramping" ] ],
|
---|
742 | [ "FEEDBACK", [ "CurrentControl", "CurrentCtrlIdle" ] ],
|
---|
743 | [ "LID_CONTROL", [ "Open", "Closed" ] ],
|
---|
744 | [ "DRIVE_CONTROL", drive_states/*[ "Armed", "Tracking", "OnTrack" ]*/ ],
|
---|
745 | [ "FTM_CONTROL", [ "Idle", "TriggerOn" ] ],
|
---|
746 | [ "FAD_CONTROL", [ "Connected", "RunInProgress" ] ],
|
---|
747 | [ "RATE_SCAN", [ "Connected" ] ],
|
---|
748 | [ "RATE_CONTROL", [ "Connected", "GlobalThresholdSet", "InProgress" ] ],
|
---|
749 | ];
|
---|
750 |
|
---|
751 |
|
---|
752 | if (!checkStates(table))
|
---|
753 | {
|
---|
754 | throw new Error("Something unexpected has happened. One of the servers "+
|
---|
755 | "is in a state in which it should not be. Please,"+
|
---|
756 | "try to find out what happened...");
|
---|
757 | }
|
---|
758 |
|
---|
759 | datalogger_subscriptions.check();
|
---|
760 |
|
---|
761 | // Check if obs.task is one of the one-time-tasks
|
---|
762 | switch (obs[sub].task)
|
---|
763 | {
|
---|
764 | case "STARTUP":
|
---|
765 | console.out(" STARTUP", "");
|
---|
766 | CloseLid();
|
---|
767 |
|
---|
768 | doDrsCalibration("startup"); // will switch the voltage off
|
---|
769 |
|
---|
770 | service_feedback.voltageOn();
|
---|
771 | service_feedback.waitForVoltageOn();
|
---|
772 |
|
---|
773 | // Before we can switch to 3000 we have to make the right DRS calibration
|
---|
774 | console.out(" Take single p.e. run.");
|
---|
775 | while (!takeRun("pedestal", 5000));
|
---|
776 |
|
---|
777 | // It is unclear what comes next, so we better switch off the voltage
|
---|
778 | service_feedback.voltageOff();
|
---|
779 | system_on = true;
|
---|
780 | break;
|
---|
781 |
|
---|
782 | case "SHUTDOWN":
|
---|
783 | console.out(" SHUTDOWN", "");
|
---|
784 | Shutdown();
|
---|
785 | system_on = false;
|
---|
786 |
|
---|
787 | // FIXME: Avoid new observations after a shutdown until
|
---|
788 | // the next startup (set run back to -2?)
|
---|
789 | console.out(" Waiting for next startup.", "");
|
---|
790 | sub++;
|
---|
791 | continue;
|
---|
792 |
|
---|
793 | case "IDLE":
|
---|
794 | v8.sleep(1000);
|
---|
795 | continue;
|
---|
796 |
|
---|
797 | case "DRSCALIB":
|
---|
798 | console.out(" DRSCALIB", "");
|
---|
799 | doDrsCalibration("drscalib"); // will switch the voltage off
|
---|
800 | break;
|
---|
801 |
|
---|
802 | case "SINGLEPE":
|
---|
803 | console.out(" SINGLE-PE", "");
|
---|
804 |
|
---|
805 | // The lid must be closes
|
---|
806 | CloseLid();
|
---|
807 |
|
---|
808 | // Check if DRS calibration is necessary
|
---|
809 | var diff = getTimeSinceLastDrsCalib();
|
---|
810 | if (diff>30 || diff==null)
|
---|
811 | doDrsCalibration("singlepe"); // will turn voltage off
|
---|
812 |
|
---|
813 | // The voltage must be on
|
---|
814 | service_feedback.voltageOn();
|
---|
815 | service_feedback.waitForVoltageOn();
|
---|
816 |
|
---|
817 | // Before we can switch to 3000 we have to make the right DRS calibration
|
---|
818 | console.out(" Take single p.e. run.");
|
---|
819 | while (!takeRun("pedestal", 5000));
|
---|
820 |
|
---|
821 | // It is unclear what comes next, so we better switch off the voltage
|
---|
822 | service_feedback.voltageOff();
|
---|
823 | break;
|
---|
824 |
|
---|
825 | case "RATESCAN":
|
---|
826 | console.out(" RATESCAN", "");
|
---|
827 |
|
---|
828 | var tm1 = new Date();
|
---|
829 |
|
---|
830 | // This is a workaround to make sure that we really catch
|
---|
831 | // the new state and not the old one
|
---|
832 | dim.send("DRIVE_CONTROL/STOP");
|
---|
833 | dim.wait("DRIVE_CONTROL", "Armed", 5000);
|
---|
834 |
|
---|
835 | // The lid must be open
|
---|
836 | OpenLid();
|
---|
837 |
|
---|
838 | // The voltage must be switched on
|
---|
839 | service_feedback.voltageOn();
|
---|
840 |
|
---|
841 | if (obs.source != undefined)
|
---|
842 | dim.send("DRIVE_CONTROL/TRACK_ON", obs[sub].source);
|
---|
843 | else
|
---|
844 | dim.send("DRIVE_CONTROL/TRACK", obs[sub].ra, obs[sub].dec);
|
---|
845 |
|
---|
846 | dim.wait("DRIVE_CONTROL", "OnTrack", 150000); // 110s for turning and 30s for stabilizing
|
---|
847 |
|
---|
848 | service_feedback.waitForVoltageOn();
|
---|
849 |
|
---|
850 | var tm2 = new Date();
|
---|
851 |
|
---|
852 | // Start rate scan
|
---|
853 | dim.send("RATE_SCAN/START_THRESHOLD_SCAN", 50, 1000, -10);
|
---|
854 |
|
---|
855 | // Lets wait if the ratescan really starts... this might take a few
|
---|
856 | // seconds because RATE_SCAN configures the ftm and is waiting for
|
---|
857 | // it to be configured.
|
---|
858 | dim.wait("RATE_SCAN", "InProgress", 10000);
|
---|
859 | dim.wait("RATE_SCAN", "Connected", 2700000);
|
---|
860 |
|
---|
861 | // this line is actually some kind of hack.
|
---|
862 | // after the Ratescan, no data is written to disk. I don't know why, but it happens all the time
|
---|
863 | // So I decided to put this line here as a kind of patchwork....
|
---|
864 | //dim.send("FAD_CONTROL/SET_FILE_FORMAT", 2);
|
---|
865 |
|
---|
866 | console.out(" Ratescan done [%.1fs, %.1fs]".$((tm2-tm1)/1000, (new Date()-tm2)/1000));
|
---|
867 | break; // case "RATESCAN"
|
---|
868 |
|
---|
869 | case "DATA":
|
---|
870 |
|
---|
871 | // ========================== case "DATA" ============================
|
---|
872 | /*
|
---|
873 | if (Sun.horizon("FACT").isUp)
|
---|
874 | {
|
---|
875 | console.out(" SHUTDOWN","");
|
---|
876 | Shutdown();
|
---|
877 | console.out(" Exit forced due to broken schedule", "");
|
---|
878 | exit();
|
---|
879 | }
|
---|
880 | */
|
---|
881 | // Calculate remaining time for this observation in minutes
|
---|
882 | var remaining = nextObs==undefined ? 0 : (nextObs.start-new Date())/60000;
|
---|
883 |
|
---|
884 | // ------------------------------------------------------------
|
---|
885 |
|
---|
886 | console.out(" Run #"+run+" (remaining "+parseInt(remaining)+"min)");
|
---|
887 |
|
---|
888 | // ----- Time since last DRS Calibration [min] ------
|
---|
889 | var diff = getTimeSinceLastDrsCalib();
|
---|
890 |
|
---|
891 | // Changine pointing position and take calibration...
|
---|
892 | // ...every four runs (every ~20min)
|
---|
893 | // ...if at least ten minutes of observation time are left
|
---|
894 | // ...if this is the first run on the source
|
---|
895 | var point = (run%4==0 && remaining>10) || run==0;
|
---|
896 |
|
---|
897 | // Take DRS Calib...
|
---|
898 | // ...every four runs (every ~20min)
|
---|
899 | // ...at last every two hours
|
---|
900 | // ...when DRS temperature has changed by more than 2deg (?)
|
---|
901 | // ...when more than 15min of observation are left
|
---|
902 | // ...no drs calibration was done yet
|
---|
903 | var drscal = (run%4==0 && (remaining>15 && diff>70)) || diff==null;
|
---|
904 |
|
---|
905 | if (point)
|
---|
906 | {
|
---|
907 | // Change wobble position every four runs,
|
---|
908 | // start with alternating wobble positions each day
|
---|
909 | var wobble = (parseInt(run/4) + parseInt(new Date()/1000/3600/24-0.5))%2+1;
|
---|
910 |
|
---|
911 | //console.out(" Move telescope to '"+source+"' "+offset+" "+wobble);
|
---|
912 | console.out(" Move telescope to '"+obs[sub].source+"' ["+wobble+"]");
|
---|
913 |
|
---|
914 | //var offset = observations[obs][2];
|
---|
915 | //var wobble = observations[obs][3 + parseInt(run/4)%2];
|
---|
916 |
|
---|
917 | //dim.send("DRIVE_CONTROL/TRACK_SOURCE", offset, wobble, source);
|
---|
918 |
|
---|
919 | dim.send("DRIVE_CONTROL/TRACK_WOBBLE", wobble, obs[sub].source);
|
---|
920 |
|
---|
921 | // Do we have to check if the telescope is really moving?
|
---|
922 | // We can cross-check the SOURCE service later
|
---|
923 | }
|
---|
924 |
|
---|
925 | if (drscal)
|
---|
926 | doDrsCalibration("data"); // will turn voltage off
|
---|
927 |
|
---|
928 | OpenLid();
|
---|
929 |
|
---|
930 | // voltage must be switched on after the lid is open for the
|
---|
931 | // feedback to adapt the voltage properly to the night-sky
|
---|
932 | // background light level.
|
---|
933 | service_feedback.voltageOn();
|
---|
934 |
|
---|
935 | // This is now th right time to wait for th drive to be stable
|
---|
936 | dim.wait("DRIVE_CONTROL", "OnTrack", 150000); // 110s for turning and 30s for stabilizing
|
---|
937 |
|
---|
938 | // Now we have to be prepared for data-taking:
|
---|
939 | // make sure voltage is on
|
---|
940 | service_feedback.waitForVoltageOn();
|
---|
941 |
|
---|
942 | // If pointing had changed, do calibration
|
---|
943 | if (point)
|
---|
944 | {
|
---|
945 | console.out(" Calibration.");
|
---|
946 |
|
---|
947 | // Calibration (2% of 20')
|
---|
948 | while (1)
|
---|
949 | {
|
---|
950 | if (!takeRun("pedestal", 1000)) // 80 Hz -> 10s
|
---|
951 | continue;
|
---|
952 | if (!takeRun("light-pulser-ext", 1000)) // 80 Hz -> 10s
|
---|
953 | continue;
|
---|
954 | break;
|
---|
955 | }
|
---|
956 | }
|
---|
957 |
|
---|
958 | console.out(" Taking data: start [5min]");
|
---|
959 |
|
---|
960 | var len = 300;
|
---|
961 | while (len>15)
|
---|
962 | {
|
---|
963 | var time = new Date();
|
---|
964 | if (takeRun("data", -1, len)) // Take data (5min)
|
---|
965 | break;
|
---|
966 |
|
---|
967 | len -= parseInt((new Date()-time)/1000);
|
---|
968 | }
|
---|
969 |
|
---|
970 | console.out(" Taking data: done");
|
---|
971 | run++;
|
---|
972 |
|
---|
973 | continue; // case "DATA"
|
---|
974 | }
|
---|
975 |
|
---|
976 | if (nextObs!=undefined && sub==obs.length-1)
|
---|
977 | console.out(" Waiting for next observation scheduled for "+nextObs.start.toUTCString(),"");
|
---|
978 |
|
---|
979 | sub++;
|
---|
980 | }
|
---|
981 |
|
---|
982 | sub_drsruns.close();
|
---|
983 |
|
---|
984 | // ================================================================
|
---|
985 | // Comments and ToDo goes here
|
---|
986 | // ================================================================
|
---|
987 |
|
---|
988 | // error handline : http://www.sitepoint.com/exceptional-exception-handling-in-javascript/
|
---|
989 | // classes: http://www.phpied.com/3-ways-to-define-a-javascript-class/
|
---|
990 | //
|
---|
991 | // Arguments: TakeFirstDrsCalib
|
---|
992 | // To be determined: How to stop the script without foreceful interruption?
|
---|