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
|
|
|
|
|
|
|
#include "pi.h"
|
|
|
|
#include "servo_private.h"
|
|
|
|
|
2011-12-13 11:57:28 +08:00
|
|
|
#define HWTS_KP 0.7
|
|
|
|
#define HWTS_KI 0.3
|
|
|
|
|
|
|
|
#define SWTS_KP 0.1
|
|
|
|
#define SWTS_KI 0.001
|
2011-11-12 19:31:18 +08:00
|
|
|
|
2012-09-29 02:46:00 +08:00
|
|
|
#define NSEC_PER_SEC 1000000000
|
|
|
|
|
2012-05-03 17:17:34 +08:00
|
|
|
/* These two take their values from the configuration file. (see ptp4l.c) */
|
2012-08-25 15:19:29 +08:00
|
|
|
double configured_pi_kp;
|
|
|
|
double configured_pi_ki;
|
2012-09-29 02:46:00 +08:00
|
|
|
double configured_pi_offset;
|
2012-05-03 17:17:34 +08:00
|
|
|
|
2011-11-12 19:31:18 +08:00
|
|
|
struct pi_servo {
|
|
|
|
struct servo servo;
|
|
|
|
double offset[2];
|
|
|
|
double local[2];
|
|
|
|
double drift;
|
|
|
|
double maxppb;
|
2011-12-13 11:57:28 +08:00
|
|
|
double kp;
|
|
|
|
double ki;
|
2012-09-29 02:46:00 +08:00
|
|
|
double max_offset;
|
2011-11-12 19:31:18 +08:00
|
|
|
int count;
|
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
double offset,
|
|
|
|
double local_ts,
|
|
|
|
enum servo_state *state)
|
|
|
|
{
|
|
|
|
double ki_term, ppb = 0.0;
|
|
|
|
struct pi_servo *s = container_of(servo, struct pi_servo, servo);
|
|
|
|
|
|
|
|
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;
|
|
|
|
*state = SERVO_UNLOCKED;
|
|
|
|
s->count = 2;
|
|
|
|
break;
|
|
|
|
case 2:
|
2012-09-21 15:27:53 +08:00
|
|
|
s->drift += (s->offset[1] - s->offset[0]) /
|
2011-11-12 19:31:18 +08:00
|
|
|
(s->local[1] - s->local[0]);
|
|
|
|
*state = SERVO_UNLOCKED;
|
|
|
|
s->count = 3;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
*state = SERVO_JUMP;
|
|
|
|
s->count = 4;
|
|
|
|
break;
|
|
|
|
case 4:
|
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
|
|
|
|
* step 3, so it is not necessary to have clock jump
|
|
|
|
* immediately. This allows re-calculating drift as in initial
|
|
|
|
* clock startup.
|
|
|
|
*/
|
|
|
|
if (s->max_offset && (s->max_offset < fabs(offset))) {
|
|
|
|
*state = SERVO_UNLOCKED;
|
|
|
|
s->count = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-13 11:57:28 +08:00
|
|
|
ki_term = s->ki * offset;
|
|
|
|
ppb = s->kp * offset + s->drift + ki_term;
|
2011-11-12 19:31:18 +08:00
|
|
|
if (ppb < -s->maxppb) {
|
|
|
|
ppb = -s->maxppb;
|
|
|
|
} else if (ppb > s->maxppb) {
|
|
|
|
ppb = s->maxppb;
|
|
|
|
} else {
|
|
|
|
s->drift += ki_term;
|
|
|
|
}
|
|
|
|
*state = SERVO_LOCKED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ppb;
|
|
|
|
}
|
|
|
|
|
2012-09-21 15:27:53 +08:00
|
|
|
struct servo *pi_servo_create(int fadj, int max_ppb, 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;
|
2012-09-21 15:27:53 +08:00
|
|
|
s->drift = fadj;
|
2011-11-12 19:31:18 +08:00
|
|
|
s->maxppb = max_ppb;
|
|
|
|
|
2012-05-03 17:17:34 +08:00
|
|
|
if (configured_pi_kp && configured_pi_ki) {
|
|
|
|
s->kp = configured_pi_kp;
|
|
|
|
s->ki = configured_pi_ki;
|
|
|
|
} else if (sw_ts) {
|
2011-12-13 11:57:28 +08:00
|
|
|
s->kp = SWTS_KP;
|
|
|
|
s->ki = SWTS_KI;
|
|
|
|
} else {
|
|
|
|
s->kp = HWTS_KP;
|
|
|
|
s->ki = HWTS_KI;
|
|
|
|
}
|
|
|
|
|
2012-09-29 02:46:00 +08:00
|
|
|
if (configured_pi_offset > 0.0) {
|
|
|
|
s->max_offset = configured_pi_offset * NSEC_PER_SEC;
|
|
|
|
} else {
|
|
|
|
s->max_offset = 0.0;
|
|
|
|
}
|
|
|
|
|
2011-11-12 19:31:18 +08:00
|
|
|
return &s->servo;
|
|
|
|
}
|