Index: /trunk/FACT++/scripts/CheckStates.js
===================================================================
--- /trunk/FACT++/scripts/CheckStates.js	(revision 17921)
+++ /trunk/FACT++/scripts/CheckStates.js	(revision 17922)
@@ -40,5 +40,5 @@
  *
  */
-function checkStates(table, timeout)
+function checkStates(table, timeout, wait)
 {
     if (timeout===undefined)
@@ -69,9 +69,14 @@
                     continue;
 
-                dim.log(table[i][0]+" in ["+states[i]+"] not as it ought to be ["+table[i][1]+"]");
+                if (!wait)
+                    dim.log(table[i][0]+" in ["+states[i]+"] not as it ought to be ["+table[i][1]+"]");
+
                 rc = false;
             }
-
-            return rc;
+            if (rc)
+                return rc;
+
+            if (!wait)
+                return false;
         }
 
Index: /trunk/FACT++/scripts/Main.js
===================================================================
--- /trunk/FACT++/scripts/Main.js	(revision 17921)
+++ /trunk/FACT++/scripts/Main.js	(revision 17922)
@@ -195,4 +195,18 @@
 
 // ================================================================
+//  Interrupt data taking in case of high currents
+// ================================================================
+dim.onchange['FEEDBACK'] = function(state)
+{
+    if ((state.name=="Critical" || state.name=="OnStandby") &&
+        (this.prev!="Critical"  && this.prev!="OnStandby"))
+    {
+        console.out("Feedback state changed from "+this.prev+" to "+state.name+" [Main.js]");
+        irq = "RESCHEDULE";
+    }
+    this.prev=state.name;
+}
+
+// ================================================================
 //  Code related to switching bias voltage on and off
 // ================================================================
@@ -203,4 +217,7 @@
 {
     if (!evt.data)
+        return;
+
+    if (this.ok==undefined)
         return;
 
@@ -223,8 +240,4 @@
     }
     avg /= 320;
-
-    if (this.ok==undefined)
-        return;
-
 
     this.ok = cnt<3;// || (this.last!=undefined && Math.abs(this.last-avg)<0.002);
@@ -306,5 +319,5 @@
         ov = 1.1;
 
-    if (this.ov!=ov && dim.state("FEEDBACK").name=="InProgress")
+    if (this.ov!=ov && dim.state("FEEDBACK").name=="InProgress") // FIXME: Warning, OnStandby, Critical if (ov<this.ov)
     {
         dim.log("Stoping feedback.");
@@ -328,4 +341,7 @@
 
         dim.send("FEEDBACK/START", ov);
+
+        // FIXME: We could miss "InProgress" if it immediately changes to "Warning"
+        //        Maybe a dim.timeout state>8 ?
         dim.wait("FEEDBACK", "InProgress", 45000);
 
@@ -365,5 +381,5 @@
 // ================================================================
 
-function Shutdown()
+function Shutdown(singlepe)
 {
     dim.log("Starting shutdown.");
@@ -419,13 +435,16 @@
     }*/
 
-    dim.log("Taking single-pe run.");
-
-    // The voltage must be on
-    service_feedback.voltageOn();
-    service_feedback.waitForVoltageOn();
-
-    // Before we can switch to 3000 we have to make the right DRS calibration
-    dim.log("Taking single p.e. run.");
-    while (!irq && !takeRun("single-pe", 10000));
+    if (singlepe)
+    {
+        dim.log("Taking single-pe run.");
+
+        // The voltage must be on
+        service_feedback.voltageOn();
+        service_feedback.waitForVoltageOn();
+
+        // Before we can switch to 3000 we have to make the right DRS calibration
+        dim.log("Taking single p.e. run.");
+        while (!irq && !takeRun("single-pe", 10000));
+    }
 
     // It is unclear what comes next, so we better switch off the voltage
@@ -528,4 +547,5 @@
          "FTM_CONTROL/TRIGGER_RATES",
          "GPS_CONTROL/NEMA",
+         "SQM_CONTROL/DATA",
          "LID_CONTROL/DATA",
          "MAGIC_LIDAR/DATA",
@@ -554,5 +574,5 @@
 
     var list = obj.data.split('\n').map(map);
-
+//     [A
     function check(name)
     {
@@ -613,6 +633,4 @@
 //console.out("Feedback init: start.");
 service_feedback.get(5000);
-
-//v8.timeout(3000, function() { var n = dim.state("FEEDBACK").name; if (n=="CurrentCtrlIdle" || n=="CurrentControl") return true; });
 
 // ----------------------------------------------------------------
@@ -708,5 +726,8 @@
 
     if (irq.toUpperCase()=="RESCHEDULE")
-        return true;
+    {
+        irq = undefined;
+        return false;
+    }
 
     if (irq.toUpperCase()=="OFF")
@@ -716,4 +737,12 @@
         return true;
     }
+
+    /*
+    if (irq.toUpperCase()=="STOP")
+    {
+        dim.send("FAD_CONTROL/CLOSE_OPEN_FILES");
+        dim.send("MCP/STOP");
+        return true;
+    }*/
 
     if (irq.toUpperCase()=="SHUTDOWN")
@@ -817,5 +846,5 @@
     }
 
