Skip to content

Commit 24cee81

Browse files
oystubdagar
authored andcommitted
gps_blending: output valid time_utc_usec
Before this fix, the time_utc_usec output from blending was always 0. This means that you wouldn't get a valid vehicle_gps_position/time_utc_usec With this commit, UTC timestamps are blended according to weights for all GPSes with a nonzero UTC timestamp value. It would be possible to simply use the first valid UTC timestamp instead of blending, but since the system timestamps are blended, it seems suitable to blend UTC timestamps as well.
1 parent c769fc7 commit 24cee81

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/modules/sensors/vehicle_gps_position/gps_blending.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,21 @@ sensor_gps_s GpsBlending::gps_blend_states(float blend_weights[GPS_MAX_RECEIVERS
493493
gps_blended_state.heading_accuracy = _gps_state[gps_best_yaw_index].heading_accuracy;
494494
}
495495

496+
// Blend UTC timestamp from all receivers that are publishing a valid time_utc_usec value
497+
double utc_weight_sum = 0.0;
498+
double utc_time_sum = 0.0;
499+
500+
for (uint8_t i = 0; i < GPS_MAX_RECEIVERS_BLEND; i++) {
501+
if (_gps_state[i].time_utc_usec > 0) {
502+
utc_time_sum += (double)_gps_state[i].time_utc_usec * (double)blend_weights[i];
503+
utc_weight_sum += (double)blend_weights[i];
504+
}
505+
}
506+
507+
if (utc_weight_sum > 0.0) {
508+
gps_blended_state.time_utc_usec = (uint64_t)(utc_time_sum / utc_weight_sum);
509+
}
510+
496511
return gps_blended_state;
497512
}
498513

src/modules/sensors/vehicle_gps_position/gps_blending_test.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,31 @@ TEST_F(GpsBlendingTest, dualReceiverFailover)
275275
EXPECT_EQ(gps_blending.getSelectedGps(), 0);
276276
EXPECT_TRUE(gps_blending.isNewOutputDataAvailable());
277277
}
278+
279+
TEST_F(GpsBlendingTest, dualReceiverUTCTime)
280+
{
281+
GpsBlending gps_blending;
282+
sensor_gps_s gps_data0 = getDefaultGpsData();
283+
sensor_gps_s gps_data1 = getDefaultGpsData();
284+
285+
// WHEN: Only GPS1 has a nonzero UTC time
286+
gps_blending = GpsBlending();
287+
gps_data1.time_utc_usec = 1700000000000000ULL;
288+
gps_blending.setGpsData(gps_data0, 0);
289+
gps_blending.setGpsData(gps_data1, 1);
290+
gps_blending.setBlendingUseHPosAccuracy(true);
291+
gps_blending.update(_time_now_us);
292+
// THEN: GPS 1 time should be used
293+
EXPECT_EQ(gps_blending.getOutputGpsData().time_utc_usec, gps_data1.time_utc_usec);
294+
295+
// WHEN: Both GPSes have a nonzero UTC time
296+
gps_blending = GpsBlending();
297+
gps_data0.time_utc_usec = 1700000000001000ULL;
298+
gps_data1.time_utc_usec = 1700000000000000ULL;
299+
gps_blending.setGpsData(gps_data0, 0);
300+
gps_blending.setGpsData(gps_data1, 1);
301+
gps_blending.setBlendingUseHPosAccuracy(true);
302+
gps_blending.update(_time_now_us);
303+
// THEN: The average of the two timestamps should be used
304+
EXPECT_EQ(gps_blending.getOutputGpsData().time_utc_usec, 1700000000000500ULL);
305+
}

0 commit comments

Comments
 (0)