Skip to content

Commit 157d1de

Browse files
committed
Add PI control to setFrequencyByBiasMillis
1 parent e7de8f1 commit 157d1de

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/SparkFun_SiT5358.cpp

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

src/SparkFun_SiT5358.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,16 @@ class SfeSiT5358Driver
199199
bool setFrequencyByBiasMillis(double bias);
200200

201201

202+
/// @brief Set the frequency according to the GNSS receiver clock bias in milliseconds
203+
/// @param bias the GNSS RX clock bias in milliseconds
204+
/// @param Pk the Proportional term
205+
/// @param Ik the Integral term
206+
/// @return true if the write is successful
207+
/// Note: the frequency change will be limited by: the pull range capabilities of the device;
208+
/// and the setMaxFrequencyChangePPB. Call getFrequencyHz to read the frequency set.
209+
bool setFrequencyByBiasMillis(double bias, double Pk = 1.0, double Ik = 0.0);
210+
211+
202212
/// @brief Convert the 4-bit pull range into text
203213
/// @return the pull range as text
204214
const char * getPullRangeControlText(uint8_t pullRange);

0 commit comments

Comments
 (0)