@@ -31,15 +31,15 @@ void InputStream::init(const InputChannel & input_channel)
31
31
input_topic_ = input_channel.input_topic ;
32
32
long_name_ = input_channel.long_name ;
33
33
short_name_ = input_channel.short_name ;
34
+ expected_interval_ = input_channel.expected_interval ; // [s]
34
35
35
36
// Initialize queue
36
37
objects_que_.clear ();
37
38
38
39
// Initialize latency statistics
39
- expected_rate_ = input_channel.expected_rate ; // [Hz]
40
40
latency_mean_ = input_channel.expected_latency ; // [s]
41
41
latency_var_ = 0.0 ;
42
- interval_mean_ = 1 / expected_rate_;
42
+ interval_mean_ = expected_interval_; // [s] (initial value)
43
43
interval_var_ = 0.0 ;
44
44
45
45
latest_measurement_time_ = node_.now ();
@@ -84,13 +84,11 @@ void InputStream::updateTimingStatus(const rclcpp::Time & now, const rclcpp::Tim
84
84
85
85
// Calculate interval, Update interval statistics
86
86
if (is_time_initialized_) {
87
- bool is_interval_regular = true ;
88
87
const double interval = (now - latest_message_time_).seconds ();
89
- // check if it is outlier
90
- if (interval > 1.5 * interval_mean_ || interval < 0.5 * interval_mean_) {
91
- // no update for the time statistics
92
- is_interval_regular = false ;
93
- }
88
+ // Check if the interval is regular
89
+ // The interval is considered regular if it is within 0.5 and 1.5 times the expected interval
90
+ bool is_interval_regular =
91
+ interval > 0.5 * expected_interval_ && interval < 1.5 * expected_interval_;
94
92
95
93
if (is_interval_regular) {
96
94
interval_mean_ = (1.0 - gain) * interval_mean_ + gain * interval;
@@ -189,17 +187,33 @@ void InputManager::getObjectTimeInterval(
189
187
const rclcpp::Time & now, rclcpp::Time & object_latest_time,
190
188
rclcpp::Time & object_oldest_time) const
191
189
{
192
- object_latest_time = now - rclcpp::Duration::from_seconds (target_latency_);
193
- object_oldest_time = now - rclcpp::Duration::from_seconds (target_latency_ + target_latency_band_);
194
-
195
- // try to include the latest message of the target stream
190
+ object_latest_time =
191
+ now - rclcpp::Duration::from_seconds (
192
+ target_stream_latency_ -
193
+ 0.1 * target_stream_latency_std_); // object_latest_time with 0.1 sigma safety margin
194
+ // check the target stream can be included in the object time interval
196
195
if (input_streams_.at (target_stream_idx_)->isTimeInitialized ()) {
197
196
rclcpp::Time latest_measurement_time =
198
197
input_streams_.at (target_stream_idx_)->getLatestMeasurementTime ();
198
+ // if the object_latest_time is newer than the next expected message time, set it older
199
+ // than the next expected message time
200
+ rclcpp::Time next_expected_message_time =
201
+ latest_measurement_time +
202
+ rclcpp::Duration::from_seconds (
203
+ target_stream_interval_ -
204
+ 1.0 *
205
+ target_stream_interval_std_); // next expected message time with 1 sigma safety margin
206
+ object_latest_time = object_latest_time > next_expected_message_time
207
+ ? next_expected_message_time
208
+ : object_latest_time;
209
+
210
+ // if the object_latest_time is older than the latest measurement time, set it as the latest
211
+ // object time
199
212
object_latest_time =
200
- object_latest_time > latest_measurement_time ? object_latest_time : latest_measurement_time ;
213
+ object_latest_time < latest_measurement_time ? latest_measurement_time : object_latest_time ;
201
214
}
202
215
216
+ object_oldest_time = object_latest_time - rclcpp::Duration::from_seconds (1.0 );
203
217
// if the object_oldest_time is older than the latest object time, set it to the latest object
204
218
// time
205
219
object_oldest_time =
@@ -209,21 +223,24 @@ void InputManager::getObjectTimeInterval(
209
223
void InputManager::optimizeTimings ()
210
224
{
211
225
double max_latency_mean = 0.0 ;
212
- uint stream_selected_idx = 0 ;
213
- double selected_idx_latency_std = 0.0 ;
214
- double selected_idx_interval = 0.0 ;
226
+ uint selected_stream_idx = 0 ;
227
+ double selected_stream_latency_std = 0.1 ;
228
+ double selected_stream_interval = 0.1 ;
229
+ double selected_stream_interval_std = 0.01 ;
215
230
216
231
{
217
232
// ANALYSIS: Get the streams statistics
233
+ // select the stream that has the maximum latency
218
234
double latency_mean, latency_var, interval_mean, interval_var;
219
235
for (const auto & input_stream : input_streams_) {
220
236
if (!input_stream->isTimeInitialized ()) continue ;
221
237
input_stream->getTimeStatistics (latency_mean, latency_var, interval_mean, interval_var);
222
238
if (latency_mean > max_latency_mean) {
223
239
max_latency_mean = latency_mean;
224
- selected_idx_latency_std = std::sqrt (latency_var);
225
- stream_selected_idx = input_stream->getIndex ();
226
- selected_idx_interval = interval_mean;
240
+ selected_stream_idx = input_stream->getIndex ();
241
+ selected_stream_latency_std = std::sqrt (latency_var);
242
+ selected_stream_interval = interval_mean;
243
+ selected_stream_interval_std = std::sqrt (interval_var);
227
244
}
228
245
229
246
/* DEBUG */
@@ -244,15 +261,12 @@ void InputManager::optimizeTimings()
244
261
245
262
// Set the target stream index, which has the maximum latency
246
263
// trigger will be called next time
247
- target_stream_idx_ = stream_selected_idx;
248
- target_latency_ = max_latency_mean - selected_idx_latency_std;
249
- target_latency_band_ = 2 * selected_idx_latency_std + selected_idx_interval;
250
-
251
- /* DEBUG */
252
- RCLCPP_INFO (
253
- node_.get_logger (), " InputManager::getObjects Target stream: %s, target latency: %f, band: %f" ,
254
- input_streams_.at (target_stream_idx_)->getLongName ().c_str (), target_latency_,
255
- target_latency_band_);
264
+ // if no stream is initialized, the target stream index will be 0 and wait for the initialization
265
+ target_stream_idx_ = selected_stream_idx;
266
+ target_stream_latency_ = max_latency_mean;
267
+ target_stream_latency_std_ = selected_stream_latency_std;
268
+ target_stream_interval_ = selected_stream_interval;
269
+ target_stream_interval_std_ = selected_stream_interval_std;
256
270
}
257
271
258
272
bool InputManager::getObjects (const rclcpp::Time & now, ObjectsList & objects_list)
0 commit comments