image.png

定义常量

  1. #define PI 3.1415926535
  2. #define deg2rad(deg) ((deg)*PI/180)
  3. #define rad2deg(rad) ((rad)*180/PI)
  4. #define AF 12.5
  5. #define AB 21.5
  6. #define EF 21.5
  7. #define CE 21.5
  8. #define BC 34.19
  9. #define CD 34.95
  10. #define bcd_angle deg2rad(120)
  11. #define half_AF (AF/2.0)

反解实现

  1. double AD2 = 0;
  2. double AD = 0;
  3. double angle_DAF = 0;
  4. double BD2 = 0;
  5. double BD = 0;
  6. double angle_BAD = 0;
  7. double angle_DBA = 0;
  8. double angle_DBC = 0;
  9. double angle_ABC = 0;
  10. double AC2 = 0;
  11. double AC = 0;
  12. double angle_BAC = 0;
  13. double angle_CAF = 0;
  14. double CF2 = 0;
  15. double CF = 0;
  16. double angle_AFC = 0;
  17. double angle_CFE = 0;
  18. //步态相关
  19. double sigma = 0;
  20. double zep = 0;
  21. double xep_b = 0;
  22. double xep_z = 0;
  23. double alpha = 0;
  24. double beta = 0;
  25. //8个舵机的转动角度
  26. double steering_angles[8] = {0};
  27. void leg_inverse(double fx,double fy,double *alpha,double *beta){
  28. //AD长度
  29. AD2 = pow((half_AF+fx),2.0)+pow(fy,2.0);
  30. AD = sqrt(AD2);
  31. //DAF夹角
  32. angle_DAF = asin(fy/AD);
  33. //BD长度
  34. BD2 = pow(BC,2.0)+pow(CD,2.0)-2*BC*CD*cos(bcd_angle);
  35. BD = sqrt(BD2);
  36. //BAD夹角
  37. angle_BAD = acos((pow(AB,2.0)+AD2-BD2)/(2*AB*AD));
  38. //alpha角度
  39. *alpha = PI-angle_BAD-angle_DAF;
  40. angle_DBA = acos((pow(AB,2.0)+BD2-AD2)/(2*AB*BD));
  41. angle_DBC = acos((pow(BC,2.0)+BD2-pow(CD,2.0))/(2*BC*BD));
  42. angle_ABC = angle_DBA-angle_DBC;
  43. //AC长度
  44. AC2 = pow(AB,2.0)+pow(BC,2.0)-2*AB*BC*cos(angle_ABC);
  45. AC = sqrt(AC2);
  46. angle_BAC = acos((pow(AB,2.0)+AC2-pow(BC,2.0))/(2*AB*AC));
  47. angle_CAF = PI-(*alpha)-angle_BAC;
  48. CF2 = pow(AF,2.0)+AC2-2*AF*AC*cos(angle_CAF);
  49. CF = sqrt(CF2);
  50. angle_AFC = acos((pow(AF,2.0)+CF2-AC2)/(2*AF*CF));
  51. angle_CFE = acos((CF2+pow(EF,2.0)-pow(CE,2.0))/(2*CF*EF));
  52. *beta = PI-angle_AFC-angle_CFE;
  53. // printf("======================\n");
  54. // printf("al:%lf ,beta:%lf\n",*alpha,*beta);
  55. }