Index: /trunk/FACT++/src/ratecontrol.cc
===================================================================
--- /trunk/FACT++/src/ratecontrol.cc	(revision 16444)
+++ /trunk/FACT++/src/ratecontrol.cc	(revision 16445)
@@ -48,4 +48,5 @@
     PixelMap fMap;
 
+    bool fPhysTriggerEnabled;
     bool fTriggerOn;
 
@@ -100,5 +101,5 @@
         //    return;
 
-        if (!fThresholds.empty())
+        if (fThresholds.empty())
             return;
 
@@ -360,6 +361,8 @@
 
         const FTM::DimStaticData &sdata = *static_cast<const FTM::DimStaticData*>(evt.GetData());
-        fTriggerOn = sdata.HasTrigger();
-
+        fPhysTriggerEnabled = sdata.HasTrigger();
+        fTriggerOn = (evt.GetQoS()&FTM::kFtmStates)==FTM::kFtmRunning;
+
+        Out() << "\n" << evt.GetTime() << ": " << (bool)fTriggerOn << " " << (bool)fPhysTriggerEnabled << endl;
         PrintThresholds(sdata);
 
@@ -371,4 +374,6 @@
     int HandleTriggerRates(const EventImp &evt)
     {
+        fTriggerOn = (evt.GetQoS()&FTM::kFtmStates)==FTM::kFtmRunning;
+
         if (fThresholds.empty())
             return GetCurrentState();
@@ -523,5 +528,5 @@
     int Calibrate()
     {
-        if (!fTriggerOn)
+        if (!fPhysTriggerEnabled)
         {
             Info("Physics trigger not enabled... CALIBRATE command ignored.");
@@ -551,5 +556,5 @@
     int CalibrateByCurrent()
     {
-        if (!fTriggerOn)
+        if (!fPhysTriggerEnabled)
         {
             Info("Physics trigger not enabled... CALIBRATE command ignored.");
@@ -596,4 +601,5 @@
             Info("No calibration requested.");
             return RateControl::State::kGlobalThresholdSet;
+            break;
 
         case 1:
@@ -615,4 +621,5 @@
     int StopRC()
     {
+        Info("Stop received.");
         return RateControl::State::kConnected;
     }
@@ -669,5 +676,7 @@
             return RateControl::State::kDisconnected;
 
-        const bool inprog = fTriggerOn && fDimRS.state()<RateScan::State::kConfiguring;
+        // Do not allow any action while a ratescan is configured or in progress
+        if (fDimRS.state()>=RateScan::State::kConfiguring)
+            return RateControl::State::kConnected;
 
         switch (GetCurrentState())
@@ -677,10 +686,18 @@
 
         case RateControl::State::kGlobalThresholdSet:
-            if (!inprog)
-                return RateControl::State::kGlobalThresholdSet;
+
+            // Wait for the trigger to get switched on before starting control loop
+            if (fTriggerOn && fPhysTriggerEnabled)
+                return RateControl::State::kInProgress;
+
+            return RateControl::State::kGlobalThresholdSet;
 
         case RateControl::State::kInProgress:
-            if (inprog)
-                return RateControl::State::kInProgress;
+
+            // Go back to connected when the trigger has been switched off
+            if (!fTriggerOn || !fPhysTriggerEnabled)
+                return RateControl::State::kConnected;
+
+            return RateControl::State::kInProgress;
         }
 
@@ -690,5 +707,5 @@
 public:
     StateMachineRateControl(ostream &out=cout) : StateMachineDim(out, "RATE_CONTROL"),
-        fTriggerOn(false), fBlock(40),
+        fPhysTriggerEnabled(false), fTriggerOn(false), fBlock(40),
         fDimFTM("FTM_CONTROL"),
         fDimRS("RATE_SCAN"),
@@ -748,5 +765,5 @@
         AddEvent("CALIBRATE_RUN", "C")
             (bind(&StateMachineRateControl::CalibrateRun, this, placeholders::_1))
-            ("Start a threshold calibration as defined in the setup for this run-type");
+            ("Start a threshold calibration as defined in the setup for this run-type, state change to InProgress is delayed until trigger enabled");
 
         AddEvent("STOP", RateControl::State::kSettingGlobalThreshold, RateControl::State::kGlobalThresholdSet, RateControl::State::kInProgress)
