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: