-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuggyClient.py
121 lines (116 loc) · 3.55 KB
/
buggyClient.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
# Kitronik Line following buggy
# Motor control
# Motor1 Motor2 function
# P8 P12 P0 P16
# 0 0 0 0 Coast
# 1 0 1 0 Forward
# 0 1 0 1 Reverse
# 1 1 1 1 Brake
from microbit import *
import radio
radio.on()
started = False
def drive(onTime, offTime):
global started
stop(0)
while True:
incoming = radio.receive()
if incoming is not None:
# display.show(incoming)
uart.write('Received: "' + incoming + '"\n')
words = incoming.split(' ')
if len(words) >= 2 and words[0] == 'buggy':
if words[1] == 'start':
started = True
elif words[1] == 'stop':
started = False
stop(0)
elif len(words) == 4 and words[1] == 'direction' and started:
x = words[2]
y = words[3]
# uart.write('x=' + x + ' y=' + y + '\n')
xf = float(x)
yf = float(y)
if yf < 1:
if xf < 1:
leftforward(int(abs(yf-2)*onTime), offTime)
elif xf > 3:
rightforward(int(abs(yf-2)*onTime), offTime)
else:
forward(int(abs(yf-2)*onTime), offTime)
elif yf > 3:
if xf < 1:
leftbackward(int(abs(yf-2)*onTime), offTime)
elif xf > 3:
rightbackward(int(abs(yf-2)*onTime), offTime)
else:
backward(int(abs(yf-2)*onTime), offTime)
else:
stop(onTime)
################################
def forward(onTime, offTime):
display.show(Image.ARROW_N)
pin8.write_digital(0)
pin12.write_digital(1)
pin0.write_digital(0)
pin16.write_digital(1)
sleep(onTime)
coast(offTime)
################################
def backward(onTime, offTime):
display.show(Image.ARROW_S)
pin8.write_digital(1)
pin12.write_digital(0)
pin0.write_digital(1)
pin16.write_digital(0)
sleep(onTime)
coast(offTime)
################################
def leftforward(onTime, offTime):
display.show(Image.ARROW_NW)
pin8.write_digital(0)
pin12.write_digital(1)
pin0.write_digital(0)
pin16.write_digital(0)
sleep(onTime)
coast(offTime)
################################
def rightforward(onTime, offTime):
display.show(Image.ARROW_NE)
pin8.write_digital(0)
pin12.write_digital(0)
pin0.write_digital(0)
pin16.write_digital(1)
sleep(onTime)
coast(offTime)
################################
def leftbackward(onTime, offTime):
display.show(Image.ARROW_SW)
pin8.write_digital(1)
pin12.write_digital(0)
pin0.write_digital(0)
pin16.write_digital(0)
sleep(onTime)
coast(offTime)
################################
def rightbackward(onTime, offTime):
display.show(Image.ARROW_SE)
pin8.write_digital(0)
pin12.write_digital(0)
pin0.write_digital(1)
pin16.write_digital(0)
sleep(onTime)
coast(offTime)
################################
def coast(offTime):
pin8.write_digital(0)
pin12.write_digital(0)
pin0.write_digital(0)
pin16.write_digital(0)
sleep(offTime)
################################
def stop(offTime):
display.clear()
coast(offTime)
################################
drive(100, 5)