forked from hamuchiwa/AutoRCCar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrc_control_test.py
69 lines (52 loc) · 2.18 KB
/
rc_control_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
__author__ = 'zhengwang'
import serial
import pygame
from pygame.locals import *
class RCTest(object):
def __init__(self):
pygame.init()
self.ser = serial.Serial('/dev/tty.usbmodem1421', 115200, timeout=1)
self.send_inst = True
self.steer()
def steer(self):
while self.send_inst:
for event in pygame.event.get():
if event.type == KEYDOWN:
key_input = pygame.key.get_pressed()
# complex orders
if key_input[pygame.K_UP] and key_input[pygame.K_RIGHT]:
print("Forward Right")
self.ser.write(chr(6))
elif key_input[pygame.K_UP] and key_input[pygame.K_LEFT]:
print("Forward Left")
self.ser.write(chr(7))
elif key_input[pygame.K_DOWN] and key_input[pygame.K_RIGHT]:
print("Reverse Right")
self.ser.write(chr(8))
elif key_input[pygame.K_DOWN] and key_input[pygame.K_LEFT]:
print("Reverse Left")
self.ser.write(chr(9))
# simple orders
elif key_input[pygame.K_UP]:
print("Forward")
self.ser.write(chr(1))
elif key_input[pygame.K_DOWN]:
print("Reverse")
self.ser.write(chr(2))
elif key_input[pygame.K_RIGHT]:
print("Right")
self.ser.write(chr(3))
elif key_input[pygame.K_LEFT]:
print("Left")
self.ser.write(chr(4))
# exit
elif key_input[pygame.K_x] or key_input[pygame.K_q]:
print 'Exit'
self.send_inst = False
self.ser.write(chr(0))
self.ser.close()
break
elif event.type == pygame.KEYUP:
self.ser.write(chr(0))
if __name__ == '__main__':
RCTest()