@@ -256,20 +256,32 @@ void SfeSiT5358Driver::setMaxFrequencyChangePPB(double ppb)
256256
257257// / @brief Set the frequency according to the GNSS receiver clock bias in milliseconds
258258// / @param bias the GNSS RX clock bias in milliseconds
259+ // / @param Pk the Proportional term (default 1.0)
260+ // / @param Ik the Integral term (default 0.0)
259261// / @return true if the write is successful
260262// / Note: the frequency change will be limited by: the pull range capabilities of the device;
261263// / and the setMaxFrequencyChangePPB. Call getFrequencyHz to read the frequency set.
262- bool SfeSiT5358Driver::setFrequencyByBiasMillis (double bias)
264+ bool SfeSiT5358Driver::setFrequencyByBiasMillis (double bias, double Pk, double Ik )
263265{
264266 double freq = getFrequencyHz ();
265267
268+ static double I;
269+ static bool initialized = false ;
270+ if (!initialized)
271+ {
272+ I = freq; // Initialize I with the current frequency for a more reasonable startup
273+ initialized = true ;
274+ }
275+
266276 double clockInterval_s = 1.0 / freq; // Convert freq to interval in seconds
267277
268278 double biasInClocks = bias / 1000.0 ; // Convert bias from millis to seconds
269279 biasInClocks /= clockInterval_s; // Convert bias to clock cycles
270280
281+ // Calculate the maximum frequency change in clock cycles
271282 double maxChangeInClocks = freq * _maxFrequencyChangePPB / 1.0e9 ;
272283
284+ // Limit biasInClocks to +/-maxChangeInClocks
273285 if (biasInClocks >= 0.0 )
274286 {
275287 if (biasInClocks > maxChangeInClocks)
@@ -281,9 +293,11 @@ bool SfeSiT5358Driver::setFrequencyByBiasMillis(double bias)
281293 biasInClocks = 0.0 - maxChangeInClocks;
282294 }
283295
284- double newFreq = freq - biasInClocks;
296+ double P = biasInClocks * Pk;
297+ double dI = biasInClocks * Ik;
298+ I += dI;
285299
286- return setFrequencyHz (newFreq );
300+ return setFrequencyHz (P + I );
287301}
288302
289303// / @brief Convert the 4-bit pull range into text
0 commit comments