Index: trunk/FACT++/src/feedback.cc
===================================================================
--- trunk/FACT++/src/feedback.cc	(revision 12066)
+++ trunk/FACT++/src/feedback.cc	(revision 12067)
@@ -67,4 +67,5 @@
     DimStampedInfo fFAD;
     DimStampedInfo fBias;
+
     DimStampedInfo *fBiasData;
 
@@ -82,8 +83,11 @@
     valarray<double> fSP;     // Set point        (target amplitudes)
 
-    double fT;
-    double fKp;               // Proportional constant
-    double fKi;               // Integral     constant
-    double fKd;               // Derivative   constant
+    double fKp;    // Proportional constant
+    double fKi;    // Integral     constant
+    double fKd;    // Derivative   constant
+    double fT;     // Time         constant (cycle time)
+    double fGain;  // Gain (conversion from a DRS voltage deviation into a BIAS voltage change at G-APD reference voltage)
+
+    double fT21;
 
     bool fOutputEnabled;
@@ -253,7 +257,7 @@
                 const Time tm0 = Time();
 
-                /*const*/ double T21 = (tm0-fStartTime).total_microseconds()/1000000.;
-                const double T10 = fT;
-                fT = T21;
+                const double T21 = fT>0 ? fT : (tm0-fStartTime).total_microseconds()/1000000.;
+                const double T10 = fT21;
+                fT21 = T21;
 
                 fStartTime = tm0;
@@ -310,16 +314,13 @@
                             // => Kd = 0.1  * gain/20s = 0.00003
 
-                            fKp = 0;
-                            fKd = 0;
-                            fKi = 0.00003*20;
-                            T21 = 1;
-
                             //valarray<double> correction = - Kp*(PV[2] - PV[1]) + Ki * dT * (SP-PV[2]) - Kd/dT * (PV[2] - 2*PV[1] + PV[0]);
                             //valarray<double> correction =
                             //    - Kp*(PV[2] - PV[1]) + Ki * T21 * (SP-PV[2]) - Kd*(PV[2]-PV[1])/T21 - Kd*(PV[0]-PV[1])/T01;
-                            const valarray<double> correction =
-                                - (fKp+fKd/T21)*(fPV[2] - fPV[1])
-                                +  fKi*T21*(fSP-fPV[2])
-                                +  fKd/T10*(fPV[1]-fPV[0]);
+                            const valarray<double> correction = fGain/1000*
+                                (
+                                 - (fKp+fKd/T21)*(fPV[2] - fPV[1])
+                                 +  fKi*T21*(fSP-fPV[2])
+                                 +  fKd/T10*(fPV[1]-fPV[0])
+                                );
 
                             vector<float> vec(2*BIAS::kNumChannels);
@@ -336,5 +337,5 @@
                                 Info("Sending correction to feedback.");
 
-                                dic_cmnd_service("BIAS_CONTROL/ADD_REFERENCE_VOLTAGES",
+                                dic_cmnd_service((char*)"BIAS_CONTROL/ADD_REFERENCE_VOLTAGES",
                                                  (void*)(vec.data()+416), 416*sizeof(float));
 
@@ -432,7 +433,9 @@
         switch (constant)
         {
-        case 0: fKi = evt.GetDouble(); break;
-        case 1: fKp = evt.GetDouble(); break;
-        case 2: fKd = evt.GetDouble(); break;
+        case 0: fKi   = evt.GetDouble(); break;
+        case 1: fKp   = evt.GetDouble(); break;
+        case 2: fKd   = evt.GetDouble(); break;
+        case 3: fT    = evt.GetDouble(); break;
+        case 4: fGain = evt.GetDouble(); break;
         default:
             Fatal("SetConstant got an unexpected constant id -- this is a program bug!");
@@ -526,7 +529,8 @@
         fFAD("FAD_CONTROL/STATE",       (void*)NULL, 0, this),
         fBias("BIAS_CONTROL/STATE",     (void*)NULL, 0, this),
+        fBiasData(0),
         fDimReference("FEEDBACK/REFERENCE", "F:416",        ""),
         fDimDeviation("FEEDBACK/DEVIATION", "F:416;F:416",  ""),
-        fBiasData(NULL), fKp(0), fKi(0), fKd(0), fOutputEnabled(false)
+        fKp(0), fKi(0), fKd(0), fT(-1), fOutputEnabled(false)
     {
         // ba::io_service::work is a kind of keep_alive for the loop.
@@ -555,17 +559,17 @@
         AddEvent("START")//, kStateIdle)
             (bind(&StateMachineFeedback::StartFeedback, this))
-            ("");
+            ("Start control loop");
 
         AddEvent("STOP")//, kStateIdle)
             (bind(&StateMachineFeedback::StopFeedback, this))
-            ("");
+            ("Stop control loop");
 
         AddEvent("ENABLE_OUTPUT", "B:1")//, kStateIdle)
             (bind(&StateMachineFeedback::EnableOutput, this, placeholders::_1))
-            ("");
+            ("Enable sending of correction values caluclated by the control loop to the biasctrl");
 
         AddEvent("STORE_REFERENCE")//, kStateIdle)
             (bind(&StateMachineFeedback::StoreReference, this))
-            ("Stored the last (averaged) value as new reference (for debug purpose only)");
+            ("Store the last (averaged) value as new reference (for debug purpose only)");
 
         AddEvent("SET_Ki", "D:1")//, kStateIdle)
@@ -580,4 +584,8 @@
             (bind(&StateMachineFeedback::SetConstant, this, placeholders::_1, 2))
             ("Set derivative constant Kd");
+
+        AddEvent("SET_T", "D:1")//, kStateIdle)
+            (bind(&StateMachineFeedback::SetConstant, this, placeholders::_1, 3))
+            ("Set time-constant. (-1 to use the cycle time, i.e. the time for the last average cycle, instead)");
 
         // Verbosity commands
@@ -600,4 +608,22 @@
         }
 
+        fGain = 5; // (BIAS)V / (DRS)V     ( 1V / 0.22V )
+
+        fKp = 0;
+        fKd = 0;
+        fKi = 0.12;
+        fT  = 1;
+
+        ostringstream msg;
+        msg << "Control loop parameters: ";
+        msg << "Kp=" << fKp << ", Kd=" << fKd << ", Ki=" << fKi << ", ";
+        if (fT>0)
+            msg << fT;
+        else
+            msg << "<auto>";
+        msg << ", Gain(BIAS/DRS)=" << fGain << "V/V";
+
+        Message(msg);
+
         return -1;
     }
@@ -616,5 +642,5 @@
 void SetupConfiguration(Configuration &conf)
 {
-    po::options_description control("BIAS control options");
+    po::options_description control("Feedback options");
     control.add_options()
         ("pixel-map-file",  var<string>("FACTmapV5a.txt"), "Pixel mapping file. Used here to get the default reference voltage.")
@@ -636,5 +662,5 @@
 {
     cout <<
-        "The ftmctrl controls the FSC (FACT Slow Control) board.\n"
+        "The feedback control the BIAS voltages based on the calibration signal.\n"
         "\n"
         "The default is that the program is started without user intercation. "
@@ -643,6 +669,6 @@
         "help message about the usuage can be brought to the screen.\n"
         "\n"
-        "Usage: fscctrl [-c type] [OPTIONS]\n"
-        "  or:  fscctrl [OPTIONS]\n";
+        "Usage: feedback [-c type] [OPTIONS]\n"
+        "  or:  feedback [OPTIONS]\n";
     cout << endl;
 }
