-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot.py
More file actions
303 lines (265 loc) · 8.47 KB
/
Copy pathrobot.py
File metadata and controls
303 lines (265 loc) · 8.47 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import network
import socket
from machine import Pin, PWM
from time import sleep
class Movement:
def __init__(self) -> None:
self.left_motor = PWM(Pin(2))
self.right_motor = PWM(Pin(3))
self.left_motor.freq(50)
self.left_motor.duty_u16(0)
self.right_motor.freq(50)
self.right_motor.duty_u16(0)
self.fwdValue = 8100
self.bckValue = 1650
def forwardStep(self, step):
step = int(step)
self.forward()
if step > 0:
sleep(step/10)
self.stop()
def backwardsStep(self, step):
step = int(step)
self.backwards()
if step > 0:
sleep(step/10)
self.stop()
def leftStep(self, step, pivot = True):
step = int(step)
self.left(pivot)
if step > 0:
sleep(step/10)
self.stop()
def rightStep(self, step, pivot = True):
step = int(step)
self.right(pivot)
if step > 0:
sleep(step/10)
self.stop()
def forward(self):
self.reset()
self.right_motor.duty_u16(self.fwdValue)
sleep(0.05)
self.left_motor.duty_u16(self.bckValue)
def backwards(self):
self.reset()
self.right_motor.duty_u16(self.bckValue)
sleep(0.05)
self.left_motor.duty_u16(self.fwdValue)
def left(self, pivot = True):
self.reset()
if pivot:
print("left pivot")
self.right_motor.duty_u16(self.bckValue)
else:
self.right_motor.duty_u16(0)
print("left turn")
sleep(0.05)
self.left_motor.duty_u16(self.bckValue)
def right(self, pivot = True):
self.reset()
self.right_motor.duty_u16(self.fwdValue)
sleep(0.05)
if pivot:
self.left_motor.duty_u16(self.fwdValue)
else:
self.left_motor.duty_u16(0)
def reset(self):
self.left_motor.duty_u16(0)
self.right_motor.duty_u16(0)
sleep(0.25)
def stop(self):
self.left_motor.duty_u16(0)
self.right_motor.duty_u16(0)
movement = Movement()
#region wifi
ssid = 'xxx'
password = 'xxx'
#endregion
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
html = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Directional Buttons</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
}
.button-container {
display: grid;
grid-template-columns: 100px 100px 100px;
grid-template-rows: 100px 100px;
gap: 10px;
}
button {
width: 100px;
height: 50px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Control Panel</h1>
<div class="button-container">
<button onclick="forward()">Forward</button>
<button onclick="makeRequest('left', 0, true)">Pivot Left</button>
<button onclick="makeRequest('right', 0, true)">Pivot Right</button>
<button onclick="backward()">Backward</button>
<button onclick="stop()">Stop</button>
<button onclick="makeRequest('left', 0, false)">Turn left</button>
<button onclick="makeRequest('right', 0, false)">Turn right</button>
</div>
<div class="button-container">
Step:
<button onclick="stepChange(false, 10)" style="height: 30px;width: 40px;">-10</button>
<button onclick="stepChange(false, 1)" style="height: 30px;width: 40px;">-1</button>
<span id="step"> 5 </span>
<button onclick="stepChange(true, 1)" style="height: 30px;width: 40px;">+1</button>
<button onclick="stepChange(true, 10)" style="height: 30px;width: 40px;">+10</button>
</div>
<div class="button-container">
<button onclick="makeRequest('forward', true, false)">Forward step</button>
<button onclick="makeRequest('left', true, false)">Turn Left step</button>
<button onclick="makeRequest('right', true, false)">Turn Right step</button>
<button onclick="makeRequest('backward', true, false)">Backward step</button>
<button onclick="makeRequest('stop', false, false)">Stop</button>
<button onclick="makeRequest('left', true, true)">Pivot Left step</button>
<button onclick="makeRequest('right', true, true)">Pivot Right step</button>
</div>
<script>
function makeRequest(direction, step, pivot = true) {
if (step) {
span = document.getElementById("step")
step = parseInt(span.innerHTML)
}else{
step = 0
}
var loc = window.location.href;
url = `${loc}${direction}?`
if (step) {
url = url + "step=" + step + "&"
}
url = url + "pivot=" + pivot
console.log(url)
fetch(url)
.then(response => {
if (!response.ok) {
throw new Error(`Network response was not ok: ${response.statusText}`);
}
console.log(`${direction} request sent successfully`);
})
.catch(error => console.error(`There was a problem with the fetch operation:`, error));
}
function forward() {
makeRequest('forward', false);
}
function backward() {
makeRequest('backward', false);
}
function left() {
makeRequest('left', false);
}
function right() {
makeRequest('right', false);
}
function stop() {
makeRequest('stop', false);
}
function stepChange(increase, increment) {
span = document.getElementById("step")
current = parseInt(span.innerHTML)
if (increase) {
newValue = current + increment
}
else {
newValue = current - increment
}
if (newValue < 1) {
newValue = 1
}
span.innerHTML = newValue
}
</script>
</body>
</html>
"""
max_wait = 30
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
sleep(3)
if wlan.status() != 3:
raise RuntimeError('network connection failed')
else:
print('connected')
status = wlan.ifconfig()
print( 'ip = ' + status[0] )
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
led = Pin("LED", Pin.OUT)
led.on()
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(1)
print('listening on', addr)
# Listen for connections
while True:
try:
cl, addr = s.accept()
print('client connected from', addr)
request = cl.recv(1024)
stepRequest = 0
request = str(request)
url = request.split(" ")[1]
print(url)
params = url.split("?")
pivot = True
if len(params) > 1:
params = params[1]
params = params.split('&')
variables = {}
for param in params:
val = param.split('=')
variables[val[0]] = val[1]
print(variables)
if 'step' in variables:
stepRequest = variables['step']
if 'pivot' in variables:
pivot = variables['pivot']
if pivot == 'false':
pivot = False
print(stepRequest)
if 'forward' in request:
movement.forwardStep(stepRequest)
if 'backward' in request:
movement.backwardsStep(stepRequest)
if 'left' in request:
movement.leftStep(stepRequest, pivot)
if 'right' in request:
movement.rightStep(stepRequest, pivot)
if 'stop' in request:
movement.stop()
if url == "/":
response = html
else:
response = "OK"
cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
cl.send(response)
cl.close()
except OSError as e:
cl.close()
print('connection closed')