/H8mini_test/src/stickvector.c

https://github.com/silver13/h8mini-testing · C · 57 lines · 32 code · 20 blank · 5 comment · 3 complexity · a2baffe095247c5de7baece8fd44cbf4 MD5 · raw file

  1. #include "config.h"
  2. #include "util.h"
  3. #include <math.h>
  4. extern float rxcopy[];
  5. extern float GEstG[3];
  6. extern float Q_rsqrt( float number );
  7. float errorvect[3];
  8. void stick_vector( float maxangle)
  9. {
  10. float stickvector[3];
  11. float pitch, roll;
  12. // rotate down vector to match stick position
  13. pitch = rxcopy[1] * maxangle * DEGTORAD + (float) TRIM_PITCH * DEGTORAD;
  14. roll = rxcopy[0] * maxangle * DEGTORAD + (float) TRIM_ROLL * DEGTORAD;
  15. stickvector[0] = fastsin( roll );
  16. stickvector[1] = fastsin( pitch );
  17. stickvector[2] = fastcos( roll ) * fastcos( pitch );
  18. float mag2 = (stickvector[0] * stickvector[0] + stickvector[1] * stickvector[1]);
  19. if ( mag2 > 0.001f )
  20. {
  21. mag2 = Q_rsqrt( mag2 / (1 - stickvector[2] * stickvector[2]) );
  22. }
  23. else mag2 = 0.707f;
  24. stickvector[0] *=mag2;
  25. stickvector[1] *=mag2;
  26. // normalize quad orientation vector to 1.00 ( it's already normalized to 2048 )
  27. float g_vect[3];
  28. for ( int i = 0 ; i <3; i++)
  29. g_vect[i] = GEstG[i] * ( 1/2048.0f);
  30. // find error between stick vector and quad orientation
  31. // vector cross product (optimized)
  32. errorvect[1]= - ( (g_vect[1] * stickvector[2]) - (g_vect[2]*stickvector[1]) );
  33. errorvect[0]= (g_vect[2] * stickvector[0]) - (g_vect[0]*stickvector[2]);
  34. // some limits just in case
  35. limitf( & errorvect[0] , 1.0);
  36. limitf( & errorvect[1] , 1.0);
  37. }