-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
83 lines (75 loc) · 2.58 KB
/
server.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
#!/usr/bin/env python3
import ev3dev.ev3 as ev3
import ev3dev.fonts as fonts
import time
import socket
s = socket.socket()
host = socket.gethostname()
print('EV3 IP address:', socket.gethostbyname(host))
port = 1234
s.bind((host, port))
s.listen(5)
claw = ev3.MediumMotor('outA')
claw.run_timed(time_sp=2000, speed_sp=-500)
claw_open = True
left_motor = ev3.LargeMotor('outB')
right_motor = ev3.LargeMotor('outC')
camera = ev3.MediumMotor('outD')
lmax = left_motor.max_speed
rmax = right_motor.max_speed
while True:
c, addr = s.accept()
print('Got connection from', addr)
c.send(('trololo').encode('utf-8'))
while True:
msg = (c.recv(1024)).decode('utf-8')
for ch in msg:
if ch == '':
break
elif ch == 'm':
left_motor.run_forever(speed_sp=lmax)
right_motor.run_forever(speed_sp=rmax)
elif ch == 'n':
left_motor.run_forever(speed_sp=-lmax)
right_motor.run_forever(speed_sp=-rmax)
elif ch == 'b':
left_motor.run_forever(speed_sp=-lmax)
right_motor.run_forever(speed_sp=rmax)
elif ch == 'v':
left_motor.run_forever(speed_sp=lmax)
right_motor.run_forever(speed_sp=-rmax)
elif ch == 'g':
left_motor.stop()
right_motor.stop()
elif ch == 'l':
left_motor.run_forever(speed_sp=lmax)
elif ch == 'j':
left_motor.run_forever(speed_sp=-lmax)
elif ch == 'k':
left_motor.stop()
elif ch == 'r':
right_motor.run_forever(speed_sp=rmax)
elif ch == 'w':
right_motor.run_forever(speed_sp=-rmax)
elif ch == 'e':
right_motor.stop()
elif ch == '1':
claw.run_timed(time_sp=2000, speed_sp=-500)
claw_open = True
elif ch == '2':
claw.run_timed(time_sp=2000, speed_sp=500)
claw_open = False
elif ch == 'z':
if claw_open:
claw.run_timed(time_sp=2000, speed_sp=500)
claw_open = False
else:
claw.run_timed(time_sp=2000, speed_sp=-500)
claw_open = True
elif ch == 'c':
camera.run_timed(time_sp=100, speed_sp=200)
elif ch == 'x':
camera.run_timed(time_sp=100, speed_sp=-200)
else:
ev3.Sound.speak(msg).wait()
c.close()