-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
251 lines (223 loc) · 7.8 KB
/
main.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
import _thread
import time
from machine import Pin
from dfled import LED
# ------------------------------------------------------------------------------------ #
from dfnetwork import NET
wifi = NET('<redacted>','<redacted>')
wifi.connect()
# ------------------------------------------------------------------------------------ #
# Global Vars
racenum = 0
race_length = 32
run_state = 1
start_time = 0
switch_delay = 3000
sb_triggered = False
sb_triggered_at = 0
sb_sensor = Pin(23, Pin.IN, Pin.PULL_DOWN)
lane1_led = LED(16)
lane1_sensor = Pin(21, Pin.IN, Pin.PULL_UP)
lane1_triggered = False
lane1_triggered_at = 0
lane1_final_time = 0
lane1_time_sec = 0
lane1_time_disp = 0
lane1_fps = 0
lane1_mph = 0
lane1_smph = 0
lane2_led = LED(17)
lane2_sensor = Pin(22, Pin.IN, Pin.PULL_UP)
lane2_triggered = False
lane2_triggered_at = 0
lane2_final_time = 0
lane2_time_sec = 0
lane2_time_disp = 0
lane2_fps = 0
lane2_mph = 0
lane2_smph = 0
# ------------------------------------------------------------------------------------ #
def reset():
print("Resetting system...")
lane1_led.on()
lane2_led.on()
global run_state
global start_time
global sb_triggered
global lane1_triggered
global lane2_triggered
global lane1_final_time
global lane2_final_time
global lane1_time_sec
global lane2_time_sec
global lane1_time_disp
global lane2_time_disp
global lane1_fps
global lane2_fps
global lane1_mph
global lane2_mph
global lane1_smph
global lane2_smph
lane1_final_time = 0
lane2_final_time = 0
lane1_time_sec = 0
lane2_time_sec = 0
lane1_time_disp = 0
lane2_time_disp = 0
lane1_fps = 0
lane2_fps = 0
lane1_mph = 0
lane2_mph = 0
lane1_smph = 0
lane2_smph = 0
lane1_triggered = False
lane2_triggered = False
run_state = 1
start_time = 0
sb_triggered = False
time.sleep(2)
lane1_led.off()
lane2_led.off()
def results_table(lanecolor, lane, lane_time_disp, lane_fps, lane_mph, lane_smph):
str = """\
<td style='background-color: {};'>
<table width=100%>
<tr>
<td colspan=2 style='font-size: 90px; font-weight: bold;'>
Lane {}
</td>
</tr>
<tr style='font-size: 70px; font-weight: bold;'>
<td>
{}
</td>
<td>
Seconds
</td>
</tr>
<tr style='font-size: 70px; font-weight: bold;'>
<td>
{}
</td>
<td>
Feet Per Second
</td>
</tr>
<tr style='font-size: 70px; font-weight: bold;'>
<td>
{}
</td>
<td>
Miles Per Hour
</td>
</tr>
<tr style='font-size: 70px; font-weight: bold;'>
<td>
{}
</td>
<td>
MPH (Scaled)
</td>
</tr>
</table>
</td>
""".format(lanecolor, lane, lane_time_disp, lane_fps, lane_mph, lane_smph)
return str
def Webserv():
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(0)
lane1color="white"
lane2color="white"
while True:
conn, addr = s.accept()
# print("Got a connection from %s" % str(addr))
request = conn.recv(1024)
header = 'HTTP/1.1 200 OK\nConnection: close\nServer: FBMiniServ\nContent-Type: text/html\n\n'
content = header
with open('header.html', 'r') as html:
content = content + html.read()
if (lane1_final_time == (lane2_final_time-1) or
lane1_final_time == (lane2_final_time+1)):
lane1color="yellow"
lane2color="yellow"
elif (lane2_final_time == (lane1_final_time-1) or
lane2_final_time == (lane1_final_time+1)):
lane1color="yellow"
lane2color="yellow"
elif lane1_final_time == lane2_final_time:
lane1color="yellow"
lane2color="yellow"
elif lane1_final_time < lane2_final_time:
lane1color="green"
lane2color="red"
elif lane1_final_time > lane2_final_time:
lane1color="red"
lane2color="green"
elif lane1_final_time == 0 and lane2_final_time ==0:
lane1color="white"
lane2color="white"
lane1 = results_table(lane1color, '1', lane1_time_disp, lane1_fps, lane1_mph, lane1_smph)
lane2 = results_table(lane2color, '2', lane2_time_disp, lane2_fps, lane2_mph, lane2_smph)
content = content + lane1 + lane2
with open('footer.html', 'r') as html:
content = content + html.read()
content = content + '\n'
conn.sendall(content)
conn.close()
# print("Connection wth %s closed" % str(addr))
_thread.start_new_thread(Webserv, ())
print('Web server running')
while True:
if run_state == 1:
if sb_sensor.value() == 0 and sb_triggered == False and time.ticks_diff(time.ticks_ms(), sb_triggered_at) > switch_delay:
sb_triggered = True
sb_triggered_at = time.ticks_ms()
start_time = time.ticks_ms()
print('Start Time: {} - Going to run state 2'.format(start_time))
run_state = 2
elif run_state == 2:
if sb_sensor.value() == 0 and time.ticks_diff(time.ticks_ms(), sb_triggered_at) > switch_delay:
print('Resetting; incomplete race')
sb_triggered_at = time.ticks_ms()
sb_triggered = False
reset()
if lane1_sensor.value() == 0 and lane1_triggered == False:
lane1_triggered = True
lane1_triggered_at = time.ticks_ms()
print('Lane 1 Triggered')
if lane2_sensor.value() == 0 and lane2_triggered == False:
lane2_triggered = True
lane2_triggered_at = time.ticks_ms()
print('Lane 2 Triggered')
if lane1_triggered and lane2_triggered:
print('Race finished')
lane1_final_time = time.ticks_diff(lane1_triggered_at, start_time)
lane2_final_time = time.ticks_diff(lane2_triggered_at, start_time)
lane1_time_sec = lane1_final_time/1000
lane2_time_sec = lane2_final_time/1000
lane1_time_disp = '{:06.3f}'.format(lane1_time_sec)
lane2_time_disp = '{:06.3f}'.format(lane2_time_sec)
lane1_fps = '{:4.2f}'.format(race_length/lane1_time_sec)
lane2_fps = '{:4.2f}'.format(race_length/lane2_time_sec)
lane1_mph = '{:4.2f}'.format((race_length/lane1_time_sec) * 0.6818)
lane2_mph = '{:4.2f}'.format((race_length/lane2_time_sec) * 0.6818)
lane1_smph = '{:4.2f}'.format(((race_length/lane1_time_sec) * 0.6818) * 25)
lane2_smph = '{:4.2f}'.format(((race_length/lane2_time_sec) * 0.6818) * 25)
print('Lane 1: TimeMS: {}, TimeS: {}, FPS: {}, MPH: {}, SMPH: {}'.format(lane1_final_time, lane1_time_disp, lane1_fps, lane1_mph, lane1_smph))
print('Lane 2: TimeMS: {}, TimeS: {}, FPS: {}, MPH: {}, SMPH: {}'.format(lane2_final_time, lane2_time_disp, lane2_fps, lane2_mph, lane2_smph))
run_state = 3
# Light up winning lane LED
if lane1_final_time < lane2_final_time:
lane1_led.on()
if lane2_final_time < lane1_final_time:
lane2_led.on()
# Write race data to file
with open('race_stats.txt', 'a+') as file:
file.write('{},{},{}\n'.format(racenum, lane1_final_time, lane2_final_time))
# Increment race number
racenum = racenum + 1
elif run_state == 3:
if sb_sensor.value() == 0:
reset()