-    // Check if sun is still up... only DATA and RATESCAN must be suppressed
+    // Check if sun is still up... only DATA and */
     if ((obs[sub].task=="DATA" || obs[sub].task=="RATESCAN") && sun.isUp)
     {
@@ -836,5 +865,5 @@
     // otherwise it is difficult to allow e.g. the STARTUP at the beginning of the night
     var power_states = sun.isUp || !system_on ? [ "DriveOff", "SystemOn" ] : [ "SystemOn" ];
-    var drive_states = sun.isUp || !system_on ?   undefined    : [ "Armed", "Tracking", "OnTrack" ];
+    var drive_states = sun.isUp || !system_on ? undefined : [ "Armed", "Tracking", "OnTrack" ];
 
     // A scheduled task was found, lets check if all servers are
@@ -858,5 +887,5 @@
          [ "AGILENT_CONTROL_80V", [ "VoltageOn"                ] ],
          [ "BIAS_CONTROL",        [ "VoltageOff", "VoltageOn", "Ramping" ] ],
-         [ "FEEDBACK",            [ "Calibrated", "InProgress" ] ],
+         [ "FEEDBACK",            [ "Calibrated", "InProgress", "OnStandby", "Warning", "Critical" ] ],
          [ "LID_CONTROL",         [ "Open", "Closed"           ] ],
          [ "DRIVE_CONTROL",       drive_states/*[ "Armed", "Tracking", "OnTrack" ]*/ ],
@@ -866,4 +895,5 @@
          [ "RATE_CONTROL",        [ "Connected", "GlobalThresholdSet", "InProgress"  ] ],
          [ "GPS_CONTROL",         [ "Locked"  ] ],
+         [ "SQM_CONTROL",         [ "Valid"  ] ],
         ];
 
@@ -872,9 +902,23 @@
     {
         throw new Error("Something unexpected has happened. One of the servers "+
-                        "is in a state in which it should not be. Please,"+
+                        "is in a state in which it should not be. Please,"+ 
                         "try to find out what happened...");
     }
 
-    datalogger_subscriptions.check();
+    datalogger_subscriptions.check();                                         
+                                                                                
+    // If this is an observation which needs the voltage to be swicthed on
+    // skip that if the voltage is not stable                                    
+    /*
+    if (obs[sub].task=="DATA" || obs[sub].task=="RATESCAN")
+    {
+        var state = dim.state("FEEDBACK").name;
+        if (state=="Warning" || state=="Critical" || state=="OnStandby")
+        {
+            v8.sleep(1000);
+            continue;
+        }
+    }*/
+
 
     // Check if obs.task is one of the one-time-tasks
@@ -909,5 +953,5 @@
 
     case "SHUTDOWN":
-        Shutdown();
+        Shutdown(true);
         system_on = false;
 
