Skip to content

Commit

Permalink
Merge pull request #3502 from shellixyz/fix_division_by_0_in_generate…
Browse files Browse the repository at this point in the history
…ThrottleCurve

Fix division by 0 in generateThrottleCurve()
  • Loading branch information
fiam committed Jul 8, 2018
2 parents d7c8a6e + 8fd2af3 commit 34eed40
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/main/fc/rc_curves.c
Expand Up @@ -17,9 +17,12 @@

#include <stdbool.h>
#include <stdint.h>
#include <math.h>

#include "platform.h"

#include "common/maths.h"

#include "fc/controlrate_profile.h"
#include "fc/rc_controls.h"
#include "fc/rc_curves.h"
Expand All @@ -31,7 +34,7 @@

#define PITCH_LOOKUP_LENGTH 7
#define YAW_LOOKUP_LENGTH 7
#define THROTTLE_LOOKUP_LENGTH 12
#define THROTTLE_LOOKUP_LENGTH 11

static int16_t lookupThrottleRC[THROTTLE_LOOKUP_LENGTH]; // lookup table for expo & mid THROTTLE
int16_t lookupThrottleRCMid; // THROTTLE curve mid point
Expand All @@ -55,12 +58,15 @@ void generateThrottleCurve(const controlRateConfig_t *controlRateConfig)
int16_t rcLookup(int32_t stickDeflection, uint8_t expo)
{
float tmpf = stickDeflection / 100.0f;
return (int16_t)((2500.0f + (float)expo * (tmpf * tmpf - 25.0f)) * tmpf / 25.0f);
return lrintf((2500.0f + (float)expo * (tmpf * tmpf - 25.0f)) * tmpf / 25.0f);
}

int16_t rcLookupThrottle(int32_t absoluteDeflection)
uint16_t rcLookupThrottle(uint16_t absoluteDeflection)
{
const int32_t lookupStep = absoluteDeflection / 100;
if (absoluteDeflection > 999)
return motorConfig()->maxthrottle;

const uint8_t lookupStep = absoluteDeflection / 100;
return lookupThrottleRC[lookupStep] + (absoluteDeflection - lookupStep * 100) * (lookupThrottleRC[lookupStep + 1] - lookupThrottleRC[lookupStep]) / 100;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/fc/rc_curves.h
Expand Up @@ -21,5 +21,5 @@ struct controlRateConfig_s;
void generateThrottleCurve(const struct controlRateConfig_s *controlRateConfig);

int16_t rcLookup(int32_t stickDeflection, uint8_t expo);
int16_t rcLookupThrottle(int32_t tmp);
uint16_t rcLookupThrottle(uint16_t tmp);
int16_t rcLookupThrottleMid(void);

0 comments on commit 34eed40

Please sign in to comment.