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
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2021-02-16 14:13:49 +01:00
|
|
|
#include <cinttypes>
|
|
|
|
#include <vector>
|
|
|
|
|
2019-08-13 21:22:13 +02:00
|
|
|
#include <villas/hook.hpp>
|
|
|
|
#include <villas/timing.h>
|
|
|
|
#include <villas/sample.h>
|
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
|
2021-07-06 16:28:10 +02:00
|
|
|
class PpsTsHook : public SingleSignalHook {
|
2019-08-13 21:22:13 +02:00
|
|
|
|
|
|
|
protected:
|
2021-07-20 12:33:21 +02:00
|
|
|
enum Mode {
|
|
|
|
SIMPLE,
|
|
|
|
HORIZON,
|
|
|
|
} mode;
|
|
|
|
|
2021-07-20 13:37:19 +02:00
|
|
|
uint64_t lastSequence;
|
|
|
|
|
2019-08-13 21:22:13 +02:00
|
|
|
double lastValue;
|
2021-07-06 16:28:10 +02:00
|
|
|
double threshold;
|
2020-10-08 12:31:20 +02:00
|
|
|
|
|
|
|
bool isSynced;
|
|
|
|
bool isLocked;
|
|
|
|
struct timespec tsVirt;
|
2021-02-16 14:13:49 +01:00
|
|
|
double timeError; /**< In seconds */
|
2021-07-06 16:28:10 +02:00
|
|
|
double periodEstimate; /**< In seconds */
|
|
|
|
double periodErrorCompensation; /**< In seconds */
|
2021-02-16 14:13:49 +01:00
|
|
|
double period; /**< In seconds */
|
2020-10-08 12:31:20 +02:00
|
|
|
uintmax_t cntEdges;
|
|
|
|
uintmax_t cntSmps;
|
|
|
|
uintmax_t cntSmpsTotal;
|
2021-07-06 16:28:10 +02:00
|
|
|
unsigned horizonCompensation;
|
|
|
|
unsigned horizonEstimation;
|
2021-02-16 14:13:49 +01:00
|
|
|
std::vector<uintmax_t> filterWindow;
|
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) :
|
2021-07-06 16:28:10 +02:00
|
|
|
SingleSignalHook(p, n, fl, prio, en),
|
2021-07-20 12:33:21 +02:00
|
|
|
mode(Mode::SIMPLE),
|
2021-07-20 13:37:19 +02:00
|
|
|
lastSequence(0),
|
2019-08-13 21:22:13 +02:00
|
|
|
lastValue(0),
|
2021-07-06 16:28:10 +02:00
|
|
|
threshold(1.5),
|
2020-10-08 12:31:20 +02:00
|
|
|
isSynced(false),
|
|
|
|
isLocked(false),
|
2021-02-16 14:13:49 +01:00
|
|
|
timeError(0.0),
|
2021-07-06 16:28:10 +02:00
|
|
|
periodEstimate(0.0),
|
|
|
|
periodErrorCompensation(0.0),
|
2020-10-08 12:31:20 +02:00
|
|
|
period(0.0),
|
|
|
|
cntEdges(0),
|
2021-02-16 13:25:37 +01:00
|
|
|
cntSmps(0),
|
|
|
|
cntSmpsTotal(0),
|
2021-07-06 16:28:10 +02:00
|
|
|
horizonCompensation(10),
|
|
|
|
horizonEstimation(10),
|
|
|
|
filterWindow(horizonEstimation + 1, 0)
|
2021-02-16 14:13:49 +01:00
|
|
|
{ }
|
2019-08-13 21:22:13 +02:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
virtual void parse(json_t *json)
|
2019-08-13 21:22:13 +02:00
|
|
|
{
|
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
|
|
|
|
2021-07-12 12:43:35 +02:00
|
|
|
SingleSignalHook::parse(json);
|
2020-08-28 19:49:36 +02:00
|
|
|
|
2021-07-20 12:33:21 +02:00
|
|
|
const char *mode_str = nullptr;
|
|
|
|
|
2020-10-08 12:31:20 +02:00
|
|
|
double fSmps = 0;
|
2021-07-09 19:05:40 +02:00
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s?: s, s?: f, s: F, s?: i, s?: i }",
|
|
|
|
"mode", &mode_str,
|
2021-07-06 16:28:10 +02:00
|
|
|
"threshold", &threshold,
|
2021-07-12 17:52:27 +02:00
|
|
|
"expected_smp_rate", &fSmps,
|
|
|
|
"horizon_estimation", &horizonEstimation,
|
|
|
|
"horizon_compensation", &horizonCompensation
|
2020-09-05 18:42:12 +00:00
|
|
|
);
|
|
|
|
if (ret)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json, err, "node-config-hook-pps_ts");
|
2020-10-08 12:31:20 +02:00
|
|
|
|
|
|
|
period = 1.0 / fSmps;
|
2019-08-13 21:22:13 +02:00
|
|
|
|
2021-07-20 12:48:24 +02:00
|
|
|
if (mode_str) {
|
|
|
|
if (!strcmp(mode_str, "simple"))
|
|
|
|
mode = Mode::SIMPLE;
|
|
|
|
else if (!strcmp(mode_str, "horizon"))
|
|
|
|
mode = Mode::HORIZON;
|
|
|
|
else
|
|
|
|
throw ConfigError(json, "node-config-hook-pps_ts-mode", "Unsupported mode: {}", mode_str);
|
|
|
|
}
|
2020-09-05 18:42:12 +00:00
|
|
|
|
|
|
|
state = State::PARSED;
|
2019-08-13 21:22:13 +02:00
|
|
|
}
|
|
|
|
|
2021-07-20 12:33:21 +02:00
|
|
|
virtual villas::node::Hook::Reason process(struct sample *smp)
|
|
|
|
{
|
|
|
|
switch (mode) {
|
|
|
|
case Mode::SIMPLE:
|
|
|
|
return processSimple(smp);
|
2021-07-12 17:52:27 +02:00
|
|
|
|
2021-07-20 12:33:21 +02:00
|
|
|
case Mode::HORIZON:
|
|
|
|
return processHorizon(smp);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return Reason::ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
villas::node::Hook::Reason processSimple(struct sample *smp)
|
2021-07-12 17:52:27 +02:00
|
|
|
{
|
|
|
|
assert(state == State::STARTED);
|
2021-07-20 12:33:21 +02:00
|
|
|
|
2021-07-12 17:52:27 +02:00
|
|
|
/* Get value of PPS signal */
|
|
|
|
float value = smp->data[signalIndex].f; // TODO check if it is really float
|
2021-07-20 12:33:21 +02:00
|
|
|
|
2021-07-12 17:52:27 +02:00
|
|
|
/* Detect Edge */
|
|
|
|
bool isEdge = lastValue < threshold && value > threshold;
|
|
|
|
if (isEdge) {
|
|
|
|
tsVirt.tv_sec = time(nullptr);
|
|
|
|
tsVirt.tv_nsec = 0;
|
|
|
|
period = 1.0 / cntSmps;
|
|
|
|
cntSmps = 0;
|
|
|
|
cntEdges++;
|
|
|
|
} else {
|
|
|
|
struct timespec tsPeriod = time_from_double(period);
|
|
|
|
tsVirt = time_add(&tsVirt, &tsPeriod);
|
|
|
|
}
|
|
|
|
|
2021-07-20 12:33:21 +02:00
|
|
|
lastValue = value;
|
2021-07-12 17:52:27 +02:00
|
|
|
cntSmps++;
|
|
|
|
|
|
|
|
if (cntEdges < 5)
|
|
|
|
return Hook::Reason::SKIP_SAMPLE;
|
|
|
|
|
|
|
|
smp->ts.origin = tsVirt;
|
|
|
|
smp->flags |= (int) SampleFlags::HAS_TS_ORIGIN;
|
|
|
|
|
|
|
|
if ((smp->sequence - lastSequence) > 1)
|
|
|
|
logger->warn("Samples missed: {} sampled missed", smp->sequence - lastSequence);
|
|
|
|
|
|
|
|
lastSequence = smp->sequence;
|
|
|
|
return Hook::Reason::OK;
|
2021-07-20 12:33:21 +02:00
|
|
|
}
|
2021-07-12 17:52:27 +02:00
|
|
|
|
2021-07-20 12:33:21 +02:00
|
|
|
villas::node::Hook::Reason processHorizon(struct 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
|
|
|
|
|
|
|
/* Get value of PPS signal */
|
2021-07-06 16:28:10 +02:00
|
|
|
float value = smp->data[signalIndex].f; // TODO check if it is really float
|
2019-08-13 21:22:13 +02:00
|
|
|
|
|
|
|
/* Detect Edge */
|
2021-07-06 16:28:10 +02:00
|
|
|
bool isEdge = lastValue < threshold && value > threshold;
|
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
|
|
|
|
2019-08-13 21:22:13 +02:00
|
|
|
if (isEdge) {
|
2020-10-08 12:31:20 +02:00
|
|
|
if (isSynced) {
|
2020-11-10 19:00:17 +01:00
|
|
|
if(tsVirt.tv_nsec > 0.5e9)
|
2021-02-16 14:13:49 +01:00
|
|
|
timeError += 1.0 - (tsVirt.tv_nsec / 1.0e9);
|
2020-11-10 19:00:17 +01:00
|
|
|
else
|
2021-02-16 14:13:49 +01:00
|
|
|
timeError -= (tsVirt.tv_nsec / 1.0e9);
|
2020-11-10 19:00:17 +01:00
|
|
|
|
2021-02-10 13:06:19 +01:00
|
|
|
|
2021-02-16 14:13:49 +01:00
|
|
|
filterWindow[cntEdges % filterWindow.size()] = cntSmpsTotal;
|
2021-07-06 16:28:10 +02:00
|
|
|
/* Estimated sample period over last 'horizonEstimation' seconds */
|
|
|
|
unsigned int tmp = cntEdges < filterWindow.size() ? cntEdges : horizonEstimation;
|
2021-02-16 14:13:49 +01:00
|
|
|
double cntSmpsAvg = (cntSmpsTotal - filterWindow[(cntEdges - tmp) % filterWindow.size()]) / tmp;
|
2021-07-06 16:28:10 +02:00
|
|
|
periodEstimate = 1.0 / cntSmpsAvg;
|
|
|
|
periodErrorCompensation = timeError / (cntSmpsAvg * horizonCompensation);
|
|
|
|
period = periodEstimate + periodErrorCompensation;
|
2020-10-08 12:31:20 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
tsVirt.tv_sec = time(nullptr);
|
|
|
|
tsVirt.tv_nsec = 0;
|
|
|
|
isSynced = true;
|
|
|
|
cntEdges = 0;
|
|
|
|
cntSmpsTotal = 0;
|
|
|
|
}
|
|
|
|
cntSmps = 0;
|
|
|
|
cntEdges++;
|
|
|
|
|
2021-07-06 16:28:10 +02:00
|
|
|
logger->debug("Time Error is: {} periodEstimate {} periodErrorCompensation {}", timeError, periodEstimate, periodErrorCompensation);
|
2020-09-10 14:53:50 +02:00
|
|
|
}
|
|
|
|
|
2020-10-08 12:31:20 +02:00
|
|
|
cntSmps++;
|
|
|
|
cntSmpsTotal++;
|
2020-09-14 16:38:15 +02:00
|
|
|
|
2020-10-08 12:31:20 +02:00
|
|
|
if (cntEdges < 5)
|
2020-09-14 16:03:36 +02:00
|
|
|
return Hook::Reason::SKIP_SAMPLE;
|
|
|
|
|
2020-10-08 12:31:20 +02:00
|
|
|
smp->ts.origin = tsVirt;
|
2019-10-03 11:44:29 +02:00
|
|
|
smp->flags |= (int) SampleFlags::HAS_TS_ORIGIN;
|
2019-08-13 21:22:13 +02:00
|
|
|
|
2020-10-08 12:31:20 +02:00
|
|
|
struct timespec tsPeriod = time_from_double(period);
|
|
|
|
tsVirt = time_add(&tsVirt, &tsPeriod);
|
|
|
|
|
|
|
|
|
2021-02-16 11:43:53 +01:00
|
|
|
if ((smp->sequence - lastSequence) > 1)
|
2021-02-16 14:15:14 +01:00
|
|
|
logger->warn("Samples missed: {} sampled missed", smp->sequence - lastSequence);
|
2020-09-10 14:53:50 +02:00
|
|
|
|
2021-02-16 11:43:53 +01:00
|
|
|
lastSequence = smp->sequence;
|
2020-09-10 14:53:50 +02:00
|
|
|
|
2019-10-03 11:44:29 +02:00
|
|
|
return Hook::Reason::OK;
|
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 */
|
|
|
|
|
|
|
|
/** @} */
|