refactor(ekf2): move the optical flow publications into OpticalFlowSource

This commit is contained in:
Marco Hauswirth authored and Marco Hauswirth committed 2026-11-18 13:52:41 +01:00
1 parent 3bd1839a72
commit 8aada67965
11 files changed
+73 -71

No files matched your search

@@ -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)
@@ -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)
@@ -446,7 +446,7 @@
*(.text._ZN13NavigatorMode3runEb)
*(.text._ZL19param_find_internalPKcb)
*(.text.uart_datasent)
*(.text._ZN4EKF221PublishOpticalFlowVelERKy)
*(.text._ZN17OpticalFlowSource14publishFlowVelERK3Ekfy)
*(.text.nxsem_destroy)
*(.text._ZN21MavlinkStreamAltitude4sendEv)
*(.text._ZN7sensors14VehicleAirData12UpdateStatusEv)
@@ -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)
@@ -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)
@@ -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)
@@ -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)) {
@@ -45,8 +45,10 @@
#if defined(MODULE_NAME)
# include <lib/parameters/param.h>
# include <uORB/PublicationMulti.hpp>
# include <uORB/Subscription.hpp>
# include <uORB/topics/vehicle_optical_flow.h>
# include <uORB/topics/vehicle_optical_flow_vel.h>
#endif // MODULE_NAME
class Ekf;
@@ -98,6 +100,12 @@ public:
#if defined(MODULE_NAME)
// configured SENS_FLOW<slot>_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<estimator_aid_source2d_s> _aid_src_pub{ORB_ID(estimator_aid_src_optical_flow)};
uORB::PublicationMulti<vehicle_optical_flow_vel_s> _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<float, estimator::State::size> &H, bool update_terrain);
-7
View File
@@ -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
+7 -40
View File
@@ -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 &timestamp)
// 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 &timestamp)
}
#endif // CONFIG_EKF2_WIND
#if defined(CONFIG_EKF2_OPTICAL_FLOW)
void EKF2::PublishOpticalFlowVel(const hrt_abstime &timestamp)
{
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)
{
-18
View File
@@ -112,10 +112,6 @@
# include <uORB/topics/vehicle_magnetometer.h>
#endif // CONFIG_EKF2_MAGNETOMETER
#if defined(CONFIG_EKF2_OPTICAL_FLOW)
# include <uORB/topics/vehicle_optical_flow_vel.h>
#endif // CONFIG_EKF2_OPTICAL_FLOW
#if defined(CONFIG_EKF2_RANGE_FINDER)
# include <uORB/topics/distance_sensor.h>
#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 &timestamp);
#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<estimator_aid_source2d_s> aid_src{ORB_ID(estimator_aid_src_optical_flow)};
uORB::PublicationMulti<vehicle_optical_flow_vel_s> 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};