Index: /trunk/FACT++/src/feedback.cc
===================================================================
--- /trunk/FACT++/src/feedback.cc	(revision 13234)
+++ /trunk/FACT++/src/feedback.cc	(revision 13235)
@@ -148,20 +148,20 @@
         const float *ptr = static_cast<float*>(fCameraTemp.getData());
 
-        double avg = 0;
-        int    num = 0;
+        double avgt = 0;
+        int    numt = 0;
         for (int i=1; i<32; i++)
             if (ptr[i]!=0)
             {
-                avg += ptr[i];
-                num++;
+                avgt += ptr[i];
+                numt++;
             }
 
-        if (num==0)
-            return;
-
-        avg /= num;
-
-
-        const float diff = (avg-25)*4./70 + fBiasOffset;
+        if (numt==0)
+            return;
+
+        avgt /= numt;
+
+
+        const float diff = (avgt-25)*4./70 + fBiasOffset;
 
         vector<float> vec(2*BIAS::kNumChannels);
@@ -169,6 +169,13 @@
             vec[i+BIAS::kNumChannels] = diff;
 
-        avg = 0;
-        num = 0;
+        double avg[2] = {   0,   0 };
+        double min[2] = {  90,  90 };
+        double max[2] = { -90, -90 };
+        int    num[2] = {   0,   0 };
+
+        vector<double> med[2];
+        med[0].resize(416);
+        med[1].resize(416);
+
         if (fControlType==kCurrents)
         {
@@ -179,20 +186,55 @@
             }
 
+            // Pixel  583: 5 31 == 191 (5)
+            // Pixel  830: 2  2 ==  66 (4)
+            // Pixel 1401: 6  1 == 193 (4)
+
             // Convert from DAC counts to uA
             const double conv = 5000e-6/4096;
+
+            // 3900 Ohm/n + 1000 Ohm + 1100 Ohm  (with n=4 or n=5)
+            const double R[2] = { 3075, 2870 };
+
             for (int i=0; i<BIAS::kNumChannels; i++)
             {
-                // 3900 Ohm/n  +  1000 Ohm  +  1150 Ohm
-                const double R  = fMap.hv(i).group()==0 ? 3125 : 2930;
+                const PixelMapEntry &hv = fMap.hv(i);
+                if (!hv)
+                    continue;
+
+                const int g = hv.group();
+
                 const double Im = double(fCurrentsAvg[i])/fCursorCur;
                 const double I  = Im>fCalibration[i] ? Im-fCalibration[i] : 0;
-                vec[i+BIAS::kNumChannels] += R * I*conv;
+
+                double U  = R[g] * I*conv;
+
+                // 510 / 390    1.30 ^1.66 =  1.55
+                // 470 / 380    1.23       =  1.41
+                // 450 / 360    1.25       =  1.45
+
+                // This is assuming that the broken pixels
+                // have a 1kOhm instead of 390 Ohm serial resistor
+                if (i==66 || i==193)
+                    U /= 2665./R[0];    // (1k)2665 / (390)2400 / (~0)2110
+                if (i==191)
+                    U /= 2594./R[1];    // (1k)2794 / (390)2320 / (~0)2110
+
+                vec[i+BIAS::kNumChannels] += U;
+
                 if (fCalibration[i]>0)
                 {
-                    avg += R * I*conv;
-                    num++;
+                    med[g][num[g]] = U;
+                    avg[g] += U;
+                    num[g]++;
+
+                    if (U<min[g])
+                        min[g] = U;
+                    if (U>max[g])
+                        max[g] = U;
                 }
             }
-            avg /= num;
+
+            sort(med[0].begin(), med[0].begin()+num[0]);
+            sort(med[1].begin(), med[1].begin()+num[1]);
 
             fCurrentsAvg.assign(BIAS::kNumChannels, 0);
@@ -214,6 +256,25 @@
 
         ostringstream msg;
-        msg << setprecision(4) << "Sending new absolute offset (" << diff << "V+" << avg << "V) to biasctrl.";
+        msg << setprecision(4) << "Sending new absolute offset (" << diff << "V+" << (avg[0]+avg[1])/(num[0]+num[1]) << "V) to biasctrl.";
         Info(msg);
+
+        if (fControlType==kCurrents)
+        {
+            msg.str("");
+            msg << "   Avg0=" << setw(5) << avg[0]/num[0]    << "  |  Avg1=" << setw(5) << avg[1]/num[1]    << endl;
+            Debug(msg);
+
+            msg.str("");
+            msg << "   Med0=" << setw(5) << med[0][num[0]/2] << "  |  Med1=" << setw(5) << med[1][num[1]/2] << endl;
+            Debug(msg);
+
+            msg.str("");
+            msg << "   Min0=" << setw(5) << min[0]           << "  |  Min1=" << setw(5) << min[1]           << endl;
+            Debug(msg);
+
+            msg.str("");
+            msg << "   Max0=" << setw(5) << max[0]           << "  |  Max1=" << setw(5) << max[1]          << endl;
+            Debug(msg);
+        }
 
         DimClient::sendCommandNB("BIAS_CONTROL/SET_ALL_CHANNELS_OFFSET",
@@ -1237,6 +1298,6 @@
 
         fCurrentRequestInterval = conf.Get<uint16_t>("current-request-interval");
-        fNumCalibIgnore  = conf.Get<uint16_t>("num-calib-ignore");
-        fNumCalibRequest = conf.Get<uint16_t>("num-calib-average");
+        fNumCalibIgnore   = conf.Get<uint16_t>("num-calib-ignore");
+        fNumCalibRequests = conf.Get<uint16_t>("num-calib-average");
 
         return -1;
