@@ -261,6 +261,8 @@ void SfeSiT5358Driver::setMaxFrequencyChangePPB(double ppb)
261261// / @return true if the write is successful
262262// / Note: the frequency change will be limited by: the pull range capabilities of the device;
263263// / and the setMaxFrequencyChangePPB. Call getFrequencyHz to read the frequency set.
264+ // / The default values for Pk and Ik come from very approximate Ziegler-Nichols tuning:
265+ // / oscillation starts when Pk is ~1.4; with a period of ~5 seconds.
264266bool SfeSiT5358Driver::setFrequencyByBiasMillis (double bias, double Pk, double Ik)
265267{
266268 double freq = getFrequencyHz ();
@@ -275,33 +277,29 @@ bool SfeSiT5358Driver::setFrequencyByBiasMillis(double bias, double Pk, double I
275277
276278 double clockInterval_s = 1.0 / freq; // Convert freq to interval in seconds
277279
278- // bias (RXClkBias, milliseconds) is the error term we are trying to drive to zero
279- double biasInClocks = bias / 1000.0 ; // Convert bias from millis to seconds
280- biasInClocks /= clockInterval_s; // Convert bias to clock cycles
280+ // Our setpoint is zero. Bias is the process value. Convert it to error
281+ double error = 0.0 - bias;
282+
283+ double errorInClocks = error / 1000.0 ; // Convert error from millis to seconds
284+ errorInClocks /= clockInterval_s; // Convert error to clock cycles
281285
282286 // Calculate the maximum frequency change in clock cycles
283287 double maxChangeInClocks = freq * _maxFrequencyChangePPB / 1.0e9 ;
284288
285- // Limit biasInClocks to +/-maxChangeInClocks
286- if (biasInClocks >= 0.0 )
289+ // Limit errorInClocks to +/-maxChangeInClocks
290+ if (errorInClocks >= 0.0 )
287291 {
288- if (biasInClocks > maxChangeInClocks)
289- biasInClocks = maxChangeInClocks;
292+ if (errorInClocks > maxChangeInClocks)
293+ errorInClocks = maxChangeInClocks;
290294 }
291295 else
292296 {
293- if (biasInClocks < (0.0 - maxChangeInClocks))
294- biasInClocks = 0.0 - maxChangeInClocks;
297+ if (errorInClocks < (0.0 - maxChangeInClocks))
298+ errorInClocks = 0.0 - maxChangeInClocks;
295299 }
296300
297- // If RxClkBias is positive, we need to reduce the oscillator frequency by making
298- // the frequency control word more negative. Both Pk and Ik must be negative.
299- if (Pk > 0.0 )
300- (Pk *= -1.0 );
301- if (Ik > 0.0 )
302- (Ik *= -1.0 );
303- double P = biasInClocks * Pk;
304- double dI = biasInClocks * Ik;
301+ double P = errorInClocks * Pk;
302+ double dI = errorInClocks * Ik;
305303 I += dI; // Add the delta to the integral
306304
307305 return setFrequencyHz (P + I); // Set the frequency to proportional plus integral
0 commit comments