Changeset 16841


Ignore:
Timestamp:
06/14/13 11:15:17 (11 years ago)
Author:
tbretz
Message:
Added the possibility to interrupt the script peacefully; changed to the output; added an additonal  check for sun-rise if a long operation took place like the drs calib; added a STOP before Tracking position is changed (this is not ideal, but currently the only way to ensure that we can easily detect the change to OnTrack and are not confused by the current OnTrack)
File:
1 edited

Legend:

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

    r16798 r16841  
    9090// ================================================================
    9191
     92var irq;
     93
    9294function doDrsCalibration(where)
    9395{
     
    98100    var tm = new Date();
    99101
    100     while (1)
     102    while (!irq)
    101103    {
    102104        dim.send("FAD_CONTROL/START_DRS_CALIBRATION");
    103         if (!takeRun("drs-pedestal", 1000))     // 40 / 20s     (50Hz)
     105        if (irq || !takeRun("drs-pedestal", 1000))     // 40 / 20s     (50Hz)
    104106            continue;
    105107
    106         if (!takeRun("drs-gain",     1000))     // 40 / 20s     (50Hz)
     108        if (irq || !takeRun("drs-gain",     1000))     // 40 / 20s     (50Hz)
    107109            continue;
    108110
    109         if (!takeRun("drs-pedestal", 1000))     // 40 / 20s     (50Hz)
     111        if (irq || !takeRun("drs-pedestal", 1000))     // 40 / 20s     (50Hz)
    110112            continue;
    111113
     
    115117    dim.send("FAD_CONTROL/SET_FILE_FORMAT", 2);
    116118
    117     while (!takeRun("drs-pedestal", 1000));     // 40 / 20s     (50Hz)
    118     while (!takeRun("drs-time",     1000));     // 40 / 20s     (50Hz)
    119 
    120     while (1)
     119    while (!irq && !takeRun("drs-pedestal", 1000));     // 40 / 20s     (50Hz)
     120    while (!irq && !takeRun("drs-time",     1000));     // 40 / 20s     (50Hz)
     121
     122    while (!irq)
    121123    {
    122124        dim.send("FAD_CONTROL/RESET_SECONDARY_DRS_BASELINE");
    123         if (takeRun("pedestal",     1000))      // 40 / 10s     (80Hz)
     125        if (takeRun("pedestal",     1000))              // 40 / 10s     (80Hz)
    124126            break;
    125127    }
     
    127129    dim.send("FAD_CONTROL/SET_FILE_FORMAT", 2);
    128130
    129     while (!takeRun("pedestal",     1000));     // 40 / 10s     (80Hz)
    130 
    131         //                                       -----------
    132         //                                       4'40 / 2'00
    133 
    134     console.out("  "+new Date().toUTCString()+": DRS calibration done [%.1f]".$((new Date()-tm)/1000));
     131    while (!irq && !takeRun("pedestal",     1000));     // 40 / 10s     (80Hz)
     132    //                                                   -----------
     133    //                                                   4'40 / 2'00
     134
     135    if (irq)
     136        console.out("  "+new Date().toUTCString()+": DRS calibration interrupted [%.1fs]".$((new Date()-tm)/1000));
     137    else
     138        console.out("  "+new Date().toUTCString()+": DRS calibration done [%.1fs]".$((new Date()-tm)/1000));
    135139}
    136140
     
    348352    function func()
    349353    {
    350         if ((this.cnt!=undefined && this.get().counter>this.cnt+10) ||
     354        if (irq ||
     355            (this.cnt!=undefined && this.get().counter>this.cnt+10) ||
    351356            (this.voltageStep && this.voltageStep<0.02))
    352357            return true;
     
    357362    v8.timeout(4*60000, func, this);
    358363
    359     console.out("  "+new Date().toUTCString()+": Voltage stable within limits [dV=%.3f, cnt=%d, %.2fs]".$(this.voltageStep, this.get().counter, (new Date()-now)/1000));
     364    if (irq)
     365        console.out("  "+new Date().toUTCString()+": Waiting for stable voltage interrupted [dU=%.3fV, cnt=%d, %.2fs]".$(this.voltageStep, this.get().counter, (new Date()-now)/1000));
     366    else
     367        console.out("  "+new Date().toUTCString()+": Voltage stable within limits [dV=%.3f, cnt=%d, %.2fs]".$(this.voltageStep, this.get().counter, (new Date()-now)/1000));
    360368}
    361369
     
    592600
    593601    if (valid)
    594         console.out("  "+new Date().toUTCString()+": Last DRS calib: %.1fmin ago".$(diff));
     602        console.out("  "+new Date().toUTCString()+": Last DRS calibration was %.1fmin ago".$(diff));
    595603    else
    596         console.out("  "+new Date().toUTCString()+": No valid drs calibration available.");
     604        console.out("  "+new Date().toUTCString()+": No valid DRS calibration available.");
    597605
    598606    return valid ? diff : null;
    599607}
     608
     609// ----------------------------------------------------------------
     610// Install interrupt handler
     611// ----------------------------------------------------------------
     612function handleIrq(cmd, args, time, user)
     613{
     614    console.out("Interrupt received:");
     615    console.out("  IRQ:  "+cmd);
     616    console.out("  Time: "+time);
     617    console.out("  User: "+user);
     618
     619    irq = cmd ? cmd : "stop";
     620
     621    // This will end a run in progress as if it where correctly stopped
     622    if (dim.state("MCP").name=="TakingData")
     623        dim.send("MCP/STOP");
     624
     625    // This will stop a rate scan in progress
     626    if (dim.state("RATE_SCAN").name=="InProgress")
     627        dim.send("RATE_SCAN/STOP");
     628}
     629
     630dimctrl.setInterruptHandler(handleIrq);
    600631
    601632// ----------------------------------------------------------------
     
    613644    var n = new Date();
    614645    if (observations.length>0 && test==-1)
    615         console.out(n.toUTCString()+": First observation scheduled for "+observations[0].start.toUTCString());
     646        console.out("  "+n.toUTCString()+": First observation scheduled for "+observations[0].start.toUTCString());
    616647    if (test>=0 && test<observations.length)
    617         console.out(n.toUTCString()+": First observation should start immediately.");
     648        console.out("  "+n.toUTCString()+": First observation should start immediately.");
    618649    if (observations.length>0 && observations[0].start>n+12*3600*1000)
    619         console.out(n.toUTCString()+": No observations scheduled for the next 12 hours!");
     650        console.out("  "+n.toUTCString()+": No observations scheduled for the next 12 hours!");
    620651    if (observations.length==0)
    621         console.out(n.toUTCString()+": No observations scheduled!");
     652        console.out("  "+n.toUTCString()+": No observations scheduled!");
    622653}
    623654
     
    633664var system_on;  // undefined
    634665
    635 while (1)
     666while (!irq)
    636667{
    637668    // Check if observation position is still valid
     
    669700    if (lastId!=obs.id) // !Object.isEqual(obs, nextObs)
    670701    {
    671         console.out("--- "+obs.id+" ---");
    672         console.out("Current time:        "+new Date().toUTCString());
    673         console.out("Current observation: "+obs.start.toUTCString());
     702        console.out("");
     703        console.out("  "+new Date().toUTCString()+": Starting new observation [id="+obs.id+"]");
     704
     705        //console.out("--- "+obs.id+" ---");
     706        //console.out("Current time:        "+new Date().toUTCString());
     707        //console.out("Current observation: "+obs.start.toUTCString());
    674708        if (nextObs!=undefined)
    675             console.out("Next    observation: "+nextObs.start.toUTCString());
     709            console.out("  "+new Date().toUTCString()+": Next observation scheduled for "+nextObs.start.toUTCString());
    676710        console.out("");
    677711
     
    723757    }
    724758
    725     console.out("\n"+(new Date()).toUTCString()+": Current measurement: "+obs[sub]);
     759
     760    if (obs[sub].task!="IDLE" && obs[sub].task!="DATA")
     761        console.out("  "+new Date().toUTCString()+": New task ["+obs[sub]+"]");
    726762
    727763    // FIXME: Maybe print a warning if Drive is on during day time!
     
    772808    switch (obs[sub].task)
    773809    {
     810    case "IDLE":
     811        v8.sleep(5000);
     812        continue;
     813
    774814    case "STARTUP":
    775         console.out("  "+new Date().toUTCString()+": New task [STARTUP]", "");
     815        //console.out("", "  "+new Date().toUTCString()+": New task [STARTUP]");
    776816        CloseLid();
    777817
    778818        doDrsCalibration("startup");  // will switch the voltage off
     819
     820        if (irq)
     821            break;
    779822
    780823        service_feedback.voltageOn();
     
    783826        // Before we can switch to 3000 we have to make the right DRS calibration
    784827        console.out("  "+new Date().toUTCString()+": Taking single p.e. run.");
    785         while (!takeRun("pedestal", 5000));
     828        while (!irq && !takeRun("pedestal", 5000));
    786829
    787830        // It is unclear what comes next, so we better switch off the voltage
     
    792835
    793836    case "SHUTDOWN":
    794         console.out("  "+new Date().toUTCString()+": New task [SHUTDOWN]", "");
     837        //console.out("  "+new Date().toUTCString()+": New task [SHUTDOWN]", "");
    795838        Shutdown();
    796839        system_on = false;
     
    803846        continue;
    804847
    805     case "IDLE":
    806         v8.sleep(1000);
    807         continue;
    808 
    809848    case "DRSCALIB":
    810         console.out("  "+new Date().toUTCString()+": New task [DRSCALIB]", "");
     849        //console.out("  "+new Date().toUTCString()+": New task [DRSCALIB]", "");
    811850        doDrsCalibration("drscalib");  // will switch the voltage off
    812        console.out("  "+new Date().toUTCString()+": Task finished [DRSCALIB]", "");
     851        console.out("  "+new Date().toUTCString()+": Task finished [DRSCALIB]", "");
    813852        break;
    814853
    815854    case "SINGLEPE":
    816         console.out("  "+new Date().toUTCString()+": New task [SINGLE-PE]", "");
     855        //console.out("  "+new Date().toUTCString()+": New task [SINGLE-PE]", "");
    817856
    818857        // The lid must be closes
     
    822861        var diff = getTimeSinceLastDrsCalib();
    823862        if (diff>30 || diff==null)
     863        {
    824864            doDrsCalibration("singlepe");  // will turn voltage off
     865            if (irq)
     866                break;
     867        }
    825868
    826869        // The voltage must be on
     
    830873        // Before we can switch to 3000 we have to make the right DRS calibration
    831874        console.out("  "+new Date().toUTCString()+": Taking single p.e. run.");
    832         while (!takeRun("pedestal", 5000));
     875        while (!irq && !takeRun("pedestal", 5000));
    833876
    834877        // It is unclear what comes next, so we better switch off the voltage
    835878        service_feedback.voltageOff();
    836        console.out("  "+new Date().toUTCString()+": Task finished [SINGLE-PE]", "");
     879        console.out("  "+new Date().toUTCString()+": Task finished [SINGLE-PE]", "");
    837880        break;
    838881
    839882    case "RATESCAN":
    840         console.out("  "+new Date().toUTCString()+": New task [RATESCAN]", "");
     883        //console.out("  "+new Date().toUTCString()+": New task [RATESCAN]", "");
    841884
    842885        var tm1 = new Date();
    843886
    844887        // This is a workaround to make sure that we really catch
    845         // the new state and not the old one
     888        // the new OnTrack state later and not the old one
    846889        dim.send("DRIVE_CONTROL/STOP");
    847         dim.wait("DRIVE_CONTROL", "Armed", 5000);
     890        dim.wait("DRIVE_CONTROL", "Armed", 15000);
    848891
    849892        // The lid must be open
     
    907950        // ------------------------------------------------------------
    908951
    909         console.out("  "+new Date().toUTCString()+": Run #"+run+"  (remaining "+parseInt(remaining)+"min)");
     952        console.out("  "+new Date().toUTCString()+": Run count "+run+" [remaining "+parseInt(remaining)+"min]");
    910953
    911954        // ----- Time since last DRS Calibration [min] ------
     
    935978            console.out("  "+new Date().toUTCString()+": Moving telescope to '"+obs[sub].source+"' [wobble="+wobble+"]");
    936979
    937             //var offset = observations[obs][2];
    938             //var wobble = observations[obs][3 + parseInt(run/4)%2];
    939 
    940             //dim.send("DRIVE_CONTROL/TRACK_SOURCE", offset, wobble, source);
     980            // This is a workaround to make sure that we really catch
     981            // the new OnTrack state later and not the old one
     982            dim.send("DRIVE_CONTROL/STOP");
     983            dim.wait("DRIVE_CONTROL", "Armed", 15000);
    941984
    942985            dim.send("DRIVE_CONTROL/TRACK_WOBBLE", wobble, obs[sub].source);
     
    947990
    948991        if (drscal)
     992        {
    949993            doDrsCalibration("data");  // will turn voltage off
    950994
     995            // Now we switch on the voltage and a significant amount of
     996            // time has been passed, so do the check again.
     997            sun = Sun.horizon(-12);
     998            if (!was_up && sun.isUp)
     999            {
     1000                console.out("", "  "+new Date().toUTCString()+": Sun rise detected.... automatic shutdown initiated!");
     1001                // FIXME: State check?
     1002                Shutdown();
     1003                system_on = false;
     1004                continue;
     1005            }
     1006        }
     1007
     1008        if (irq)
     1009            break;
     1010
    9511011        OpenLid();
     1012
    9521013
    9531014        // voltage must be switched on after the lid is open for the
     
    9691030
    9701031            // Calibration (2% of 20')
    971             while (1)
     1032            while (!irq)
    9721033            {
    973                 if (!takeRun("pedestal",         1000))  // 80 Hz  -> 10s
     1034                if (irq || !takeRun("pedestal",         1000))  // 80 Hz  -> 10s
    9741035                    continue;
    975                 if (!takeRun("light-pulser-ext", 1000))  // 80 Hz  -> 10s
     1036                if (irq || !takeRun("light-pulser-ext", 1000))  // 80 Hz  -> 10s
    9761037                    continue;
    9771038                break;
     
    10011062
    10021063    if (nextObs!=undefined && sub==obs.length-1)
    1003         console.out("  "+new Date().toUTCString()+": Waiting for next observation scheduled at "+nextObs.start.toUTCString(),"");
     1064        console.out("  "+new Date().toUTCString()+": Next observation will start at "+nextObs.start.toUTCString(),"");
    10041065
    10051066    sub++;
     
    10071068
    10081069sub_drsruns.close();
     1070
     1071console.out("  "+new Date().toUTCString()+": Left main loop [irq="+irq+"]");
     1072
     1073if (irq.toUpperCase()=="SHUTDOWN")
     1074    Shutdown();
    10091075
    10101076// ================================================================
     
    10141080// error handline : http://www.sitepoint.com/exceptional-exception-handling-in-javascript/
    10151081// classes: http://www.phpied.com/3-ways-to-define-a-javascript-class/
    1016 //
    1017 // Arguments: TakeFirstDrsCalib
    1018 // To be determined: How to stop the script without foreceful interruption?
Note: See TracChangeset for help on using the changeset viewer.