forked from richardcochran/linuxptp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pi.c
149 lines (132 loc) · 3.4 KB
/
pi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* @file pi.c
* @brief Implements a Proportional Integral clock servo.
* @note Copyright (C) 2011 Richard Cochran <[email protected]>
*
* 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>
#include <math.h>
#include "pi.h"
#include "servo_private.h"
#define HWTS_KP 0.7
#define HWTS_KI 0.3
#define SWTS_KP 0.1
#define SWTS_KI 0.001
#define NSEC_PER_SEC 1000000000
/* These two take their values from the configuration file. (see ptp4l.c) */
double configured_pi_kp = 0.0;
double configured_pi_ki = 0.0;
double configured_pi_offset = 0.0;
struct pi_servo {
struct servo servo;
double offset[2];
double local[2];
double drift;
double maxppb;
double kp;
double ki;
double max_offset;
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:
s->drift += (s->offset[1] - s->offset[0]) /
(s->local[1] - s->local[0]);
*state = SERVO_UNLOCKED;
s->count = 3;
break;
case 3:
*state = SERVO_JUMP;
s->count = 4;
break;
case 4:
/*
* 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;
}
ki_term = s->ki * offset;
ppb = s->kp * offset + s->drift + ki_term;
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;
}
struct servo *pi_servo_create(int fadj, int max_ppb, int sw_ts)
{
struct pi_servo *s;
s = calloc(1, sizeof(*s));
if (!s)
return NULL;
s->servo.destroy = pi_destroy;
s->servo.sample = pi_sample;
s->drift = fadj;
s->maxppb = max_ppb;
if (configured_pi_kp && configured_pi_ki) {
s->kp = configured_pi_kp;
s->ki = configured_pi_ki;
} else if (sw_ts) {
s->kp = SWTS_KP;
s->ki = SWTS_KI;
} else {
s->kp = HWTS_KP;
s->ki = HWTS_KI;
}
if (configured_pi_offset > 0.0) {
s->max_offset = configured_pi_offset * NSEC_PER_SEC;
} else {
s->max_offset = 0.0;
}
return &s->servo;
}