Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions core/include/pj_drone_core/mavlink/decoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <variant>
#include <vector>
Expand All @@ -31,9 +30,6 @@ struct Message {
std::string topic; // "mav/<sysid>.<compid>/<MSG_NAME>"
std::shared_ptr<const Schema> schema;
std::vector<Value> 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<int64_t> embedded_timestamp_ns;
};

/// Stateful MAVLink byte-stream parser (wraps mavlink_parse_char on channel 0, as the PJ3 plugin did).
Expand Down
29 changes: 0 additions & 29 deletions core/src/mavlink/decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -94,32 +92,6 @@ std::shared_ptr<const Schema> 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<int64_t> 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<uint64_t>(payload, f.wire_offset);
if (v > 0) {
return static_cast<int64_t>(v) * 1000;
}
return std::nullopt;
}
if (std::strcmp(f.name, kFieldTimeBootMs) == 0 && f.type == MAVLINK_TYPE_UINT32_T) {
const auto v = load<uint32_t>(payload, f.wire_offset);
if (v > 0) {
return static_cast<int64_t>(v) * 1000000;
}
return std::nullopt;
}
}
return std::nullopt;
}

} // namespace

struct StreamParser::Impl {
Expand All @@ -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) {
Expand Down
36 changes: 4 additions & 32 deletions core/tests/mavlink_decoder_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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");
Expand All @@ -60,18 +58,7 @@ TEST(MavlinkDecoder, DecodesAttitudeWithBootTimeAndNativeTypes) {
EXPECT_EQ(std::get<uint32_t>(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};
Expand All @@ -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<uint64_t>(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);

Expand All @@ -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<uint32_t>(out[0].values[custom]), 5U);
}
Expand Down Expand Up @@ -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<uint32_t>(out[1].values[indexOf(out[1], "time_boot_ms")]), 200U);
}

TEST(MavlinkDecoder, GarbageBytesProduceNoMessages) {
Expand Down
1 change: 1 addition & 0 deletions pj3/DataStreamMavlink/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<sysid>.<compid>/<MSG_NAME>/<field>`.
- **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.
29 changes: 2 additions & 27 deletions pj3/DataStreamMavlink/datastream_mavlink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>(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<double>(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<std::mutex> lock(mutex());
Expand Down Expand Up @@ -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});
}
}
}
3 changes: 0 additions & 3 deletions pj3/DataStreamMavlink/mavlink/mavlink_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions pj4/plugins/data_stream_mavlink/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions pj4/plugins/data_stream_mavlink/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<sysid>.<compid>/<MSG_NAME>/<field>`.
- **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.
3 changes: 1 addition & 2 deletions pj4/plugins/data_stream_mavlink/mavlink_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const PJ::sdk::BoundFieldValue>(row_));
binding.topic, PJ::Timestamp{recv_ns}, PJ::Span<const PJ::sdk::BoundFieldValue>(row_));
}

mavlink_detail::MavlinkDialog dialog_;
Expand Down
Loading