0.4.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
12namespace nm = NAV::NodeManager;
14
15#include "NodeData/NodeData.hpp"
16
17namespace NAV::experimental
18{
19
21 : Node("z^-1")
22{
23 LOG_TRACE("{}: called", name);
24
25 _hasConfig = true;
26 _guiConfigDefaultWindowSize = { 305, 70 };
27 kind = Kind::Simple;
28
31}
32
34{
35 LOG_TRACE("{}: called", nameId());
36}
37
38std::string Delay::typeStatic()
39{
40 return "Delay";
41}
42
43std::string Delay::type() const
44{
45 return typeStatic();
46}
47
48std::string Delay::category()
49{
50 return "Experimental/Simple";
51}
52
54{
55 if (ImGui::InputInt(fmt::format("Delay length##{}", size_t(id)).c_str(), &_delayLength))
56 {
57 _delayLength = std::max(_delayLength, 1);
58 LOG_DEBUG("{}: delayLength changed to {}", nameId(), _delayLength);
59 if (name.starts_with("z^-"))
60 {
61 name = fmt::format("z^-{}", _delayLength);
62 }
63
64 while (_buffer.size() > static_cast<size_t>(_delayLength))
65 {
66 _buffer.pop_front();
67 }
68 }
69}
70
71[[nodiscard]] json Delay::save() const
72{
73 LOG_TRACE("{}: called", nameId());
74
75 json j;
76
77 j["delayLength"] = _delayLength;
78
79 return j;
80}
81
82void Delay::restore(json const& j)
83{
84 LOG_TRACE("{}: called", nameId());
85
86 if (j.contains("delayLength"))
87 {
88 j.at("delayLength").get_to(_delayLength);
89 }
90}
91
93{
94 LOG_TRACE("{}: called", nameId());
95
96 _buffer.clear();
97
98 return true;
99}
100
102{
103 LOG_TRACE("{}: called", nameId());
104}
105
106bool Delay::onCreateLink(OutputPin& startPin, InputPin& endPin)
107{
108 LOG_TRACE("{}: called for {} ==> {}", nameId(), size_t(startPin.id), size_t(endPin.id));
109
110 if (endPin.parentNode->id != id)
111 {
112 return true; // Link on Output Port
113 }
114
115 // New Link on the Input port, but the previously connected dataIdentifier is different from the new one.
116 // Then remove all links.
117 if (outputPins.at(OUTPUT_PORT_INDEX_FLOW).dataIdentifier != startPin.dataIdentifier)
118 {
119 outputPins.at(OUTPUT_PORT_INDEX_FLOW).deleteLinks();
120 }
121
122 // Update the dataIdentifier of the output pin to the same as input pin
123 outputPins.at(OUTPUT_PORT_INDEX_FLOW).dataIdentifier = startPin.dataIdentifier;
124
125 // Refresh all links connected to the output pin
126 for (auto& link : outputPins.at(OUTPUT_PORT_INDEX_FLOW).links)
127 {
128 if (auto* connectedPin = link.getConnectedPin())
129 {
130 outputPins.at(OUTPUT_PORT_INDEX_FLOW).recreateLink(*connectedPin);
131 }
132 }
133
134 return true;
135}
136
137void Delay::delayObs(NAV::InputPin::NodeDataQueue& queue, size_t /* pinIdx */)
138{
139 if (_buffer.size() == static_cast<size_t>(_delayLength))
140 {
141 auto oldest = _buffer.front();
142 _buffer.pop_front();
143 _buffer.push_back(queue.extract_front());
144
145 LOG_DATA("{}: Delay pushing out message: {}", nameId(), _buffer.back()->insTime.toGPSweekTow());
146
148 {
150 }
151
153 }
154 else
155 {
156 _buffer.push_back(queue.extract_front());
157 }
158}
159
160} // 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.
Manages all Nodes.
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:410
std::vector< OutputPin > outputPins
List of output pins.
Definition Node.hpp:399
Node(std::string name)
Constructor.
Definition Node.cpp:30
Kind kind
Kind of the Node.
Definition Node.hpp:393
bool callbacksEnabled
Enables the callbacks.
Definition Node.hpp:402
std::string nameId() const
Node name and id.
Definition Node.cpp:253
std::string name
Name of the Node.
Definition Node.hpp:395
void invokeCallbacks(size_t portIndex, const std::shared_ptr< const NodeData > &data)
Calls all registered callbacks on the specified output port.
Definition Node.cpp:180
ax::NodeEditor::NodeId id
Unique Id of the Node.
Definition Node.hpp:391
bool _hasConfig
Flag if the config window should be shown.
Definition Node.hpp:413
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:71
bool onCreateLink(OutputPin &startPin, InputPin &endPin) override
Called when a new link is to be established.
Definition Delay.cpp:106
void guiConfig() override
ImGui config window which is shown on double click.
Definition Delay.cpp:53
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:48
Delay()
Default constructor.
Definition Delay.cpp:20
~Delay() override
Destructor.
Definition Delay.cpp:33
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:43
void delayObs(InputPin::NodeDataQueue &queue, size_t pinIdx)
Delays the observation.
Definition Delay.cpp:137
void deinitialize() override
Deinitialize the node.
Definition Delay.cpp:101
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:38
bool initialize() override
Initialize the node.
Definition Delay.cpp:92
void restore(const json &j) override
Restores the node from a json object.
Definition Delay.cpp:82
OutputPin * CreateOutputPin(Node *node, 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.
InputPin * CreateInputPin(Node *node, 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.
@ Flow
NodeData Trigger.
Definition Pin.hpp:52