WarpTwin
Documentation for WarpTwin models and classes.
Loading...
Searching...
No Matches
LatencyUtil.hpp
Go to the documentation of this file.
1/******************************************************************************
2* Copyright (c) ATTX INC 2025. All Rights Reserved.
3*
4* This software and associated documentation (the "Software") are the
5* proprietary and confidential information of ATTX, INC. The Software is
6* furnished under a license agreement between ATTX and the user organization
7* and may be used or copied only in accordance with the terms of the agreement.
8* Refer to 'license/attx_license.adoc' for standard license terms.
9*
10* EXPORT CONTROL NOTICE: THIS SOFTWARE MAY INCLUDE CONTENT CONTROLLED UNDER THE
11* INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) OR THE EXPORT ADMINISTRATION
12* REGULATIONS (EAR99). No part of the Software may be used, reproduced, or
13* transmitted in any form or by any means, for any purpose, without the express
14* written permission of ATTX, INC.
15******************************************************************************/
16/*
17Latency Util file
18
19Author: Will Burken
20*/
21
22#ifndef MODELS_SUPPORT_LATENCY_UTIL_H
23#define MODELS_SUPPORT_LATENCY_UTIL_H
24
26#include <queue>
27
28namespace warptwin {
29
40 template <typename T>
42 public:
43 // Model params
44 // NAME TYPE DEFAULT VALUE
45
46 // Model-specific implementations of startup and derivative
48
59 int start(int latency = 0, T start_val = T());
60
71 int step(T input_value, T &output_value);
72
75 int reset();
76
77 protected:
78 std::queue<T> _latency_queue;
82 bool _first_run = true;
83 };
84
85
86 template <typename T>
87 int LatencyUtil<T>::start(int latency, T start_val) {
88
89 _lat_sim_rate = _exc.rate(); // Get the simulation rate in Hz
90 _default_value = start_val; // Set the default fill value
91
92 // Check if simulation rate is zero hz (this means that the simulation step size is 0.0 seconds)
93 // This check avoids a divide by zerror error
94 if (_lat_sim_rate.asFrequency() < 1e-15) {
96 }
97
98 // Convert latency to step size and check for valid range
99 int step_size = std::ceil((double)latency / ((double)_exc.rate().asMilliseconds())); // Convert latency and sim rate to steps
100 if(step_size < 0) {
101 return ERROR_INVALID_RANGE;
102 }
103
104 // Fill the queue with the start value
105 for(int i = 0; i < step_size; i++) {
106 _latency_queue.push(start_val);
107 }
108
109 // Warn the user if the latency does not line up with the simulaiton rate
110 if (not (std::abs(std::fmod((double)latency, _exc.rate().asMilliseconds())) < 1e-10)) {
112 }
113
114 return NO_ERROR;
115 }
116
117 template <typename T>
118 int LatencyUtil<T>::step(T input_value, T &output_value) {
119 // Check if the simulation rate has changed
120 // Get the current simulation rate
121 clockwerk::Time current_rate;
122 _exc.time().step_end_time().subtract( _exc.time().step_start_time(), current_rate); // Get the current simulation rate
123
124 // Convert the current rate to frequency and check for a significant change
125 if (std::abs(current_rate.asFrequency() - _lat_sim_rate.asFrequency()) > 0.001) {
126 // Also check if the current_rate is zero, if so then we are preforming the zero step at the beginning of run and do not want to throw error
127 if (current_rate.asFrequency() > 1e-15 and not _first_run) {
128 // If the simulation rate has changed, throw an error
129 std::string error = "LatencyUtil: Simulation rate has changed from "
130 + std::to_string(_lat_sim_rate.asFloatingPoint())
131 + " to " + std::to_string(current_rate.asFloatingPoint());
132 _exc.os().sysLog(0.0, "", error.c_str(), error.size());
133 return ERROR_STEP_OVERRUN;
134 }
135 // Set first run boolean tracker to false
136 _first_run = false;
137 }
138
139 // Read the input value and write it to the queue
140 _latency_queue.push(input_value);
141
142 // Ensure we have a value to output
143 if(_latency_queue.empty()) {
144 return ERROR_QUEUE_EMPTY;
145 }
146
147 // Take the output value from the front of the queue
148 output_value = _latency_queue.front();
149 _latency_queue.pop();
150
151 return NO_ERROR;
152 }
153
154 template <typename T>
156
157 // For the size of the queue, add default value and discard oldest value
158 for (typename std::queue<T>::size_type i = 0; i < _latency_queue.size(); ++i) {
160 _latency_queue.pop();
161 }
162
163 return NO_ERROR;
164 }
165
166}
167
168#endif
Wrapper to manage and convert time as timespce.
Definition Time.h:53
floating_point asFloatingPoint() const
Definition Time.h:80
floating_point asFrequency()
Function to convert time to frequency in Hz.
Definition Time.cpp:36
bool _first_run
Tracks the first time the model is stepped.
Definition LatencyUtil.hpp:82
SimulationExecutive & _exc
Reference to the simulation executive.
Definition LatencyUtil.hpp:79
clockwerk::Time _lat_sim_rate
Simulation rate in milliseconds.
Definition LatencyUtil.hpp:80
int step(T input_value, T &output_value)
Step the latency model.
Definition LatencyUtil.hpp:118
int reset()
Function for reseting the latency queue.
Definition LatencyUtil.hpp:155
int start(int latency=0, T start_val=T())
Starting the latency model.
Definition LatencyUtil.hpp:87
LatencyUtil(SimulationExecutive &exc)
Definition LatencyUtil.hpp:47
T _default_value
Default value to fill the queue with.
Definition LatencyUtil.hpp:81
std::queue< T > _latency_queue
Buffer to hold latency values.
Definition LatencyUtil.hpp:78
Implementation of the executive class for simulation.
Definition SimulationExecutive.h:64
#define ERROR_STEP_OVERRUN
Error in the case where dynamics model overruns steps or gets out of whack.
Definition clockwerkerrors.h:153
#define ERROR_QUEUE_EMPTY
Error in case the queue is empty.
Definition clockwerkerrors.h:198
#define ERROR_INVALID_RANGE
Definition clockwerkerrors.h:53
#define ERROR_DIVIDE_BY_ZERO
Error code in the case where some fool tried to divide by zero.
Definition clockwerkerrors.h:49
#define WARNING_POORLY_MATCHED_RATES
Warning when certain a simulation and model rate don't match well and may cause issues.
Definition clockwerkerrors.h:169
Class to propagate CR3BP dynamics in characteristic units.
Definition statistics.hpp:22
int16 start() override
Class to execute logging.
const int NO_ERROR
Definition simlicense.cpp:30