@@ -1037,23 +1081,31 @@
         service_feedback.waitForVoltageOn();
 
-        var tm2 = new Date();
-
-        dim.log("Starting ratescan.");
-
-        // Start rate scan
-        dim.send("RATE_SCAN/START_THRESHOLD_SCAN", 50, 1000, -10);
-
-        // Lets wait if the ratescan really starts... this might take a few
-        // seconds because RATE_SCAN configures the ftm and is waiting for
-        // it to be configured.
-        dim.wait("RATE_SCAN", "InProgress", 10000);
-        dim.wait("RATE_SCAN", "Connected", 2700000);
-
-        // this line is actually some kind of hack. 
-        // after the Ratescan, no data is written to disk. I don't know why, but it happens all the time
-        // So I decided to put this line here as a kind of patchwork....
-        //dim.send("FAD_CONTROL/SET_FILE_FORMAT", 6);
-
-        dim.log("Ratescan done [%.1fs, %.1fs]".$((tm2-tm1)/1000, (new Date()-tm2)/1000));
+        if (!irq)
+        {
+            var tm2 = new Date();
+
+            dim.log("Starting ratescan.");
+
+            // Start rate scan
+            dim.send("RATE_SCAN/START_THRESHOLD_SCAN", 50, 1000, -10);
+
+            // Lets wait if the ratescan really starts... this might take a few
+            // seconds because RATE_SCAN configures the ftm and is waiting for
+            // it to be configured.
+            dim.wait("RATE_SCAN", "InProgress", 10000);
+            dim.wait("RATE_SCAN", "Connected", 2700000);
+
+            // Here one could implement a watchdog for the feedback as well, but what is the difference
+            // whether finally one has to find out if the feedback was in the correct state
+            // or the ratescan was interrupted?
+
+            // this line is actually some kind of hack.
+            // after the Ratescan, no data is written to disk. I don't know why, but it happens all the time
+            // So I decided to put this line here as a kind of patchwork....
+            //dim.send("FAD_CONTROL/SET_FILE_FORMAT", 6);
+
+            dim.log("Ratescan done [%.1fs, %.1fs]".$((tm2-tm1)/1000, (new Date()-tm2)/1000));
+        }
+
         dim.log("Task finished [RATESCAN]");
         console.out("");
@@ -1072,4 +1124,5 @@
         }
     */
+
         // Calculate remaining time for this observation in minutes
         var remaining = nextObs==undefined ? 0 : (nextObs.start-new Date())/60000;
@@ -1134,5 +1187,5 @@
 
         if (irq)
-            break;
+            continue;
 
         OpenLid();
@@ -1140,4 +1193,12 @@
         // This is now th right time to wait for th drive to be stable
         dim.wait("DRIVE_CONTROL", "OnTrack", 150000); // 110s for turning and 30s for stabilizing
+
+        // Now check the voltage... (do not start a lot of stuff just to do nothing)
+        var state = dim.state("FEEDBACK").name;
+        if (state=="Warning" || state=="Critical" || state=="OnStandby")
+        {
+            v8.sleep(60000);
+            continue;
+        }
 
         // Now we are 'OnTrack', so we can ramp to nominal voltage
@@ -1147,5 +1208,5 @@
 
         // If pointing had changed, do calibration
