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.
This commit is contained in:
Jacob Dahl authored and GitHub committed 2026-09-08 20:14:07 -06:00
1 parent b8023ac8f9
commit 4af7b4e77e
10 files changed
+143 -23

No files matched your search

+5 -2
View File
@@ -63,7 +63,8 @@ void UavcanAccelBridge::imu_sub_cb(const uavcan::ReceivedDataStructure<uavcan::e
{
uavcan_bridge::Channel *channel = get_channel_for_node(msg.getSrcNodeID().get(), msg.getIfaceIndex());
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_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::ReceivedDataStructure<uavcan::e
return;
}
const uavcan_bridge::ImuRateSample sample = uavcan_bridge::imu_rate_sample(timestamp_sample,
msg.integration_interval, msg.accelerometer_latest, msg.accelerometer_integral);
accel->set_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) {
+2 -1
View File
@@ -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);
+5 -5
View File
@@ -75,11 +75,11 @@ void UavcanGyroBridge::imu_sub_cb(const uavcan::ReceivedDataStructure<uavcan::eq
return;
}
const hrt_abstime timestamp_sample = (msg.timestamp.usec > 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) {
+2 -1
View File
@@ -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)};
@@ -39,6 +39,7 @@
#include <containers/List.hpp>
#include <uavcan/uavcan.hpp>
#include <drivers/drv_hrt.h>
#include <drivers/drv_orb_dev.h>
#include <lib/drivers/device/Device.hpp>
#include <uORB/uORB.h>
@@ -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<typename LatestT, typename IntegralT>
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<hrt_abstime>(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
/**
@@ -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]);
@@ -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;
+62 -10
View File
@@ -37,7 +37,10 @@
#include <uavcan/equipment/ahrs/RawIMU.hpp>
#include <uORB/Subscription.hpp>
#include <uORB/SubscriptionCallback.hpp>
#include <uORB/topics/sensor_accel.h>
#include <uORB/topics/sensor_gyro.h>
#include <uORB/topics/vehicle_imu.h>
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<typename SampleT>
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
@@ -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
@@ -37,10 +37,22 @@
#include <uavcan/uavcan.hpp>
#include <uavcan/node/publisher.hpp>
#include <drivers/drv_hrt.h>
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<UavcanPublisherBase *>
{
public: