From 4af7b4e77ecedcb527e676d2a952bfd3b3fc235d Mon Sep 17 00:00:00 2001 From: Jacob Dahl <37091262+dakejahl@users.noreply.github.com> Date: Tue, 8 Sep 2026 20:14:07 -0600 Subject: [PATCH] fix(uavcan): publish UNKNOWN node timestamps until time-synced and map them through the bus time base on the FC (#28176) * fix(uavcannode): publish UNKNOWN until time-synced and fix RawIMU integration_interval units The RawIMU and RangeSensor publishers computed the bus timestamp as getUtcTime() - (hrt_now - sample_hrt). getUtcTime() is 0 until the time-sync master disciplines the node's clock, so the unsigned subtraction underflowed and the truncated uint56 field went out as ~7.2e16 us. Publish UNKNOWN until the clock is disciplined, from one shared helper. RawIMU.integration_interval is specified in seconds but was assigned vehicle_imu.delta_angle_dt, which is in microseconds. * fix(uavcan): map node timestamps through the bus time base in the sensor bridges The accel, gyro and rangefinder bridges took msg.timestamp.usec verbatim as timestamp_sample whenever it was non-zero, which assumes the bus shared time base is HRT. It is not: the FC seeds its bus clock from HRT only after its own init has run, so the base leads HRT by that duration, and the FC may also be a slave to a lower node ID master. An undisciplined node, or one stamping a foreign epoch, put the sample time arbitrarily far from HRT. The CAN ISR stamps every received transfer in the same bus base the node used, so the sample age is the difference between the two. Bound that age to a plausible transport window and subtract it from the receive time, falling back to the receive time otherwise, in one shared helper. * fix(uavcan): stamp RelPosHeading and BatteryInfoAux in the bus base, map RelPosHeading on the FC Both node publishers put node-local time into a bus-base field, and the FC took the RelPosHeading stamp verbatim as HRT. Same defect as RawIMU and RangeSensorMeasurement, same two helpers. * fix(uavcan): read the bus clock and HRT together when mapping node timestamps The ISR receive stamp is taken at the first frame of a transfer while HRT was read in the callback, so the transfer and scheduling latency stayed in timestamp_sample as jitter. Sampling both clocks at the same instant cancels the bus-to-HRT offset exactly. The node-side helper takes the node for the same reason. The FC bus base leads HRT by the free-running timer phase at seeding, not by the init duration; correct the comment. * fix(uavcan): send true point samples in RawIMU and consume the integral on the FC RawIMU.*_latest carried the window mean, delta / integration_interval, under a field the DSDL defines as the latest point sample. The mean lags the message timestamp by half the interval, 2.5 ms at IMU_INTEG_RATE 200, and the FC published it as an instantaneous rate at the window end. The node now fills *_latest from the sensor_gyro / sensor_accel instance behind vehicle_imu, the same batch-end sample every PX4 IMU driver produces, and keeps the integrals and timestamp as they were. The FC bridges now consume the integral when one is present: divided by its interval it is float32 rather than float16, anti-aliased against the node's full-rate stream, and successive means spaced one interval apart integrate back to the node's exact delta angle in VehicleIMU. It is stamped at the window centroid. The point sample is used only when the node reports no integral, as the DSDL specifies. --- src/drivers/uavcan/sensors/accel.cpp | 7 +- src/drivers/uavcan/sensors/gnss_relative.cpp | 3 +- src/drivers/uavcan/sensors/gyro.cpp | 10 +-- src/drivers/uavcan/sensors/rangefinder.cpp | 3 +- src/drivers/uavcan/sensors/sensor_bridge.hpp | 52 ++++++++++++++ .../uavcannode/Publishers/BatteryInfo.hpp | 2 +- .../Publishers/RangeSensorMeasurement.hpp | 2 +- src/drivers/uavcannode/Publishers/RawIMU.hpp | 72 ++++++++++++++++--- .../uavcannode/Publishers/RelPosHeading.hpp | 3 +- .../Publishers/UavcanPublisherBase.hpp | 12 ++++ 10 files changed, 143 insertions(+), 23 deletions(-) diff --git a/src/drivers/uavcan/sensors/accel.cpp b/src/drivers/uavcan/sensors/accel.cpp index 9f3d6a20ca1..2ea483a1973 100644 --- a/src/drivers/uavcan/sensors/accel.cpp +++ b/src/drivers/uavcan/sensors/accel.cpp @@ -63,7 +63,8 @@ void UavcanAccelBridge::imu_sub_cb(const uavcan::ReceivedDataStructure 0) ? msg.timestamp.usec : hrt_absolute_time(); + const hrt_abstime timestamp_sample = uavcan_bridge::sample_timestamp(msg.timestamp.usec, + _sub_imu_data.getNode().getUtcTime().toUSec(), hrt_absolute_time()); if (channel == nullptr) { // Something went wrong - no channel to publish on; return @@ -77,8 +78,10 @@ void UavcanAccelBridge::imu_sub_cb(const uavcan::ReceivedDataStructureset_error_count(0); - accel->update(timestamp_sample, msg.accelerometer_latest[0], msg.accelerometer_latest[1], msg.accelerometer_latest[2]); + accel->update(sample.timestamp_sample, sample.x, sample.y, sample.z); // Register device capability if not already done if (_node_info_publisher != nullptr) { diff --git a/src/drivers/uavcan/sensors/gnss_relative.cpp b/src/drivers/uavcan/sensors/gnss_relative.cpp index 8cb5bf344eb..5944d61bc37 100644 --- a/src/drivers/uavcan/sensors/gnss_relative.cpp +++ b/src/drivers/uavcan/sensors/gnss_relative.cpp @@ -63,7 +63,8 @@ void UavcanGnssRelativeBridge::rel_pos_heading_sub_cb(const { sensor_gnss_relative_s sensor_gnss_relative{}; - sensor_gnss_relative.timestamp_sample = uavcan::UtcTime(msg.timestamp).toUSec(); + sensor_gnss_relative.timestamp_sample = uavcan_bridge::sample_timestamp(msg.timestamp.usec, + _sub_rel_pos_heading.getNode().getUtcTime().toUSec(), hrt_absolute_time()); sensor_gnss_relative.heading_valid = msg.reported_heading_acc_available; sensor_gnss_relative.heading = math::radians(msg.reported_heading_deg); diff --git a/src/drivers/uavcan/sensors/gyro.cpp b/src/drivers/uavcan/sensors/gyro.cpp index fca4ba7b4f6..0b4f1cb9100 100644 --- a/src/drivers/uavcan/sensors/gyro.cpp +++ b/src/drivers/uavcan/sensors/gyro.cpp @@ -75,11 +75,11 @@ void UavcanGyroBridge::imu_sub_cb(const uavcan::ReceivedDataStructure 0) ? msg.timestamp.usec : hrt_absolute_time(); - gyro->update(timestamp_sample, - msg.rate_gyro_latest[0], - msg.rate_gyro_latest[1], - msg.rate_gyro_latest[2]); + const hrt_abstime timestamp_sample = uavcan_bridge::sample_timestamp(msg.timestamp.usec, + _sub_imu_data.getNode().getUtcTime().toUSec(), hrt_absolute_time()); + const uavcan_bridge::ImuRateSample sample = uavcan_bridge::imu_rate_sample(timestamp_sample, + msg.integration_interval, msg.rate_gyro_latest, msg.rate_gyro_integral); + gyro->update(sample.timestamp_sample, sample.x, sample.y, sample.z); // Register device capability if not already done if (_node_info_publisher != nullptr) { diff --git a/src/drivers/uavcan/sensors/rangefinder.cpp b/src/drivers/uavcan/sensors/rangefinder.cpp index 89fabf2bde7..589cab15373 100644 --- a/src/drivers/uavcan/sensors/rangefinder.cpp +++ b/src/drivers/uavcan/sensors/rangefinder.cpp @@ -176,7 +176,8 @@ void UavcanRangefinderBridge::range_sub_cb(const quality = 100; } - const hrt_abstime timestamp_sample = (msg.timestamp.usec > 0) ? msg.timestamp.usec : hrt_absolute_time(); + const hrt_abstime timestamp_sample = uavcan_bridge::sample_timestamp(msg.timestamp.usec, + _sub_range_data.getNode().getUtcTime().toUSec(), hrt_absolute_time()); if (orientation == distance_sensor_s::ROTATION_CUSTOM) { const matrix::Quatf q_sensor{uavcan_orientation_to_euler(msg.beam_orientation_in_body_frame)}; diff --git a/src/drivers/uavcan/sensors/sensor_bridge.hpp b/src/drivers/uavcan/sensors/sensor_bridge.hpp index 239bae66f14..4515b0d8769 100644 --- a/src/drivers/uavcan/sensors/sensor_bridge.hpp +++ b/src/drivers/uavcan/sensors/sensor_bridge.hpp @@ -39,6 +39,7 @@ #include #include +#include #include #include #include @@ -94,6 +95,57 @@ struct Channel { void *h_driver{nullptr}; uint8_t iface_index{0}; }; + +// Node timestamps are in the bus shared time base, whose offset from HRT is +// arbitrary: the FC seeds its bus clock from HRT at whatever phase the driver's +// free-running timer happens to be, and a lower node ID master can discipline +// it. Subtracting the bus-time age from an HRT reading taken at the same +// instant cancels the offset; the ISR receive stamp would leave the transfer +// and scheduling latency in the result. UNKNOWN, unconverged and foreign-epoch +// stamps fall back to the receive time. +inline hrt_abstime sample_timestamp(uint64_t node_timestamp_us, uint64_t bus_now_us, hrt_abstime now) +{ + static constexpr uint64_t kMaxTransportDelay = 100_ms; + + if (node_timestamp_us > 0 && bus_now_us >= node_timestamp_us + && (bus_now_us - node_timestamp_us) < kMaxTransportDelay) { + return now - (bus_now_us - node_timestamp_us); + } + + return now; +} + +/** + * Rate and sample time to publish from a RawIMU field pair. + * + * When an integral is present its mean over the interval is preferred over the + * point sample: it is float32 rather than float16, it is anti-aliased against the + * node's full-rate stream, and successive means spaced one interval apart + * integrate back to the node's exact deltas in VehicleIMU. A mean belongs at the + * centroid of its window, half an interval before the acquisition timestamp that + * closes it. Without an integral the point sample is used at the timestamp. + */ +static constexpr float kMaxImuIntegrationInterval = 0.1f; + +struct ImuRateSample { + hrt_abstime timestamp_sample; + float x; + float y; + float z; +}; + +template +inline ImuRateSample imu_rate_sample(hrt_abstime timestamp_sample, float integration_interval, + const LatestT &latest, const IntegralT &integral) +{ + if (integration_interval > 0.f && integration_interval < kMaxImuIntegrationInterval) { + const float inv_dt = 1.f / integration_interval; + const hrt_abstime half_interval = static_cast(integration_interval * 0.5e6f); + return {timestamp_sample - half_interval, integral[0] *inv_dt, integral[1] *inv_dt, integral[2] *inv_dt}; + } + + return {timestamp_sample, latest[0], latest[1], latest[2]}; +} } // namespace uavcan_bridge /** diff --git a/src/drivers/uavcannode/Publishers/BatteryInfo.hpp b/src/drivers/uavcannode/Publishers/BatteryInfo.hpp index 2a0addac978..bcb5fa7e958 100644 --- a/src/drivers/uavcannode/Publishers/BatteryInfo.hpp +++ b/src/drivers/uavcannode/Publishers/BatteryInfo.hpp @@ -84,7 +84,7 @@ public: if (uORB::SubscriptionCallbackWorkItem::update(&battery)) { ardupilot::equipment::power::BatteryInfoAux battery_info_aux{}; - battery_info_aux.timestamp.usec = battery.timestamp; + battery_info_aux.timestamp.usec = bus_timestamp_usec(getNode(), battery.timestamp); for (uint8_t i = 0; i < battery.cell_count && i < arraySize(battery_status_s::voltage_cell_v); i++) { battery_info_aux.voltage_cell.push_back(battery.voltage_cell_v[i]); diff --git a/src/drivers/uavcannode/Publishers/RangeSensorMeasurement.hpp b/src/drivers/uavcannode/Publishers/RangeSensorMeasurement.hpp index ca8226b15a2..7bbd0db1fe5 100644 --- a/src/drivers/uavcannode/Publishers/RangeSensorMeasurement.hpp +++ b/src/drivers/uavcannode/Publishers/RangeSensorMeasurement.hpp @@ -75,7 +75,7 @@ public: if (uORB::SubscriptionCallbackWorkItem::update(&dist)) { uavcan::equipment::range_sensor::Measurement range_sensor{}; - range_sensor.timestamp.usec = getNode().getUtcTime().toUSec() - (hrt_absolute_time() - dist.timestamp); + range_sensor.timestamp.usec = bus_timestamp_usec(getNode(), dist.timestamp); range_sensor.sensor_id = get_instance(); range_sensor.range = dist.current_distance; range_sensor.field_of_view = dist.h_fov; diff --git a/src/drivers/uavcannode/Publishers/RawIMU.hpp b/src/drivers/uavcannode/Publishers/RawIMU.hpp index 6cd015e573d..4424235c5cc 100644 --- a/src/drivers/uavcannode/Publishers/RawIMU.hpp +++ b/src/drivers/uavcannode/Publishers/RawIMU.hpp @@ -37,7 +37,10 @@ #include +#include #include +#include +#include #include namespace uavcannode @@ -76,23 +79,47 @@ public: uavcan::equipment::ahrs::RawIMU raw_imu{}; - raw_imu.timestamp.usec = getNode().getUtcTime().toUSec() - (hrt_absolute_time() - - vehicle_imu.timestamp_sample); + // timestamp is the acquisition time of the latest sample, which is also the + // end of the integration window: VehicleIMU stamps vehicle_imu with the + // sensor sample that closed the window. + raw_imu.timestamp.usec = bus_timestamp_usec(getNode(), vehicle_imu.timestamp_sample); - raw_imu.integration_interval = vehicle_imu.delta_angle_dt; - // raw_imu.integration_interval = vehicle_imu.delta_velocity_dt; + // integration_interval is in seconds; delta_angle_dt is in microseconds + raw_imu.integration_interval = vehicle_imu.delta_angle_dt * 1e-6f; - raw_imu.rate_gyro_latest[0] = (vehicle_imu.delta_angle[0] / vehicle_imu.delta_angle_dt) * 1000000; - raw_imu.rate_gyro_latest[1] = (vehicle_imu.delta_angle[1] / vehicle_imu.delta_angle_dt) * 1000000; - raw_imu.rate_gyro_latest[2] = (vehicle_imu.delta_angle[2] / vehicle_imu.delta_angle_dt) * 1000000; + // *_latest is the latest point sample, not the window mean: the mean lags + // the timestamp by half the interval, and consumers that want the mean + // have the integral. The sensor topics are the driver's uncalibrated + // output, as a point sample from any other IMU driver would be. + sensor_gyro_s gyro; + + if (latest_sample(_sensor_gyro_sub, vehicle_imu.gyro_device_id, gyro)) { + raw_imu.rate_gyro_latest[0] = gyro.x; + raw_imu.rate_gyro_latest[1] = gyro.y; + raw_imu.rate_gyro_latest[2] = gyro.z; + + } else { + raw_imu.rate_gyro_latest[0] = (vehicle_imu.delta_angle[0] / vehicle_imu.delta_angle_dt) * 1000000; + raw_imu.rate_gyro_latest[1] = (vehicle_imu.delta_angle[1] / vehicle_imu.delta_angle_dt) * 1000000; + raw_imu.rate_gyro_latest[2] = (vehicle_imu.delta_angle[2] / vehicle_imu.delta_angle_dt) * 1000000; + } raw_imu.rate_gyro_integral[0] = vehicle_imu.delta_angle[0]; raw_imu.rate_gyro_integral[1] = vehicle_imu.delta_angle[1]; raw_imu.rate_gyro_integral[2] = vehicle_imu.delta_angle[2]; - raw_imu.accelerometer_latest[0] = (vehicle_imu.delta_velocity[0] / vehicle_imu.delta_velocity_dt) * 1000000; - raw_imu.accelerometer_latest[1] = (vehicle_imu.delta_velocity[1] / vehicle_imu.delta_velocity_dt) * 1000000; - raw_imu.accelerometer_latest[2] = (vehicle_imu.delta_velocity[2] / vehicle_imu.delta_velocity_dt) * 1000000; + sensor_accel_s accel; + + if (latest_sample(_sensor_accel_sub, vehicle_imu.accel_device_id, accel)) { + raw_imu.accelerometer_latest[0] = accel.x; + raw_imu.accelerometer_latest[1] = accel.y; + raw_imu.accelerometer_latest[2] = accel.z; + + } else { + raw_imu.accelerometer_latest[0] = (vehicle_imu.delta_velocity[0] / vehicle_imu.delta_velocity_dt) * 1000000; + raw_imu.accelerometer_latest[1] = (vehicle_imu.delta_velocity[1] / vehicle_imu.delta_velocity_dt) * 1000000; + raw_imu.accelerometer_latest[2] = (vehicle_imu.delta_velocity[2] / vehicle_imu.delta_velocity_dt) * 1000000; + } raw_imu.accelerometer_integral[0] = vehicle_imu.delta_velocity[0]; raw_imu.accelerometer_integral[1] = vehicle_imu.delta_velocity[1]; @@ -104,5 +131,30 @@ public: uORB::SubscriptionCallbackWorkItem::registerCallback(); } } + +private: + // Copy the newest sample of the sensor instance feeding vehicle_imu, re-selecting + // the instance if the one subscribed does not carry the expected device id. + template + static bool latest_sample(uORB::Subscription &sub, uint32_t device_id, SampleT &sample) + { + if (sub.copy(&sample) && sample.device_id == device_id) { + return true; + } + + for (uint8_t instance = 0; instance < ORB_MULTI_MAX_INSTANCES; instance++) { + uORB::Subscription candidate{sub.orb_id(), instance}; + + if (candidate.copy(&sample) && sample.device_id == device_id) { + sub.ChangeInstance(instance); + return true; + } + } + + return false; + } + + uORB::Subscription _sensor_gyro_sub{ORB_ID(sensor_gyro)}; + uORB::Subscription _sensor_accel_sub{ORB_ID(sensor_accel)}; }; } // namespace uavcannode diff --git a/src/drivers/uavcannode/Publishers/RelPosHeading.hpp b/src/drivers/uavcannode/Publishers/RelPosHeading.hpp index 9201ebd1013..182561930f2 100644 --- a/src/drivers/uavcannode/Publishers/RelPosHeading.hpp +++ b/src/drivers/uavcannode/Publishers/RelPosHeading.hpp @@ -78,8 +78,7 @@ public: if (uORB::SubscriptionCallbackWorkItem::update(&sensor_gnss_relative)) { ardupilot::gnss::RelPosHeading rel_pos_heading{}; - // TODO: FIX (timestamp_sample and UAVCAN timestamp) - rel_pos_heading.timestamp = uavcan::UtcTime::fromUSec(getNode().getMonotonicTime().toUSec()); + rel_pos_heading.timestamp.usec = bus_timestamp_usec(getNode(), sensor_gnss_relative.timestamp_sample); rel_pos_heading.reported_heading_acc_available = sensor_gnss_relative.heading_valid; // bool rel_pos_heading.reported_heading_deg = math::degrees(sensor_gnss_relative.heading); // float32 diff --git a/src/drivers/uavcannode/Publishers/UavcanPublisherBase.hpp b/src/drivers/uavcannode/Publishers/UavcanPublisherBase.hpp index d5e9039732d..aeb902a6f67 100644 --- a/src/drivers/uavcannode/Publishers/UavcanPublisherBase.hpp +++ b/src/drivers/uavcannode/Publishers/UavcanPublisherBase.hpp @@ -37,10 +37,22 @@ #include #include +#include namespace uavcannode { +// Acquisition time of a sample taken at sample_hrt, in the bus shared time +// base. The bus time is 0 until the time-sync slave disciplines the clock, and +// the subtraction is unsigned, so publish UNKNOWN rather than underflow the +// uint56 field. +inline uint64_t bus_timestamp_usec(const uavcan::INode &node, hrt_abstime sample_hrt) +{ + const uint64_t bus_now_us = node.getUtcTime().toUSec(); + const uint64_t sample_age_us = hrt_absolute_time() - sample_hrt; + return (bus_now_us > sample_age_us) ? (bus_now_us - sample_age_us) : 0; +} + class UavcanPublisherBase : public IntrusiveSortedListNode { public: