/Silverware/src/stickvector.c

https://github.com/silver13/Eachine-E011 · C · 73 lines · 38 code · 27 blank · 8 comment · 6 complexity · 99222a949e479f8d7d39fd4c138e5717 MD5 · raw file

  1. #include "config.h"
  2. #include "util.h"
  3. #include <math.h>
  4. #include <string.h>
  5. extern float GEstG[3];
  6. extern float Q_rsqrt( float number );
  7. // error vector between stick position and quad orientation
  8. // this is the output of this function
  9. float errorvect[3];
  10. // cache the last result so it does not get calculated everytime
  11. float last_rx[2] = {13.13f , 12.12f};
  12. float stickvector[3] = { 0 , 0 , 1};
  13. void stick_vector( float rx_input[] , float maxangle)
  14. {
  15. // only compute stick rotation if values changed
  16. if ( last_rx[0] == rx_input[0] && last_rx[1] == rx_input[1] )
  17. {
  18. }
  19. else
  20. {
  21. last_rx[0] = rx_input[0];
  22. last_rx[1] = rx_input[1];
  23. float pitch, roll;
  24. // rotate down vector to match stick position
  25. pitch = rx_input[1] * MAX_ANGLE_HI * DEGTORAD + (float) TRIM_PITCH * DEGTORAD;
  26. roll = rx_input[0] * MAX_ANGLE_HI * DEGTORAD + (float) TRIM_ROLL * DEGTORAD;
  27. stickvector[0] = fastsin( roll );
  28. stickvector[1] = fastsin( pitch );
  29. stickvector[2] = fastcos( roll ) * fastcos( pitch );
  30. float mag2 = (stickvector[0] * stickvector[0] + stickvector[1] * stickvector[1]);
  31. if ( mag2 > 0.001f )
  32. {
  33. mag2 = Q_rsqrt( mag2 / (1 - stickvector[2] * stickvector[2]) );
  34. }
  35. else mag2 = 0.707f;
  36. stickvector[0] *=mag2;
  37. stickvector[1] *=mag2;
  38. }
  39. // find error between stick vector and quad orientation
  40. // vector cross product
  41. errorvect[1]= -((GEstG[1]*stickvector[2]) - (GEstG[2]*stickvector[1]));
  42. errorvect[0]= (GEstG[2]*stickvector[0]) - (GEstG[0]*stickvector[2]);
  43. // some limits just in case
  44. limitf( &errorvect[0] , 1.0);
  45. limitf( &errorvect[1] , 1.0);
  46. }