Index: /trunk/FACT++/src/feedback.cc
===================================================================
--- /trunk/FACT++/src/feedback.cc	(revision 12993)
+++ /trunk/FACT++/src/feedback.cc	(revision 12994)
@@ -504,7 +504,4 @@
         fPV[2] = valarray<double>(&avg, 1);
 
-        const double T21 = 1; // feedback is  1s
-        const double T10 = 1; // feedback is 20s
-
         // => Kp = 0.01 * gain     = 0.00005
         // => Ki = 0.8  * gain/20s = 0.00025
@@ -518,4 +515,8 @@
 
         // correction = (fSP[0]-fPV[2])*fKi
+        /*
+        const double T21 = 1; // feedback is  1s
+        const double T10 = 1; // feedback is 20s
+
         const valarray<double> correction = 1./fGain/1000*
             (
@@ -523,4 +524,12 @@
              +  fKi*T21*(fSP[0]-fPV[2])
              +  fKd/T10*(fPV[1]-fPV[0])
+            );
+        */
+
+        // pow of 1.6 comes from the non-linearity of the
+        // amplitude vs bias voltage
+        const valarray<double> correction = 1./fGain/1000*
+            (
+             fKi*(pow(fSP[0], 1./1.6)-pow(fPV[2], 1./1.6))
             );
 
@@ -679,8 +688,8 @@
     }
 
-    void ResetData()
-    {
-        fData.clear();
-        fData.resize(500);
+    void ResetData(int16_t n=-1)
+    {
+        fData.assign(n>0 ? n : fData.size(), vector<float>(0));
+
         fCursor = 0;
         fStartTime = Time();
@@ -700,7 +709,10 @@
     }
 
-    int StartFeedback()
-    {
-        ResetData();
+    int StartFeedback(const EventImp &evt)
+    {
+        if (!CheckEventSize(evt.GetSize(), "StartFeedback", 2))
+            return kSM_FatalError;
+
+        ResetData(evt.GetShort());
 
         fControlType = kFeedback;
@@ -709,8 +721,10 @@
     }
 
-    int StartFeedbackGlobal()
-    {
-        ResetData();
-        fData.resize(5);
+    int StartFeedbackGlobal(const EventImp &evt)
+    {
+        if (!CheckEventSize(evt.GetSize(), "StartFeedbackGlobal", 2))
+            return kSM_FatalError;
+
+        ResetData(evt.GetShort());
 
         fControlType = kFeedbackGlobal;
@@ -970,11 +984,13 @@
                      "Calibrating current offsets.");
 
-        AddEvent("START_FEEDBACK_CONTROL", kStateConnectedFAD, kStateConnected)
-            (bind(&StateMachineFeedback::StartFeedback, this))
-            ("Start the feedback control loop");
-
-        AddEvent("START_GLOBAL_FEEDBACK", kStateConnectedFAD, kStateConnected)
-            (bind(&StateMachineFeedback::StartFeedbackGlobal, this))
-            ("Start the global feedback control loop");
+        AddEvent("START_FEEDBACK_CONTROL", "S:1", kStateConnectedFAD, kStateConnected)
+            (bind(&StateMachineFeedback::StartFeedback, this, placeholders::_1))
+            ("Start the feedback control loop"
+             "|Num[short]:Number of events 'medianed' to calculate the correction value");
+
+        AddEvent("START_GLOBAL_FEEDBACK", "S:1", kStateConnectedFAD, kStateConnected)
+            (bind(&StateMachineFeedback::StartFeedbackGlobal, this, placeholders::_1))
+            ("Start the global feedback control loop"
+             "Num[short]:Number of events averaged to calculate the correction value");
 
         AddEvent("START_TEMP_CONTROL", "F:1", kStateConnectedFSC, kStateConnected)
@@ -1054,5 +1070,5 @@
         fKp = 0;
         fKd = 0;
-        fKi = 0.66;
+        fKi = 0.75;
         fT  = 1;
 
