AP_Math: added helper for16bit float conversions

This commit is contained in:
Tom Pittenger 2021-04-22 08:54:02 -07:00 committed by Tom Pittenger
parent 5e2dbd93fc
commit ba6bb21560
2 changed files with 17 additions and 0 deletions

View File

@ -405,6 +405,18 @@ void fill_nanf(float *f, uint16_t count)
}
#endif
// Convert 16-bit fixed-point to float
float fixed2float(const uint16_t input, const uint8_t fractional_bits)
{
return ((float)input / (float)(1U << fractional_bits));
}
// Convert float to 16-bit fixed-point
uint16_t float2fixed(const float input, const uint8_t fractional_bits)
{
return (uint16_t)(roundf(input * (1U << fractional_bits)));
}
/*
calculate turn rate in deg/sec given a bank angle and airspeed for a
fixed wing aircraft

View File

@ -297,6 +297,11 @@ float calc_lowpass_alpha_dt(float dt, float cutoff_freq);
void fill_nanf(float *f, uint16_t count);
#endif
// from https://embeddedartistry.com/blog/2018/07/12/simple-fixed-point-conversion-in-c/
// Convert to/from 16-bit fixed-point and float
float fixed2float(const uint16_t input, const uint8_t fractional_bits = 8);
uint16_t float2fixed(const float input, const uint8_t fractional_bits = 8);
/*
calculate turn rate in deg/sec given a bank angle and airspeed for a
fixed wing aircraft