comparison python/server.py @ 0:cf2cd5a35d24 draft

Initial commit
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Mon, 15 May 2017 16:36:08 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:cf2cd5a35d24
1 import socket
2 import math
3
4 from robot import Robot
5 from gait import Creep
6
7
8 def main():
9 robot = Robot()
10 gait = Creep(robot)
11 run = gait.run()
12
13 sock = socket.socket()
14 addr = socket.getaddrinfo("0.0.0.0", 1010)[0][4]
15 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
16 sock.setblocking(False)
17 sock.bind(addr)
18 sock.listen(1)
19 try:
20 while True:
21 try:
22 client, addr = sock.accept()
23 except OSError:
24 next(run)
25 else:
26 try:
27 client.setblocking(False)
28 while True:
29 command = client.read(1)
30 if command == b' ':
31 gait.dx = 0
32 gait.dy = 0
33 gait.rotation = 0
34 elif command == b'w':
35 gait.dy = min(3, gait.dy + 0.25)
36 elif command == b's':
37 gait.dy = max(-3, gait.dy - 0.25)
38 elif command == b'a':
39 gait.rotation = min(math.pi/90,
40 gait.rotation + math.pi/360)
41 elif command == b'd':
42 gait.rotation = max(-math.pi/90,
43 gait.rotation - math.pi/360)
44 next(run)
45 finally:
46 client.close()
47 finally:
48 sock.close()