changeset 14:3b9ed9cd63ef

Add simple client and server
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Thu, 07 Apr 2016 15:37:19 +0200
parents 94feb678de75
children 5e5d58fa1440
files client.py doc/scad/Makefile doc/scad/leg01.scad doc/scad/leg02.scad doc/scad/tote.png python/gait.py python/main.py python/server.py
diffstat 8 files changed, 89 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/client.py	Thu Apr 07 15:37:19 2016 +0200
@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import socket
+import sys
+
+import pygame
+
+
+pygame.init()
+pygame.display.set_mode((100, 100))
+
+sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+sock.connect((sys.argv[1], 1010))
+sock.setblocking(False)
+
+while True:
+    event = pygame.event.wait()
+    if event.type == pygame.KEYDOWN:
+        key = event.key
+        if key == pygame.K_UP:
+            sock.send("w")
+        elif key == pygame.K_DOWN:
+            sock.send("s")
+        elif key == pygame.K_LEFT:
+            sock.send("a")
+        elif key == pygame.K_RIGHT:
+            sock.send("d")
+        elif key == pygame.K_SPACE:
+            sock.send(" ")
+        elif key == pygame.K_ESCAPE:
+            break
+    elif event.type == pygame.QUIT:
+        break
+sock.close()
--- a/doc/scad/Makefile	Thu Apr 07 09:40:26 2016 +0200
+++ b/doc/scad/Makefile	Thu Apr 07 15:37:19 2016 +0200
@@ -10,7 +10,8 @@
 	body01.png
 
 SIZE=1280,960
-COLORSCHEME=BeforeDawn
+COLORSCHEME=Nature
+#BeforeDawn
 SCAD=openscad --imgsize=$(SIZE) --projection=p -m make --colorscheme=$(COLORSCHEME) --csglimit=100000
 
 all: images
--- a/doc/scad/leg01.scad	Thu Apr 07 09:40:26 2016 +0200
+++ b/doc/scad/leg01.scad	Thu Apr 07 15:37:19 2016 +0200
@@ -1,4 +1,4 @@
 use <parts.scad>
 use <tote.scad>
 
-coxa(10);
+coxa(0);
--- a/doc/scad/leg02.scad	Thu Apr 07 09:40:26 2016 +0200
+++ b/doc/scad/leg02.scad	Thu Apr 07 15:37:19 2016 +0200
@@ -1,4 +1,4 @@
 use <parts.scad>
 use <tote.scad>
 
-femur(10);
+femur(0);
Binary file doc/scad/tote.png has changed
--- a/python/gait.py	Thu Apr 07 09:40:26 2016 +0200
+++ b/python/gait.py	Thu Apr 07 15:37:19 2016 +0200
@@ -72,11 +72,12 @@
         return (2, 0, 3, 1)   # forward
 
 
-    def run(self, steps):
-        for step in range(steps):
+    def run(self):
+        while True:
             leg = self.robot.legs[self.last_leg]
             for frame in self.step(leg):
                 self.move()
                 self.robot.update()
                 delay(40) # 25 FPS
+                yield
             self.last_leg = self.step_order()[self.last_leg]
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/main.py	Thu Apr 07 15:37:19 2016 +0200
@@ -0,0 +1,9 @@
+import network
+import server
+
+wlan = network.WLAN(network.STA_IF)
+ap = network.WLAN(network.AP_IF)
+while not wlan.isconnected() and not ap.isconnected():
+    pass
+
+server.main()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/server.py	Thu Apr 07 15:37:19 2016 +0200
@@ -0,0 +1,38 @@
+import socket
+import math
+
+from robot import Robot
+from gait import Creep
+
+
+def main():
+    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(0)
+
+    robot = Robot()
+    gait = Creep(robot)
+
+    while True:
+        client, addr = sock.accept()
+        client.setblocking(False)
+        for frame in gait.run():
+            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)
+
+
+