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

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