fix(vision_target_estimator): reacquire target after attitude resets (#29339)

Co-authored-by: jonas <jonas.perolini@rigi.tech>
This commit is contained in:
Jonas Peroliniandjonas authored and GitHub committed 2026-10-19 19:07:44 +02:00
1 parent b2bce30859
commit a99287097a
7 files changed
+219 -12

No files matched your search

@@ -69,10 +69,21 @@ bool VTEOrientation::init()
return true;
}
void VTEOrientation::resetFilter()
void VTEOrientation::resetFilter(hrt_abstime min_sample_time)
{
// Keep the frame boundary across subsequent timeout/task resets.
if (min_sample_time > _min_sample_time) {
_min_sample_time = min_sample_time;
}
_estimator_initialized = false;
_target_est_yaw.resetHistory();
if (min_sample_time != 0) {
_orientation_msg = {};
_orientation_msg.timestamp = nowUs();
_target_orientation_pub.publish(_orientation_msg);
}
}
void VTEOrientation::update()
@@ -79,8 +79,8 @@ public:
/** Prepare per-axis history buffers. Call once at startup. */
bool init();
/** Drop the estimator state so the next valid observation re-initializes it. */
void resetFilter();
/** Drop the estimator state. A nonzero sample boundary also publishes invalid yaw. */
void resetFilter(hrt_abstime min_sample_time = 0);
/** Timeout after which the whole filter is declared stale (see @c timedOut). */
void setVteTimeout(hrt_abstime tout) { _vte_timeout_us = tout; }
@@ -146,7 +146,7 @@ private:
inline bool isMeasRecent(hrt_abstime ts) const
{
return !hasTimedOut(ts, _meas_recent_timeout_us);
return ts >= _min_sample_time && !hasTimedOut(ts, _meas_recent_timeout_us);
}
bool isVisionDataValid(const fiducial_marker_yaw_report_s &fiducial_marker_yaw);
@@ -162,6 +162,7 @@ private:
KF_orientation _target_est_yaw{};
vte_orientation_s _orientation_msg{};
hrt_abstime _min_sample_time{0}; // Discard measurements from before an attitude reset.
hrt_abstime _last_predict{0}; // timestamp of last filter prediction
hrt_abstime _last_update{0}; // timestamp of last filter update (used to check timeout)
hrt_abstime _vision_warn_last{0};
@@ -124,8 +124,22 @@ bool VTEPosition::init()
return true;
}
void VTEPosition::resetFilter()
void VTEPosition::resetFilter(hrt_abstime min_sample_time)
{
// Keep the frame boundary across subsequent timeout/task resets.
if (min_sample_time > _min_sample_time) {
_min_sample_time = min_sample_time;
}
if (min_sample_time != 0) {
// These cached values use the old frame, even if published after the reset's sample time.
_local_position.valid = false;
_local_velocity.valid = false;
_pos_rel_gnss.valid = false;
_gps_pos_offset_ned.valid = false;
_velocity_offset_ned.valid = false;
}
_estimator_initialized = false;
_last_relative_meas_fused_time = 0;
_bias.set = false;
@@ -84,8 +84,8 @@ public:
/** Validate generated state sizes and prepare per-axis history buffers. Call once at startup. */
bool init();
/** Drop the estimator state. Cached external inputs (local pos/vel, offsets) are preserved. */
void resetFilter();
/** Drop the estimator state. A nonzero sample boundary also invalidates cached NED inputs. */
void resetFilter(hrt_abstime min_sample_time = 0);
/** Cache the task-specific absolute reference used to build the mission GNSS observation. */
void setMissionPosition(double lat_deg, double lon_deg, float alt_m);
@@ -241,12 +241,12 @@ private:
inline bool isMeasRecent(hrt_abstime ts) const
{
return !hasTimedOut(ts, _meas_recent_timeout_us);
return ts >= _min_sample_time && !hasTimedOut(ts, _meas_recent_timeout_us);
}
inline bool isMeasUpdated(hrt_abstime ts) const
{
return !hasTimedOut(ts, _meas_updated_timeout_us);
return ts >= _min_sample_time && !hasTimedOut(ts, _meas_updated_timeout_us);
}
inline bool hasNewPositionSensorData(const ObsValidMaskU &fusion_mask) const
@@ -429,6 +429,7 @@ private:
KF_position _target_est_pos[vtest::Axis::size] {};
hrt_abstime _min_sample_time{0}; // Discard measurements from before an attitude reset.
hrt_abstime _last_predict{0}; // timestamp of last filter prediction
hrt_abstime _last_update{0}; // timestamp of last filter update (used to check timeout)
hrt_abstime _uav_gps_vel_warn_last{0};
@@ -488,6 +488,28 @@ void VisionTargetEst::restartTimedOutEstimators()
}
}
void VisionTargetEst::handleAttitudeReset(const vehicle_attitude_s &attitude)
{
if (attitude.timestamp == 0) {
return;
}
if (_attitude_reset_counter_valid && attitude.quat_reset_counter != _attitude_reset_counter) {
// A frame jump is not target motion. Reacquire rather than keeping a state/bias
// whose observations were rotated using the previous attitude solution.
const hrt_abstime sample_time = attitude.timestamp_sample != 0 ? attitude.timestamp_sample : attitude.timestamp;
// A future timestamp must not prevent reacquisition until the clock catches up.
const hrt_abstime reset_time = math::min(sample_time, nowUs());
_vte_position.resetFilter(reset_time);
_vte_orientation.resetFilter(reset_time);
resetAccDownsample();
PX4_WARN("VTE attitude reset, reacquiring target");
}
_attitude_reset_counter = attitude.quat_reset_counter;
_attitude_reset_counter_valid = true;
}
void VisionTargetEst::updateEstimators()
{
perf_begin(_cycle_perf);
@@ -526,6 +548,14 @@ void VisionTargetEst::updateEstimators()
resetAccDownsample();
}
}
} else {
// Reset detection also runs when position input processing is stopped.
vehicle_attitude_s attitude{};
if (_vehicle_attitude_sub.update(&attitude)) {
handleAttitudeReset(attitude);
}
}
if (_vte_orientation_enabled && _orientation_estimator_running
@@ -649,7 +679,9 @@ bool VisionTargetEst::pollLocalPose(LocalPose &local_pose)
local_pose.vel_xyz(2) = vehicle_local_position.vz;
local_pose.vel_valid = vehicle_local_position.v_xy_valid && vehicle_local_position.v_z_valid;
local_pose.timestamp = vehicle_local_position.timestamp;
// Use sample time so a queued pre-reset pose cannot pass the frame boundary check.
local_pose.timestamp = vehicle_local_position.timestamp_sample != 0
? vehicle_local_position.timestamp_sample : vehicle_local_position.timestamp;
return true;
}
@@ -668,6 +700,9 @@ bool VisionTargetEst::pollEstimatorInput(matrix::Vector3f &vehicle_acc_ned, matr
return false;
}
// Check the same attitude sample that will rotate this cycle's inputs.
handleAttitudeReset(vehicle_attitude);
if (vehicle_acceleration_updated) {
_vehicle_acc_body.xyz = matrix::Vector3f(vehicle_acceleration.xyz);
_vehicle_acc_body.timestamp = vehicle_acceleration.timestamp;
@@ -129,6 +129,8 @@ protected:
void restartTimedOutEstimators();
// Collect inputs, downsample acceleration, and tick the enabled estimators once per cycle.
void updateEstimators();
// Invalidate the old NED state and delayed observations when EKF attitude jumps.
void handleAttitudeReset(const vehicle_attitude_s &attitude);
void updatePosEst(const matrix::Vector3f &gps_pos_offset_ned,
const matrix::Vector3f &vel_offset_ned,
const bool vel_offset_updated,
@@ -175,6 +177,9 @@ protected:
uORB::SubscriptionCallbackWorkItem _vehicle_attitude_sub{this, ORB_ID(vehicle_attitude)};
uint8_t _attitude_reset_counter{0};
bool _attitude_reset_counter_valid{false};
uORB::Subscription _vehicle_gnss_sub{ORB_ID(vehicle_gnss)};
uORB::Subscription _vehicle_local_position_sub{ORB_ID(vehicle_local_position)};
uORB::Subscription _vehicle_acceleration_sub{ORB_ID(vehicle_acceleration)};
@@ -163,7 +163,9 @@ public:
using vte::VisionTargetEst::kMinAccDownsampleTimeoutUs;
using vte::VisionTargetEst::kPosUpdatePeriodUs;
using vte::VisionTargetEst::kYawUpdatePeriodUs;
using vte::VisionTargetEst::LocalPose;
using vte::VisionTargetEst::pollEstimatorInput;
using vte::VisionTargetEst::pollLocalPose;
using vte::VisionTargetEst::publishVteInput;
using vte::VisionTargetEst::startPosEst;
using vte::VisionTargetEst::startYawEst;
@@ -175,6 +177,10 @@ public:
using vte::VisionTargetEst::updateTaskTopics;
using vte::VisionTargetEst::updateWhenIntervalElapsed;
bool positionInitialized() const { return _vte_position._estimator_initialized; }
void seedBias() { _vte_position._bias.set = true; }
bool biasSet() const { return _vte_position._bias.set; }
void clearCurrentTask() { _current_task_ptr = nullptr; }
void setCurrentTaskToPrecLand() { _current_task_ptr = &_prec_land_task; }
bool isCurrentTaskPrecLand() const { return _current_task_ptr == &_prec_land_task; }
@@ -282,10 +288,11 @@ protected:
ASSERT_TRUE(_param_guard.setInt(name, value));
}
void publishAttitude(const matrix::Quaternionf &q, hrt_abstime timestamp)
void publishAttitude(const matrix::Quaternionf &q, hrt_abstime timestamp, uint8_t reset_counter = 0)
{
vehicle_attitude_s msg{};
msg.timestamp = timestamp;
msg.timestamp = msg.timestamp_sample = timestamp;
msg.quat_reset_counter = reset_counter;
q.copyTo(msg.q);
ASSERT_TRUE(_attitude_pub->publish(msg));
}
@@ -1351,3 +1358,136 @@ TEST_F(VisionTargetEstTest, UpdateEstimatorsDropsStaleAcceleration)
EXPECT_EQ(_vte->_acc_sample_count, 0u);
expectVectorNear(_vte->_vehicle_acc_ned_sum, matrix::Vector3f{});
}
// A heading reset changes the frame of vision/acceleration input, not the target's motion.
TEST_F(VisionTargetEstTest, AttitudeResetReacquiresAndRejectsOldFrameSamples)
{
vte::SensorFusionMaskU mask{};
mask.flags.use_vision_pos = 1;
_vte->_vte_position.setVteAidMask(mask.value);
_vte->_vte_orientation.setVteAidMask(mask.value);
ASSERT_TRUE(_vte->_vte_position.init());
ASSERT_TRUE(_vte->_vte_orientation.init());
_vte->_vte_orientation_enabled = true;
_vte->_orientation_estimator_running = true; // Position stays stopped: exercise yaw-only reset detection.
uORB::Publication<fiducial_marker_pos_report_s> position_pub{ORB_ID(fiducial_marker_pos_report)};
uORB::Publication<fiducial_marker_yaw_report_s> yaw_pub{ORB_ID(fiducial_marker_yaw_report)};
uORB::SubscriptionData<vte_orientation_s> orientation{ORB_ID(vte_orientation)};
const auto publish_target = [&](hrt_abstime timestamp) {
ASSERT_TRUE(vte_test::publishVisionPos(position_pub, matrix::Vector3f(1.f, 0.f, -1.f),
vte_test::identityQuat(), matrix::Vector3f(0.01f, 0.01f, 0.01f), timestamp));
ASSERT_TRUE(vte_test::publishVisionYaw(yaw_pub, 0.2f, 0.01f, timestamp));
};
const hrt_abstime initial_time = vte_test::nowUs();
publishAttitude(vte_test::identityQuat(), initial_time, 7); // A nonzero first counter is not a reset.
_vte->_vte_position.setLocalVelocity(matrix::Vector3f{}, true, initial_time);
publish_target(initial_time);
_vte->_vte_position.update(matrix::Vector3f{});
_vte->updateEstimators();
ASSERT_TRUE(_vte->positionInitialized());
ASSERT_TRUE(orientation.update());
ASSERT_TRUE(orientation.get().orientation_valid);
_vte->seedBias();
_vte->_acc_sample_count = 3;
_vte->_vehicle_acc_ned_sum = matrix::Vector3f(1.f, 2.f, 3.f);
// Invalidate yaw immediately, before its next scheduled update.
vte_test::advanceMicroseconds(1_ms);
// A future attitude timestamp must still allow reacquisition from fresh samples below.
publishAttitude(vte_test::identityQuat(), vte_test::nowUs() + 1_s, 8);
_vte->updateEstimators();
EXPECT_FALSE(_vte->positionInitialized());
EXPECT_FALSE(_vte->biasSet());
EXPECT_EQ(_vte->_acc_sample_count, 0u);
expectVectorNear(_vte->_vehicle_acc_ned_sum, matrix::Vector3f{});
ASSERT_TRUE(orientation.update());
EXPECT_FALSE(orientation.get().orientation_valid);
// A normal reset must retain the boundary for old packets arriving on a later cycle.
_vte->_vte_position.resetFilter();
_vte->_vte_orientation.resetFilter();
vte_test::advanceMicroseconds(20_ms);
_vte->_vte_position.setLocalVelocity(matrix::Vector3f{}, true, vte_test::nowUs());
publish_target(initial_time);
_vte->_vte_position.update(matrix::Vector3f{});
_vte->updateEstimators();
EXPECT_FALSE(_vte->positionInitialized());
EXPECT_FALSE(orientation.update());
vte_test::advanceMicroseconds(20_ms);
const hrt_abstime fresh_time = vte_test::nowUs();
_vte->_vte_position.setLocalVelocity(matrix::Vector3f{}, true, fresh_time);
publish_target(fresh_time);
_vte->_vte_position.update(matrix::Vector3f{});
_vte->updateEstimators();
ASSERT_TRUE(_vte->positionInitialized());
ASSERT_TRUE(orientation.update());
EXPECT_TRUE(orientation.get().orientation_valid);
// Ordinary updates with an unchanged reset counter preserve the new estimate.
vte_test::advanceMicroseconds(20_ms);
publishAttitude(vte_test::identityQuat(), vte_test::nowUs(), 8);
_vte->updateEstimators();
EXPECT_TRUE(_vte->positionInitialized());
}
TEST_F(VisionTargetEstTest, PollEstimatorInputResetsCachedFrame)
{
ASSERT_TRUE(_vte->_vte_position.init());
matrix::Vector3f acceleration{}, position_offset{}, velocity_offset{};
matrix::Quaternionf attitude{};
bool acceleration_valid = false;
const hrt_abstime initial_time = vte_test::nowUs();
publishAttitude(vte_test::identityQuat(), initial_time);
ASSERT_TRUE(_vte->pollEstimatorInput(acceleration, attitude, position_offset, velocity_offset, false,
acceleration_valid));
_vte->seedBias();
_vte->_acc_sample_count = 3;
_vte->_vehicle_acc_ned_sum = matrix::Vector3f(1.f, 2.f, 3.f);
// A cached NED value can have a timestamp newer than the delayed attitude reset.
vte_test::advanceMicroseconds(2_ms);
_vte->_vte_position.setLocalVelocity(matrix::Vector3f{}, true, vte_test::nowUs());
publishAttitude(vte_test::identityQuat(), initial_time + 1_ms, 1);
ASSERT_TRUE(_vte->pollEstimatorInput(acceleration, attitude, position_offset, velocity_offset, false,
acceleration_valid));
EXPECT_FALSE(_vte->biasSet());
EXPECT_EQ(_vte->_acc_sample_count, 0u);
expectVectorNear(_vte->_vehicle_acc_ned_sum, matrix::Vector3f{});
vte::SensorFusionMaskU mask{};
mask.flags.use_vision_pos = 1;
_vte->_vte_position.setVteAidMask(mask.value);
uORB::Publication<fiducial_marker_pos_report_s> vision{ORB_ID(fiducial_marker_pos_report)};
// Fresh vision must wait for fresh velocity instead of using the old frame's cache.
for (const bool fresh_velocity : {false, true}) {
const hrt_abstime timestamp = vte_test::advanceMicroseconds(1_ms);
if (fresh_velocity) {
_vte->_vte_position.setLocalVelocity(matrix::Vector3f{}, true, timestamp);
}
ASSERT_TRUE(vte_test::publishVisionPos(vision, matrix::Vector3f{}, vte_test::identityQuat(),
matrix::Vector3f(0.01f, 0.01f, 0.01f), timestamp));
_vte->_vte_position.update(matrix::Vector3f{});
EXPECT_EQ(_vte->positionInitialized(), fresh_velocity);
}
}
TEST_F(VisionTargetEstTest, PollLocalPoseUsesSampleTime)
{
uORB::Publication<vehicle_local_position_s> publication{ORB_ID(vehicle_local_position)};
vehicle_local_position_s message{};
message.timestamp = vte_test::nowUs();
for (const hrt_abstime sample_time : {message.timestamp - 1_ms, hrt_abstime{0}}) {
message.timestamp_sample = sample_time;
ASSERT_TRUE(publication.publish(message));
VisionTargetEstTestable::LocalPose pose{};
ASSERT_TRUE(_vte->pollLocalPose(pose));
EXPECT_EQ(pose.timestamp, sample_time ? sample_time : message.timestamp);
}
}