2019-06-27 01:26:13 +02:00
|
|
|
/** Gate 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-06-27 01:26: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 <cmath>
|
|
|
|
#include <string>
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
#include <villas/hook.hpp>
|
|
|
|
#include <villas/node.h>
|
|
|
|
#include <villas/sample.h>
|
2019-06-27 02:20:58 +02:00
|
|
|
#include <villas/timing.h>
|
2019-06-27 01:26:13 +02:00
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
|
2021-07-06 16:28:10 +02:00
|
|
|
class GateHook : public SingleSignalHook {
|
2019-06-27 01:26:13 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
enum class Mode {
|
|
|
|
ABOVE,
|
|
|
|
BELOW,
|
|
|
|
RISING_EDGE,
|
|
|
|
FALLING_EDGE
|
|
|
|
} mode;
|
|
|
|
|
|
|
|
double threshold;
|
|
|
|
double duration;
|
2019-06-27 02:20:58 +02:00
|
|
|
int samples;
|
2019-06-27 01:26:13 +02:00
|
|
|
double previousValue;
|
|
|
|
|
2019-06-27 02:20:58 +02:00
|
|
|
bool active;
|
|
|
|
uint64_t startSequence;
|
|
|
|
timespec startTime;
|
|
|
|
|
2019-06-27 01:26:13 +02:00
|
|
|
public:
|
2020-08-25 21:00:52 +02:00
|
|
|
GateHook(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),
|
2019-06-27 01:26:13 +02:00
|
|
|
mode(Mode::RISING_EDGE),
|
|
|
|
threshold(0.5),
|
2019-06-27 02:20:58 +02:00
|
|
|
duration(-1),
|
|
|
|
samples(-1),
|
|
|
|
previousValue(std::numeric_limits<double>::quiet_NaN()),
|
2020-09-11 14:57:05 +02:00
|
|
|
active(false),
|
|
|
|
startSequence(0)
|
2019-06-27 01:26:13 +02:00
|
|
|
{ }
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
virtual void parse(json_t *json)
|
2019-06-27 01:26:13 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
json_error_t err;
|
|
|
|
|
|
|
|
const char *mode_str;
|
|
|
|
|
|
|
|
assert(state != State::STARTED);
|
|
|
|
|
2021-07-06 16:28:10 +02:00
|
|
|
SingleSignalHook::parse(json);
|
2020-08-28 19:49:36 +02:00
|
|
|
|
2021-07-06 16:28:10 +02:00
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s?: F, s?: F, s?: i, s?: s }",
|
2019-06-27 01:26:13 +02:00
|
|
|
"threshold", &threshold,
|
|
|
|
"duration", &duration,
|
2019-06-27 02:20:58 +02:00
|
|
|
"samples", &samples,
|
2019-06-27 01:26:13 +02:00
|
|
|
"mode", &mode_str
|
|
|
|
);
|
|
|
|
if (ret)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json, err, "node-config-hook-gate");
|
2019-06-27 01:26:13 +02:00
|
|
|
|
|
|
|
if (mode_str) {
|
|
|
|
if (!strcmp(mode_str, "above"))
|
|
|
|
mode = Mode::ABOVE;
|
|
|
|
else if (!strcmp(mode_str, "below"))
|
|
|
|
mode = Mode::BELOW;
|
|
|
|
else if (!strcmp(mode_str, "rising_edge"))
|
|
|
|
mode = Mode::RISING_EDGE;
|
|
|
|
else if (!strcmp(mode_str, "falling_edge"))
|
|
|
|
mode = Mode::FALLING_EDGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
state = State::PARSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void prepare()
|
|
|
|
{
|
|
|
|
assert(state == State::CHECKED);
|
|
|
|
|
2021-07-09 19:05:40 +02:00
|
|
|
SingleSignalHook::prepare();
|
|
|
|
|
2019-06-27 01:26:13 +02:00
|
|
|
/* Check if signal type is float */
|
|
|
|
auto sig = (struct signal *) vlist_at(&signals, signalIndex);
|
|
|
|
if (sig->type != SignalType::FLOAT)
|
|
|
|
throw RuntimeError("Gate signal must be of type float");
|
|
|
|
|
|
|
|
state = State::PREPARED;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Hook::Reason process(sample *smp)
|
|
|
|
{
|
|
|
|
assert(state == State::STARTED);
|
|
|
|
|
|
|
|
Hook::Reason reason;
|
2019-06-27 02:20:58 +02:00
|
|
|
double value = smp->data[signalIndex].f;
|
2019-06-27 01:26:13 +02:00
|
|
|
|
2019-06-27 02:20:58 +02:00
|
|
|
if (active) {
|
|
|
|
if (duration > 0 && time_delta(&smp->ts.origin, &startTime) < duration)
|
|
|
|
reason = Reason::OK;
|
|
|
|
else if (samples > 0 && smp->sequence - startSequence < (uint64_t) samples)
|
|
|
|
reason = Reason::OK;
|
|
|
|
else {
|
|
|
|
reason = Reason::SKIP_SAMPLE;
|
|
|
|
active = false;
|
|
|
|
}
|
|
|
|
}
|
2019-06-27 01:26:13 +02:00
|
|
|
|
2019-06-27 02:20:58 +02:00
|
|
|
if (!active) {
|
|
|
|
switch (mode) {
|
|
|
|
case Mode::ABOVE:
|
|
|
|
reason = value > threshold ? Reason::OK : Reason::SKIP_SAMPLE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Mode::BELOW:
|
|
|
|
reason = value < threshold ? Reason::OK : Reason::SKIP_SAMPLE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Mode::RISING_EDGE:
|
|
|
|
reason = (!std::isnan(previousValue) && value > previousValue) ? Reason::OK : Reason::SKIP_SAMPLE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Mode::FALLING_EDGE:
|
|
|
|
reason = (!std::isnan(previousValue) && value < previousValue) ? Reason::OK : Reason::SKIP_SAMPLE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
reason = Reason::ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reason == Reason::OK) {
|
|
|
|
startTime = smp->ts.origin;
|
|
|
|
startSequence = smp->sequence;
|
|
|
|
active = true;
|
|
|
|
}
|
2019-06-27 01:26:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
previousValue = value;
|
|
|
|
|
|
|
|
return reason;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Register hook */
|
2020-06-14 15:00:02 +02:00
|
|
|
static char n[] = "gate";
|
|
|
|
static char d[] = "Skip samples only if an enable signal is under a specified threshold";
|
|
|
|
static HookPlugin<GateHook, n, d, (int) Hook::Flags::NODE_READ | (int) Hook::Flags::PATH> p;
|
2019-06-27 01:26:13 +02:00
|
|
|
|
|
|
|
} /* namespace node */
|
|
|
|
} /* namespace villas */
|
|
|
|
|
|
|
|
/** @} */
|