Skip to content

Commit

Permalink
Merge pull request betaflight#11126 from mathiasvr/pr-int-exp
Browse files Browse the repository at this point in the history
Use macros for power with integer exponents
  • Loading branch information
blckmn committed Jun 28, 2022
2 parents 88ae4e9 + 1b95e3c commit 00cd0e5
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/main/common/maths.h
Expand Up @@ -26,6 +26,7 @@
#define sq(x) ((x)*(x))
#endif
#define power3(x) ((x)*(x)*(x))
#define power5(x) ((x)*(x)*(x)*(x)*(x))

// Undefine this for use libc sinf/cosf. Keep this defined to use fast sin/cos approximations
#define FAST_MATH // order 9 approximation
Expand Down
2 changes: 1 addition & 1 deletion src/main/fc/rc.c
Expand Up @@ -208,7 +208,7 @@ float applyKissRates(const int axis, float rcCommandf, const float rcCommandfAbs
float applyActualRates(const int axis, float rcCommandf, const float rcCommandfAbs)
{
float expof = currentControlRateProfile->rcExpo[axis] / 100.0f;
expof = rcCommandfAbs * (powf(rcCommandf, 5) * expof + rcCommandf * (1 - expof));
expof = rcCommandfAbs * (power5(rcCommandf) * expof + rcCommandf * (1 - expof));

const float centerSensitivity = currentControlRateProfile->rcRates[axis] * 10.0f;
const float stickMovement = MAX(0, currentControlRateProfile->rates[axis] * 10.0f - centerSensitivity);
Expand Down
4 changes: 2 additions & 2 deletions src/main/flight/pid.c
Expand Up @@ -338,7 +338,7 @@ float pidCompensateThrustLinearization(float throttle)
if (pidRuntime.thrustLinearization != 0.0f) {
// for whoops where a lot of TL is needed, allow more throttle boost
const float throttleReversed = (1.0f - throttle);
throttle /= 1.0f + pidRuntime.throttleCompensateAmount * powf(throttleReversed, 2);
throttle /= 1.0f + pidRuntime.throttleCompensateAmount * sq(throttleReversed);
}
return throttle;
}
Expand All @@ -348,7 +348,7 @@ float pidApplyThrustLinearization(float motorOutput)
if (pidRuntime.thrustLinearization != 0.0f) {
if (motorOutput > 0.0f) {
const float motorOutputReversed = (1.0f - motorOutput);
motorOutput *= 1.0f + powf(motorOutputReversed, 2) * pidRuntime.thrustLinearization;
motorOutput *= 1.0f + sq(motorOutputReversed) * pidRuntime.thrustLinearization;
}
}
return motorOutput;
Expand Down
2 changes: 1 addition & 1 deletion src/main/flight/pid_init.c
Expand Up @@ -393,7 +393,7 @@ void pidInitConfig(const pidProfile_t *pidProfile)

#ifdef USE_THRUST_LINEARIZATION
pidRuntime.thrustLinearization = pidProfile->thrustLinearization / 100.0f;
pidRuntime.throttleCompensateAmount = pidRuntime.thrustLinearization - 0.5f * powf(pidRuntime.thrustLinearization, 2);
pidRuntime.throttleCompensateAmount = pidRuntime.thrustLinearization - 0.5f * sq(pidRuntime.thrustLinearization);
#endif

#if defined(USE_D_MIN)
Expand Down
2 changes: 1 addition & 1 deletion src/main/io/gps.c
Expand Up @@ -1787,7 +1787,7 @@ static void GPS_calculateDistanceFlownVerticalSpeed(bool initialize)
int32_t dir;
GPS_distance_cm_bearing(&gpsSol.llh.lat, &gpsSol.llh.lon, &lastCoord[GPS_LATITUDE], &lastCoord[GPS_LONGITUDE], &dist, &dir);
if (gpsConfig()->gps_use_3d_speed) {
dist = sqrtf(powf(gpsSol.llh.altCm - lastAlt, 2.0f) + powf(dist, 2.0f));
dist = sqrtf(sq(gpsSol.llh.altCm - lastAlt) + sq(dist));
}
GPS_distanceFlownInCm += dist;
}
Expand Down

0 comments on commit 00cd0e5

Please sign in to comment.