Advertisement
Lucky134Lucky

Untitled

May 15th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. {code}
  2. import time
  3. from rover import Rover
  4.  
  5. rover = Rover()
  6.  
  7. camera_horizontal = 0
  8. camera_vertical = 0
  9.  
  10. grippers_vertical = 0
  11.  
  12. light = 0
  13.  
  14.  
  15. class _GetchUnix:
  16. def __init__(self):
  17. import tty, sys
  18.  
  19. def __call__(self):
  20. import sys, tty, termios
  21. fd = sys.stdin.fileno()
  22. old_settings = termios.tcgetattr(fd)
  23. try:
  24. tty.setraw(sys.stdin.fileno())
  25. ch = sys.stdin.read(1)
  26. finally:
  27. termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
  28. return ch
  29.  
  30.  
  31. def writeMotors(leftA, leftB, rightA, rightB):
  32. rover.digital_write("MOTOR_L_A", leftA)
  33. rover.digital_write("MOTOR_L_B", leftB)
  34. rover.digital_write("MOTOR_R_A", rightA)
  35. rover.digital_write("MOTOR_R_B", rightB)
  36.  
  37. writeMotors(0,0,0,0)
  38.  
  39. def moveForward():
  40. writeMotors(1, 0, 1, 0)
  41. time.sleep(0.1)
  42. writeMotors(0,0,0,0)
  43.  
  44. return
  45.  
  46.  
  47. def moveBackward():
  48. writeMotors(0, 1, 0, 1)
  49. time.sleep(0.1)
  50. writeMotors(0,0,0,0)
  51. return
  52.  
  53.  
  54. def moveLeft():
  55. writeMotors(0, 1, 1, 0)
  56. time.sleep(0.1)
  57. writeMotors(0,0,0,0)
  58. return
  59.  
  60.  
  61. def moveRight():
  62. writeMotors(1, 0, 0, 1)
  63. time.sleep(0.1)
  64. writeMotors(0,0,0,0)
  65. return
  66.  
  67.  
  68. def scan():
  69. print("scanned result", rover.scan())
  70.  
  71.  
  72. def catch():
  73. rover.servo_write("GRIPPER_GRIP_LEFT", 100)
  74. rover.servo_write("GRIPPER_GRIP_RIGHT", 100)
  75. time.sleep(0.2)
  76.  
  77.  
  78. def releaseCatched():
  79. rover.servo_write("GRIPPER_GRIP_LEFT", 0)
  80. rover.servo_write("GRIPPER_GRIP_RIGHT", 0)
  81. time.sleep(0.2)
  82.  
  83.  
  84. def cameraUp():
  85. global camera_vertical
  86.  
  87. if (camera_vertical > 10):
  88. camera_vertical -= 5
  89.  
  90. rover.servo_write("CAMERA_VERTICAL", camera_vertical)
  91.  
  92.  
  93. def cameraDown():
  94. global camera_vertical
  95.  
  96. if (camera_vertical < 100):
  97. camera_vertical += 5
  98.  
  99. rover.servo_write("CAMERA_VERTICAL", camera_vertical)
  100.  
  101.  
  102. def cameraRight():
  103. global camera_horizontal
  104.  
  105. if (camera_horizontal > 10):
  106. camera_horizontal -= 5
  107.  
  108. rover.servo_write("CAMERA_HORIZONTAL", camera_horizontal)
  109.  
  110.  
  111. def cameraLeft():
  112. global camera_horizontal
  113.  
  114. if (camera_horizontal < 100):
  115. camera_horizontal += 5
  116.  
  117. rover.servo_write("CAMERA_HORIZONTAL", camera_horizontal)
  118.  
  119.  
  120. def grippersUp():
  121. global grippers_vertical
  122.  
  123. if (grippers_vertical >= 5):
  124. grippers_vertical -= 5
  125.  
  126. rover.servo_write("GRIPPER_VERTICAL", grippers_vertical)
  127.  
  128.  
  129. def grippersDown():
  130. global grippers_vertical
  131.  
  132. if (grippers_vertical < 100):
  133. grippers_vertical += 5
  134.  
  135. rover.servo_write("GRIPPER_VERTICAL", grippers_vertical)
  136.  
  137.  
  138. def toggleLight():
  139. global light
  140.  
  141. if (light == 1):
  142. light = 0
  143. rover.digital_write("LIGHT", light)
  144. elif (light == 0):
  145. light = 1
  146. rover.digital_write("LIGHT", light)
  147.  
  148.  
  149. def handle(char):
  150. if (char == 'w'):
  151. moveForward()
  152. elif (char == 'a'):
  153. moveLeft()
  154. elif (char == 's'):
  155. moveBackward()
  156. elif (char == 'd'):
  157. moveRight()
  158. elif (char == 'f'):
  159. scan()
  160. elif (char == '4'):
  161. cameraLeft()
  162. elif (char == '6'):
  163. cameraRight()
  164. elif (char == '8'):
  165. cameraUp()
  166. elif (char == '2'):
  167. cameraDown()
  168. elif (char == 'j'):
  169. catch()
  170. elif (char == 'l'):
  171. releaseCatched()
  172. elif (char == 'i'):
  173. grippersUp()
  174. elif (char == 'k'):
  175. grippersDown()
  176. elif (char == 'e'):
  177. toggleLight()
  178.  
  179.  
  180. while True:
  181. time.sleep(0.01)
  182.  
  183. c = _GetchUnix()()
  184. if (c == 'q'):
  185. break
  186.  
  187. handle(c)
  188. {code}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement