Changeset 15098


Ignore:
Timestamp:
03/18/13 14:52:05 (12 years ago)
Author:
tbretz
Message:
Improved and compacted output; disable feedback while voltage should be switched off (to avoid the command being rejacted because of the Ramping state); some logic fixed which could cause exceptions (e.g. trigger switched on during ratescan); added a safety check for the parking position at the end of the shutdown; fixed the messages printed before the main loop is started; added a check that the last event is a shutdown; added Idle task; implemented the new scheme with schedules observation with several measurements
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/scripts/Main.js

    r15030 r15098  
    66
    77//this is just the class implementation of 'Observation'
    8 include('scripts/Observation_class.js'); 
     8include('scripts/Observation_class.js');
    99
    1010dimctrl.defineState(37, "TimeOutBeforeTakingData", "MCP took more than 5minutes to start TakingData");
     
    1212
    1313// error handline : http://www.sitepoint.com/exceptional-exception-handling-in-javascript/
    14 // clases: http://www.phpied.com/3-ways-to-define-a-javascript-class/
     14// classes: http://www.phpied.com/3-ways-to-define-a-javascript-class/
    1515//
    1616// Arguments: TakeFirstDrsCalib
    1717// To be determined: How to stop the script without foreceful interruption?
    18 
    19 // adapt states of drivectrl
    2018
    2119//
     
    8280// fad_connections.reconnect() is not really good, but at least it has FAD in it.
    8381var service_con = new Subscription("FAD_CONTROL/CONNECTIONS");
     82
    8483/**
    8584 * call-back function of FAD_CONTROL/CONNECTIONS
     
    102101        return;
    103102
    104     dim.alarm("FAD board loss detected...");
     103    //m.alarm("FAD board loss detected...");
    105104    dim.send("MCP/RESET");
    106105    dim.send("FAD_CONTROL/CLOSE_OPEN_FILES");
     
    157156        time = -1;
    158157
    159     console.out("  Take run N="+count+" T="+time+"s ["+type+"]");
    160     // change rats for cal runs1!!!
     158    console.out("  Take run: N="+count+" T="+time+"s ["+type+"]");
    161159
    162160    dim.send("MCP/START", time?time:-1, count?count:-1, type);
    163 
    164    
    165161
    166162    // What could be a reasonable timeout here?
     
    263259    while (Sun.horizon( horizon_parameter ).isUp)
    264260    {
    265        var minutes_until_sunset = (Sun.horizon( horizon_parameter ).set - new Date())/60000;
    266        console.out("Open Lid - Info: ");
    267        console.out("    Sun is above FACT-horizon, lid cannot be opened.");
    268        console.out("    sleeping " + minutes_until_sunset + "minutes ...");
    269        v8.sleep(60000);
    270     }
    271 
    272     console.out("Open lid: start");
     261        var now = new Date();
     262        var minutes_until_sunset = (Sun.horizon( horizon_parameter ).set - now)/60000;
     263        console.out(now.toUTCString()+": Sun above FACT-horizon, lid cannot be opened: sleeping 1min, remaining %.1fmin".$(minutes_until_sunset));
     264        v8.sleep(60000);
     265    }
     266
     267    var isClosed = dim.state("LID_CONTROL").name=="Closed";
    273268
    274269    // Wait for lid to be open
    275     if (dim.state("LID_CONTROL").name=="Closed")
     270    if (isClosed)
     271    {
     272        console.out("  Open lid: start");
    276273        dim.send("LID_CONTROL/OPEN");
     274    }
    277275    dim.wait("LID_CONTROL", "Open", 30000);
    278     console.out("Open lid: done");
     276
     277    if (isClosed)
     278        console.out("  Open lid: done");
    279279}
    280280
    281281function CloseLid()
    282282{
    283     console.out("Close lid: start");
     283    var isOpen = dim.state("LID_CONTROL").name=="Open";
    284284
    285285    // Wait for lid to be open
    286     if (dim.state("LID_CONTROL").name=="Open")
     286    if (isOpen)
     287    {
     288        console.out("  Close lid: start");
    287289        dim.send("LID_CONTROL/CLOSE");
     290    }
    288291    dim.wait("LID_CONTROL", "Closed", 30000);
    289292
    290     console.out("Close lid: end");
     293    if (isOpen)
     294        console.out("  Close lid: end");
    291295}
    292296
     
    303307service_feedback.voltageOff = function()
    304308{
    305     console.out("  Voltage off: start");
    306 
    307     var isOn = dim.state("BIAS_CONTROL").name=="VoltageOn";
    308 
     309    var state = dim.state("BIAS_CONTROL").name;
     310
     311    // check of feedback has to be switched on
     312    var isOn = state=="VoltageOn" || state=="Ramping";
    309313    if (isOn)
    310314    {
     315        console.out("  Voltage off: start");
     316
     317        // Supress the possibility that the bias control is
     318        // ramping and will reject the command to switch the
     319        // voltage off
     320        var isControl = dim.state("FEEDBACK").name=="CurrentControl";
     321        if (isControl)
     322        {
     323            console.out("  Suspending feedback.");
     324            dim.send("FEEDBACK/ENABLE_OUTPUT", false);
     325            dim.wait("FEEDBACK", "CurrentCtrlIdle", 3000);
     326        }
     327
     328        // Switch voltage off
    311329        console.out("  Voltage on: switch off");
    312330        dim.send("BIAS_CONTROL/SET_ZERO_VOLTAGE");
     331
     332        // If the feedback was enabled, re-enable it
     333        if (isControl)
     334        {
     335            console.out("  Resuming feedback.");
     336            dim.send("FEEDBACK/ENABLE_OUTPUT", true);
     337            dim.wait("FEEDBACK", "CurrentControl", 3000);
     338        }
    313339    }
    314340
     
    318344    // dim.wait("FEEDBACK", "CurrentCtrlIdle", 1000);
    319345
    320     console.out("  Voltage off: end");
     346    if (isOn)
     347        console.out("  Voltage off: end");
    321348}
    322349
     
    347374    //    throw new Error("Sun is above FACT-horizon, voltage cannot be switched on.");
    348375
    349     console.out("  Voltage on: start");
    350 
    351376    var isOff = dim.state("BIAS_CONTROL").name=="VoltageOff";
    352 
    353377    if (isOff)
    354378    {
    355379        console.out("  Voltage on: switch on");
    356         // console.out(JSON.stringify(dim.state("BIAS_CONTROL")));
     380        //console.out(JSON.stringify(dim.state("BIAS_CONTROL")));
    357381
    358382        dim.send("BIAS_CONTROL/SET_GLOBAL_DAC", 1);
     
    371395        console.out("  Voltage on: cnt="+this.cnt);
    372396    }
    373 
    374     console.out("  Voltage on: end");
    375 }
    376 
    377 
     397}
    378398
    379399service_feedback.waitForVoltageOn = function()
     
    390410    // So we want one recent temperature update
    391411    //  and three recent current updates
    392     console.out("  Voltage wait: start");
     412
     413    // Avoid output if condition is already fulfilled
     414    if (this.cnt && this.get().counter>this.cnt+2)
     415        return;
     416
     417    // FIXME: timeout missing
     418    console.out("  Feedback wait: start");
     419
     420    var now = new Date();
    393421    while (this.cnt==undefined || this.get().counter<=this.cnt+2)
    394422        v8.sleep();
    395     console.out("  Voltage wait: end [cnt="+this.get().counter+"]");
     423
     424    console.out("  Feedback wait: end [cnt=%d, %.2fs]".$(this.get().counter, (new Date()-now)/1000));
    396425}
    397426
     
    460489// this file just contains the definition of
    461490// the variable observations, which builds our nightly schedule, hence the filename
    462 include('scripts/schedule.js'); 
    463    
     491include('scripts/schedule.js');
     492
    464493// make Observation objects from user input and check if 'date' is increasing.
    465494for (var i=0; i<observations.length; i++)
    466495{
    467496    observations[i] = new Observation(observations[i]);
    468    
     497
    469498    // check if the start date given by the user is increasing.
    470499    if (i>0 && observations[i].start <= observations[i-1].start)
     
    474503    }
    475504}
    476 
    477 
    478505
    479506// ----------------------------------------------------------------
     
    486513//        also Startup needs DRS_RUNS
    487514var service_drs = new Subscription("FAD_CONTROL/DRS_RUNS");
    488 service_drs.get(5000, false);
     515service_drs.get(5000);
    489516
    490517console.out("Drs runs init: end.");
     
    511538    //console.out("  => [SHUTDOWN] called.");
    512539    console.out("Shutdown: start");
     540
     541    dim.send("FTM_CONTROL/STOP_TRIGGER");
    513542    service_feedback.voltageOff();
    514543    CloseLid();
    515544    dim.send("DRIVE_CONTROL/PARK");
     545
     546    console.out("Waiting for telescope to park. This may take a while.");
     547
     548    // FIXME: This might not work is the drive is already close to park position
    516549    dim.wait("DRIVE_CONTROL", "Moving", 3000);
    517     dim.wait("DRIVE_CONTROL", "Armed", 120000);
     550    dim.wait("DRIVE_CONTROL", "Armed", 120000);
     551
     552    // dim.wait("DRIVE_CONTROL", "Locked", 3000);
     553
     554    var sub = new Subscription("DRIVE_CONTROL/POINTING_POSITION");
     555    sub.get(5000);  // FIXME: Proper error message in case of failure
     556
     557    function func()
     558    {
     559        var report = sub.get();
     560
     561        var zd = report.obj['Zd'];
     562        var az = report.obj['Az'];
     563
     564        if (zd>100 && Math.abs(az)<1)
     565            return true;
     566
     567        return undefined;
     568    }
     569
     570    v8.timeout(150000, func);
     571
     572    // FIXME: Add a check for the position here!
     573
    518574    console.out("Shutdown: end");
    519575}
     
    525581    if (now==undefined)
    526582        now = new Date();
    527    
     583
    528584    if (isNaN(now.valueOf()))
    529585        throw new Error("Date argument in getObservation invalid.");
     
    541597//      the reader at this point has no idea why run is -2 ... this is the first time she
    542598//      reads about this variable and there is not a word of explanation found.
    543 var run = -2;
     599var run = -2; // getObservation never called
     600var sub;
    544601var lastObs;
    545602
    546603var test = getObservation();
    547 if (test!=undefined && test<observations.length)
    548 {
    549     var t = observations[test].start;
     604if (test!=undefined)
     605{
    550606    var n = new Date();
    551     if (t>n)
    552         console.out(n.toUTCString()+": First observation scheduled for "+t.toUTCString());
    553     else
     607    if (test==-1)
     608        console.out(n.toUTCString()+": First observation scheduled for "+observations[0].start.toUTCString());
     609    if (test>=0 && test<observations.length)
    554610        console.out(n.toUTCString()+": First observation should start immediately.");
    555     if (t>n+12*3600*1000)
     611    if (observations[0].start>n+12*3600*1000)
    556612        console.out(n.toUTCString()+": No observations scheduled for the next 12 hours!");
    557613}
     
    565621        exit();
    566622
     623    // we are still waiting for the first observation in the schedule
    567624    if (idxObs==-1)
    568625    {
     626        // flag that the first observation will be in the future
     627        run = -1;
    569628        v8.sleep(1000);
    570629        continue;
     
    575634
    576635    // Check if observation target has changed
    577     if (lastObs!=idxObs)
     636    if (lastObs!=idxObs) // !Object.isEqual(obs, nextObs)
    578637    {
    579638        console.out("--- "+idxObs+" ---");
    580         console.out("Current time:        "+new Date());
    581         console.out("Current observation: "+obs);
    582         console.out("Next    observation: "+nextObs);
     639        console.out("Current time:        "+new Date().toUTCString());
     640        console.out("Current observation: "+obs.start.toUTCString());
     641        if (nextObs!=undefined)
     642            console.out("Next    observation: "+nextObs.start.toUTCString());
    583643        console.out("");
    584644
     
    586646        // a scheduled 'START', so we have to check if the
    587647        // telescop is operational already
    588         if (run==-2)
     648        sub = 0;
     649        if (run<0)
    589650        {
    590651            Startup();   // -> Bias On/Off?, Lid open/closed?
     
    592653        }
    593654
     655        // The first observation had a start-time in the past...
     656        // In this particular case start with the last entry
     657        // in the list of measurements
     658        if (run==-2)
     659            sub = obs.length-1;
     660
    594661        run = 0;
    595662    }
    596663    lastObs = idxObs;
    597664
    598     // We have performed startup or shutdown... wait for next observation
    599     if (run==-1)
     665    if (nextObs==undefined && obs[obs.length-1].task!="SHUTDOWN")
     666        throw Error("Last scheduled measurement must be a shutdown.");
     667
     668    // We are done with all measurement slots for this
     669    // observation... wait for next observation
     670    if (sub>=obs.length)
    600671    {
    601672        v8.sleep(1000);
    602673        continue;
    603674    }
     675// add time
     676    console.out("\n"+(new Date()).toUTCString()+": Current measurement: "+obs[sub]);
    604677
    605678    // Check if obs.task is one of the one-time-tasks
    606     switch (obs.task)
     679    switch (obs[sub].task)
    607680    {
    608681    case "STARTUP":
     
    627700
    628701        console.out("  Waiting for first scheduled observation.","");
    629         run = -1;
     702        sub++;
    630703        continue;
    631704
     
    635708
    636709        console.out("  Waiting for next startup.", "");
    637         run = -1;
     710        sub++;
     711        continue;
     712
     713    case "IDLE":
     714        // FIXME: Checks missing whether a shutdown is needed...
     715        v8.sleep(1000);
    638716        continue;
    639717
     
    641719        console.out("  RATESCAN  ");
    642720
     721        if (dim.state("FTM_CONTROL").name=="TriggerOn")
     722        {
     723            dim.send("FTM_CONTROL/STOP_TRIGGER");
     724            dim.wait("FTM_CONTROL", "Idle", 3000);
     725        }
     726
    643727        dim.send("DRIVE_CONTROL/STOP");
    644728        dim.wait("DRIVE_CONTROL", "Armed", 5000);
    645729
     730        OpenLid();
     731
     732        service_feedback.voltageOn();
     733
    646734        if (obs.source != undefined)
    647             dim.send("DRIVE_CONTROL/TRACK_ON", obs.source);
     735            dim.send("DRIVE_CONTROL/TRACK_ON", obs[sub].source);
    648736        else
    649             dim.send("DRIVE_CONTROL/TRACK", obs.ra, obs.dec);
    650 
    651         //OpenLid();   
    652         dim.wait("DRIVE_CONTROL", "OnTrack", 300000);
    653 
    654         OpenLid();
    655 
    656         service_feedback.voltageOn();
     737            dim.send("DRIVE_CONTROL/TRACK", obs[sub].ra, obs[sub].dec);
     738
     739        dim.wait("DRIVE_CONTROL", "OnTrack", 150000); // 110s for turning and 30s for stabilizing
     740
    657741        service_feedback.waitForVoltageOn();
    658742
     
    702786
    703787        console.out("Ratescan done.");
    704         run = -1;
     788        sub++;
    705789        continue;
    706790    }
     791
     792    // ========================== case "DATA" ============================
    707793/*
    708794    if (Sun.horizon("FACT").isUp)
     
    715801*/
    716802    // Calculate remaining time for this observation in minutes
    717     var remaining = (nextObs.start-new Date())/60000;
     803    var remaining = nextObs==undefined ? 0 : (nextObs.start-new Date())/60000;
    718804
    719805    // ------------------------------------------------------------
     
    755841    }
    756842
    757     console.out("  Checking states: end.");
    758 
    759843    // ------------------------------------------------------------
    760844
    761     console.out("  Run #"+run+"  ("+parseInt(remaining)+"min)");
     845    console.out("  Run #"+run+"  (remaining "+parseInt(remaining)+"min)");
    762846
    763847    // ----- Time since last DRS Calibration [min] ------
    764     var runs = service_drs.get(0, false);
     848    var runs = service_drs.get(0);
    765849    var diff = (new Date()-runs.time)/60000;
    766850
    767851    // Warning: 'roi=300' is a number which is not intrisically fixed
    768852    //          but can change depending on the taste of the observers
    769     var valid = runs.data[1][2]>0 && runs.data[0]==300;
     853    var valid = runs.obj['run'][2]>0 && runs.obj['roi']==300;
    770854
    771855    if (valid)
     
    795879
    796880        //console.out("  Move telescope to '"+source+"' "+offset+" "+wobble);
    797         console.out("  Move telescope to '"+obs.source+"' ["+wobble+"]");
     881        console.out("  Move telescope to '"+obs[sub].source+"' ["+wobble+"]");
    798882
    799883        //var offset = observations[obs][2];
     
    802886        //dim.send("DRIVE_CONTROL/TRACK_SOURCE", offset, wobble, source);
    803887
    804         dim.send("DRIVE_CONTROL/TRACK_WOBBLE", wobble+1, obs.source);
     888        dim.send("DRIVE_CONTROL/TRACK_WOBBLE", wobble+1, obs[sub].source);
    805889
    806890        // Do we have to check if the telescope is really moving?
     
    856940    }
    857941
    858     //v8.sleep(360000);
    859942    console.out("  Taking data: done");
    860 
    861943    run++;
    862944}
Note: See TracChangeset for help on using the changeset viewer.