2019-08-13 21:22:13 +02:00
|
|
|
/** Timestamp hook.
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2020-01-20 17:17:00 +01:00
|
|
|
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
|
2019-08-13 21:22:13 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* 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 3 of the License, or
|
|
|
|
* 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
/** @addtogroup hooks Hook functions
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <villas/hook.hpp>
|
|
|
|
#include <villas/timing.h>
|
|
|
|
#include <villas/sample.h>
|
|
|
|
|
|
|
|
#include <villas/log.h>
|
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
|
2020-09-14 16:11:22 +02:00
|
|
|
class TimeSync {
|
|
|
|
protected:
|
|
|
|
//Algorithms stability parameters
|
|
|
|
//Examples from Paper:
|
|
|
|
//const double p = 0.99;
|
|
|
|
//const double k1 = 1.1;
|
|
|
|
//const double k2 = 1.0;
|
|
|
|
//Tuned parameters:
|
|
|
|
const double p = 1.2;
|
|
|
|
const double k1 = 1.3;
|
|
|
|
const double k2 = 0.9;
|
|
|
|
const double c = 0.7; // ~= mü_max
|
|
|
|
const double tau = 1.; //Time between synchronizations
|
|
|
|
//We assume no skew of our internal clock
|
|
|
|
const double r = 1.;
|
|
|
|
|
|
|
|
//Time estimate
|
|
|
|
uint64_t x;
|
|
|
|
//Skew correction
|
|
|
|
double s;
|
|
|
|
//Dampening Factor
|
|
|
|
double y;
|
|
|
|
public:
|
2020-09-14 16:38:15 +02:00
|
|
|
TimeSync() : x(0.5e9), s(1.), y(0.)
|
2020-09-14 16:11:22 +02:00
|
|
|
{
|
|
|
|
//Check stability
|
|
|
|
assert( tau < (p*(k2-p*(k1-k2)))/
|
|
|
|
(c*(k1-p*(k1-k2))*(k1-p*(k1-k2))) );
|
|
|
|
assert( 2*k1/3*p > k1-k2 );
|
|
|
|
assert( k1-k2 > 0 );
|
|
|
|
assert( 2 > p && p > 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the current time Error
|
|
|
|
*
|
|
|
|
* @actualTime The actual time from an external clock
|
|
|
|
* @nanoseconds the nanoseconds since the last synchronization event
|
|
|
|
*/
|
|
|
|
int64_t timeError(const uint64_t actualTime, const uint64_t nanoseconds)
|
|
|
|
{
|
|
|
|
uint64_t currentEstimate = timeFromNanoseconds(nanoseconds);
|
|
|
|
return (int64_t)actualTime + (int64_t)tau*1e9 - (int64_t)currentEstimate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** synchronizes with external clock
|
|
|
|
*
|
|
|
|
* @actualTime nanoseconds at which event should occur
|
|
|
|
* @return nanoseconds estimate
|
|
|
|
*/
|
|
|
|
uint64_t synchronize(const uint64_t actualTime, const uint64_t nanoseconds)
|
|
|
|
{
|
|
|
|
double t_err;
|
|
|
|
uint64_t ret;
|
2020-09-15 15:06:10 +02:00
|
|
|
//Time estimate
|
|
|
|
x = timeFromNanoseconds(nanoseconds);
|
2020-09-14 16:11:22 +02:00
|
|
|
//Time Error
|
|
|
|
t_err = ((int64_t)actualTime + (int64_t)tau*1e9 - (int64_t)x)/1e9;
|
2020-09-15 15:06:10 +02:00
|
|
|
//Skew correction or internal frequency / external frequency
|
|
|
|
s = s + k1*(t_err) - k2*y;
|
|
|
|
//Dampening Factor
|
|
|
|
y = p*(t_err) + (1-p)*y;
|
2020-09-14 16:11:22 +02:00
|
|
|
|
|
|
|
//Clip skew correction
|
|
|
|
if (s > 1.1) {
|
|
|
|
s = 1.1;
|
|
|
|
} else if (s < 0.9) {
|
|
|
|
s = 0.9;
|
|
|
|
}
|
2020-09-14 16:38:15 +02:00
|
|
|
printf("time error: %f, nanoseconds: %lu, x: %ld, s: %f, y: %f\n", t_err, nanoseconds, x, s, y);
|
2020-09-14 16:11:22 +02:00
|
|
|
|
|
|
|
ret = x;
|
|
|
|
//If time estimate gets too large, reset x
|
|
|
|
if ( x > tau*1e9) {
|
|
|
|
x -= tau*1e9;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t time(const double partOfFullSecond)
|
|
|
|
{
|
|
|
|
return x + partOfFullSecond*tau*r*s*1e9;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns time estimate based on the nanoseconds that passed since
|
|
|
|
* last synchrsonization
|
|
|
|
*
|
|
|
|
* @return nanoesecond estimate
|
|
|
|
*/
|
|
|
|
uint64_t timeFromNanoseconds(const uint64_t n)
|
|
|
|
{
|
|
|
|
return time(n / 1.e9);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-13 21:22:13 +02:00
|
|
|
class PpsTsHook : public Hook {
|
|
|
|
|
|
|
|
protected:
|
|
|
|
double lastValue;
|
|
|
|
double thresh;
|
|
|
|
unsigned idx;
|
2020-09-11 18:44:47 +02:00
|
|
|
uint64_t lastSeqNr;
|
2019-08-13 21:22:13 +02:00
|
|
|
unsigned edgeCounter;
|
|
|
|
timespec realTime;
|
2020-09-14 16:38:15 +02:00
|
|
|
timespec lastEdge;
|
2020-09-10 14:53:50 +02:00
|
|
|
uint64_t last_sequence;
|
2020-09-14 16:11:22 +02:00
|
|
|
TimeSync ts;
|
2020-09-15 15:06:10 +02:00
|
|
|
bool converged;
|
2019-08-13 21:22:13 +02:00
|
|
|
|
|
|
|
public:
|
2020-08-25 21:00:52 +02:00
|
|
|
PpsTsHook(struct vpath *p, struct vnode *n, int fl, int prio, bool en = true) :
|
2019-08-13 21:22:13 +02:00
|
|
|
Hook(p, n, fl, prio, en),
|
|
|
|
lastValue(0),
|
|
|
|
thresh(1.5),
|
|
|
|
idx(0),
|
|
|
|
lastSeqNr(0),
|
|
|
|
edgeCounter(0),
|
2020-09-10 14:53:50 +02:00
|
|
|
realTime({ 0, 0 }),
|
2020-09-14 16:38:15 +02:00
|
|
|
lastEdge({0,0}),
|
2020-09-14 16:03:36 +02:00
|
|
|
last_sequence(0),
|
2020-09-15 15:06:10 +02:00
|
|
|
ts(),
|
|
|
|
converged(false)
|
2019-08-13 21:22:13 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void parse(json_t *cfg)
|
|
|
|
{
|
2020-09-05 18:42:12 +00:00
|
|
|
int ret;
|
|
|
|
json_error_t err;
|
2019-08-13 21:22:13 +02:00
|
|
|
|
2020-09-05 18:42:12 +00:00
|
|
|
assert(state != State::STARTED);
|
2019-08-13 21:22:13 +02:00
|
|
|
|
2020-08-28 19:49:36 +02:00
|
|
|
Hook::parse(cfg);
|
|
|
|
|
2020-09-10 14:53:50 +02:00
|
|
|
ret = json_unpack_ex(cfg, &err, 0, "{ s: i, s?: f, s?: f}",
|
2020-09-05 18:42:12 +00:00
|
|
|
"signal_index", &idx,
|
|
|
|
"threshold", &thresh,
|
2020-09-10 14:53:50 +02:00
|
|
|
"pll_gain", &pll_gain
|
2020-09-05 18:42:12 +00:00
|
|
|
);
|
|
|
|
if (ret)
|
|
|
|
throw ConfigError(cfg, err, "node-config-hook-pps_ts");
|
2019-08-13 21:22:13 +02:00
|
|
|
|
|
|
|
info("parsed config thresh=%f signal_index=%d", thresh, idx);
|
|
|
|
|
2020-09-05 18:42:12 +00:00
|
|
|
|
|
|
|
state = State::PARSED;
|
2019-08-13 21:22:13 +02:00
|
|
|
}
|
|
|
|
|
2019-10-03 11:44:29 +02:00
|
|
|
virtual villas::node::Hook::Reason process(sample *smp)
|
2019-08-13 21:22:13 +02:00
|
|
|
{
|
2019-10-03 11:44:29 +02:00
|
|
|
assert(state == State::STARTED);
|
2019-08-13 21:22:13 +02:00
|
|
|
|
2020-09-15 15:06:10 +02:00
|
|
|
static const uint64_t targetVal = 0.5e9;
|
2019-08-13 21:22:13 +02:00
|
|
|
/* Get value of PPS signal */
|
|
|
|
float value = smp->data[idx].f; // TODO check if it is really float
|
2020-09-14 16:38:15 +02:00
|
|
|
//uint64_t seqNr = smp->sequence;
|
2019-08-13 21:22:13 +02:00
|
|
|
|
|
|
|
/* Detect Edge */
|
|
|
|
bool isEdge = lastValue < thresh && value > thresh;
|
2020-09-11 18:44:47 +02:00
|
|
|
|
2020-09-15 15:06:10 +02:00
|
|
|
lastValue = value;
|
2020-09-11 18:44:47 +02:00
|
|
|
|
2020-09-14 16:03:36 +02:00
|
|
|
|
2020-09-14 16:11:22 +02:00
|
|
|
auto now = time_now();
|
2020-09-14 16:38:15 +02:00
|
|
|
uint64_t timediff = (now.tv_nsec - lastEdge.tv_nsec)+(now.tv_sec - lastEdge.tv_sec)*1e9;
|
|
|
|
|
2020-09-15 15:06:10 +02:00
|
|
|
// nsec is between 0.5e9 and 1.5e9 when converged
|
2020-09-14 16:38:15 +02:00
|
|
|
realTime.tv_nsec = ts.timeFromNanoseconds(timediff);
|
2020-09-15 15:06:10 +02:00
|
|
|
realTime.tv_sec = lastEdge.tv_sec;
|
2020-09-14 16:03:36 +02:00
|
|
|
|
2020-09-15 15:06:10 +02:00
|
|
|
// when not converged, nsec might be really high.
|
|
|
|
// using a loop assures no time jumps.
|
|
|
|
//TODO: can nsec also become negative?
|
|
|
|
while (realTime.tv_nsec >= 1e9) {
|
2020-09-14 16:03:36 +02:00
|
|
|
realTime.tv_sec++;
|
|
|
|
realTime.tv_nsec -= 1e9;
|
|
|
|
}
|
|
|
|
|
2019-08-13 21:22:13 +02:00
|
|
|
if (isEdge) {
|
|
|
|
edgeCounter++;
|
2020-09-15 15:06:10 +02:00
|
|
|
ts.synchronize(targetVal, timediff);
|
2020-09-14 16:38:15 +02:00
|
|
|
lastEdge = now;
|
2020-09-15 15:06:10 +02:00
|
|
|
converged = abs(ts.timeError(targetVal, timediff)) < 0.001;
|
2020-09-10 14:53:50 +02:00
|
|
|
}
|
|
|
|
|
2020-09-14 16:38:15 +02:00
|
|
|
/*if(isEdge){
|
2020-09-14 16:03:36 +02:00
|
|
|
info("Edge detected: seq=%lu, realTime.nsec=%lu, timeErr=%f , timePeriod=%f, changeVal=%f", seqNr,realTime.tv_nsec, timeErr, periodTime, changeVal);
|
2020-09-14 16:38:15 +02:00
|
|
|
}*/
|
|
|
|
|
2019-08-13 21:22:13 +02:00
|
|
|
|
2020-09-15 15:06:10 +02:00
|
|
|
//if (!converged || edgeCounter < 2)
|
2020-09-14 16:03:36 +02:00
|
|
|
if (edgeCounter < 2)
|
|
|
|
return Hook::Reason::SKIP_SAMPLE;
|
|
|
|
|
2019-08-13 21:22:13 +02:00
|
|
|
/* Update timestamp */
|
|
|
|
smp->ts.origin = realTime;
|
2019-10-03 11:44:29 +02:00
|
|
|
smp->flags |= (int) SampleFlags::HAS_TS_ORIGIN;
|
2019-08-13 21:22:13 +02:00
|
|
|
|
2020-09-10 14:53:50 +02:00
|
|
|
if((smp->sequence - last_sequence) > 1 )
|
2020-09-14 16:03:36 +02:00
|
|
|
warning("Samples missed: %li sampled missed", smp->sequence - last_sequence);
|
2020-09-10 14:53:50 +02:00
|
|
|
|
|
|
|
last_sequence = smp->sequence;
|
|
|
|
|
2019-10-03 11:44:29 +02:00
|
|
|
return Hook::Reason::OK;
|
2019-08-13 21:22:13 +02:00
|
|
|
}
|
2020-09-05 18:42:12 +00:00
|
|
|
|
|
|
|
~PpsTsHook(){
|
|
|
|
}
|
2019-08-13 21:22:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Register hook */
|
2020-06-14 15:00:02 +02:00
|
|
|
static char n[] = "pps_ts";
|
|
|
|
static char d[] = "Timestamp samples based GPS PPS signal";
|
|
|
|
static HookPlugin<PpsTsHook, n, d, (int) Hook::Flags::NODE_READ | (int) Hook::Flags::NODE_WRITE | (int) Hook::Flags::PATH> p;
|
2019-08-13 21:22:13 +02:00
|
|
|
|
|
|
|
} /* namespace node */
|
|
|
|
} /* namespace villas */
|
|
|
|
|
|
|
|
/** @} */
|