-        if (point)
+        if (!irq && point)
         {
             dim.log("Starting calibration.");
@@ -1177,5 +1238,5 @@
         {
             var len = 300;
-            while (len>15)
+            while (!irq && len>15)
             {
                 var time = new Date();
Index: /trunk/FACT++/scripts/Startup.js
===================================================================
--- /trunk/FACT++/scripts/Startup.js	(revision 17921)
+++ /trunk/FACT++/scripts/Startup.js	(revision 17922)
@@ -96,5 +96,5 @@
 loop.add(handleFeedbackConnected); // Feedback needs FAD to be Connected
 loop.add(handleRatectrlConnected);
-//loop.add(handleLidClosed);
+loop.add(handleLidClosed);
 loop.run();
 
@@ -194,5 +194,5 @@
 if ((dim.state("PWR_CONTROL").index&16)==0)
 {
-    console.out("Drive cabinet not power... Switching on.");
+    console.out("Drive cabinet not powered... Switching on.");
     dim.send("PWR_CONTROL/TOGGLE_DRIVE");
     v8.timeout(5000, function() { if (dim.state("PWR_CONTROL").index&16) return true; });
@@ -290,5 +290,5 @@
 // ================================================================
 
-// FIXME: Check if there is a startup shceduled, if not do not force
+// FIXME: Check if there is a startup scheduled, if not do not force
 // drive to be switched on
 
@@ -300,4 +300,5 @@
  [ "SMART_FACT"    ],
  [ "TEMPERATURE"   ],
+ [ "EVENT_SERVER",        [ "Running", "Standby" ] ],
  [ "DATA_LOGGER",         [ "NightlyFileOpen", "WaitForRun", "Logging" ] ],
  [ "FSC_CONTROL",         [ "Connected"                       ] ],
@@ -312,10 +313,13 @@
  [ "RATE_SCAN",           [ "Connected"                       ] ],
  [ "RATE_CONTROL",        [ "Connected"                       ] ],
+ [ "DRIVE_CONTROL",       [ "Armed", "Tracking", "OnTrack", "Locked" ] ],
  [ "LID_CONTROL",         [ "Open", "Closed"                  ] ],
- [ "DRIVE_CONTROL",       [ "Armed", "Tracking", "OnTrack", "Locked" ] ],
  [ "FTM_CONTROL",         [ "Valid", "TriggerOn"              ] ],
  [ "FAD_CONTROL",         [ "Connected", "WritingData"        ] ],
  [ "GPS_CONTROL",         [ "Locked" ] ],
+ [ "SQM_CONTROL",         [ "Valid" ] ],
 ];
+
+
 
 if (!checkStates(table))
Index: /trunk/FACT++/scripts/takeRun.js
===================================================================
--- /trunk/FACT++/scripts/takeRun.js	(revision 17921)
+++ /trunk/FACT++/scripts/takeRun.js	(revision 17922)
@@ -173,4 +173,28 @@
     //}
 
+    // ================================================================
+    //  Function for Critical voltage
+    // ================================================================
+
+    // INSTALL a watchdog... send FAD_CONTROL/CLOSE_OPEN_FILES
+    // could send MCP/RESET as well but would result in a timeout
+    var callback = dim.onchange['FEEDBACK'];
+    dim.onchange['FEEDBACK'] = function(state)
+    {
+        if (callback)
+            callback.call(this, state);
+
+        if ((state.name=="Critical" || state.name=="OnStandby") &&
+            (this.last!="Critical"  && this.last!="OnStandby"))
+        {
+            console.out("Feedback state changed from "+this.last+" to "+state.name+" [takeRun.js]");
+
+            // Includes FAD_CONTROL/CLOSE_ALL_OPEN_FILES
+            dim.send("MCP/STOP");
+        }
+
+        this.last=state.name;
+    }
+
     // Here we could check and handle fad losses
 
@@ -241,5 +265,22 @@
     }
 
+    // This is to check if we have missed the event. This can happen as
+    // a race condition when the MCP/STOP is sent by the event handler
+    // but the run was not yet fully configured.
+    var statefb = dim.state("FEEDBACK").name;
+    if (statefb=="Critical" || statefb=="OnStandby")
+    {
+        console.out("Run started by FEEDBACK in state "+statefb);
+        dim.send("MCP/STOP"); // Includes FAD_CONTROL/CLOSE_ALL_OPEN_FILES
+
+        dim.onchange['FEEDBACK'] = callback;
+
+        return true;
+    }
+
     dim.wait("MCP", "Idle", time>0 ? time*1250 : undefined); // run time plus 25%
+
+    // REMOVE watchdog
+    dim.onchange['FEEDBACK'] = callback;
 
     if (incomplete)
