Skip to content

Commit 62f03d6

Browse files
committed
Update error term
1 parent 5bd7662 commit 62f03d6

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

src/SparkFun_SiT5358.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
264266
bool 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

src/SparkFun_SiT5358.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,9 @@ class SfeSiT5358Driver
198198
/// @return true if the write is successful
199199
/// Note: the frequency change will be limited by: the pull range capabilities of the device;
200200
/// and the setMaxFrequencyChangePPB. Call getFrequencyHz to read the frequency set.
201-
bool setFrequencyByBiasMillis(double bias, double Pk = 1.0, double Ik = 0.0);
201+
/// The default values for Pk and Ik come from very approximate Ziegler-Nichols tuning:
202+
/// oscillation starts when Pk is ~1.4; with a period of ~5 seconds.
203+
bool setFrequencyByBiasMillis(double bias, double Pk = 0.63, double Ik = 0.151);
202204

203205

204206
/// @brief Convert the 4-bit pull range into text

0 commit comments

Comments
 (0)