1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/lib/hooks/pps_ts.cpp

177 lines
4.4 KiB
C++
Raw Permalink Normal View History

/** 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
* @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 <cinttypes>
#include <vector>
#include <villas/hook.hpp>
#include <villas/timing.h>
#include <villas/sample.h>
namespace villas {
namespace node {
class PpsTsHook : public Hook {
protected:
double lastValue;
double thresh;
unsigned idx;
2021-02-16 11:43:53 +01:00
uint64_t lastSequence;
bool isSynced;
bool isLocked;
struct timespec tsVirt;
double timeError; /**< In seconds */
double periodEst; /**< In seconds */
double periodErrComp; /**< In seconds */
double period; /**< In seconds */
uintmax_t cntEdges;
uintmax_t cntSmps;
uintmax_t cntSmpsTotal;
unsigned horizonComp;
unsigned horizonEst;
std::vector<uintmax_t> filterWindow;
public:
2020-08-25 21:00:52 +02:00
PpsTsHook(struct vpath *p, struct vnode *n, int fl, int prio, bool en = true) :
Hook(p, n, fl, prio, en),
lastValue(0),
thresh(1.5),
idx(0),
2021-02-16 11:43:53 +01:00
lastSequence(0),
isSynced(false),
isLocked(false),
timeError(0.0),
2021-02-16 13:25:37 +01:00
periodEst(0.0),
periodErrComp(0.0),
period(0.0),
cntEdges(0),
2021-02-16 13:25:37 +01:00
cntSmps(0),
cntSmpsTotal(0),
2020-10-14 11:38:24 +02:00
horizonComp(10),
horizonEst(10),
filterWindow(horizonEst + 1, 0)
{ }
2021-02-16 14:15:14 +01:00
virtual void parse(json_t *json)
{
2020-09-05 18:42:12 +00:00
int ret;
json_error_t err;
2020-09-05 18:42:12 +00:00
assert(state != State::STARTED);
2021-02-16 14:15:14 +01:00
Hook::parse(json);
2020-08-28 19:49:36 +02:00
double fSmps = 0;
2021-02-16 14:15:14 +01:00
ret = json_unpack_ex(json, &err, 0, "{ s: i, s?: f, s: F}",
2020-09-05 18:42:12 +00:00
"signal_index", &idx,
"threshold", &thresh,
"expected_smp_rate", &fSmps
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");
period = 1.0 / fSmps;
2021-02-16 14:15:14 +01:00
logger->debug("Parsed config thresh={} signal_index={} nominal_period={}", thresh, idx, period);
2020-09-05 18:42:12 +00:00
state = State::PARSED;
}
2021-06-29 10:45:12 -04:00
virtual villas::node::Hook::Reason process(struct sample *smp)
{
2019-10-03 11:44:29 +02:00
assert(state == State::STARTED);
/* Get value of PPS signal */
float value = smp->data[idx].f; // TODO check if it is really float
/* Detect Edge */
bool isEdge = lastValue < thresh && value > thresh;
2020-09-11 18:44:47 +02:00
lastValue = value;
2020-09-11 18:44:47 +02:00
if (isEdge) {
if (isSynced) {
if(tsVirt.tv_nsec > 0.5e9)
timeError += 1.0 - (tsVirt.tv_nsec / 1.0e9);
else
timeError -= (tsVirt.tv_nsec / 1.0e9);
2021-02-10 13:06:19 +01:00
filterWindow[cntEdges % filterWindow.size()] = cntSmpsTotal;
/* Estimated sample period over last 'horizonEst' seconds */
unsigned int tmp = cntEdges < filterWindow.size() ? cntEdges : horizonEst;
double cntSmpsAvg = (cntSmpsTotal - filterWindow[(cntEdges - tmp) % filterWindow.size()]) / tmp;
periodEst = 1.0 / cntSmpsAvg;
periodErrComp = timeError / (cntSmpsAvg * horizonComp);
period = periodEst + periodErrComp;
}
else {
tsVirt.tv_sec = time(nullptr);
tsVirt.tv_nsec = 0;
isSynced = true;
cntEdges = 0;
cntSmpsTotal = 0;
}
cntSmps = 0;
cntEdges++;
2021-02-16 14:15:14 +01:00
logger->debug("Time Error is: {} periodEst {} periodErrComp {}", timeError, periodEst, periodErrComp);
}
cntSmps++;
cntSmpsTotal++;
2020-09-14 16:38:15 +02:00
if (cntEdges < 5)
2020-09-14 16:03:36 +02:00
return Hook::Reason::SKIP_SAMPLE;
smp->ts.origin = tsVirt;
2019-10-03 11:44:29 +02:00
smp->flags |= (int) SampleFlags::HAS_TS_ORIGIN;
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);
2021-02-16 11:43:53 +01:00
lastSequence = smp->sequence;
2019-10-03 11:44:29 +02:00
return Hook::Reason::OK;
}
};
/* Register hook */
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;
} /* namespace node */
} /* namespace villas */
/** @} */