-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive.py
executable file
·67 lines (47 loc) · 1.15 KB
/
drive.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
#!/usr/bin/python
import pygame
import math
import sys
import gzip
import pickle
from pyfann import libfann
from serial_car import SerialCar
from vision import Vision
def handle_events():
global window, pause
for event in pygame.event.get():
if event.type == pygame.QUIT:
robot.set_speed(0, 0)
sys.exit(0)
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
robot.set_speed(0, 0)
sys.exit(0)
elif event.type == pygame.JOYBUTTONUP:
if event.button == 8:
robot.set_speed(0, 0)
sys.exit(0)
elif event.button == 1:
pause = not pause
pygame.init()
pause = False
vision = Vision("Driver")
joystick = pygame.joystick.Joystick(0)
joystick.init()
robot = SerialCar('/dev/ttyUSB0', 38400)
out_values = [0, 0]
nn = libfann.neural_net()
nn.create_from_file(sys.argv[1])
try:
while True:
handle_events()
in_values = vision.frame()
out_values = nn.run(in_values)
if pause:
robot.set_speed(0, 0)
else:
robot.set_speed(out_values[0], out_values[1])
print "Speed: {} forward, {} turn".format(out_values[0], out_values[1])
except KeyboardInterrupt:
print "Terminated..."
robot.set_speed(0, 0)