Index: trunk/FACT++/scripts/Main.js
===================================================================
--- trunk/FACT++/scripts/Main.js	(revision 17323)
+++ trunk/FACT++/scripts/Main.js	(revision 17324)
@@ -115,5 +115,5 @@
     }
 
-    dim.send("FAD_CONTROL/SET_FILE_FORMAT", 2);
+    dim.send("FAD_CONTROL/SET_FILE_FORMAT", 6);
 
     while (!irq && !takeRun("drs-pedestal", 1000));     // 40 / 20s     (50Hz)
@@ -127,5 +127,5 @@
     }
 
-    dim.send("FAD_CONTROL/SET_FILE_FORMAT", 2);
+    dim.send("FAD_CONTROL/SET_FILE_FORMAT", 6);
 
     while (!irq && !takeRun("pedestal",     1000));     // 40 / 10s     (80Hz)
@@ -222,7 +222,7 @@
 
 
-    this.ok = cnt==0;// || (this.last!=undefined && Math.abs(this.last-avg)<0.002);
-
-    console.out("  DeltaUov=%.3f (%.3f) [N(>0.033V)=%d]".$(avg, Math.abs(this.last-avg), cnt));
+    this.ok = cnt<3;// || (this.last!=undefined && Math.abs(this.last-avg)<0.002);
+
+    console.out("  DeltaUov=%.3f (%.3f) [N(>0.033V)=%d]".$(avg, avg-this.last, cnt));
 
     this.last = avg;
@@ -244,4 +244,10 @@
     {
         dim.log("Switching voltage off.");
+
+        if (dim.state("FTM_CONTROL").name=="TriggerOn")
+        {
+            dim.send("FTM_CONTROL/STOP_TRIGGER");
+            dim.wait("FTM_CONTROL", "Valid", 3000);
+        }
 
         // Supress the possibility that the bias control is
@@ -250,4 +256,7 @@
         dim.send("FEEDBACK/STOP");
         dim.wait("FEEDBACK", "Calibrated", 3000);
+
+        // Make sure we are not in Ramping anymore
+        dim.wait("BIAS_CONTROL", "VoltageOn", 3000);
 
         // Switch voltage off
@@ -285,23 +294,38 @@
 //      For convenience.
 
-service_feedback.voltageOn = function()
-{
-    //if (Sun.horizon("FACT").isUp)
-    //    throw new Error("Sun is above FACT-horizon, voltage cannot be switched on.");
-
-    var isOff = dim.state("BIAS_CONTROL").name=="VoltageOff";
+service_feedback.voltageOn = function(ov)
+{
+    if (isNaN(ov))
+        ov = 1.1;
+
+    if (this.ov!=ov && dim.state("FEEDBACK").name=="InProgress")
+    {
+        dim.log("Stoping feedback.");
+        if (dim.state("FTM_CONTROL").name=="TriggerOn")
+        {
+            dim.send("FTM_CONTROL/STOP_TRIGGER");
+            dim.wait("FTM_CONTROL", "Valid", 3000);
+        }
+
+        dim.send("FEEDBACK/STOP");
+        dim.wait("FEEDBACK", "Calibrated", 3000);
+
+        // Make sure we are not in Ramping anymore
+        dim.wait("BIAS_CONTROL", "VoltageOn", 3000);
+    }
+
+    var isOff = dim.state("FEEDBACK").name=="Calibrated";
     if (isOff)
     {
-        dim.log("Switching voltage on.");
-
-        dim.send("FEEDBACK/START", 1.1);
+        dim.log("Switching voltage to Uov="+ov+"V.");
+
+        dim.send("FEEDBACK/START", ov);
         dim.wait("FEEDBACK", "InProgress", 45000);
+
+        this.ov = ov;
     }
 
     // Wait until voltage on
     dim.wait("BIAS_CONTROL", "VoltageOn", 60000); // FIXME: 30000?
-
-//    if (isOff)
-//        this.ok = false;
 }
 
@@ -340,6 +364,9 @@
     var now1 = new Date();
 
-    service_feedback.voltageOff();
-    CloseLid(); 
+    var bias = dim.state("BIAS_CONTROL").name;
+    if (bias=="VoltageOn" || bias=="Ramping")
+        service_feedback.voltageOn(0);
+
+    CloseLid();
 
     var now2 = new Date();
@@ -369,4 +396,29 @@
 
     v8.timeout(150000, func);
+
+    /*
+    // Check if DRS calibration is necessary
+    var diff = getTimeSinceLastDrsCalib();
+    if (diff>30 || diff==null)
+    {
+        doDrsCalibration("singlepe");  // will turn voltage off
+        if (irq)
+            break;
+    }*/
+
+    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
+    service_feedback.voltageOff();
+
+    dim.log("Finishing shutdown.");
 
     var now3 = new Date();
@@ -600,5 +652,5 @@
 // Make sure we will write files
 // ----------------------------------------------------------------
-dim.send("FAD_CONTROL/SET_FILE_FORMAT", 2);
+dim.send("FAD_CONTROL/SET_FILE_FORMAT", 6);
 
 // ----------------------------------------------------------------
@@ -856,4 +908,49 @@
         break;
 
+    case "OVTEST":
+        dim.send("DRIVE_CONTROL/PARK");
+        dim.send("FEEDBACK/STOP");
+
+        // The lid must be closes
+        CloseLid();
+
+        console.out("Waiting for telescope to park. This may take a while.");
+        dim.wait("DRIVE_CONTROL", "Locked", 3000);
+
+        // Check if DRS calibration is necessary
+        var diff = getTimeSinceLastDrsCalib();
+        if (diff>30 || diff==null)
+        {
+            doDrsCalibration("ovtest");  // will turn voltage off
+            if (irq)
+                break;
+        }
+
+        // The voltage must be on
+        service_feedback.voltageOn(0.4);
+        service_feedback.waitForVoltageOn();
+
+        dim.log("Taking single p.e. run (0.4V)");
+        while (!irq && !takeRun("single-pe", 10000));
+
+        for (var i=5; i<18; i++)
+        {
+            dim.send("FEEDBACK/STOP");
+            dim.wait("FEEDBACK", "Calibrated", 3000);
+            dim.wait("BIAS_CONTROL", "VoltageOn", 3000);
+            dim.send("FEEDBACK/START", i*0.1);
+            dim.wait("FEEDBACK", "InProgress", 45000);
+            dim.wait("BIAS_CONTROL", "VoltageOn", 60000); // FIXME: 30000?
+            service_feedback.waitForVoltageOn();
+            dim.log("Taking single p.e. run ("+(i*0.1)+"V)");
+            while (!irq && !takeRun("single-pe", 10000));
+        }
+
+        // It is unclear what comes next, so we better switch off the voltage
+        service_feedback.voltageOff();
+        dim.log("Task finished [OVTEST]");
+        console.out("");
+        break;
+
     case "RATESCAN":
         var tm1 = new Date();
@@ -867,6 +964,6 @@
         OpenLid();
 
-        // The voltage must be switched on
-        service_feedback.voltageOn();
+        // Switch the voltage to a reduced level (Ubd)
+        service_feedback.voltageOn(0);
 
         if (obs.source != undefined)
@@ -883,4 +980,7 @@
         dim.wait("DRIVE_CONTROL", "OnTrack", 150000); // 110s for turning and 30s for stabilizing
 
+        // Now tracking stable, switch voltage to nominal level and wait
+        // for stability.
+        service_feedback.voltageOn();
         service_feedback.waitForVoltageOn();
 
@@ -901,5 +1001,5 @@
         // 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", 2);
+        //dim.send("FAD_CONTROL/SET_FILE_FORMAT", 6);
 
         dim.log("Ratescan done [%.1fs, %.1fs]".$((tm2-tm1)/1000, (new Date()-tm2)/1000));
@@ -946,4 +1046,7 @@
         if (point)
         {
+            // Switch the voltage to a reduced voltage level
+            service_feedback.voltageOn(0);
+
             // Change wobble position every four runs,
             // start with alternating wobble positions each day
@@ -983,15 +1086,10 @@
         OpenLid();
 
-
-        // voltage must be switched on after the lid is open for the
-        // feedback to adapt the voltage properly to the night-sky
-        // background light level.
-        service_feedback.voltageOn();
-
         // 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 we have to be prepared for data-taking:
-        // make sure voltage is on
+        // Now we are 'OnTrack', so we can ramp to nominal voltage
+        // and wait for the feedback to get stable
+        service_feedback.voltageOn();
         service_feedback.waitForVoltageOn();
 
