Gearmotorpid.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "Sub.h"
  2. #include <AP_UAVCAN/AP_UAVCAN.h>
  3. //履带PID类,包含左右履带两个
  4. TrackPidClass::TrackPidClass(float _p1,float _i1,float _d1,float _p2,float _i2,float _d2){
  5. motor1_head_target = 0;
  6. motor1_head_measure = 0;
  7. error_head1 = 0;
  8. error_last_head1 = 0;
  9. error_last2_head1 = 0;
  10. p1 = _p1;
  11. i1 = _i1;
  12. d1 = _d1;
  13. sum1 = 0.0;
  14. motor1_out = 0;
  15. motor2_head_target = 0;
  16. motor2_head_measure = 0;
  17. error_head2 = 0;
  18. error_last_head2 = 0;
  19. error_last2_head2 = 0;
  20. p2 = _p2;
  21. i2 = _i2;
  22. d2 = _d2;
  23. sum2 =0.0;
  24. motor2_out = 0;
  25. }
  26. int16_t TrackPidClass::updatePID1(float target,float measure,float _error,float MAX){
  27. float out=0.0;
  28. //未解锁复位
  29. if (!sub.motors.armed()) {
  30. sum1=0.0;
  31. }
  32. //误差防阶跃
  33. error_head1 = -measure;//-target+measure;
  34. if (error_head1<-180.0)
  35. {
  36. error_head1 +=360.0;
  37. }
  38. else if(error_head1>180.0){
  39. error_head1 -=360.0;
  40. }
  41. sum1+=error_head1;
  42. sum1 = constrain_float(sum1,-(MAX-31),MAX-31);
  43. out = p1*error_head1 +i1*sum1 +d1*(error_head1-error_last_head1);
  44. motor1_out =(int16_t)constrain_float(out,-(MAX-31),MAX-31);
  45. error_last_head1= error_head1;
  46. return motor1_out;
  47. }
  48. //履带PID
  49. //IN:目标值 和测量值
  50. //out:PWM
  51. int16_t TrackPidClass::updatePID2(float target,float measure,float _error,float MAX){
  52. //未解锁复位
  53. float out=0.0;
  54. if (!sub.motors.armed()) {
  55. sum2=0.0;
  56. }
  57. //误差防阶跃
  58. error_head2 = _error;//target-measure;
  59. if (error_head2<-180.0)
  60. {
  61. error_head2 +=360.0;
  62. }
  63. else if(error_head2>180.0){
  64. error_head2 -=360.0;
  65. }
  66. sum2+=error_head2;
  67. sum2 = constrain_float(sum2,-(MAX-31),MAX-31);
  68. out = p2*error_head2 +i2*sum2 + d2*(error_head2-error_last_head2);
  69. motor2_out =(int16_t)constrain_float(out,-(MAX-31),MAX-31);
  70. error_last_head2 = error_head2;
  71. return motor2_out;
  72. }