Skip to content

Commit

Permalink
Merge pull request #329 from Candas1/faster_atan2
Browse files Browse the repository at this point in the history
Faster atan2
  • Loading branch information
runger1101001 authored Oct 29, 2023
2 parents 4d117ba + b780a45 commit bc2349b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/common/foc_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,33 @@ __attribute__((weak)) void _sincos(float a, float* s, float* c){
*c = _cos(a);
}

// fast_atan2 based on https://math.stackexchange.com/a/1105038/81278
// Via Odrive project
// https://github.com/odriverobotics/ODrive/blob/master/Firmware/MotorControl/utils.cpp
// This function is MIT licenced, copyright Oskar Weigl/Odrive Robotics
// The origin for Odrive atan2 is public domain. Thanks to Odrive for making
// it easy to borrow.
__attribute__((weak)) float _atan2(float y, float x) {
// a := min (|x|, |y|) / max (|x|, |y|)
float abs_y = fabsf(y);
float abs_x = fabsf(x);
// inject FLT_MIN in denominator to avoid division by zero
float a = min(abs_x, abs_y) / (max(abs_x, abs_y));
// s := a * a
float s = a * a;
// r := ((-0.0464964749 * s + 0.15931422) * s - 0.327622764) * s * a + a
float r =
((-0.0464964749f * s + 0.15931422f) * s - 0.327622764f) * s * a + a;
// if |y| > |x| then r := 1.57079637 - r
if (abs_y > abs_x) r = 1.57079637f - r;
// if x < 0 then r := 3.14159274 - r
if (x < 0.0f) r = 3.14159274f - r;
// if y < 0 then r := -r
if (y < 0.0f) r = -r;

return r;
}


// normalizing radian angle to [0,2PI]
__attribute__((weak)) float _normalizeAngle(float angle){
Expand Down
5 changes: 5 additions & 0 deletions src/common/foc_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ float _cos(float a);
*/
void _sincos(float a, float* s, float* c);

/**
* Function approximating atan2
*
*/
float _atan2(float y, float x);

/**
* normalizing radian angle to [0,2PI]
Expand Down

0 comments on commit bc2349b

Please sign in to comment.