Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. # VEX EDR Python-Project
  2. import sys
  3. import vex
  4.  
  5. #region config
  6. l_encoder = vex.Encoder(1, True)
  7. r_encoder = vex.Encoder(3, True)
  8. gyro = vex.GyroSensor(1)
  9. l_motor = vex.Motor(1, True)
  10. arm_motor = vex.Motor(4)
  11. claw_motor = vex.Motor(5)
  12. r_motor = vex.Motor(10)
  13. joystick = vex.Joystick()
  14. #endregion config
  15.  
  16. class PID:
  17. """A class implementing a PID controller."""
  18. def __init__(self, p, i, d, get_current_time, get_feedback_value):
  19. # p, i and d values
  20. self.p, self.i, self.d = p, i, d
  21.  
  22. # saves the functions that return the time and the feedback
  23. self.get_current_time = get_current_time
  24. self.get_feedback_value = get_feedback_value
  25.  
  26. self.reset()
  27.  
  28.  
  29. def reset(self):
  30. """Resets/creates all of the non-constant variables of the class."""
  31. # reset PID values
  32. self.proportional, self.integral, self.derivative = 0, 0, 0
  33.  
  34. # reset previous time and error variables
  35. self.previous_time, self.previous_error = 0, 0
  36.  
  37.  
  38. def get_value(self):
  39. """Calculates and returns the PID value."""
  40. # calculate the error (how far off the goal are we)
  41. error = self.goal - self.get_feedback_value()
  42.  
  43. current_time = self.get_current_time()
  44.  
  45. # variables used to calculate proportional and integral
  46. delta_time = current_time - self.previous_time
  47. delta_error = error - self.previous_error
  48.  
  49. self.proportional = self.p * error # calculate proportional
  50. self.integral += error * delta_time # calculate integral
  51.  
  52. self.derivative = 0
  53. if (delta_time > 0):
  54. self.derivative = delta_error / delta_time # calculate derivative
  55.  
  56. # update error and time values
  57. self.previous_time = current_time
  58. self.previous_error = error
  59.  
  60. # return the "normalized" PID value
  61. pid = self.proportional + (self.i * self.integral) + (self.d * self.derivative)
  62. if pid > 1:
  63. return 1
  64. elif pid < -1:
  65. return -1
  66. else:
  67. return pid
  68.  
  69.  
  70. def set_goal(self, goal):
  71. """Sets the goal of the PID controller."""
  72. self.goal = goal
  73.  
  74.  
  75. def arcade_drive(rotate, drive, left_motor, right_motor):
  76. # variables to determine the quadrants
  77. maximum = max(abs(drive), abs(rotate))
  78. total, difference = drive + rotate, drive - rotate
  79.  
  80. # go by quadrants and set the speed accordingly
  81. if drive >= 0:
  82. if rotate >= 0:
  83. left_motor(maximum)
  84. right_motor(difference)
  85. else:
  86. left_motor(total)
  87. right_motor(maximum)
  88. else:
  89. if rotate >= 0:
  90. left_motor(total)
  91. right_motor(-maximum)
  92. else:
  93. left_motor(-maximum)
  94. right_motor(difference)
  95.  
  96.  
  97. class PolynomialFunction:
  98. """A class implementing a polynomial function controller."""
  99.  
  100. def __init__(self, coefficients, get_feedback_value):
  101. """Note that the valid x values for the polynomial function coefficients
  102. should be from 0 to 1 (the function is scaled by the goal)."""
  103. self.coefficients = coefficients # the coefficients of the function
  104. self.get_feedback_value = get_feedback_value # the feedback function
  105.  
  106.  
  107. def get_value(self):
  108. """Returns the polynomial function value at feedback function value."""
  109. # calculate the x coordinate (by "stretching" the function by goal)
  110. x = self.get_feedback_value() / abs(self.goal)
  111.  
  112. if x > 1:
  113. return 0
  114.  
  115. # calculate function value using Horner's method
  116. value = self.coefficients[0]
  117. for i in range(1, len(self.coefficients)):
  118. value = x * value + self.coefficients[i]
  119.  
  120. # make sure that value isn't above 1
  121. if value > 1:
  122. value = 1
  123.  
  124. # if goal is negative, function value is negative
  125. return value if self.goal > 0 else -value
  126.  
  127.  
  128. def set_goal(self, goal):
  129. """Sets the goal of the controller."""
  130. self.goal = goal
  131.  
  132.  
  133. angle_pid = PID(0.2, 0.002, 0.015, sys.clock, gyro.gyro_degrees)
  134. angle_pid.set_goal(0)
  135.  
  136. drive_controller = PolynomialFunction([-14.30556, 28.61111, -21.28194, 6.976389, 0.15], l_encoder.value)
  137. drive_controller.set_goal(100 / 0.091776)
  138.  
  139. while True:
  140. arm_motor.run(joystick.axis3())
  141. claw_motor.run(joystick.axis4())
  142.  
  143. if joystick.b6down():
  144. arcade_drive(joystick.axis1(), -joystick.axis2(), l_motor.run, r_motor.run)
  145.  
  146. if joystick.b6up():
  147. sys.sleep(0.05)
  148. pid_value = angle_pid.get_value()
  149. arcade_drive(pid_value * 35, 0, l_motor.run, r_motor.run)
  150.  
  151. if joystick.b5up():
  152. sys.sleep(0.05)
  153. pid_value = angle_pid.get_value()
  154. drive_value = drive_controller.get_value()
  155.  
  156. arcade_drive(pid_value * 35, drive_value * 80, l_motor.run, r_motor.run)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement