Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. /* library needed to include the int16_t and int32_t types.
  2. it is needed also in the floating point controllers for the
  3. definition of outputs and inputs. */
  4. #include <inttypes.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7.  
  8. #define FRAC_BITS 6 //16-bits, 6 fractional bits. 2^6 = 64.
  9. #define int2fix(x) ((x)<<FRAC_BITS) // shifts x to the left by a nr of bits
  10. #define fix2int(x) ((x)>>FRAC_BITS) // shifts x to the right by a nr of bits
  11. /*********************************/
  12. // define controller states here //
  13. /*********************************/
  14. /*
  15. Here use only int16_t variables for the fixed-point
  16. implementation.
  17. */
  18.  
  19. int16_t pos_fixed(int16_t r, int16_t y){
  20. // To prevent that the variables are re-allocated every
  21. // time (and therefore erased) you can use the keyword static
  22.  
  23. static int16_t x1 = 0; // The estimated angular velocity state (not shifted up)
  24. static int16_t x2 = 0; // The estimated angular position state (not shifted up)
  25. static int16_t v = 0; // Integral term of the control signal (not shifted up)
  26.  
  27. int16_t x1_new = 0; // The updated x1-state (not shifted up)
  28. int16_t x2_new = 0; // The updated x2-state (not shifted up)
  29. int16_t v_new = 0; // The updated v-state (not shifted up)
  30.  
  31. // The phi matrix is the A matrix for the discretised system.
  32. int16_t phi11_fp = 64; //Fixed point value of the first member of the phi matrix, original value: 0.9940;
  33. int16_t phi12_fp = 0; //Fixed point value of the second member of the phi matrix, original value: 0
  34. int16_t phi21_fp = 16; //Fixed point value of the third member of the phi matrix, original value: 0.2493;
  35. int16_t phi22_fp = 64; //Fixed point value of the fourth member of the phi matrix, original value: 1;
  36.  
  37. // The gamma vector is the B matrix for the discretised system.
  38. int16_t gamma1_fp = 7; //Fixed point value of the first member of the gamma vector, original value: 0.1122;
  39. int16_t gamma2_fp = 1; //Fixed point value of the second member of the gamma vector, original value: 0.0140;
  40.  
  41. // The L vector is the feedback term
  42. int16_t L1_fp = 211; //Fixed point value of the first member of the L vector, original value: 3.2898;
  43. int16_t L2_fp = 114; //Fixed point value of the second member of the L vector, original value: 1.7831;
  44.  
  45. // Lr is the scaling factor to achieve the right amplitude of the step response
  46. int16_t lr_fp = 114; //Fixed point value of the scaling factor, original value: 1.7831;
  47.  
  48. // Ke is the augmented observer vector
  49. int16_t Ke1_fp = 128; //Fixed point value of the first member of the Ke vector, original value: 2.0361;
  50. int16_t Ke2_fp = 80; //Fixed point value of the second member of the gamma vector, original value: 1.2440;
  51. int16_t Kev_fp = 205; //Fixed point value of the third member of the gamma vector, original value: 3.2096;
  52.  
  53. int16_t u = 0; // The control signal (not shifted up)
  54. int16_t eps = 0; // Epsilon is (measured angular pos) - (estimated angular position), so it is the estimated angular pos error. (not shifted up)
  55.  
  56. /**********************************/
  57. // Implement your controller here //
  58. /**********************************/
  59. /*
  60. Use only int32_t and int16_t variables for the fixed-point
  61. implementation.
  62. */
  63.  
  64. //The factors of the u-value are calculated separately, cast to int32_t and shifted down to avoid overflow, before being summed.
  65. int32_t lr_r = fix2int(lr_fp*r);
  66. int32_t L1_x1 = fix2int(L1_fp*x1);
  67. int32_t L2_x2 = fix2int(L2_fp*x2);
  68.  
  69. u = lr_r-L1_x1-L2_x2-v; //Function for u: u = lr_fp*r-L1_fp*x1-L2_fp*x2-v
  70.  
  71. //Calculate the new epsilon value (for the next sample)
  72. eps = y-x2;
  73.  
  74. //update the accumulated control signal value
  75. int32_t u_v = u+v;
  76.  
  77. //The factors of the next x1-value are calculated separately, cast to int32_t and shifted down to avoid overflow, before being summed.
  78. int32_t phi11_x1 = fix2int(phi11_fp*x1);
  79. int32_t phi12_x2 = fix2int(phi12_fp*x2);
  80. int32_t gamma1_uv = fix2int(gamma1_fp*u_v);
  81. int32_t K1_eps = fix2int(Ke1_fp*eps);
  82.  
  83. x1_new = phi11_x1 + phi12_x2 + gamma1_uv + K1_eps; // The estimated angular velocity of the next sample
  84.  
  85. //The factors of the next x2-value are calculated separately, cast to int32_t and shifted down to avoid overflow, before being summed.
  86. int32_t phi21_x1 = fix2int(phi21_fp*x1);
  87. int32_t phi22_x2 = fix2int(phi22_fp*x2);
  88. int32_t gamma2_uv = fix2int(gamma2_fp*u_v);
  89. int32_t K2_eps = fix2int(Ke2_fp*eps);
  90.  
  91. x2_new = phi21_x1 + phi22_x2 + gamma2_uv + K2_eps; // The estimated angular position of the next sample
  92.  
  93. //The factors of the next v-value are calculated separately, cast to int32_t and shifted down to avoid overflow, before being summed.
  94. int32_t Kev_eps = fix2int(Kev_fp*eps);
  95. v_new = Kev_eps + v; //The updated integral value of the control signal
  96.  
  97. //Update variables for next loop
  98. x1 = x1_new;
  99. x2 = x2_new;
  100. v = v_new;
  101.  
  102. if(u > 511){ //make sure the control signal is not too big
  103. u = 511;
  104. }else if(u < -512){ //and not too small
  105. u = -512;
  106. }
  107.  
  108. return u; // write output to D/A and end function
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement