Changeset 18263


Ignore:
Timestamp:
06/23/15 15:51:15 (9 years ago)
Author:
dneise
Message:
some more steps towards different handler functions
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/FACT++_scripts_refactoring/Startup.js

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