Index: trunk/FACT++/scripts/Main.js
===================================================================
--- trunk/FACT++/scripts/Main.js	(revision 15136)
+++ trunk/FACT++/scripts/Main.js	(revision 15137)
@@ -170,4 +170,7 @@
 }
 
+var startrun = new Subscription("FAD_CONTROL/START_RUN");
+startrun.get(5000);
+
 function takeRun(type, count, time)
 {
@@ -177,5 +180,6 @@
         time = -1;
 
-    console.out("  Take run: N="+count+" T="+time+"s ["+type+"]");
+    var nextrun = startrun.get().obj['next'];
+    console.out("  Take run %3d".$(nextrun)+": N="+count+" T="+time+"s ["+type+"]");
 
     dim.send("MCP/START", time?time:-1, count?count:-1, type);
@@ -221,5 +225,5 @@
     dim.wait("MCP", "Idle");
 
-    console.out("  Take run: end");
+    //console.out("  Take run: end");
 
     // DN: currently reconnect() never returns false 
@@ -235,5 +239,5 @@
 function doDrsCalibration()
 {
-    console.out("  DRS cal: start");
+    //console.out("  DRS cal: start");
     service_feedback.voltageOff();
 
@@ -270,5 +274,5 @@
     dim.wait("RATE_CONTROL", "Connected", 3000);
 
-    console.out("  DRS cal: end");
+    //console.out("  DRS cal: end");
 }
 
@@ -319,4 +323,28 @@
 
 var service_feedback = new Subscription("FEEDBACK/DEVIATION");
+
+service_feedback.onchange = function(evt)
+{
+    if (this.cnt && evt.counter>this.cnt+12)
+        return;
+
+    this.voltageStep = null;
+    if (!evt.obj)
+        return;
+
+    var delta = evt.obj['DeltaBias'];
+
+    var avg = 0;
+    for (var i=0; i<320; i++)
+        avg += delta[i];
+    avg /= 320;
+
+    if (this.previous)
+        this.voltageStep = Math.abs(avg-this.previous);
+
+    this.previous = avg;
+
+    console.out("  DeltaV="+this.voltageStep);
+}
 
 // DN:  Why is voltageOff() implemented as 
@@ -413,6 +441,9 @@
     if (isOff)
     {
+        console.out("  Voltage on: cnt="+this.cnt);
+
+        this.previous = undefined;
         this.cnt = this.get().counter;
-        console.out("  Voltage on: cnt="+this.cnt);
+        this.voltageStep = undefined;
     }
 }
@@ -433,5 +464,5 @@
 
     // Avoid output if condition is already fulfilled
-    if (this.cnt && this.get().counter>this.cnt+2)
+    if (this.cnt && this.get().counter>this.cnt+10)
         return;
 
@@ -440,8 +471,9 @@
 
     var now = new Date();
-    while (this.cnt==undefined || this.get().counter<=this.cnt+2)
+    //v8.timeout(90000, function() { if (!(this.cnt==undefined || this.get().counter<=this.cnt+2)) return true; });
+    while ((this.cnt==undefined || this.get().counter<=this.cnt+10) && (!this.voltageStep || this.voltageStep>0.02))
         v8.sleep();
 
-    console.out("  Feedback wait: end [cnt=%d, %.2fs]".$(this.get().counter, (new Date()-now)/1000));
+    console.out("  Feedback wait: end [dV=%.3f, cnt=%d, %.2fs]".$(this.voltageStep, this.get().counter, (new Date()-now)/1000));
 }
 
@@ -547,6 +579,7 @@
 
     // FIXME: This might not work is the drive is already close to park position
-    dim.wait("DRIVE_CONTROL", "Moving", 3000);
-    dim.wait("DRIVE_CONTROL", "Armed", 120000);
+    dim.wait("DRIVE_CONTROL", "Locked", 3000);
+    //dim.wait("DRIVE_CONTROL", "Moving", 3000);
+    //dim.wait("DRIVE_CONTROL", "Armed", 120000);
 
     // dim.wait("DRIVE_CONTROL", "Locked", 3000);
@@ -620,4 +653,6 @@
     if (idxObs===undefined)
         exit();
+
+    // FIXME: Check missing whether a shutdown is needed...
 
     // we are still waiting for the first observation in the schedule
@@ -684,6 +719,7 @@
         CloseLid();
 
-        console.out("  Take DRS calibration.");
+        console.out("  Take DRS calibration [startup]");
         doDrsCalibration();  // -> VoltageOff
+        console.out("  DRS calibration done.");
 
         service_feedback.voltageOn();
@@ -712,5 +748,4 @@
 
     case "IDLE":
-        // FIXME: Checks missing whether a shutdown is needed...
         v8.sleep(1000);
         continue;
@@ -876,5 +911,5 @@
         // Change wobble position every four runs,
         // start with alternating wobble positions each day
-        var wobble = (parseInt(run/4) + parseInt(new Date()/1000/3600/24-0.5))%2;
+        var wobble = (parseInt(run/4) + parseInt(new Date()/1000/3600/24-0.5))%2+1;
 
         //console.out("  Move telescope to '"+source+"' "+offset+" "+wobble);
@@ -886,5 +921,5 @@
         //dim.send("DRIVE_CONTROL/TRACK_SOURCE", offset, wobble, source);
 
-        dim.send("DRIVE_CONTROL/TRACK_WOBBLE", wobble+1, obs[sub].source);
+        dim.send("DRIVE_CONTROL/TRACK_WOBBLE", wobble, obs[sub].source);
 
         // Do we have to check if the telescope is really moving?
@@ -894,6 +929,7 @@
     if (drscal)
     {
-        console.out("  Take DRS calibration.");
+        console.out("  Take DRS calibration [interleaved]");
         doDrsCalibration();  // -> VoltageOff
+        console.out("  DRS calibration done.");
     }
 
