Commit d7b813da authored by Alessandro Rubini's avatar Alessandro Rubini

bugfix: servo: don't throw out precision

When calculating the running average (and the average between two as
well), we must first add all items and then divide. Everyone does like
this, so we should as well, to avoid loosing the low bits

Example: 3 and 5, average is 4: (3+5) / 2 = 4, but 3/2 + 5/2 = 3.
Signed-off-by: Alessandro Rubini's avatarAlessandro Rubini <rubini@gnudd.com>
parent 9bf96bd8
......@@ -86,13 +86,14 @@ void pp_update_delay(struct pp_instance *ppi, TimeInternal *correction_field)
else if (owd_fltr->s_exp > 1 << s)
owd_fltr->s_exp = 1 << s;
/* filter 'meanPathDelay' */
owd_fltr->y = (owd_fltr->s_exp - 1) *
owd_fltr->y / owd_fltr->s_exp +
(mpd->nanoseconds / 2 +
owd_fltr->nsec_prev / 2) / owd_fltr->s_exp;
/* Use the average between current value and previous one */
mpd->nanoseconds = (mpd->nanoseconds + owd_fltr->nsec_prev) / 2;
owd_fltr->nsec_prev = mpd->nanoseconds;
/* filter 'meanPathDelay' (running average) */
owd_fltr->y = (owd_fltr->y * (owd_fltr->s_exp - 1) + mpd->nanoseconds)
/ owd_fltr->s_exp;
mpd->nanoseconds = owd_fltr->y;
pp_diag(ppi, servo, 1, "delay filter %d, %d\n",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment