Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. #include "Balanduino.h"
  2. #include "Controller.h"
  3. #include <Arduino.h> // Standard Arduino header
  4. #include <Wire.h> // Official Arduino Wire library
  5. #include <Kalman.h>
  6. #include <avrpins.h>
  7.  
  8. static Kalman kalman;
  9.  
  10. // Define the parameters for the inner PID-controller (continuous)
  11.  
  12. double Kp_inner = 28.5309;
  13. double Ki_inner = 241.5285;
  14. double Kd_inner = 2.4831;
  15. double Tf_inner = 0.04;
  16.  
  17. double P_inner = 0.0;
  18. double I_inner = 0.0;
  19. double D_inner = 0.0;
  20.  
  21. double e_inner_old = 0.0;
  22.  
  23. // Define the parameters for the outer PI-controller (continuous)
  24. double Kp_outer = -0.0069;
  25. double Ki_outer = 0;
  26. double Kd_outer = -0.0333;
  27. double Tf_outer = 1.06;
  28.  
  29. double P_outer = 0.0;
  30. double I_outer = 0.0;
  31. double D_outer = 0.0;
  32.  
  33. double e_outer_old = 0.0;
  34.  
  35. double balanduino_pos = 0.0;
  36. double reference; // Reference value (In Swedish: Börvärde)
  37.  
  38. void setup()
  39. {
  40. // Initialize all sensor hardwares, filters and observers
  41. initialize();
  42. // Setup pulse generator for the setpoint
  43. // First argument is the low value of the pulse
  44. // Second argument is the hight value of the pulse
  45. // Third argument is the pulse width in microseconds
  46. setup_setpoint_generator_pulse(0.0, 1.0, 10000000);
  47. }
  48.  
  49. void loop()
  50. {
  51. //
  52. // Part 1: Sanity check - make sure that the motors are ok
  53. //
  54. checkMotors();
  55.  
  56. //
  57. // Part 2: Time management - read actual time and
  58. // calculate the time since last sample time.
  59. //
  60. unsigned long timer_us = micros(); // Time of current sample in microseconds
  61. h = (double)(timer_us - pidTimer_us) / 1000000.0; // Time since last sample in seconds
  62. pidTimer_us = timer_us; // Save the time of this sampleTime of last sample
  63.  
  64.  
  65. // Part 3: Read the angular orientation (rad) of the robot
  66. double theta = getTheta();
  67.  
  68. // Drive motors if the robot is not lying down. If it is lying down, it has to be put in a vertical position before it starts to balance again.
  69. // When balancing it has to deviate more ±45 degrees form 0 degrees before it stops trying to balance
  70. if ((layingDown && (pitch < cfg.targetAngle - 10 || pitch > cfg.targetAngle + 10)) || (!layingDown && (pitch < cfg.targetAngle - 45 || pitch > cfg.targetAngle + 45))) {
  71. layingDown = true; // The robot is in a unsolvable position, so turn off both motors and wait until it's vertical again
  72. stopAndReset();
  73. // The robot is not laying down
  74. } else {
  75. // It's no longer laying down
  76. layingDown = false;
  77.  
  78. //
  79. // Part 4: Read the longitudinal velocity (m/s) of the robot
  80. //
  81. double v = getSpeed(h);
  82. balanduino_pos = balanduino_pos + v * h;
  83.  
  84. // Outer PI-controller:
  85. //
  86. // Part 5: Generate setpoint value (In Swedish: Börvärde)
  87. //
  88. reference = setpoint_generator_pulse();
  89. //double v_r = 0;
  90.  
  91. //
  92. // Part 6: Generate the control error for the outer loop, i.e. difference
  93. // between the reference_value and the actual_output.
  94. //
  95. //double e_outer = v_r - v; // Control error
  96. double e_outer = reference - balanduino_pos; // Control error
  97.  
  98. double c0 = Kp_outer;
  99. double c1 = Tf_outer/(Tf_outer+h);
  100. double c2 = Kd_outer/(Tf_outer+h);
  101. double c3 = Ki_outer*h;
  102. //
  103. // Part 7: Calculate control output (In Swedish: Styrsignal)
  104. //
  105. // Implement your PI-controller here:
  106. //
  107. // Pseudo-kod for a PID-controller
  108. // P = c0 * e
  109. // D = c1 * D + c2 * (e - eold)
  110. // u = P + I + D // Bestäm totala styrsignalen
  111. // daout("u", u) // Skriv styrsignalen till DA-omv.
  112. // I = I + c3 * e // Uppdatera integraldelen
  113. // eold = e
  114.  
  115. P_outer = Kp_outer * e_outer;
  116. D_outer = c1 * D_outer + c2 * (e_outer - e_outer_old);
  117. double u_outer = P_outer + I_outer+ D_outer; // Calculate control output
  118. I_outer = I_outer + c3 * e_outer;
  119. e_outer_old = e_outer;
  120.  
  121. // Inner PID-controller:
  122. //
  123. // Part 8: Generate setpoint value for inner controller (In Swedish: Börvärde)
  124. //
  125. double theta_r = u_outer;
  126.  
  127. //
  128. // Part 9: Generate the control error for the inner loop, i.e. difference
  129. // between the reference_value and the actual_output.
  130. //
  131. double e_inner = theta_r - theta; // Control error
  132.  
  133. c0 = Kp_inner;
  134. c1 = Tf_inner/(Tf_inner+h);
  135. c2 = Kd_inner/(Tf_inner+h);
  136. c3 = Ki_inner*h;
  137. //
  138. // Part 10: Calculate control output (In Swedish: Styrsignal)
  139. //
  140. // Implement your PID-controller here:
  141. //
  142. // Pseudo-kod for a PID-controller
  143. // P = c0 * e
  144. // D = c1 * D + c2 * (e - eold)
  145. // u = P + I + D // Bestäm totala styrsignalen
  146. // daout("u", u) // Skriv styrsignalen till DA-omv.
  147. // I = I + c3 * e // Uppdatera integraldelen
  148. // eold = e
  149.  
  150. P_inner = c0 * e_inner;
  151. D_inner = c1 * D_inner + c2 * (e_inner - e_inner_old);
  152. double u = P_inner+D_inner+I_inner; // Calculate control output
  153. double saturated_u = constrain(u, -12.0, 12.0); // Make sure the calculated output is -12 <= u <= 12 (Min voltage -12V, max voltage +12V)
  154.  
  155. I_inner = I_inner + c3 * e_inner;
  156. e_inner_old = e_inner;
  157.  
  158.  
  159. //
  160. // Part 11: Actuate the control output by sending
  161. // the control output to the motors
  162. //
  163. actuateControlSignal(saturated_u);
  164. }
  165.  
  166.  
  167. // Update the motor encoder values
  168. //updateEncoders();
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement