Index: /trunk/FACT++/scripts/Main.js
===================================================================
--- /trunk/FACT++/scripts/Main.js	(revision 15145)
+++ /trunk/FACT++/scripts/Main.js	(revision 15146)
@@ -29,5 +29,5 @@
 // ----------------------------------------------------------------
 
-dimctrl.defineState(37, "TimeOutBeforeTakingData", "MCP took more than 5minutes to start TakingData");
+//dimctrl.defineState(37, "TimeOutBeforeTakingData", "MCP took more than 5minutes to start TakingData");
 
 
@@ -185,5 +185,4 @@
     dim.send("MCP/START", time?time:-1, count?count:-1, type);
 
-    // What could be a reasonable timeout here?
     // FIXME: Replace by callback?
     //
@@ -216,12 +215,57 @@
     //  
 
-    if (!dim.wait("MCP", "TakingData", -300000) )
-    {
-        console.out("MCP took longer than 5 minutes to start TakingData");
-        console.out("maybe this idicates a problem with one of the FADs?");
-        dimctrl.setState(37);
-        dim.wait("MCP", "TakingData", 500);
-    }
-    dim.wait("MCP", "Idle");
+    // Use this if you use the rate control to calibrate by rates
+    //if (!dim.wait("MCP", "TakingData", -300000) )
+    //{
+    //    throw new Error("MCP took longer than 5 minutes to start TakingData"+
+    //                    "maybe this idicates a problem with one of the FADs?");
+    //}
+
+    // Here we could check and handle fad losses
+    try
+    {
+        dim.wait("MCP", "TakingData", 15000);
+    }
+    catch (e)
+    {
+        if (dim.state("MCP").name=="Configuring3" &&
+            dim.state("FAD_CONTROL").name=="Configuring2")
+        {
+            conssole.out("");
+            conssole.out("Waiting for fadctrl to get configured timed out... checking for FAD loss.");
+            conssole.out("");
+
+            var con  = service_con.get();
+            var stat = con.obj['status'];
+
+            var reset = [ ];
+
+            for (var i=0; i<40; i++)
+                if (stat[i]!=0x43)
+                {
+                    console.out("  FAD %2d".$(i)+" not in Configured state.");
+                    reset.push(i/10);
+                }
+
+            reset = reset.filter(function(elem,pos){return reset.indexOf(elem)==pos;});
+
+            if (reset.length>0)
+            {
+                console.out("");
+                console.out("  FADs belong to crates "+reset);
+            }
+        }
+
+        dim.send("MCP/RESET");
+
+        // If it works, we can decide here what to do...
+
+        throw e;
+    }
+
+    if (time>0)
+        dim.wait("MCP", "Idle", time*1250); // run time plus 25%
+    else
+        dim.wait("MCP", "Idle"); 
 
     //console.out("  Take run: end");
@@ -271,6 +315,6 @@
     }
 
-    dim.send("RATE_CONTROL/STOP"); // get out of GlobalThresholdSet
-    dim.wait("RATE_CONTROL", "Connected", 3000);
+    //dim.send("RATE_CONTROL/STOP"); // get out of GlobalThresholdSet
+    //dim.wait("RATE_CONTROL", "Connected", 3000);
 
     //console.out("  DRS cal: end");
@@ -470,6 +514,13 @@
     console.out("  Feedback wait: start");
 
+    function func(service)
+    {
+        if ((service.cnt!=undefined && service.get().counter>service.cnt+10) ||
+            (service.voltageStep && service.voltageStep<0.02))
+            return true;
+    }
+
     var now = new Date();
-    //v8.timeout(90000, function() { if (!(this.cnt==undefined || this.get().counter<=this.cnt+2)) return true; });
+    //v8.timeout(5*60000, func, this);
     while ((this.cnt==undefined || this.get().counter<=this.cnt+10) && (!this.voltageStep || this.voltageStep>0.02))
         v8.sleep();
@@ -601,9 +652,17 @@
     }
 
+    var now = new Date();
     v8.timeout(150000, func);
 
-    // FIXME: Add a check for the position here!
-
-    console.out("Shutdown: end");
+    var report = sub.get();
+
+    console.out("");
+    console.out("Shutdown procedure seems to be finished...");
+    console.out("  Telescope at Zd="+report.obj['Zd']+"deg Az="+report.obj['Az']+"deg");
+    console.out("  Please make sure the park position was reached");
+    console.out("  and the telescope is not moving anymore.");
+    console.out("  Please check that the lid is closed and the voltage switched off.");
+    console.out("");
+    console.out("Shutdown: end ["+(new Date()-now)/1000+"s]");
 }
 
@@ -708,5 +767,5 @@
         continue;
     }
-// add time
+
     console.out("\n"+(new Date()).toUTCString()+": Current measurement: "+obs[sub]);
 
@@ -732,6 +791,6 @@
         service_feedback.voltageOff();
 
-        dim.send("RATE_CONTROL/STOP"); // get out of GlobalThresholdSet
-        dim.wait("RATE_CONTROL", "Connected", 3000);
+        //dim.send("RATE_CONTROL/STOP"); // get out of GlobalThresholdSet
+        //dim.wait("RATE_CONTROL", "Connected", 3000);
 
         console.out("  Waiting for first scheduled observation.","");
@@ -751,13 +810,47 @@
         continue;
 
+    case "DRSCALIB":
+        console.out("  DRSCALIB", "");
+
+        console.out("  Take DRS calibration [drscalib]");
+        doDrsCalibration();  // -> VoltageOff
+        console.out("  DRS calibration done.");
+
+        //dim.send("RATE_CONTROL/STOP"); // get out of GlobalThresholdSet
+        //dim.wait("RATE_CONTROL", "Connected", 3000);
+
+        sub++;
+        continue;
+
+    case "SINGLEPE":
+
+        // FIXME: Check whether a DRS calib is necessary.
+
+        service_feedback.voltageOn();
+        service_feedback.waitForVoltageOn();
+
+        // Before we can switch to 3000 we have to make the right DRS calibration
+        console.out("  Take single p.e. run.");
+        while (!takeRun("pedestal", 5000));
+
+        service_feedback.voltageOff();
+
+        //dim.send("RATE_CONTROL/STOP"); // get out of GlobalThresholdSet
+        //dim.wait("RATE_CONTROL", "Connected", 3000);
+
+        sub++;
+        continue;
+
     case "RATESCAN":
         console.out("  RATESCAN  ");
-
+        /*
         if (dim.state("FTM_CONTROL").name=="TriggerOn")
         {
             dim.send("FTM_CONTROL/STOP_TRIGGER");
             dim.wait("FTM_CONTROL", "Idle", 3000);
-        }
-
+        }*/
+
+        // This is a workaround to make sure that we really catch
+        // the new state and not the old one
         dim.send("DRIVE_CONTROL/STOP");
         dim.wait("DRIVE_CONTROL", "Armed", 5000);
@@ -794,8 +887,8 @@
              [ "FEEDBACK",        [ "CurrentControl"           ] ],
              [ "RATE_SCAN",       [ "Connected"                ] ],
-             [ "RATE_CONTROL",    [ "Connected", "InProgress"  ] ],
+             [ "RATE_CONTROL",    [ "Connected", "GlobalThresholdSet", "InProgress"  ] ],
              [ "LID_CONTROL",     [ "Open"                     ] ],
              [ "DRIVE_CONTROL",   [ "Tracking", "OnTrack"      ] ],
-             [ "FTM_CONTROL",     [ "Idle",                    ] ],
+             [ "FTM_CONTROL",     [ "Idle", "TriggerOn"        ] ],
              [ "FAD_CONTROL",     [ "Connected", "WritingData" ] ],
             ];
@@ -811,5 +904,7 @@
         dim.send("RATE_SCAN/START_THRESHOLD_SCAN", 50, 1000, -10);
 
-        // lets wait if the Ratescan really starts .. it should be started after 10sec max.        
+        // 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);
@@ -818,5 +913,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", 2);
 
         console.out("Ratescan done.");
@@ -860,5 +955,5 @@
          [ "FEEDBACK",        [ "CurrentCtrlIdle", "CurrentControl"        ] ],
          [ "RATE_SCAN",       [ "Connected"                                ] ],
-         [ "RATE_CONTROL",    [ "Connected", "InProgress"                  ] ],
+         [ "RATE_CONTROL",    [ "Connected", "GlobalThresholdSet", "InProgress" ] ],
          [ "LID_CONTROL",     [ "Open", "Closed"                           ] ],
          [ "DRIVE_CONTROL",   [ "Armed", "Tracking", "OnTrack"             ] ],
