Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {code}
- import time
- from rover import Rover
- rover = Rover()
- camera_horizontal = 0
- camera_vertical = 0
- grippers_vertical = 0
- light = 0
- class _GetchUnix:
- def __init__(self):
- import tty, sys
- def __call__(self):
- import sys, tty, termios
- fd = sys.stdin.fileno()
- old_settings = termios.tcgetattr(fd)
- try:
- tty.setraw(sys.stdin.fileno())
- ch = sys.stdin.read(1)
- finally:
- termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
- return ch
- def writeMotors(leftA, leftB, rightA, rightB):
- rover.digital_write("MOTOR_L_A", leftA)
- rover.digital_write("MOTOR_L_B", leftB)
- rover.digital_write("MOTOR_R_A", rightA)
- rover.digital_write("MOTOR_R_B", rightB)
- writeMotors(0,0,0,0)
- def moveForward():
- writeMotors(1, 0, 1, 0)
- time.sleep(0.1)
- writeMotors(0,0,0,0)
- return
- def moveBackward():
- writeMotors(0, 1, 0, 1)
- time.sleep(0.1)
- writeMotors(0,0,0,0)
- return
- def moveLeft():
- writeMotors(0, 1, 1, 0)
- time.sleep(0.1)
- writeMotors(0,0,0,0)
- return
- def moveRight():
- writeMotors(1, 0, 0, 1)
- time.sleep(0.1)
- writeMotors(0,0,0,0)
- return
- def scan():
- print("scanned result", rover.scan())
- def catch():
- rover.servo_write("GRIPPER_GRIP_LEFT", 100)
- rover.servo_write("GRIPPER_GRIP_RIGHT", 100)
- time.sleep(0.2)
- def releaseCatched():
- rover.servo_write("GRIPPER_GRIP_LEFT", 0)
- rover.servo_write("GRIPPER_GRIP_RIGHT", 0)
- time.sleep(0.2)
- def cameraUp():
- global camera_vertical
- if (camera_vertical > 10):
- camera_vertical -= 5
- rover.servo_write("CAMERA_VERTICAL", camera_vertical)
- def cameraDown():
- global camera_vertical
- if (camera_vertical < 100):
- camera_vertical += 5
- rover.servo_write("CAMERA_VERTICAL", camera_vertical)
- def cameraRight():
- global camera_horizontal
- if (camera_horizontal > 10):
- camera_horizontal -= 5
- rover.servo_write("CAMERA_HORIZONTAL", camera_horizontal)
- def cameraLeft():
- global camera_horizontal
- if (camera_horizontal < 100):
- camera_horizontal += 5
- rover.servo_write("CAMERA_HORIZONTAL", camera_horizontal)
- def grippersUp():
- global grippers_vertical
- if (grippers_vertical >= 5):
- grippers_vertical -= 5
- rover.servo_write("GRIPPER_VERTICAL", grippers_vertical)
- def grippersDown():
- global grippers_vertical
- if (grippers_vertical < 100):
- grippers_vertical += 5
- rover.servo_write("GRIPPER_VERTICAL", grippers_vertical)
- def toggleLight():
- global light
- if (light == 1):
- light = 0
- rover.digital_write("LIGHT", light)
- elif (light == 0):
- light = 1
- rover.digital_write("LIGHT", light)
- def handle(char):
- if (char == 'w'):
- moveForward()
- elif (char == 'a'):
- moveLeft()
- elif (char == 's'):
- moveBackward()
- elif (char == 'd'):
- moveRight()
- elif (char == 'f'):
- scan()
- elif (char == '4'):
- cameraLeft()
- elif (char == '6'):
- cameraRight()
- elif (char == '8'):
- cameraUp()
- elif (char == '2'):
- cameraDown()
- elif (char == 'j'):
- catch()
- elif (char == 'l'):
- releaseCatched()
- elif (char == 'i'):
- grippersUp()
- elif (char == 'k'):
- grippersDown()
- elif (char == 'e'):
- toggleLight()
- while True:
- time.sleep(0.01)
- c = _GetchUnix()()
- if (c == 'q'):
- break
- handle(c)
- {code}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement