Index: trunk/FACT++/src/feedback.cc
===================================================================
--- trunk/FACT++/src/feedback.cc	(revision 16966)
+++ trunk/FACT++/src/feedback.cc	(revision 16967)
@@ -149,5 +149,5 @@
         med[1].resize(416);
 
-        const float *Ravg = fCalibration.data()+BIAS::kNumChannels*2; // Measured resistance
+        //const float *Ravg = fCalibration.data()+BIAS::kNumChannels*2; // Measured resistance
 
         vector<float> vec(2*BIAS::kNumChannels+2);
@@ -171,4 +171,8 @@
                 return GetCurrentState();
 
+            vector<double> dI;
+            vector<double> R8;
+            vector<double> R9;
+
             for (int i=0; i<BIAS::kNumChannels; i++)
             {
@@ -189,6 +193,9 @@
                 const int N = hv.group() ? 5 : 4;
 
-                // 100 Ohm measurement resistor for current measurement
-                const double R2 = 100;
+                // Average measured ADC value for this channel
+                const double adc = double(fCurrentsAvg[i])/fCursorCur * (5000/4096.); // [uA]
+
+                // Current through ~100Ohm measurement resistor
+                const double I8 = (adc-dI[i])*100/R8[i];
 
                 // Serial resistors (one 1kOhm at the output of the bias crate, one 1kOhm in the camera)
@@ -205,50 +212,26 @@
 
                 // Total resistance of branch with diode
-                const double R3 = R4+R5;
-
-                // Measured calibration resistor
-                const double R1 = Ravg[i] - R2;
-
-                // Voltage output of bias crate
-                const double Uout = fBiasVolt[i];
-
-                // Average current measured for this channel
-                const double Imes = double(fCurrentsAvg[i])/fCursorCur * (5000/4096.); // [uA]
-
-                // Voltage drop at measurement resistor R2 is define
-                // bythe measured current and the resistor
-                const double U2 = R2*Imes;
-
-                // The voltage seen by the calibration resistor R1 is defined by the
-                // bias crate output voltage minus the drop at the measurement resistor R2
-                const double U1 = Uout - U2;
-
-                // The current through the resistor R1 is defined
-                // by the applied voltage and the resistor
-                const double I1 = U1/R1;
-
-                // The current through the diode branch is the measured current
-                // minus the current through the calibration resistor R1
-                const double I3 = Imes - I1;
-
-                // The voltage drop in the diode branch (without the diode) is defined by the
-                // resistor and the current. It is 0 below the breakdown voltage of the G-APD
-                // is reached at the G-APD. This is the case when the output voltage minus
-                // the voltage drop at the calibration resistor reaches the breakdown voltage.
-                const double U3 = Uout-U2<Ubd ? 0 : R3*I3;
-
-                // Voltage drop at measurement resistor R2 and
-                // the total serial resistor R3 in the diode branch
-                const double Udrp = U2 + U3;
-
-                // Voltage finally at each G-APD (bias crate output voltage minus voltage drop)
-                const double Uapd = Uout - Udrp;
-
-                // The over-voltage seen by the G-APD (the voltage above the breakdown voltage) is
-                const double Uov = Uapd<Ubd ? 0 : Uapd - Ubd;
+                const double R = R4+R5;
+
+                // Applied voltage at calibration resistors, according to
+                // biasctrl
+                const double U9 = fBiasVolt[i];
+
+                // Current through calibration resistors
+                // FIXME: Get that from biasctrl!!!
+                const double I9 = U9/R9[i];
+
+                // Current in R4/R5 branch
+                const double Iout = I8>I9 ? I8 - I9 : 0;
+
+                // Voltage drop in R4/R5 branch
+                const double Udrp = R*Iout;
+
+                // Current overvoltage
+                const double Uov = U9-Udrp-Ubd>0 ? U9-Udrp-Ubd : 0;
 
                 // The current through one G-APD is the sum divided by the number of G-APDs
                 // (assuming identical serial resistors)
-                double Iapd = I3/N;
+                double Iapd = Iout/N;
 
                 // This is assuming that the broken pixels have a 390 Ohm instead of 3900 Ohm serial resistor
@@ -267,5 +250,5 @@
                 // If the G-APD voltage is above the breakdown voltage we have the current through the
                 // G-APD and the over-voltage applied to the G-APD to calculate its differential resistor.
-                if (Uapd>Ubd)
+                if (Iapd>0)
                 {
                     // The differential resistance of the G-APD, i.e. the dependence of the
@@ -276,17 +259,9 @@
                     const double Iov = (1.1+fBiasOffset)/Rapd;
 
-                    // This gives us an ohmic resistance Rov of the G-APD at the set-point
-                    const double Rest = (Ubd+1.1+fBiasOffset)/Iov;
-
-                    // This lets us estimate the total resistance Rtot of the circuit at the set-point
-                    const double R3b  = R4 + (R5+Rest)/N;
-                    const double Rtot = R2 + 1/(1/R1 + 1/R3b);
-
-                    // From this we can estimate the output voltage we need to get the
-                    // over-voltage at the G-APD as anticipated
-                    const double r    = 1 + R3/R1 - (R2 + R3 + R3*R2/R1)/Rtot;
-                    const double Uset = (Ubd+1.1+fBiasOffset)/r;
-
-                    Uoff[i] = Uset - fVoltGapd[i];
+                    // Estimate set point for over-voltage
+                    const double Uset = (1.1+fBiasOffset) + Ubd + R*Iov*N;
+
+                    // Voltage set point as a difference between breakdown voltage and set point
+                    Uoff[i] = Uset - fBiasVolt[i];
                 }
 
