changeset 117:d543503fed43

Move main loop to kubik.py
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Mon, 07 Jul 2014 21:06:55 +0000
parents 84a6ec24a915
children b0bb6a86bc51
files creep.py kubik.py
diffstat 2 files changed, 43 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/creep.py	Mon Jul 07 20:17:32 2014 +0000
+++ b/creep.py	Mon Jul 07 21:06:55 2014 +0000
@@ -1,14 +1,13 @@
-import pygame
 import kubik
 
-
-STRIDE = 42
-SPEED = 1
-SWING = 18
-SWING_SPEED = 2
-SPREAD = 30
-HEIGHT = -30
-STEP = -10
+STRIDE = 45
+SPEED = 3
+SWING = 24
+SWING_SPEED = 4
+SPREAD = 35
+HEIGHT = -35
+STEP = -5
+OVERLAP = 1
 FPS = 24
 
 
@@ -48,23 +47,14 @@
 
 
 if __name__ == '__main__':
-    pygame.mixer.init()
-    clock = pygame.time.Clock()
     robot = kubik.Robot()
     delay = (2 * STRIDE / SPEED + 6) / 4
     animations = [
         swing(robot),
-        creep(robot.legs[0], 1, delay * 0 + 4, robot),
-        creep(robot.legs[1], 1, delay * 1 - 4, robot),
-        creep(robot.legs[3], -1, delay * 2 + 4, robot),
-        creep(robot.legs[2], -1, delay * 3 - 4, robot),
+        creep(robot.legs[0], 1, delay * 0 + OVERLAP, robot),
+        creep(robot.legs[1], 1, delay * 1 - OVERLAP, robot),
+        creep(robot.legs[3], -1, delay * 2 + OVERLAP, robot),
+        creep(robot.legs[2], -1, delay * 3 - OVERLAP, robot),
     ]
     robot.home()
-    try:
-        while True:
-            for animation in animations:
-                next(animation, None)
-            robot.update()
-            clock.tick(FPS)
-    finally:
-        robot.home()
+    robot.run_animations(animations, FPS)
--- a/kubik.py	Mon Jul 07 20:17:32 2014 +0000
+++ b/kubik.py	Mon Jul 07 21:06:55 2014 +0000
@@ -2,6 +2,7 @@
 
 import serial
 import maestro
+import pygame
 
 
 def _solve_triangle(a, b, c):
@@ -38,8 +39,16 @@
     """Standard servo."""
 
 
-class AnkleServo(maestro.Servo):
-    """Standard servo."""
+class AnkleServo2(maestro.Servo):
+    min_position = 864 * 4
+    max_position = 2432 * 4
+    max_degrees = 133
+
+
+class AnkleServo1(maestro.Servo):
+    min_position = 560 * 4
+    max_position = 2160 * 4
+    max_degrees = 136
 
 
 class Leg(object):
@@ -103,19 +112,19 @@
         self.legs = [
             Leg(HipServo(m, 1, trim=250),
                 KneeServo(m, 2, trim=-200),
-                AnkleServo(m, 0, trim=0),
+                AnkleServo1(m, 0, trim=0, center=1500 * 4),
                 45),
             Leg(HipServo(m, 4, trim=-100),
                 KneeServo(m, 5, reverse=True, trim=-200),
-                AnkleServo(m, 3, reverse=True, trim=-400),
+                AnkleServo2(m, 3, reverse=True, trim=-400, center=1500 * 4),
                 -45),
             Leg(HipServo(m, 7, reverse=True, trim=150),
                 KneeServo(m, 8, trim=100),
-                AnkleServo(m, 6, trim=0),
+                AnkleServo1(m, 6, trim=0, center=1500 * 4),
                 -45),
             Leg(HipServo(m, 10, reverse=True, trim=-250),
                 KneeServo(m, 11, reverse=True, trim=-100),
-                AnkleServo(m, 9, reverse=True, trim=100),
+                AnkleServo2(m, 9, reverse=True, trim=100, center=1500 * 4),
                 45),
         ]
 
@@ -125,8 +134,22 @@
         self.legs[1].move_to((30, 30, -35))
         self.legs[2].move_to((30, 30, -35))
         self.legs[3].move_to((30, -30, -35))
-        self.update()
 
     def update(self):
         for leg in self.legs:
             leg.update()
+
+    def run_animations(self, animations, fps=12):
+        clock = pygame.time.Clock()
+        try:
+            while animations:
+                for animation in list(animations):
+                    try:
+                        next(animation)
+                    except StopIteration:
+                        animations.remove(animation)
+                self.update()
+                clock.tick(fps)
+        finally:
+            self.home()
+            self.update()