Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. # This program demonstrates usage of the servos.
  2. # Keep the robot in a safe location before running this program,
  3. # as it will immediately begin moving.
  4. # See https://learn.adafruit.com/adafruit-16-channel-pwm-servo-hat-for-raspberry-pi/ for more details.
  5.  
  6. import time
  7. import Adafruit_PCA9685
  8. import signal
  9. import math
  10.  
  11. from encoders import resetCounts, getSpeeds, getCounts, initEncoders
  12.  
  13.  
  14. # The servo hat uses its own numbering scheme within the Adafruit library.
  15. # 0 represents the first servo, 1 for the second, and so on.
  16. LSERVO = 0
  17. RSERVO = 1
  18.  
  19. # This function is called when Ctrl+C is pressed.
  20. # It's intended for properly exiting the program.
  21. def ctrlC(signum, frame):
  22. print("Exiting")
  23.  
  24. # Stop the servos
  25. pwm.set_pwm(LSERVO, 0, 0);
  26. pwm.set_pwm(RSERVO, 0, 0);
  27.  
  28. exit()
  29.  
  30. # Attach the Ctrl+C signal interrupt
  31. signal.signal(signal.SIGINT, ctrlC)
  32.  
  33. # Initialize the servo hat library.
  34. pwm = Adafruit_PCA9685.PCA9685()
  35.  
  36. # 50Hz is used for the frequency of the servos.
  37. pwm.set_pwm_freq(50)
  38.  
  39. # Write an initial value of 1.5, which keeps the servos stopped.
  40. # Due to how servos work, and the design of the Adafruit library,
  41. # the value must be divided by 20 and multiplied by 4096.
  42. pwm.set_pwm(LSERVO, 0, math.floor(1.5 / 20 * 4096));
  43. pwm.set_pwm(RSERVO, 0, math.floor(1.5 / 20 * 4096));
  44.  
  45. def setSpeedsPWM(pwmLeft, pwmRight, t):
  46. pwm.set_pwm(LSERVO, 0, math.floor(pwmLeft / 20 * 4096));
  47. pwm.set_pwm(RSERVO, 0, math.floor(pwmRight / 20 * 4096));
  48. time.sleep(t)
  49. pwm.set_pwm(LSERVO, 0, math.floor(1.5 / 20 * 4096));
  50. pwm.set_pwm(RSERVO, 0, math.floor(1.5 / 20 * 4096));
  51.  
  52.  
  53.  
  54. def calibrateSpeeds():
  55. pwmLeft = 1.5
  56. pwmRight = 1.5
  57. pwm.set_pwm(LSERVO, 0, math.floor(1.5 / 20 * 4096));
  58. pwm.set_pwm(RSERVO, 0, math.floor(1.5 / 20 * 4096));
  59. time.sleep(1)
  60. resetCounts()
  61. file2write=open("calibrate.txt", 'w')
  62. file2write.write("LeftPWM RightPWM LeftSpeed(RPS) RightSpeed(RPS) \n")
  63. while pwmLeft < 1.7:
  64. resetCounts()
  65. pwmLeft += 0.01
  66. pwmRight += 0.01
  67. pwm.set_pwm(LSERVO, 0, math.floor(pwmLeft / 20 * 4096));
  68. pwm.set_pwm(RSERVO, 0, math.floor(pwmRight / 20 * 4096));
  69. time.sleep(3)
  70. if pwmLeft == 1.51:
  71. print()
  72. else:
  73. file2write.write('%.4f ' % pwmLeft)
  74. file2write.write('%.4f ' % pwmRight)
  75. out = getSpeeds()
  76. file2write.write('%.4f %.4f \n' % out)
  77. resetCounts()
  78.  
  79. time.sleep(1.5)
  80. pwmLeft = 1.5
  81. pwmRight = 1.5
  82. pwm.set_pwm(LSERVO, 0, math.floor(pwmLeft / 20 * 4096));
  83. pwm.set_pwm(RSERVO, 0, math.floor(pwmRight / 20 * 4096));
  84. time.sleep(1.5)
  85. resetCounts()
  86.  
  87. while pwmLeft > 1.3:
  88. resetCounts()
  89. pwmLeft -= 0.01
  90. pwmRight -= 0.01
  91. pwm.set_pwm(LSERVO, 0, math.floor(pwmLeft / 20 * 4096));
  92. pwm.set_pwm(RSERVO, 0, math.floor(pwmRight / 20 * 4096));
  93. time.sleep(3)
  94. if pwmLeft == 1.49:
  95. print()
  96. else:
  97. file2write.write('%.4f ' % pwmLeft)
  98. file2write.write('%.4f ' % pwmRight)
  99. out = getSpeeds()
  100. file2write.write('%.4f %.4f \n' % out)
  101. resetCounts()
  102.  
  103. pwm.set_pwm(LSERVO, 0, math.floor(1.5 / 20 * 4096));
  104. pwm.set_pwm(RSERVO, 0, math.floor(1.5 / 20 * 4096));
  105.  
  106. file2write.close()
  107.  
  108.  
  109.  
  110.  
  111.  
  112. def setSpeedsRPS(rpsLeft, rpsRight, t):
  113. pwmLeft = 1.5
  114. pwmRight = 1.5
  115. count = 0
  116. if rpsLeft > 0.815 or rpsRight > 0.815:
  117. print("The robot cannot move this quickly.")
  118.  
  119. else:
  120. array = []
  121. file2read = open("calibrate.txt", "r")
  122. next(file2read)
  123. for line in file2read:
  124. fields = line.split(" ")
  125. pwmL = float(fields[0])
  126. pwmR = float(fields[1])
  127. speedL = float(fields[2])
  128. speedR = float(fields[3])
  129. count += 1
  130.  
  131.  
  132.  
  133.  
  134. if pwmL > 1.50:
  135. pwmRight = pwmR
  136. if speedL == round(rpsLeft, 4):
  137. pwmLeft = pwmL
  138. print("speedl: ", speedL)
  139. print("rpsLeft: ", rpsLeft)
  140. print("left: ", pwmLeft)
  141. count = 0
  142.  
  143. if count == 19:
  144. pwmRight = pwmR
  145. if rpsRight == 0:
  146. pwmRight = 0
  147. print("Right: ", pwmRight)
  148. if rpsLeft == 0:
  149. pwmRight = pwmL
  150. pwmLeft = 0
  151.  
  152. if pwmLeft == 1.5:
  153. print("left: Invalid RPS")
  154. if pwmRight == 1.5:
  155. print("right: Invalid RPS")
  156. else:
  157. setSpeedsPWM(pwmLeft, pwmRight, t)
  158.  
  159. #Rectangle function
  160. # setSpeedsPWM(pwmRight, pwmRight - .4, t)
  161.  
  162.  
  163.  
  164. '''
  165.  
  166. if direction == 1 and pwmL > 1.5:
  167. if round(speedL, 1) == round(rpsLeft, 1):
  168. pwmLeft = round(pwmL, 3)
  169. print("left: ", pwmLeft)
  170. if round(speedR, 1) == round(rpsRight, 1):
  171. pwmRight = round(pwmR, 3)
  172. print("right: ", pwmRight)
  173. elif direction == 0 and pwmR > 1.5:
  174. if round(speedL, 1) == round(rpsLeft, 1):
  175. pwmLeft = round(pwmL, 3)
  176. print("left: ", pwmLeft)
  177. if round(speedR, 1) == round(rpsRight, 1):
  178. pwmRight = round(pwmR, 3)
  179. print("right: ", pwmRight)
  180.  
  181. if pwmLeft == 1.5:
  182. print("left: Invalid RPS")
  183. if pwmRight == 1.5:
  184. print("right: Invalid RPS")
  185. else:
  186. setSpeedsPWM(pwmLeft, pwmRight)
  187.  
  188. time.sleep(4)
  189. pwm.set_pwm(LSERVO, 0, math.floor(1.5 / 20 * 4096));
  190. pwm.set_pwm(RSERVO, 0, math.floor(1.5 / 20 * 4096));
  191. '''
  192.  
  193. def setSpeedsIPS(ipsLeft, ipsRight, time):
  194. ipsLeft = ipsLeft / 8.2
  195. left = round(ipsLeft, 5)
  196. ipsRight = ipsRight / 8.2
  197. right = round(ipsRight, 5)
  198. if ipsLeft > .8125 or ipsRight > .8125:
  199. print("Speed is too great")
  200. if ipsLeft < .125 or ipsRight < .125:
  201. print("Speed is too slow.")
  202. else:
  203. setSpeedsRPS(ipsLeft, ipsRight, time)
  204.  
  205. #def setSpeedVW(v, w):
  206.  
  207.  
  208. #v = (ipsRight + ipsLeft)/2
  209. #w = (ipsRight - ipsLeft)/3.95
  210.  
  211. initEncoders()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement