2011-11-12 19:31:18 +08:00
|
|
|
/**
|
|
|
|
* @file pi.c
|
|
|
|
* @brief Implements a Proportional Integral clock servo.
|
|
|
|
* @note Copyright (C) 2011 Richard Cochran <richardcochran@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
2012-09-29 02:46:00 +08:00
|
|
|
#include <math.h>
|
2011-11-12 19:31:18 +08:00
|
|
|
|
2015-08-14 04:48:25 +08:00
|
|
|
#include "config.h"
|
2011-11-12 19:31:18 +08:00
|
|
|
#include "pi.h"
|
2013-06-28 23:09:14 +08:00
|
|
|
#include "print.h"
|
2011-11-12 19:31:18 +08:00
|
|
|
#include "servo_private.h"
|
|
|
|
|
2013-06-28 23:09:14 +08:00
|
|
|
#define HWTS_KP_SCALE 0.7
|
|
|
|
#define HWTS_KI_SCALE 0.3
|
|
|
|
#define SWTS_KP_SCALE 0.1
|
|
|
|
#define SWTS_KI_SCALE 0.001
|
2011-12-13 11:57:28 +08:00
|
|
|
|
2013-06-28 23:09:14 +08:00
|
|
|
#define MAX_KP_NORM_MAX 1.0
|
|
|
|
#define MAX_KI_NORM_MAX 2.0
|
2011-11-12 19:31:18 +08:00
|
|
|
|
2013-06-17 03:31:20 +08:00
|
|
|
#define FREQ_EST_MARGIN 0.001
|
2012-09-29 02:46:00 +08:00
|
|
|
|
2011-11-12 19:31:18 +08:00
|
|
|
struct pi_servo {
|
|
|
|
struct servo servo;
|
2013-01-15 00:09:09 +08:00
|
|
|
int64_t offset[2];
|
|
|
|
uint64_t local[2];
|
2011-11-12 19:31:18 +08:00
|
|
|
double drift;
|
2011-12-13 11:57:28 +08:00
|
|
|
double kp;
|
|
|
|
double ki;
|
2014-01-08 21:23:46 +08:00
|
|
|
double last_freq;
|
2011-11-12 19:31:18 +08:00
|
|
|
int count;
|
2015-08-14 04:48:25 +08:00
|
|
|
/* configuration: */
|
|
|
|
double configured_pi_kp;
|
2015-08-14 04:54:22 +08:00
|
|
|
double configured_pi_ki;
|
2015-08-14 05:02:06 +08:00
|
|
|
double configured_pi_kp_scale;
|
2015-08-15 03:21:25 +08:00
|
|
|
double configured_pi_kp_exponent;
|
2015-08-15 03:25:27 +08:00
|
|
|
double configured_pi_kp_norm_max;
|
2015-08-15 03:31:16 +08:00
|
|
|
double configured_pi_ki_scale;
|
2015-08-15 03:35:47 +08:00
|
|
|
double configured_pi_ki_exponent;
|
2015-08-15 03:41:21 +08:00
|
|
|
double configured_pi_ki_norm_max;
|
2011-11-12 19:31:18 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static void pi_destroy(struct servo *servo)
|
|
|
|
{
|
|
|
|
struct pi_servo *s = container_of(servo, struct pi_servo, servo);
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double pi_sample(struct servo *servo,
|
2013-01-15 00:09:09 +08:00
|
|
|
int64_t offset,
|
|
|
|
uint64_t local_ts,
|
2015-03-26 23:32:15 +08:00
|
|
|
double weight,
|
2011-11-12 19:31:18 +08:00
|
|
|
enum servo_state *state)
|
|
|
|
{
|
|
|
|
struct pi_servo *s = container_of(servo, struct pi_servo, servo);
|
2014-01-08 21:23:46 +08:00
|
|
|
double ki_term, ppb = s->last_freq;
|
|
|
|
double freq_est_interval, localdiff;
|
2011-11-12 19:31:18 +08:00
|
|
|
|
|
|
|
switch (s->count) {
|
|
|
|
case 0:
|
|
|
|
s->offset[0] = offset;
|
|
|
|
s->local[0] = local_ts;
|
|
|
|
*state = SERVO_UNLOCKED;
|
|
|
|
s->count = 1;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
s->offset[1] = offset;
|
|
|
|
s->local[1] = local_ts;
|
2013-01-15 00:09:11 +08:00
|
|
|
|
2013-02-06 00:36:10 +08:00
|
|
|
/* Make sure the first sample is older than the second. */
|
|
|
|
if (s->local[0] >= s->local[1]) {
|
2013-06-18 18:09:36 +08:00
|
|
|
*state = SERVO_UNLOCKED;
|
2013-02-06 00:36:10 +08:00
|
|
|
s->count = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-17 03:31:20 +08:00
|
|
|
/* Wait long enough before estimating the frequency offset. */
|
|
|
|
localdiff = (s->local[1] - s->local[0]) / 1e9;
|
|
|
|
localdiff += localdiff * FREQ_EST_MARGIN;
|
|
|
|
freq_est_interval = 0.016 / s->ki;
|
|
|
|
if (freq_est_interval > 1000.0) {
|
|
|
|
freq_est_interval = 1000.0;
|
|
|
|
}
|
|
|
|
if (localdiff < freq_est_interval) {
|
|
|
|
*state = SERVO_UNLOCKED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-01-08 22:44:37 +08:00
|
|
|
/* Adjust drift by the measured frequency offset. */
|
|
|
|
s->drift += (1e9 - s->drift) * (s->offset[1] - s->offset[0]) /
|
|
|
|
(s->local[1] - s->local[0]);
|
2014-03-14 01:34:17 +08:00
|
|
|
|
|
|
|
if (s->drift < -servo->max_frequency)
|
|
|
|
s->drift = -servo->max_frequency;
|
|
|
|
else if (s->drift > servo->max_frequency)
|
|
|
|
s->drift = servo->max_frequency;
|
|
|
|
|
|
|
|
if ((servo->first_update &&
|
|
|
|
servo->first_step_threshold &&
|
|
|
|
servo->first_step_threshold < fabs(offset)) ||
|
|
|
|
(servo->step_threshold &&
|
|
|
|
servo->step_threshold < fabs(offset)))
|
2013-06-20 12:30:15 +08:00
|
|
|
*state = SERVO_JUMP;
|
|
|
|
else
|
|
|
|
*state = SERVO_LOCKED;
|
|
|
|
|
2013-01-15 00:09:10 +08:00
|
|
|
ppb = s->drift;
|
2013-01-15 00:09:11 +08:00
|
|
|
s->count = 2;
|
2011-11-12 19:31:18 +08:00
|
|
|
break;
|
2013-01-15 00:09:11 +08:00
|
|
|
case 2:
|
2012-09-29 02:46:00 +08:00
|
|
|
/*
|
|
|
|
* reset the clock servo when offset is greater than the max
|
|
|
|
* offset value. Note that the clock jump will be performed in
|
2013-02-06 00:36:11 +08:00
|
|
|
* step 1, so it is not necessary to have clock jump
|
2012-09-29 02:46:00 +08:00
|
|
|
* immediately. This allows re-calculating drift as in initial
|
|
|
|
* clock startup.
|
|
|
|
*/
|
2014-03-14 01:34:17 +08:00
|
|
|
if (servo->step_threshold &&
|
|
|
|
servo->step_threshold < fabs(offset)) {
|
2012-09-29 02:46:00 +08:00
|
|
|
*state = SERVO_UNLOCKED;
|
|
|
|
s->count = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-03-26 23:32:17 +08:00
|
|
|
ki_term = s->ki * offset * weight;
|
|
|
|
ppb = s->kp * offset * weight + s->drift + ki_term;
|
2014-03-14 01:34:17 +08:00
|
|
|
if (ppb < -servo->max_frequency) {
|
|
|
|
ppb = -servo->max_frequency;
|
|
|
|
} else if (ppb > servo->max_frequency) {
|
|
|
|
ppb = servo->max_frequency;
|
2011-11-12 19:31:18 +08:00
|
|
|
} else {
|
|
|
|
s->drift += ki_term;
|
|
|
|
}
|
|
|
|
*state = SERVO_LOCKED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-01-08 21:23:46 +08:00
|
|
|
s->last_freq = ppb;
|
2011-11-12 19:31:18 +08:00
|
|
|
return ppb;
|
|
|
|
}
|
|
|
|
|
2013-05-16 22:31:45 +08:00
|
|
|
static void pi_sync_interval(struct servo *servo, double interval)
|
|
|
|
{
|
2013-06-28 23:09:14 +08:00
|
|
|
struct pi_servo *s = container_of(servo, struct pi_servo, servo);
|
|
|
|
|
2015-08-15 03:21:25 +08:00
|
|
|
s->kp = s->configured_pi_kp_scale * pow(interval, s->configured_pi_kp_exponent);
|
2015-08-15 03:25:27 +08:00
|
|
|
if (s->kp > s->configured_pi_kp_norm_max / interval)
|
|
|
|
s->kp = s->configured_pi_kp_norm_max / interval;
|
2013-06-28 23:09:14 +08:00
|
|
|
|
2015-08-15 03:35:47 +08:00
|
|
|
s->ki = s->configured_pi_ki_scale * pow(interval, s->configured_pi_ki_exponent);
|
2015-08-15 03:41:21 +08:00
|
|
|
if (s->ki > s->configured_pi_ki_norm_max / interval)
|
|
|
|
s->ki = s->configured_pi_ki_norm_max / interval;
|
2013-06-28 23:09:14 +08:00
|
|
|
|
|
|
|
pr_debug("PI servo: sync interval %.3f kp %.3f ki %.6f",
|
|
|
|
interval, s->kp, s->ki);
|
2013-05-16 22:31:45 +08:00
|
|
|
}
|
|
|
|
|
2013-10-23 00:57:14 +08:00
|
|
|
static void pi_reset(struct servo *servo)
|
|
|
|
{
|
|
|
|
struct pi_servo *s = container_of(servo, struct pi_servo, servo);
|
|
|
|
|
|
|
|
s->count = 0;
|
|
|
|
}
|
|
|
|
|
2015-08-14 04:48:25 +08:00
|
|
|
struct servo *pi_servo_create(struct config *cfg, int fadj, int sw_ts)
|
2011-11-12 19:31:18 +08:00
|
|
|
{
|
|
|
|
struct pi_servo *s;
|
|
|
|
|
|
|
|
s = calloc(1, sizeof(*s));
|
|
|
|
if (!s)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
s->servo.destroy = pi_destroy;
|
|
|
|
s->servo.sample = pi_sample;
|
2013-05-16 22:31:45 +08:00
|
|
|
s->servo.sync_interval = pi_sync_interval;
|
2013-10-23 00:57:14 +08:00
|
|
|
s->servo.reset = pi_reset;
|
2012-09-21 15:27:53 +08:00
|
|
|
s->drift = fadj;
|
2014-01-08 21:23:46 +08:00
|
|
|
s->last_freq = fadj;
|
2013-06-28 23:09:14 +08:00
|
|
|
s->kp = 0.0;
|
|
|
|
s->ki = 0.0;
|
2015-08-14 04:48:25 +08:00
|
|
|
s->configured_pi_kp = config_get_double(cfg, NULL, "pi_proportional_const");
|
2015-08-14 04:54:22 +08:00
|
|
|
s->configured_pi_ki = config_get_double(cfg, NULL, "pi_integral_const");
|
2015-08-14 05:02:06 +08:00
|
|
|
s->configured_pi_kp_scale = config_get_double(cfg, NULL, "pi_proportional_scale");
|
2015-08-15 03:21:25 +08:00
|
|
|
s->configured_pi_kp_exponent =
|
|
|
|
config_get_double(cfg, NULL, "pi_proportional_exponent");
|
2015-08-15 03:25:27 +08:00
|
|
|
s->configured_pi_kp_norm_max =
|
|
|
|
config_get_double(cfg, NULL, "pi_proportional_norm_max");
|
2015-08-15 03:31:16 +08:00
|
|
|
s->configured_pi_ki_scale =
|
|
|
|
config_get_double(cfg, NULL, "pi_integral_scale");
|
2015-08-15 03:35:47 +08:00
|
|
|
s->configured_pi_ki_exponent =
|
|
|
|
config_get_double(cfg, NULL, "pi_integral_exponent");
|
2015-08-15 03:41:21 +08:00
|
|
|
s->configured_pi_ki_norm_max =
|
|
|
|
config_get_double(cfg, NULL, "pi_integral_norm_max");
|
2011-11-12 19:31:18 +08:00
|
|
|
|
2015-08-14 04:54:22 +08:00
|
|
|
if (s->configured_pi_kp && s->configured_pi_ki) {
|
2013-06-28 23:09:14 +08:00
|
|
|
/* Use the constants as configured by the user without
|
|
|
|
adjusting for sync interval unless they make the servo
|
|
|
|
unstable. */
|
2015-08-14 05:02:06 +08:00
|
|
|
s->configured_pi_kp_scale = s->configured_pi_kp;
|
2015-08-15 03:31:16 +08:00
|
|
|
s->configured_pi_ki_scale = s->configured_pi_ki;
|
2015-08-15 03:21:25 +08:00
|
|
|
s->configured_pi_kp_exponent = 0.0;
|
2015-08-15 03:35:47 +08:00
|
|
|
s->configured_pi_ki_exponent = 0.0;
|
2015-08-15 03:25:27 +08:00
|
|
|
s->configured_pi_kp_norm_max = MAX_KP_NORM_MAX;
|
2015-08-15 03:41:21 +08:00
|
|
|
s->configured_pi_ki_norm_max = MAX_KI_NORM_MAX;
|
2015-08-15 03:31:16 +08:00
|
|
|
} else if (!s->configured_pi_kp_scale || !s->configured_pi_ki_scale) {
|
2013-06-28 23:09:14 +08:00
|
|
|
if (sw_ts) {
|
2015-08-14 05:02:06 +08:00
|
|
|
s->configured_pi_kp_scale = SWTS_KP_SCALE;
|
2015-08-15 03:31:16 +08:00
|
|
|
s->configured_pi_ki_scale = SWTS_KI_SCALE;
|
2013-06-28 23:09:14 +08:00
|
|
|
} else {
|
2015-08-14 05:02:06 +08:00
|
|
|
s->configured_pi_kp_scale = HWTS_KP_SCALE;
|
2015-08-15 03:31:16 +08:00
|
|
|
s->configured_pi_ki_scale = HWTS_KI_SCALE;
|
2013-06-28 23:09:14 +08:00
|
|
|
}
|
2011-12-13 11:57:28 +08:00
|
|
|
}
|
|
|
|
|
2011-11-12 19:31:18 +08:00
|
|
|
return &s->servo;
|
|
|
|
}
|