/
wheel.py
127 lines (104 loc) · 2.36 KB
/
wheel.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import RPi.GPIO as gpio
import time
import sys
import Tkinter as tk
import pygame
import sonar
pygame.init()
pygame.display.set_mode([100,100])
def init():
gpio.setmode(gpio.BOARD)
gpio.setup(7, gpio.OUT)
gpio.setup(11, gpio.OUT)
gpio.setup(13, gpio.OUT)
gpio.setup(15, gpio.OUT)
def forward(t):
gpio.output(7, False)
gpio.output(11, True)
gpio.output(13, True)
gpio.output(15, False)
time.sleep(t)
gpio.cleanup()
def reverse(t):
gpio.output(7, True)
gpio.output(11, False)
gpio.output(13, False)
gpio.output(15, True)
time.sleep(t)
gpio.cleanup()
def rightTurn(t):
gpio.output(7, False)
gpio.output(11,True)
gpio.output(13, False)
gpio.output(15, True)
time.sleep(t)
gpio.cleanup()
def leftTurn(t):
gpio.output(7,True)
gpio.output(11,False)
gpio.output(13,True)
gpio.output(15,False)
time.sleep(t)
gpio.cleanup()
def getChar():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
termios.tcsetattr(fd, termios.TCSADRAIN,old_settings)
return ch
def update():
#wtf????
distance = sonar.distance('cm')
print distance
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
pass
#init()
#forward(0.03)
keystate = pygame.key.get_pressed()
if keystate[pygame.K_w] and distance > 30:
init()
forward(0.03)
if keystate[pygame.K_s]:
init()
reverse(0.03)
if keystate[pygame.K_d]:
init()
rightTurn(0.03)
if keystate[pygame.K_a]:
init()
leftTurn(0.03)
if keystate[pygame.K_q]:
sys.exit()
#def key_input(event):
# print 'Key:', event.char
# key_press = event.char
# if key_press.lower() == 'w':
# init()
# forward(0.030)
#
# elif key_press.lower() == 's':
# init()
# reverse(0.030)
# elif key_press.lower() == 'd':
# init()
# rightTurn()
# elif key_press.lower() == 'a':
# init()
# leftTurn()
# elif key_press.lower() == 'q':
# gpio.cleanup()
# sys.exit(1)
#command = tk.Tk()
#command.bind('<KeyPress>', key_input)
#command.mainloop()
#forward()
#time.sleep(1)
#reverse()
#time.sleep(1)
#rightTurn()
#time.sleep(1)
while True:
update()