Index: /trunk/FACT++/scripts/Main.js
===================================================================
--- /trunk/FACT++/scripts/Main.js	(revision 15361)
+++ /trunk/FACT++/scripts/Main.js	(revision 15362)
@@ -133,5 +133,5 @@
     incomplete = inc;
 
-    console.out("Sending MCP/STOP");
+    console.out("", "Incomplete event detected, sending MCP/STOP");
     dim.send("MCP/STOP");
 }
@@ -241,6 +241,6 @@
     }
 
-    console.out("   ...waiting for 5s");
-    v8.sleep(5000);
+    console.out("   ...waiting for 3s");
+    v8.sleep(3000);
 
     for (var i=0; i<list.length; i++)
@@ -266,4 +266,5 @@
 
     incomplete = 0;
+
     dim.send("MCP/START", time?time:-1, count?count:-1, type);
 
@@ -337,4 +338,5 @@
 
         /*** FOR REMOVE ***/
+        /*
         var reset = [ ];
 
@@ -353,5 +355,5 @@
             console.out("  FADs belong to crate(s): "+reset);
             console.out("");
-        }
+        }*/
         /**** FOR REMOVE ****/
 
@@ -363,5 +365,5 @@
         reconnect(list, "configuration");
 
-        throw e;
+        return false;
     }
 
@@ -370,11 +372,8 @@
     if (incomplete)
     {
-        console.out("Incomplete: "+incomplete);
-
         console.out("");
         console.out("MCP:         "+dim.state("MCP").name);
         console.out("FAD_CONTROL: "+dim.state("FAD_CONTROL").name);
         console.out("FTM_CONTROL: "+dim.state("FTM_CONTROL").name);
-        console.out("");
 
         dim.wait("MCP",         "Idle", 3000);
@@ -392,5 +391,7 @@
         reconnect(list, "data taking");
 
-        throw new Error("In-run FAD loss detected.");
+        return false;
+
+        //throw new Error("In-run FAD loss detected.");
     }
 
@@ -421,36 +422,31 @@
             continue;
 
-        // Does that fix the runopen before runclose problem?
-        //dim.wait("FAD_CONTROL", "Connected", 3000);
-        //v8.sleep(1000);
-
         if (!takeRun("drs-gain",     1000))     // 40 / 20s     (50Hz)
             continue;
 
-        // Does that fix the runopen before runclose problem?
-        //dim.wait("FAD_CONTROL", "Connected", 3000);
-        //v8.sleep(1000);
-
         if (!takeRun("drs-pedestal", 1000))     // 40 / 20s     (50Hz)
             continue;
 
-        dim.send("FAD_CONTROL/SET_FILE_FORMAT", 2);
-        if (!takeRun("drs-pedestal", 1000))     // 40 / 20s     (50Hz)
-            continue;
-        if (!takeRun("drs-time",     1000))     // 40 / 20s     (50Hz)
-            continue;
-
+        break;
+    }
+
+    dim.send("FAD_CONTROL/SET_FILE_FORMAT", 2);
+
+    while (!takeRun("drs-pedestal", 1000));     // 40 / 20s     (50Hz)
+    while (!takeRun("drs-time",     1000));     // 40 / 20s     (50Hz)
+
+    while (1)
+    {
         dim.send("FAD_CONTROL/RESET_SECONDARY_DRS_BASELINE");
-        if (!takeRun("pedestal",     1000))     // 40 / 10s     (80Hz)
-            continue;
-
-        dim.send("FAD_CONTROL/SET_FILE_FORMAT", 2);
-        if (!takeRun("pedestal",     1000))     // 40 / 10s     (80Hz)
-            continue;
+        if (takeRun("pedestal",     1000))      // 40 / 10s     (80Hz)
+            break;
+    }
+
+    dim.send("FAD_CONTROL/SET_FILE_FORMAT", 2);
+
+    while (!takeRun("pedestal",     1000));     // 40 / 10s     (80Hz)
+
         //                                       -----------
         //                                       4'40 / 2'00
-
-        break;
-    }
 
     console.out("  DRS calibration done [%.1f]".$((new Date()-tm)/1000));
@@ -712,7 +708,9 @@
     dim.send("FEEDBACK/ENABLE_OUTPUT", false);
     dim.send("FTM_CONTROL/STOP_TRIGGER");
-
-    dim.wait("FEEDBACK", "CurrentCtrlIdle", 3000);
-    dim.wait("FTM_CONTROL", "Idle", 3000);
+    dim.send("BIAS_CONTROL/DISCONNECT");
+
+    dim.wait("FEEDBACK",     "CurrentCtrlIdle",  3000);
+    dim.wait("FTM_CONTROL",  "Idle",             3000);
+    dim.wait("BIAS_CONTROL", "Disconnected",     3000);
 
     var report = sub.get();
@@ -911,5 +909,5 @@
 var sub;
 var lastId;
-var sun = Sun.horizon(-13);
+var sun = Sun.horizon(-12);
 var system_on;  // undefined
 
@@ -933,5 +931,5 @@
     // Check if we have to take action do to sun-rise
     var was_up = sun.isUp;
-    sun = Sun.horizon(-13);
+    sun = Sun.horizon(-12);
     if (!was_up && sun.isUp)
     {
@@ -1006,5 +1004,9 @@
     console.out("\n"+(new Date()).toUTCString()+": Current measurement: "+obs[sub]);
 
-    var power_states = sun.isUp || system_on===false ? [ "DriveOff" ] : [ "SystemOn" ];
+    // FIXME: Maybe print a warning if Drive is on during day time!
+
+    // It is not ideal that we allow the drive to be on during day time, but
+    // otherwise it is difficult to allow e.g. the STARTUP at the beginning of the night
+    var power_states = sun.isUp || system_on===false ? [ "DriveOff", "SystemOn" ] : [ "SystemOn" ];
     var drive_states = sun.isUp || system_on===false ?   undefined    : [ "Armed", "Tracking", "OnTrack" ];
 
@@ -1039,5 +1041,5 @@
     if (!checkStates(table))
     {
-        throw new Error("Something unexpected has happened. One of the servers"+
+        throw new Error("Something unexpected has happened. One of the servers "+
                         "is in a state in which it should not be. Please,"+
                         "try to find out what happened...");
