diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/server.py	Mon May 15 16:36:08 2017 +0200
@@ -0,0 +1,48 @@
+import socket
+import math
+
+from robot import Robot
+from gait import Creep
+
+
+def main():
+    robot = Robot()
+    gait = Creep(robot)
+    run = gait.run()
+
+    sock = socket.socket()
+    addr = socket.getaddrinfo("0.0.0.0", 1010)[0][4]
+    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+    sock.setblocking(False)
+    sock.bind(addr)
+    sock.listen(1)
+    try:
+        while True:
+            try:
+                client, addr = sock.accept()
+            except OSError:
+                next(run)
+            else:
+                try:
+                    client.setblocking(False)
+                    while True:
+                        command = client.read(1)
+                        if command == b' ':
+                            gait.dx = 0
+                            gait.dy = 0
+                            gait.rotation = 0
+                        elif command == b'w':
+                            gait.dy = min(3, gait.dy + 0.25)
+                        elif command == b's':
+                            gait.dy = max(-3, gait.dy - 0.25)
+                        elif command == b'a':
+                            gait.rotation = min(math.pi/90,
+                                                gait.rotation + math.pi/360)
+                        elif command == b'd':
+                            gait.rotation = max(-math.pi/90,
+                                                gait.rotation - math.pi/360)
+                        next(run)
+                finally:
+                    client.close()
+    finally:
+        sock.close()