-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_python2.py
110 lines (83 loc) · 2.45 KB
/
main_python2.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
'''
REMEMBER TO CHANGE THE CONSTANTS
OR WE WILL BE MAKING AN ORGANIZER
'''
DEBUG = True #set to true if debugging / developing
if DEBUG:
from threading import Thread
from lib import dummy_io as io
else:
from lib import io
import sys
from lib import background_listener as bl
from Queue import Queue
from time import sleep
from lib import event_loop
from config import *
if DEBUG:
#this allows us to input via keyboard
def input_cb():
event_loop.queue('user', input())
input_cb()
input_thread = Thread(None, input_cb)
input_thread.daemon = True
input_thread.start()
def sr_success(phrase):
commands = phrase.split(' ')
for command in commands:
event_loop.queue('user', command)
def sr_failure(e):
print e
stop_listening = bl.start_listening(sr_success, sr_failure)
def on_collide():
event_loop.queue('internal', 'collide')
io.on_input(INPUT_COLLIDE, on_collide, io.RISING)
motor_r = io.Motor(MOTORR_A, MOTORR_B, MOTORR_E)
motor_l = io.Motor(MOTORL_A, MOTORL_B, MOTORL_E)
def handle_event(type, param):
if type == 'user':
if param == 'left':
print 'Moving left...'
motor_r.forward()
motor_l.back()
sleep(TURN_TIME)
motor_r.stop()
motor_l.stop()
elif param == 'right':
print 'Moving right...'
motor_r.back()
motor_l.forward()
sleep(TURN_TIME)
motor_r.stop()
motor_l.stop()
elif param == 'forward' or param == 'go':
print 'Moving forward...'
motor_r.forward()
motor_l.forward()
elif param == 'back' or param == 'reverse':
print 'Moving back...'
motor_r.back()
motor_l.back()
elif param == 'stop':
print 'Stopping motors..'
#turns motors off
motor_r.stop()
motor_l.stop()
print 'Motors stopped'
elif type == 'internal':
if param == 'collide':
print 'Collided!'
motor_r.back()
motor_l.back()
sleep(COLL_REVERSE_TIME)
motor_r.stop()
motor_l.stop()
elif param == 'exit':
print 'Cleaning up...'
motor_r.stop()
motot_l.stop()
io.cleanup()
stop_listening()
print 'Exiting'
sys.exit(0)
event_loop.start(handle_event)