Miroslav Lichvar
2013-12-20 15:51:35 UTC
When the drift value is adjusted by the newly measured frequency offset,
multiply the frequencies instead of adding the measured offset to the
old value to get accurate result even when updating a large drift.
Signed-off-by: Miroslav Lichvar <***@redhat.com>
---
pi.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/pi.c b/pi.c
index bd78e40..ccad98e 100644
--- a/pi.c
+++ b/pi.c
@@ -107,8 +107,10 @@ static double pi_sample(struct servo *servo,
break;
}
- s->drift += (s->offset[1] - s->offset[0]) * 1e9 /
- (s->local[1] - s->local[0]);
+ /* Adjust drift by the measured frequency offset. */
+ s->drift = 1e9 - (1e9 - s->drift) *
+ (1.0 - (double)(s->offset[1] - s->offset[0]) /
+ (s->local[1] - s->local[0]));
if (s->drift < -s->maxppb)
s->drift = -s->maxppb;
else if (s->drift > s->maxppb)
multiply the frequencies instead of adding the measured offset to the
old value to get accurate result even when updating a large drift.
Signed-off-by: Miroslav Lichvar <***@redhat.com>
---
pi.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/pi.c b/pi.c
index bd78e40..ccad98e 100644
--- a/pi.c
+++ b/pi.c
@@ -107,8 +107,10 @@ static double pi_sample(struct servo *servo,
break;
}
- s->drift += (s->offset[1] - s->offset[0]) * 1e9 /
- (s->local[1] - s->local[0]);
+ /* Adjust drift by the measured frequency offset. */
+ s->drift = 1e9 - (1e9 - s->drift) *
+ (1.0 - (double)(s->offset[1] - s->offset[0]) /
+ (s->local[1] - s->local[0]));
if (s->drift < -s->maxppb)
s->drift = -s->maxppb;
else if (s->drift > s->maxppb)
--
1.8.3.1
1.8.3.1