-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontroller.py
285 lines (244 loc) · 7.7 KB
/
controller.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import time
import create
import urllib2
from urllib2 import HTTPError
from urllib2 import URLError
import subprocess
import json
from messages import Messages
from collections import deque
import datetime
#Constants
SERVER = "http://dpdbot.deploydapp.com"
# SERVER = "http://localhost/~jcross"
global bot
global current_program
# subprocess.call(['say', Messages.failure()])
def say(something):
subprocess.call(['say', something])
def robot_data(data):
print 'The robot is telling me something', data
def start_game ():
print 'Let the games begin.'
global current_program
current_program = load_program()
if current_program is not False:
start_program()
else:
time.sleep(10)
start_game()
def stop_bot ():
bot.command('stop');
def start_program ():
#initialize the driver
POLLING_MILLISECONDS = 50.0
POLLING_SECONDS = POLLING_MILLISECONDS / 1000.0
global bot
global current_program
current_program['time'] = 0
current_program['distance'] = 0
bot = create.Create('/dev/tty.usbserial-ftDII8Z7')
bot.toFullMode()
print "current_program"
print current_program
did_finish = False
did_hit_wall = False
first_time = True
steps = current_program['stepsJson']
steps = json.loads(steps)
steps = deque(steps)
print 'steps deque'
print steps
bot.Go(-10)
time.sleep(1)
bot.Go(0, 90)
time.sleep(2)
bot.stop()
time.sleep(0.5)
def drive_forward (step):
print "drive_forward"
bot.Go(500)
return step['seconds']
def drive_backward (step):
print "drive_backward"
bot.Go(-500)
return step['seconds']
def turn_right (step):
print "turn_right"
bot.Go(0, 90)
# return time to sleep
return step['degrees'] / 90
def turn_left (step):
print "turn_left"
bot.Go(0, -90)
#return time to sleep
return step['degrees'] / 90
commands = {
'driveForward' : drive_forward,
'driveBackward': drive_backward,
'turnRight' : turn_right,
'turnLeft' : turn_left
}
while 1:
try:
step = steps.popleft()
print step
except (Exception):
print "No more steps"
break
if did_hit_wall:
"Hit a wall"
break
seconds = commands.get(step['type'])(step)
start_time = datetime.datetime.now()
print "start time"
print start_time
current_time = datetime.datetime.now()
print "current time"
print current_time
delta = current_time - start_time
print "delta"
print delta
while seconds > delta.seconds:
#Doesn't care what's running right now.
current_program['time'] += delta.microseconds / 10000000.0
if step['type'] == 'driveForward' or step['type'] == 'driveBackward':
current_program['distance'] += ((delta.microseconds / 10000000.0) * 500.0)
else:
print "step type was not driving"
print step['type']
sensors = bot.sensors()
print sensors[7]
print sensors[13]
#Check sensors for bumps or finish
if not first_time and 1 in sensors[7]:
did_hit_wall = True
break
if not first_time and sensors[13] == 1:
did_finish = True
break
first_time = False
#Sleep then loop
time.sleep(0.015)
current_time = datetime.datetime.now()
delta = current_time - start_time
print "stopping bot"
bot.stop()
print "seeking doc"
bot.seekDock()
if did_finish == True:
mission_success()
elif did_hit_wall:
mission_crashed()
else:
mission_failed()
def get_twitter_bio (sn):
try:
response = urllib2.urlopen("http://api.twitter.com/1/users/show.json?screen_name="+sn)
data = response.read()
user = json.loads(data)
print user['description']
return user['description']
except (Exception):
return False
def load_program ():
#Load the top program from the server.
#TODO: Check again later if no program is loaded
try:
reqUrl = SERVER+'/programs?q='+urllib2.quote('{"executed":false}')
print reqUrl
program = urllib2.urlopen(reqUrl)
program = program.read()
print 'program retrieved'
print program
program = json.loads(program)
print 'json parsed'
print program
program = program[0]
print 'program from index'
print program
say('Program loaded')
#Load the user's Twitter bio if possible.
if program.has_key('twitterHandle'):
handle = program['twitterHandle'].replace('@', '')
bio = get_twitter_bio(program['twitterHandle'])
if bio:
say(Messages.greeting(program['twitterHandle'], bio))
else:
say(Messages.greeting(program['twitterHandle']))
else:
say('say', 'Oops, could not find twitter handle.')
return program
except Exception as e:
print "No program to load"
print e
return False
def start_after_dock():
global bot
global current_program
print "distance before calculating"
print current_program['distance']
current_program['distance'] = (current_program['distance'] * 0.0393700787) / 12
try:
print "About to post score"
#TODO: add distance and time
valueObject = json.dumps({'twitterHandle': current_program['twitterHandle'], 'robotSecretKey': 'DPDSECRET123', 'completed':current_program['completed'], 'time': current_program['time'], 'distance': current_program['distance']})
print valueObject
route = SERVER+'/scores'
score = urllib2.urlopen(route, valueObject)
score = score.read()
print score
scoreId = json.loads(score)['_id']
opener = urllib2.build_opener(urllib2.HTTPHandler)
programs_route = SERVER+'/programs/' + current_program['_id']
current_program['executed'] = True
current_program['scoreId'] = scoreId
print programs_route
program_json = json.dumps(current_program)
print "program_json"
print program_json
request = urllib2.Request(programs_route, data=program_json)
request.add_header('Content-Type', 'application/json')
request.get_method = lambda: 'PUT'
print "executing programs request"
program = opener.open(request)
except HTTPError as e:
print "HTTPError"
print e
print e.code
except URLError as e:
print "HttpError"
print e.reason
countdown = 2
while countdown > 0:
countdown -= 0.25
#TODO: Set timeout and warn user to put back on dock
print bot.sensors()[21]
if bot.sensors()[21] == 2:
break
time.sleep(0.25)
say("You have five seconds to put me back on the dock.")
time.sleep(5)
bot.close()
start_game()
def mission_success ():
print "mission_success"
say(Messages.success())
global current_program
current_program['completed'] = True
start_after_dock()
def mission_crashed():
print "mission_crashed"
global current_program
say(Messages.failure())
current_program['completed'] = False
start_after_dock()
def mission_failed ():
print "mission_failed"
say("You disappoint me")
global current_program
current_program['completed'] = False
start_after_dock()
say("Let's Play")
start_game()
# say(Messages.greeting('voodootikigod', get_twitter_bio('voodootikigod')))