0.5.1
Loading...
Searching...
No Matches
Delay.cpp
Go to the documentation of this file.
1// This file is part of INSTINCT, the INS Toolkit for Integrated
2// Navigation Concepts and Training by the Institute of Navigation of
3// the University of Stuttgart, Germany.
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9#include "Delay.hpp"
10
12
13#include "NodeData/NodeData.hpp"
14
15namespace NAV::experimental
16{
17
19 : Node("z^-1")
20{
21 LOG_TRACE("{}: called", name);
22
23 _hasConfig = true;
24 _guiConfigDefaultWindowSize = { 305, 70 };
25 kind = Kind::Simple;
26
29}
30
32{
33 LOG_TRACE("{}: called", nameId());
34}
35
36std::string Delay::typeStatic()
37{
38 return "Delay";
39}
40
41std::string Delay::type() const
42{
43 return typeStatic();
44}
45
46std::string Delay::category()
47{
48 return "Experimental/Simple";
49}
50
52{
53 if (ImGui::InputInt(fmt::format("Delay length##{}", size_t(id)).c_str(), &_delayLength))
54 {
55 _delayLength = std::max(_delayLength, 1);
56 LOG_DEBUG("{}: delayLength changed to {}", nameId(), _delayLength);
57 if (name.starts_with("z^-"))
58 {
59 name = fmt::format("z^-{}", _delayLength);
60 }
61
62 while (_buffer.size() > static_cast<size_t>(_delayLength))
63 {
64 _buffer.pop_front();
65 }
66 }
67}
68
69[[nodiscard]] json Delay::save() const
70{
71 LOG_TRACE("{}: called", nameId());
72
73 json j;
74
75 j["delayLength"] = _delayLength;
76
77 return j;
78}
79
80void Delay::restore(json const& j)
81{
82 LOG_TRACE("{}: called", nameId());
83
84 if (j.contains("delayLength"))
85 {
86 j.at("delayLength").get_to(_delayLength);
87 }
88}
89
91{
92 LOG_TRACE("{}: called", nameId());
93
94 _buffer.clear();
95
96 return true;
97}
98
100{
101 LOG_TRACE("{}: called", nameId());
102}
103
104bool Delay::onCreateLink(OutputPin& startPin, InputPin& endPin)
105{
106 LOG_TRACE("{}: called for {} ==> {}", nameId(), size_t(startPin.id), size_t(endPin.id));
107
108 if (endPin.parentNode->id != id)
109 {
110 return true; // Link on Output Port
111 }
112
113 // New Link on the Input port, but the previously connected dataIdentifier is different from the new one.
114 // Then remove all links.
115 if (outputPins.at(OUTPUT_PORT_INDEX_FLOW).dataIdentifier != startPin.dataIdentifier)
116 {
117 outputPins.at(OUTPUT_PORT_INDEX_FLOW).deleteLinks();
118 }
119
120 // Update the dataIdentifier of the output pin to the same as input pin
121 outputPins.at(OUTPUT_PORT_INDEX_FLOW).dataIdentifier = startPin.dataIdentifier;
122
123 // Refresh all links connected to the output pin
124 for (auto& link : outputPins.at(OUTPUT_PORT_INDEX_FLOW).links)
125 {
126 if (auto* connectedPin = link.getConnectedPin())
127 {
128 outputPins.at(OUTPUT_PORT_INDEX_FLOW).recreateLink(*connectedPin);
129 }
130 }
131
132 return true;
133}
134
135void Delay::delayObs(NAV::InputPin::NodeDataQueue& queue, size_t /* pinIdx */)
136{
137 if (_buffer.size() == static_cast<size_t>(_delayLength))
138 {
139 auto oldest = _buffer.front();
140 _buffer.pop_front();
141 _buffer.push_back(queue.extract_front());
142
143 LOG_DATA("{}: Delay pushing out message: {}", nameId(), _buffer.back()->insTime.toGPSweekTow());
144
146 {
148 }
149
151 }
152 else
153 {
154 _buffer.push_back(queue.extract_front());
155 }
156}
157
158} // namespace NAV::experimental
Delay Node.
Save/Load the Nodes.
nlohmann::json json
json namespace
#define LOG_DEBUG
Debug information. Should not be called on functions which receive observations (spamming)
Definition Logger.hpp:67
#define LOG_DATA
All output which occurs repeatedly every time observations are received.
Definition Logger.hpp:29
#define LOG_TRACE
Detailled info to trace the execution of the program. Should not be called on functions which receive...
Definition Logger.hpp:65
Abstract NodeData Class.
Input pins of nodes.
Definition Pin.hpp:491
TsDeque< std::shared_ptr< const NAV::NodeData > > NodeDataQueue
Node data queue type.
Definition Pin.hpp:707
static std::string type()
Returns the type of the data class.
Definition NodeData.hpp:45
ImVec2 _guiConfigDefaultWindowSize
Definition Node.hpp:522
std::vector< OutputPin > outputPins
List of output pins.
Definition Node.hpp:511
Node(std::string name)
Constructor.
Definition Node.cpp:29
Kind kind
Kind of the Node.
Definition Node.hpp:505
OutputPin * CreateOutputPin(const char *name, Pin::Type pinType, const std::vector< std::string > &dataIdentifier, OutputPin::PinData data=static_cast< void * >(nullptr), int idx=-1)
Create an Output Pin object.
Definition Node.cpp:278
bool callbacksEnabled
Enables the callbacks.
Definition Node.hpp:514
std::string nameId() const
Node name and id.
Definition Node.cpp:323
std::string name
Name of the Node.
Definition Node.hpp:507
InputPin * CreateInputPin(const char *name, Pin::Type pinType, const std::vector< std::string > &dataIdentifier={}, InputPin::Callback callback=static_cast< InputPin::FlowFirableCallbackFunc >(nullptr), InputPin::FlowFirableCheckFunc firable=nullptr, int priority=0, int idx=-1)
Create an Input Pin object.
Definition Node.cpp:252
void invokeCallbacks(size_t portIndex, const std::shared_ptr< const NodeData > &data)
Calls all registered callbacks on the specified output port.
Definition Node.cpp:179
ax::NodeEditor::NodeId id
Unique Id of the Node.
Definition Node.hpp:503
bool _hasConfig
Flag if the config window should be shown.
Definition Node.hpp:525
Output pins of nodes.
Definition Pin.hpp:338
Node * parentNode
Reference to the parent node.
Definition Pin.hpp:307
ax::NodeEditor::PinId id
Unique Id of the Pin.
Definition Pin.hpp:297
std::vector< std::string > dataIdentifier
One or multiple Data Identifiers (Unique name which is used for data flows)
Definition Pin.hpp:305
auto extract_front()
Returns a copy of the first element in the container and removes it from the container.
Definition TsDeque.hpp:494
json save() const override
Saves the node into a json object.
Definition Delay.cpp:69
bool onCreateLink(OutputPin &startPin, InputPin &endPin) override
Called when a new link is to be established.
Definition Delay.cpp:104
void guiConfig() override
ImGui config window which is shown on double click.
Definition Delay.cpp:51
int _delayLength
The amount to delay messages for.
Definition Delay.hpp:83
static std::string category()
String representation of the Class Category.
Definition Delay.cpp:46
Delay()
Default constructor.
Definition Delay.cpp:18
~Delay() override
Destructor.
Definition Delay.cpp:31
static constexpr size_t OUTPUT_PORT_INDEX_FLOW
Flow.
Definition Delay.hpp:68
std::string type() const override
String representation of the Class Type.
Definition Delay.cpp:41
void delayObs(InputPin::NodeDataQueue &queue, size_t pinIdx)
Delays the observation.
Definition Delay.cpp:135
void deinitialize() override
Deinitialize the node.
Definition Delay.cpp:99
std::deque< std::shared_ptr< const NodeData > > _buffer
Buffer to delay data.
Definition Delay.hpp:86
static std::string typeStatic()
String representation of the Class Type.
Definition Delay.cpp:36
bool initialize() override
Initialize the node.
Definition Delay.cpp:90
void restore(const json &j) override
Restores the node from a json object.
Definition Delay.cpp:80
@ Flow
NodeData Trigger.
Definition Pin.hpp:52