Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EKF2: optical flow refactoring #22377

Merged
merged 4 commits into from
Nov 22, 2023
Merged

EKF2: optical flow refactoring #22377

merged 4 commits into from
Nov 22, 2023

Conversation

bresch
Copy link
Member

@bresch bresch commented Nov 15, 2023

replaces #21005

Solved Problem

The optical flow control logic is way too complicated to read, has a lot of strange behaviors due to the multiple possible paths in the code and several strange legacy checks.
Furthermore, it tries to compensate the flow due to angular motion by popping the data from the buffer early to be able to accumulate gyro data during the flow integration interval, which makes the code even more complicated. I also think that it wasn't working as expected and that most of the time, only 1 gyro sample is available between the start of the integration and the midpoint (where we want to fuse it), so IMO, there is no real benefit of doing it this way. Also note that this integration is also now done in VehicleOpticalFlow.

Solution

  • convert to flow rate before sending to EKF2 backend
  • align flow rate data at midpoint of integration time
  • allow the flow fusion and terrain estimator to run before arming (not only really useful for bench tests but also makes the navigation more consistent before and after arming).

Changelog Entry

For release notes:

Refactor EKF2 optical flow fusion

Test coverage

  • Unit tests
  • flight tests

@bresch bresch marked this pull request as draft November 15, 2023 16:00
@bresch bresch self-assigned this Nov 15, 2023
@bresch bresch added the EKF2 label Nov 15, 2023
@bresch bresch requested a review from dagar November 15, 2023 16:00
@bresch
Copy link
Member Author

bresch commented Nov 17, 2023

Short indoor flight replay comparison. The navigation result is the same, except pre-flight where main is constantly resetting the position to 0 while this PR lets the EKF navigate using flow even before arming.

This pr:
(log of the actual flight: https://logs.px4.io/plot_app?log=5ff01712-e33a-4902-9a91-93706e17308a)
Screenshot from 2023-11-17 12-03-07

Main:
Screenshot from 2023-11-17 12-02-51

@bresch bresch marked this pull request as ready for review November 17, 2023 11:06
@bresch
Copy link
Member Author

bresch commented Nov 17, 2023

Replayed with the flow gyro set to NAN, the result is also almost identical
Screenshot from 2023-11-17 12-09-51

@@ -66,6 +66,10 @@ void Ekf::runTerrainEstimator(const imuSample &imu_delayed)
if (!_control_status.flags.in_air) {
_last_on_ground_posD = _state.pos(2);
_control_status.flags.rng_fault = false;

} else if (!_control_status_prev.flags.in_air) {
// Let the estimator run freely before arming for bench testing purposes, but reset on takeoff
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I ask why this reset is required in transition to takeoff?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I've seen cases where a distance sensor would report a high valid data (~3m) while on ground and since since the velocity = flow_rate * distance, it's safer to always takeoff with a small distance estimate.
If the distance is under-estimated, the drone will drift a bit, but if it's over-estimated it can directly diverge, which is much more dangerous.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comment describing this directly in the code.

@bresch
Copy link
Member Author

bresch commented Nov 20, 2023

Outdoor flights:

No issues, good flight behavior.

Velocity accuracy compared to GNSS (EKF2 only fuses optical flow)
Screenshot from 2023-11-20 12-15-49

@bresch
Copy link
Member Author

bresch commented Nov 20, 2023

Cleaned-up the commit history and rebased on main.

bresch added 3 commits November 20, 2023 10:43
Use flow rates instead of integrals in backend. This allows us to delay
the data to the mitpoint integration time and simplifies the code in
general.
Gyro compensation can still be done in EKF2 if needed, but the
flow module normally already appends the correct gyro data to the flow
message.
required to parse larger topics
@dagar dagar merged commit e568b4a into main Nov 22, 2023
84 of 89 checks passed
@dagar dagar deleted the pr-ekf2-flow-refactor branch November 22, 2023 02:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants