From 8aada6796556ae998187ea7c2db96793f41d3e0f Mon Sep 17 00:00:00 2001 From: Marco Hauswirth Date: Mon, 16 Nov 2026 16:56:48 +0100 Subject: [PATCH] refactor(ekf2): move the optical flow publications into OpticalFlowSource --- .../scripts/itcm_functions_includes.ld | 2 +- .../scripts/itcm_functions_includes.ld | 2 +- .../scripts/itcm_functions_includes.ld | 2 +- .../scripts/itcm_functions_includes.ld | 2 +- .../scripts/itcm_functions_includes.ld | 2 +- .../scripts/itcm_functions_includes.ld | 2 +- .../aid_sources/optical_flow/optical_flow.cpp | 47 +++++++++++++++++++ .../optical_flow/optical_flow_source.hpp | 13 +++++ src/modules/ekf2/EKF/ekf.h | 7 --- src/modules/ekf2/EKF2.cpp | 47 +++---------------- src/modules/ekf2/EKF2.hpp | 18 ------- 11 files changed, 73 insertions(+), 71 deletions(-) diff --git a/boards/agam/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld b/boards/agam/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld index 58a96d058ab..062532fbbe4 100644 --- a/boards/agam/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld +++ b/boards/agam/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld @@ -516,7 +516,7 @@ *(.text._ZN13NavigatorMode3runEb) *(.text._ZL19param_find_internalPKcb) *(.text.uart_datasent) -*(.text._ZN4EKF221PublishOpticalFlowVelERKy) +*(.text._ZN17OpticalFlowSource14publishFlowVelERK3Ekfy) *(.text.nxsem_destroy) *(.text.file_write) *(.text._ZN21MavlinkStreamAltitude4sendEv) diff --git a/boards/ark/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld b/boards/ark/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld index bbcfab71dc0..ad2c4d3293c 100644 --- a/boards/ark/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld +++ b/boards/ark/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld @@ -502,7 +502,7 @@ *(.text._ZN13NavigatorMode3runEb) *(.text._ZL19param_find_internalPKcb) *(.text.uart_datasent) -*(.text._ZN4EKF221PublishOpticalFlowVelERKy) +*(.text._ZN17OpticalFlowSource14publishFlowVelERK3Ekfy) *(.text.nxsem_destroy) *(.text.file_write) *(.text._ZN21MavlinkStreamAltitude4sendEv) diff --git a/boards/nxp/mr-navq95/nuttx-config/scripts/itcm_functions_includes.ld b/boards/nxp/mr-navq95/nuttx-config/scripts/itcm_functions_includes.ld index ca22774088d..8c54fb85554 100644 --- a/boards/nxp/mr-navq95/nuttx-config/scripts/itcm_functions_includes.ld +++ b/boards/nxp/mr-navq95/nuttx-config/scripts/itcm_functions_includes.ld @@ -446,7 +446,7 @@ *(.text._ZN13NavigatorMode3runEb) *(.text._ZL19param_find_internalPKcb) *(.text.uart_datasent) -*(.text._ZN4EKF221PublishOpticalFlowVelERKy) +*(.text._ZN17OpticalFlowSource14publishFlowVelERK3Ekfy) *(.text.nxsem_destroy) *(.text._ZN21MavlinkStreamAltitude4sendEv) *(.text._ZN7sensors14VehicleAirData12UpdateStatusEv) diff --git a/boards/nxp/mr-tropic/nuttx-config/scripts/itcm_functions_includes.ld b/boards/nxp/mr-tropic/nuttx-config/scripts/itcm_functions_includes.ld index 79de3d7fc6b..a360ba18946 100644 --- a/boards/nxp/mr-tropic/nuttx-config/scripts/itcm_functions_includes.ld +++ b/boards/nxp/mr-tropic/nuttx-config/scripts/itcm_functions_includes.ld @@ -507,7 +507,7 @@ *(.text._ZN13NavigatorMode3runEb) *(.text._ZL19param_find_internalPKcb) *(.text.uart_datasent) -*(.text._ZN4EKF221PublishOpticalFlowVelERKy) +*(.text._ZN17OpticalFlowSource14publishFlowVelERK3Ekfy) *(.text.nxsem_destroy) *(.text.file_write) *(.text._ZN21MavlinkStreamAltitude4sendEv) diff --git a/boards/nxp/tropic-community/nuttx-config/scripts/itcm_functions_includes.ld b/boards/nxp/tropic-community/nuttx-config/scripts/itcm_functions_includes.ld index 5e0cf3fd048..120d3751fb3 100644 --- a/boards/nxp/tropic-community/nuttx-config/scripts/itcm_functions_includes.ld +++ b/boards/nxp/tropic-community/nuttx-config/scripts/itcm_functions_includes.ld @@ -506,7 +506,7 @@ *(.text._ZN13NavigatorMode3runEb) *(.text._ZL19param_find_internalPKcb) *(.text.uart_datasent) -*(.text._ZN4EKF221PublishOpticalFlowVelERKy) +*(.text._ZN17OpticalFlowSource14publishFlowVelERK3Ekfy) *(.text.nxsem_destroy) *(.text.file_write) *(.text._ZN21MavlinkStreamAltitude4sendEv) diff --git a/boards/px4/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld b/boards/px4/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld index 11d85e07b62..7ce23a5f90a 100644 --- a/boards/px4/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld +++ b/boards/px4/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld @@ -516,7 +516,7 @@ *(.text._ZN13NavigatorMode3runEb) *(.text._ZL19param_find_internalPKcb) *(.text.uart_datasent) -*(.text._ZN4EKF221PublishOpticalFlowVelERKy) +*(.text._ZN17OpticalFlowSource14publishFlowVelERK3Ekfy) *(.text.nxsem_destroy) *(.text.file_write) *(.text._ZN21MavlinkStreamAltitude4sendEv) diff --git a/src/modules/ekf2/EKF/aid_sources/optical_flow/optical_flow.cpp b/src/modules/ekf2/EKF/aid_sources/optical_flow/optical_flow.cpp index 3b56e3d1695..47841913450 100644 --- a/src/modules/ekf2/EKF/aid_sources/optical_flow/optical_flow.cpp +++ b/src/modules/ekf2/EKF/aid_sources/optical_flow/optical_flow.cpp @@ -109,6 +109,53 @@ float OpticalFlowSource::delayMs() const return delay_ms; } +void OpticalFlowSource::advertise() +{ + _aid_src_pub.advertise(); + _vel_pub.advertise(); +} + +void OpticalFlowSource::publishAidSource(uint64_t timestamp, uint8_t estimator_instance) +{ + if (_aid_src.timestamp_sample > _aid_src_pub_last) { + estimator_aid_source2d_s aid_src{_aid_src}; + aid_src.estimator_instance = estimator_instance; + aid_src.timestamp = timestamp; + _aid_src_pub.publish(aid_src); + + _aid_src_pub_last = _aid_src.timestamp_sample; + } +} + +void OpticalFlowSource::publishFlowVel(const Ekf &ekf, uint64_t timestamp) +{ + const uint64_t timestamp_sample = _aid_src.timestamp_sample; + + if ((timestamp_sample != 0) && (timestamp_sample > _vel_pub_last)) { + vehicle_optical_flow_vel_s flow_vel{}; + flow_vel.timestamp_sample = timestamp_sample; + + _vel_body.copyTo(flow_vel.vel_body); + ekf.getFlowVelNE(_slot).copyTo(flow_vel.vel_ne); + + _vel_body_lpf.getState().copyTo(flow_vel.vel_body_filtered); + ekf.getFilteredFlowVelNE(_slot).copyTo(flow_vel.vel_ne_filtered); + + _sample_delayed.flow_rate.copyTo(flow_vel.flow_rate_uncompensated); + _rate_compensated.copyTo(flow_vel.flow_rate_compensated); + + _sample_delayed.gyro_rate.copyTo(flow_vel.gyro_rate); + + _gyro_bias.copyTo(flow_vel.gyro_bias); + _ref_body_rate.copyTo(flow_vel.ref_gyro); + + flow_vel.timestamp = timestamp; + _vel_pub.publish(flow_vel); + + _vel_pub_last = timestamp_sample; + } +} + bool OpticalFlowSource::updateSample(Ekf &ekf, vehicle_optical_flow_s &optical_flow) { if (!_sub.update(&optical_flow)) { diff --git a/src/modules/ekf2/EKF/aid_sources/optical_flow/optical_flow_source.hpp b/src/modules/ekf2/EKF/aid_sources/optical_flow/optical_flow_source.hpp index c946b59f77c..d14e46a494b 100644 --- a/src/modules/ekf2/EKF/aid_sources/optical_flow/optical_flow_source.hpp +++ b/src/modules/ekf2/EKF/aid_sources/optical_flow/optical_flow_source.hpp @@ -45,8 +45,10 @@ #if defined(MODULE_NAME) # include +# include # include # include +# include #endif // MODULE_NAME class Ekf; @@ -98,6 +100,12 @@ public: #if defined(MODULE_NAME) // configured SENS_FLOW_DELAY, 0 while the slot is disabled float delayMs() const; + + void advertise(); + + // publish once per new flow sample + void publishAidSource(uint64_t timestamp, uint8_t estimator_instance); + void publishFlowVel(const Ekf &ekf, uint64_t timestamp); #endif // MODULE_NAME private: @@ -123,6 +131,11 @@ private: } _param_handles{}; uORB::Subscription _sub{ORB_ID(vehicle_optical_flow)}; + + uORB::PublicationMulti _aid_src_pub{ORB_ID(estimator_aid_src_optical_flow)}; + uORB::PublicationMulti _vel_pub{ORB_ID(estimator_optical_flow_vel)}; + uint64_t _aid_src_pub_last{0}; + uint64_t _vel_pub_last{0}; #endif // MODULE_NAME bool fuse(Ekf &ekf, matrix::Vector &H, bool update_terrain); diff --git a/src/modules/ekf2/EKF/ekf.h b/src/modules/ekf2/EKF/ekf.h index d394b0f64dc..d2e7f7fd976 100644 --- a/src/modules/ekf2/EKF/ekf.h +++ b/src/modules/ekf2/EKF/ekf.h @@ -145,13 +145,6 @@ public: const Vector2f &getFilteredFlowVelBody(uint8_t slot = 0) const { return _flow_aiding.source(slot)._vel_body_lpf.getState(); } Vector2f getFilteredFlowVelNE(uint8_t slot = 0) const { return Vector2f(_R_to_earth * Vector3f(getFilteredFlowVelBody(slot)(0), getFilteredFlowVelBody(slot)(1), 0.f)); } - const Vector2f &getFlowCompensated(uint8_t slot = 0) const { return _flow_aiding.source(slot)._rate_compensated; } - const Vector2f &getFlowUncompensated(uint8_t slot = 0) const { return _flow_aiding.source(slot)._sample_delayed.flow_rate; } - - const Vector3f getFlowGyro(uint8_t slot = 0) const { return _flow_aiding.source(slot)._sample_delayed.gyro_rate; } - const Vector3f &getFlowGyroBias(uint8_t slot = 0) const { return _flow_aiding.source(slot)._gyro_bias; } - const Vector3f &getFlowRefBodyRate(uint8_t slot = 0) const { return _flow_aiding.source(slot)._ref_body_rate; } - uint8_t getPrimaryFlowSlot() const { return _flow_aiding.primarySlot(); } #endif // CONFIG_EKF2_OPTICAL_FLOW diff --git a/src/modules/ekf2/EKF2.cpp b/src/modules/ekf2/EKF2.cpp index d6d7e74a18c..b27bd1b822c 100644 --- a/src/modules/ekf2/EKF2.cpp +++ b/src/modules/ekf2/EKF2.cpp @@ -374,8 +374,7 @@ void EKF2::AdvertiseTopics() for (uint8_t i = 0; i < MAX_OF_INSTANCES; i++) { if (_ekf.flowSource(i).params.ctrl) { - _optical_flow_pubs[i].aid_src.advertise(); - _optical_flow_pubs[i].vel.advertise(); + _ekf.flowSource(i).advertise(); } } @@ -889,7 +888,11 @@ void EKF2::Run() #endif // CONFIG_EKF2_GNSS #if defined(CONFIG_EKF2_OPTICAL_FLOW) - PublishOpticalFlowVel(now); + + for (uint8_t i = 0; i < MAX_OF_INSTANCES; i++) { + _ekf.flowSource(i).publishFlowVel(_ekf, _replay_mode ? now : hrt_absolute_time()); + } + #endif // CONFIG_EKF2_OPTICAL_FLOW UpdateAccelCalibration(now); @@ -1207,8 +1210,7 @@ void EKF2::PublishAidSourceStatus(const hrt_abstime ×tamp) // optical flow for (uint8_t i = 0; i < MAX_OF_INSTANCES; i++) { - PublishAidSourceStatus(timestamp, _ekf.aid_src_optical_flow(i), _optical_flow_pubs[i].aid_src_last, - _optical_flow_pubs[i].aid_src); + _ekf.flowSource(i).publishAidSource(_replay_mode ? timestamp : hrt_absolute_time(), _instance); } #endif // CONFIG_EKF2_OPTICAL_FLOW @@ -2209,41 +2211,6 @@ void EKF2::PublishWindEstimate(const hrt_abstime ×tamp) } #endif // CONFIG_EKF2_WIND -#if defined(CONFIG_EKF2_OPTICAL_FLOW) -void EKF2::PublishOpticalFlowVel(const hrt_abstime ×tamp) -{ - for (uint8_t i = 0; i < MAX_OF_INSTANCES; i++) { - const hrt_abstime timestamp_sample = _ekf.aid_src_optical_flow(i).timestamp_sample; - - if ((timestamp_sample != 0) && (timestamp_sample > _optical_flow_pubs[i].vel_last)) { - - vehicle_optical_flow_vel_s flow_vel{}; - flow_vel.timestamp_sample = timestamp_sample; - - _ekf.getFlowVelBody(i).copyTo(flow_vel.vel_body); - _ekf.getFlowVelNE(i).copyTo(flow_vel.vel_ne); - - _ekf.getFilteredFlowVelBody(i).copyTo(flow_vel.vel_body_filtered); - _ekf.getFilteredFlowVelNE(i).copyTo(flow_vel.vel_ne_filtered); - - _ekf.getFlowUncompensated(i).copyTo(flow_vel.flow_rate_uncompensated); - _ekf.getFlowCompensated(i).copyTo(flow_vel.flow_rate_compensated); - - _ekf.getFlowGyro(i).copyTo(flow_vel.gyro_rate); - - _ekf.getFlowGyroBias(i).copyTo(flow_vel.gyro_bias); - _ekf.getFlowRefBodyRate(i).copyTo(flow_vel.ref_gyro); - - flow_vel.timestamp = _replay_mode ? timestamp : hrt_absolute_time(); - - _optical_flow_pubs[i].vel.publish(flow_vel); - - _optical_flow_pubs[i].vel_last = timestamp_sample; - } - } -} -#endif // CONFIG_EKF2_OPTICAL_FLOW - #if defined(CONFIG_EKF2_AIRSPEED) void EKF2::UpdateAirspeedSample(ekf2_timestamps_s &ekf2_timestamps) { diff --git a/src/modules/ekf2/EKF2.hpp b/src/modules/ekf2/EKF2.hpp index 5e23a8cee46..0f9b932ab41 100644 --- a/src/modules/ekf2/EKF2.hpp +++ b/src/modules/ekf2/EKF2.hpp @@ -112,10 +112,6 @@ # include #endif // CONFIG_EKF2_MAGNETOMETER -#if defined(CONFIG_EKF2_OPTICAL_FLOW) -# include -#endif // CONFIG_EKF2_OPTICAL_FLOW - #if defined(CONFIG_EKF2_RANGE_FINDER) # include #endif // CONFIG_EKF2_RANGE_FINDER @@ -229,9 +225,6 @@ private: void UpdateGnssYawSample(); # endif // CONFIG_EKF2_GNSS_YAW #endif // CONFIG_EKF2_GNSS -#if defined(CONFIG_EKF2_OPTICAL_FLOW) - void PublishOpticalFlowVel(const hrt_abstime ×tamp); -#endif // CONFIG_EKF2_OPTICAL_FLOW #if defined(CONFIG_EKF2_MAGNETOMETER) void UpdateMagSample(ekf2_timestamps_s &ekf2_timestamps); #endif // CONFIG_EKF2_MAGNETOMETER @@ -344,17 +337,6 @@ private: uORB::Subscription _ranging_beacon_sub {ORB_ID(ranging_beacon)}; #endif // CONFIG_EKF2_RANGING_BEACON -#if defined(CONFIG_EKF2_OPTICAL_FLOW) - struct OpticalFlowPublications { - uORB::PublicationMulti aid_src{ORB_ID(estimator_aid_src_optical_flow)}; - uORB::PublicationMulti vel{ORB_ID(estimator_optical_flow_vel)}; - hrt_abstime aid_src_last{0}; - hrt_abstime vel_last{0}; - }; - - OpticalFlowPublications _optical_flow_pubs[MAX_OF_INSTANCES] {}; -#endif // CONFIG_EKF2_OPTICAL_FLOW - #if defined(CONFIG_EKF2_BAROMETER) uint8_t _baro_calibration_count {0}; uint32_t _device_id_baro{0};