moved isqrt to filter.h
This commit is contained in:
parent
288aa914e9
commit
546178c700
21
fw/filter.h
21
fw/filter.h
@ -2,6 +2,7 @@
|
||||
#define _FILTER_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <assert.h>
|
||||
|
||||
|
||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
@ -23,4 +24,24 @@ static_assert(-4 >> 1 == -2, ">> doesn't do sign extension");
|
||||
#define I16_FP_EMA_K8(x, s) (int16_t)((((int32_t)(x)<<8) - (x) + (s)) >> 8)
|
||||
#define I16_FP_EMA_K16(x, s) (int16_t)((((int32_t)(x)<<16) - (x) + (s)) >> 16)
|
||||
|
||||
// Integer square root (binary search)
|
||||
// https://en.wikipedia.org/wiki/Integer_square_root
|
||||
static inline uint16_t isqrt(uint32_t x)
|
||||
{
|
||||
uint16_t l = 0; // lower bound of the square root
|
||||
uint16_t r = x + 1; // upper bound of the square root
|
||||
|
||||
while (l != r - 1) {
|
||||
uint32_t m = (l + r) / 2; // midpoint to test
|
||||
if (m * m <= x) {
|
||||
l = m;
|
||||
} else {
|
||||
r = m;
|
||||
}
|
||||
}
|
||||
|
||||
return l;
|
||||
}
|
||||
|
||||
|
||||
#endif // _FILTER_H
|
||||
|
||||
20
fw/main.c
20
fw/main.c
@ -345,26 +345,6 @@ static inline void pwm_set(uint16_t pulse_width)
|
||||
}
|
||||
|
||||
|
||||
// Integer square root (binary search)
|
||||
// https://en.wikipedia.org/wiki/Integer_square_root
|
||||
static inline uint16_t isqrt(uint32_t x)
|
||||
{
|
||||
uint16_t l = 0; // lower bound of the square root
|
||||
uint16_t r = x + 1; // upper bound of the square root
|
||||
|
||||
while (l != r - 1) {
|
||||
uint32_t m = (l + r) / 2; // midpoint to test
|
||||
if (m * m <= x) {
|
||||
l = m;
|
||||
} else {
|
||||
r = m;
|
||||
}
|
||||
}
|
||||
|
||||
return l;
|
||||
}
|
||||
|
||||
|
||||
// Input: temperature difference
|
||||
// Output: duty-cycle 0-max_duty
|
||||
uint16_t pid(int16_t delta, int16_t max_duty)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user