Repository navigation
mixer: thrust linearization, ported from Betaflight #12121
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
Open
MrScothh
wants to merge
12
commits into
iNavFlight:next-10.1
Choose a base branch
from
MrScothh:feature/thrust-linearization
base: next-10.1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+275
−3
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f8e4ce0
asyncfatfs: keep afatfs_poll out of the ITCM scheduler
MrScothh 104163b
mixer: throttle boost, ported from Betaflight
MrScothh b05bab3
mixer: thrust linearization, ported from Betaflight
MrScothh e6f73d3
Merge branch 'feature/throttle-boost' into feature/thrust-linearization
MrScothh 0d96c54
mixer: restart the throttle boost after any skipped pass or a cutoff …
MrScothh 8d50e9f
mixer: thrust linearization only with a throttle weight of 1 on every…
MrScothh a21ec12
mixer: keep the SRXL2 motor fields on every target
MrScothh 4558d5a
Merge branch 'feature/throttle-boost' into feature/thrust-linearization
MrScothh 8f33325
blackbox: throttle_boost and throttle_boost_cutoff in the log header
MrScothh 90687f8
Merge branch 'feature/throttle-boost' into feature/thrust-linearization
MrScothh b2fd306
blackbox: thrust_linear in the log header
MrScothh aadd73f
Merge maintenance-10.x into feature/thrust-linearization
MrScothh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * This file is part of INAV. | ||
| * | ||
| * INAV is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * INAV is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with INAV. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| // Betaflight 2026.6 (#15226), on 0-1 over the throttle range with e = thrust_linear / 100; sound only for e <= 1 | ||
|
|
||
| // Raises low motor outputs, where thrust grows more slowly than the command | ||
| static inline float thrustLinearCurve(float u, float e) | ||
| { | ||
| const float inv = 1.0f - u; | ||
| return u * (1.0f + e * inv * (1.0f + e * (inv - u))); | ||
| } | ||
|
|
||
| // Inverse of the curve to second order in e, applied to the throttle so hover stays where it was | ||
| static inline float thrustLinearCompensate(float t, float e) | ||
| { | ||
| return t * (1.0f - e * (1.0f - t)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| * This file is part of INAV. | ||
| * | ||
| * INAV is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * INAV is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with INAV. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #include <math.h> | ||
|
|
||
| extern "C" { | ||
| #include "flight/thrust_linear.h" | ||
| } | ||
|
|
||
| #include "unittest_macros.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| // thrust_linear 0-100 in steps of 10 | ||
| static float strength(int step) | ||
| { | ||
| return step / 10.0f; | ||
| } | ||
|
|
||
| TEST(ThrustLinearTest, OffIsIdentity) | ||
| { | ||
| for (int i = 0; i <= 100; i++) { | ||
| const float x = i / 100.0f; | ||
| EXPECT_FLOAT_EQ(thrustLinearCurve(x, 0.0f), x); | ||
| EXPECT_FLOAT_EQ(thrustLinearCompensate(x, 0.0f), x); | ||
| } | ||
| } | ||
|
|
||
| TEST(ThrustLinearTest, MatchesBetaflight) | ||
| { | ||
| // Betaflight 2026.6 pidApplyThrustLinearization() / pidCompensateThrustLinearization(), worked by hand | ||
| EXPECT_NEAR(thrustLinearCurve(0.3f, 0.5f), 0.426f, 1e-5f); | ||
| EXPECT_NEAR(thrustLinearCompensate(0.3f, 0.5f), 0.195f, 1e-5f); | ||
| EXPECT_NEAR(thrustLinearCurve(0.8f, 1.0f), 0.864f, 1e-5f); | ||
| } | ||
|
|
||
| TEST(ThrustLinearTest, CurveKeepsEndsAndOrder) | ||
| { | ||
| for (int s = 0; s <= 10; s++) { | ||
| const float e = strength(s); | ||
| EXPECT_NEAR(thrustLinearCurve(0.0f, e), 0.0f, 1e-6f); | ||
| EXPECT_NEAR(thrustLinearCurve(1.0f, e), 1.0f, 1e-6f); | ||
| float previous = 0.0f; | ||
| for (int i = 1; i <= 1000; i++) { | ||
| const float u = i / 1000.0f; | ||
| const float out = thrustLinearCurve(u, e); | ||
| EXPECT_GE(out, previous) << "e " << e << " u " << u; | ||
| EXPECT_GE(out, u - 1e-6f) << "raises, never lowers: e " << e << " u " << u; | ||
| EXPECT_LE(out, 1.0f + 1e-6f) << "e " << e << " u " << u; | ||
| previous = out; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| TEST(ThrustLinearTest, CompensationNeverNegative) | ||
| { | ||
| for (int s = 0; s <= 10; s++) { | ||
| for (int i = 0; i <= 1000; i++) { | ||
| EXPECT_GE(thrustLinearCompensate(i / 1000.0f, strength(s)), 0.0f); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| TEST(ThrustLinearTest, HoverStaysWhereItWas) | ||
| { | ||
| // Worst cases near 15 % throttle: 0.81 % of the range at 50, 2.5 % at 70, 8.7 % at 100 | ||
| for (int s = 0; s <= 10; s++) { | ||
| const float e = strength(s); | ||
| const float tolerance = e <= 0.5f ? 0.0085f : (e <= 0.7f ? 0.026f : 0.088f); | ||
| for (int i = 100; i <= 1000; i++) { | ||
| const float t = i / 1000.0f; | ||
| EXPECT_NEAR(thrustLinearCurve(thrustLinearCompensate(t, e), e), t, tolerance) << "e " << e << " t " << t; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.