diff --git a/core/include/pj_drone_core/mavlink/decoder.hpp b/core/include/pj_drone_core/mavlink/decoder.hpp index 5a17450..bd31383 100644 --- a/core/include/pj_drone_core/mavlink/decoder.hpp +++ b/core/include/pj_drone_core/mavlink/decoder.hpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include @@ -31,9 +30,6 @@ struct Message { std::string topic; // "mav/./" std::shared_ptr schema; std::vector values; // parallel to *schema - /// Hardware time embedded in the payload (`time_usec` or `time_boot_ms`), in nanoseconds. Empty when the message - /// has none or it is zero; callers then use the receive time. - std::optional embedded_timestamp_ns; }; /// Stateful MAVLink byte-stream parser (wraps mavlink_parse_char on channel 0, as the PJ3 plugin did). diff --git a/core/src/mavlink/decoder.cpp b/core/src/mavlink/decoder.cpp index 3481037..2f2b085 100644 --- a/core/src/mavlink/decoder.cpp +++ b/core/src/mavlink/decoder.cpp @@ -12,8 +12,6 @@ namespace pj_drone::mavlink { namespace { constexpr uint8_t kChannel = MAVLINK_COMM_0; -constexpr const char* kFieldTimeUsec = "time_usec"; -constexpr const char* kFieldTimeBootMs = "time_boot_ms"; // Indexed by MAVLINK_TYPE_* (0=CHAR, 1=UINT8, 2=INT8, 3=UINT16, 4=INT16, 5=UINT32, 6=INT32, 7=UINT64, 8=INT64, // 9=FLOAT, 10=DOUBLE). constexpr uint8_t kTypeSize[11] = {1, 1, 1, 2, 2, 4, 4, 8, 8, 4, 8}; @@ -94,32 +92,6 @@ std::shared_ptr buildSchema(const mavlink_message_info_t& info) { return schema; } -// First pass of the PJ3 plugin: the first scalar time_usec (uint64) / time_boot_ms (uint32) field decides; a zero value -// ends the search without a timestamp. -std::optional findEmbeddedTimestamp(const mavlink_message_info_t& info, const char* payload) { - for (unsigned i = 0; i < info.num_fields; ++i) { - const mavlink_field_info_t& f = info.fields[i]; - if (f.array_length != 0) { - continue; - } - if (std::strcmp(f.name, kFieldTimeUsec) == 0 && f.type == MAVLINK_TYPE_UINT64_T) { - const auto v = load(payload, f.wire_offset); - if (v > 0) { - return static_cast(v) * 1000; - } - return std::nullopt; - } - if (std::strcmp(f.name, kFieldTimeBootMs) == 0 && f.type == MAVLINK_TYPE_UINT32_T) { - const auto v = load(payload, f.wire_offset); - if (v > 0) { - return static_cast(v) * 1000000; - } - return std::nullopt; - } - } - return std::nullopt; -} - } // namespace struct StreamParser::Impl { @@ -139,7 +111,6 @@ struct StreamParser::Impl { m.compid = msg.compid; m.msgid = msg.msgid; m.topic = "mav/" + std::to_string(msg.sysid) + "." + std::to_string(msg.compid) + "/" + info->name; - m.embedded_timestamp_ns = findEmbeddedTimestamp(*info, payload); auto& schema = schemas[msg.msgid]; if (!schema) { diff --git a/core/tests/mavlink_decoder_test.cpp b/core/tests/mavlink_decoder_test.cpp index 2c5a075..3c648e2 100644 --- a/core/tests/mavlink_decoder_test.cpp +++ b/core/tests/mavlink_decoder_test.cpp @@ -34,7 +34,7 @@ size_t indexOf(const mav::Message& m, const std::string& name) { } // namespace -TEST(MavlinkDecoder, DecodesAttitudeWithBootTimeAndNativeTypes) { +TEST(MavlinkDecoder, DecodesAttitudeWithNativeTypes) { mavlink_message_t msg; mavlink_msg_attitude_pack(7, 3, &msg, 1500, 0.5F, -0.25F, 1.0F, 0.1F, 0.2F, 0.3F); @@ -47,8 +47,6 @@ TEST(MavlinkDecoder, DecodesAttitudeWithBootTimeAndNativeTypes) { EXPECT_EQ(m.topic, "mav/7.3/ATTITUDE"); EXPECT_EQ(m.sysid, 7); EXPECT_EQ(m.compid, 3); - ASSERT_TRUE(m.embedded_timestamp_ns.has_value()); - EXPECT_EQ(*m.embedded_timestamp_ns, 1500LL * 1000000LL); ASSERT_EQ(m.values.size(), m.schema->size()); const size_t roll = indexOf(m, "roll"); @@ -60,18 +58,7 @@ TEST(MavlinkDecoder, DecodesAttitudeWithBootTimeAndNativeTypes) { EXPECT_EQ(std::get(m.values[boot]), 1500U); } -TEST(MavlinkDecoder, ZeroEmbeddedTimeMeansNoTimestamp) { - mavlink_message_t msg; - mavlink_msg_attitude_pack(1, 1, &msg, 0, 0, 0, 0, 0, 0, 0); - - mav::StreamParser parser; - parser.reset(); - const auto out = parse(parser, toBytes(msg)); - ASSERT_EQ(out.size(), 1U); - EXPECT_FALSE(out[0].embedded_timestamp_ns.has_value()); -} - -TEST(MavlinkDecoder, TimeUsecIsConvertedToNanoseconds) { +TEST(MavlinkDecoder, TimeUsecIsDecodedAsRegularField) { mavlink_message_t msg; const float q[4] = {1.0F, 0.0F, 0.0F, 0.0F}; const float cov[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; @@ -81,25 +68,11 @@ TEST(MavlinkDecoder, TimeUsecIsConvertedToNanoseconds) { parser.reset(); const auto out = parse(parser, toBytes(msg)); ASSERT_EQ(out.size(), 1U); - ASSERT_TRUE(out[0].embedded_timestamp_ns.has_value()); - EXPECT_EQ(*out[0].embedded_timestamp_ns, 1700000000123456000LL); const size_t usec = indexOf(out[0], "time_usec"); EXPECT_EQ(std::get(out[0].values[usec]), 1700000000123456ULL); } -TEST(MavlinkDecoder, SystemTimeUsesBootTimeNotUnixTime) { - mavlink_message_t msg; - mavlink_msg_system_time_pack(1, 1, &msg, 1700000000123456ULL, 42); - - mav::StreamParser parser; - parser.reset(); - const auto out = parse(parser, toBytes(msg)); - ASSERT_EQ(out.size(), 1U); - ASSERT_TRUE(out[0].embedded_timestamp_ns.has_value()); - EXPECT_EQ(*out[0].embedded_timestamp_ns, 42LL * 1000000LL); -} - -TEST(MavlinkDecoder, MessageWithoutTimeFieldHasNoEmbeddedTimestamp) { +TEST(MavlinkDecoder, DecodesHeartbeat) { mavlink_message_t msg; mavlink_msg_heartbeat_pack(1, 1, &msg, MAV_TYPE_QUADROTOR, MAV_AUTOPILOT_ARDUPILOTMEGA, 81, 5, MAV_STATE_ACTIVE); @@ -108,7 +81,6 @@ TEST(MavlinkDecoder, MessageWithoutTimeFieldHasNoEmbeddedTimestamp) { const auto out = parse(parser, toBytes(msg)); ASSERT_EQ(out.size(), 1U); EXPECT_EQ(out[0].topic, "mav/1.1/HEARTBEAT"); - EXPECT_FALSE(out[0].embedded_timestamp_ns.has_value()); const size_t custom = indexOf(out[0], "custom_mode"); EXPECT_EQ(std::get(out[0].values[custom]), 5U); } @@ -155,7 +127,7 @@ TEST(MavlinkDecoder, ReassemblesFramesSplitAcrossFeeds) { EXPECT_LE(out.size(), 1U); parser.feed(bytes.data() + half, bytes.size() - half, out); ASSERT_EQ(out.size(), 2U); - EXPECT_EQ(*out[1].embedded_timestamp_ns, 200LL * 1000000LL); + EXPECT_EQ(std::get(out[1].values[indexOf(out[1], "time_boot_ms")]), 200U); } TEST(MavlinkDecoder, GarbageBytesProduceNoMessages) { diff --git a/pj3/DataStreamMavlink/README.md b/pj3/DataStreamMavlink/README.md index 399c0c4..af3d532 100644 --- a/pj3/DataStreamMavlink/README.md +++ b/pj3/DataStreamMavlink/README.md @@ -7,4 +7,5 @@ Streams live MAVLink telemetry over UDP, TCP, or Serial into PlotJuggler 3.x. - **Three transports:** UDP (default port 14550), TCP client, and Serial port — switchable from the connection dialog. - **Zero-config field discovery:** every numeric field in every MAVLink message is automatically mapped to a plot series named `mav/.//`. - **Multi-vehicle:** differentiates streams by `sysid.compid`, so data from multiple vehicles on the same link is kept separate. +- **Receive-time axis:** every sample is stamped with the PC's wall-clock time when it arrived. Payload times (`time_usec`, `time_boot_ms`) are plotted as regular fields. - **Message Interval control:** via the **"Message Intervals…"** toolbar action, view live message rates and send `SET_MESSAGE_INTERVAL` commands back to the vehicle to tune what gets streamed and how fast. diff --git a/pj3/DataStreamMavlink/datastream_mavlink.cpp b/pj3/DataStreamMavlink/datastream_mavlink.cpp index fbcaa94..5644c8d 100644 --- a/pj3/DataStreamMavlink/datastream_mavlink.cpp +++ b/pj3/DataStreamMavlink/datastream_mavlink.cpp @@ -272,32 +272,7 @@ void DataStreamMavlink::onMessageDecoded(mavlink_message_t msg, double recvTimeS const std::string prefix = std::string("mav/") + std::to_string(msg.sysid) + "." + std::to_string(msg.compid) + "/" + info->name; - // First pass: detect embedded hardware timestamp - double timestamp = recvTimeSec; - for (unsigned i = 0; i < info->num_fields; ++i) { - const mavlink_field_info_t& f = info->fields[i]; - if (f.array_length != 0) { - continue; - } - if (std::strcmp(f.name, Mav::FIELD_TIME_USEC) == 0 && f.type == MAVLINK_TYPE_UINT64_T) { - uint64_t v; - std::memcpy(&v, payload + f.wire_offset, 8); - if (v > 0) { - timestamp = static_cast(v) * 1e-6; - } - break; - } - if (std::strcmp(f.name, Mav::FIELD_TIME_BOOT_MS) == 0 && f.type == MAVLINK_TYPE_UINT32_T) { - uint32_t v; - std::memcpy(&v, payload + f.wire_offset, 4); - if (v > 0) { - timestamp = static_cast(v) * 1e-3; - } - break; - } - } - - // Second pass: push all numeric fields into dataMap (under mutex). + // Push all numeric fields into dataMap (under mutex). // The lock is released before this function returns so that batchProcessed // (which emits dataReceived) is never called while the mutex is held. std::lock_guard lock(mutex()); @@ -379,7 +354,7 @@ void DataStreamMavlink::onMessageDecoded(mavlink_message_t msg, double recvTimeS break; } } - dataMap().getOrCreateNumeric(key).pushBack({timestamp, value}); + dataMap().getOrCreateNumeric(key).pushBack({recvTimeSec, value}); } } } diff --git a/pj3/DataStreamMavlink/mavlink/mavlink_constants.h b/pj3/DataStreamMavlink/mavlink/mavlink_constants.h index aee8d71..93c726c 100644 --- a/pj3/DataStreamMavlink/mavlink/mavlink_constants.h +++ b/pj3/DataStreamMavlink/mavlink/mavlink_constants.h @@ -16,8 +16,5 @@ constexpr uint16_t CMD_REQUEST_MESSAGE = 512; constexpr uint32_t MSGID_MESSAGE_INTERVAL = 244; -constexpr const char* FIELD_TIME_USEC = "time_usec"; -constexpr const char* FIELD_TIME_BOOT_MS = "time_boot_ms"; - constexpr uint8_t TYPE_SIZE[11] = {1, 1, 1, 2, 2, 4, 4, 8, 8, 4, 8}; } // namespace Mav diff --git a/pj4/plugins/data_stream_mavlink/CHANGELOG.md b/pj4/plugins/data_stream_mavlink/CHANGELOG.md index c48b294..d864a08 100644 --- a/pj4/plugins/data_stream_mavlink/CHANGELOG.md +++ b/pj4/plugins/data_stream_mavlink/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## [Unreleased] + +### Fixed +- All messages are stamped with the wall-clock receive time. Before, a message with a non-zero `time_usec` / + `time_boot_ms` used that value, which is time since boot on most autopilots, while other messages (HEARTBEAT, + SYS_STATUS, ...) used the receive time, so one stream mixed two clocks decades apart. `time_usec` / `time_boot_ms` + are still plotted as regular fields. (#26) + ## [0.1.0] - 2026-10-03 ### Added diff --git a/pj4/plugins/data_stream_mavlink/README.md b/pj4/plugins/data_stream_mavlink/README.md index 8e236d3..7afa74b 100644 --- a/pj4/plugins/data_stream_mavlink/README.md +++ b/pj4/plugins/data_stream_mavlink/README.md @@ -7,5 +7,6 @@ Streams live MAVLink telemetry over UDP, TCP, or Serial into PlotJuggler 4.x. - **Three transports:** UDP (default port 14550), TCP client, and Serial port — switchable from the connection dialog. - **Zero-config field discovery:** every numeric field in every MAVLink message is automatically mapped to a plot series named `mav/.//`. - **Multi-vehicle:** differentiates streams by `sysid.compid`, so data from multiple vehicles on the same link is kept separate. +- **Receive-time axis:** every sample is stamped with the PC's wall-clock time when it arrived. Payload times (`time_usec`, `time_boot_ms`) are plotted as regular fields. > The **Message Intervals** control is only available in the PJ3 version. diff --git a/pj4/plugins/data_stream_mavlink/mavlink_source.cpp b/pj4/plugins/data_stream_mavlink/mavlink_source.cpp index 0ba5dc3..c829bd0 100644 --- a/pj4/plugins/data_stream_mavlink/mavlink_source.cpp +++ b/pj4/plugins/data_stream_mavlink/mavlink_source.cpp @@ -160,9 +160,8 @@ class MavlinkSource : public PJ::StreamSourceBase { for (size_t i = 0; i < message.values.size(); ++i) { row_.push_back({binding.fields[i], toValueRef(message.values[i])}); } - const int64_t ts = message.embedded_timestamp_ns.value_or(recv_ns); return writeHost().appendBoundRecord( - binding.topic, PJ::Timestamp{ts}, PJ::Span(row_)); + binding.topic, PJ::Timestamp{recv_ns}, PJ::Span(row_)); } mavlink_detail::MavlinkDialog dialog_;