changeset 122:7261169ff322

rotate
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Tue, 08 Jul 2014 20:52:32 +0000
parents dd9e28e1a97a
children 241c09fc1364
files kubik.py rotate.py
diffstat 2 files changed, 48 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/kubik.py	Tue Jul 08 20:02:35 2014 +0000
+++ b/kubik.py	Tue Jul 08 20:52:32 2014 +0000
@@ -62,12 +62,17 @@
     tibia = 36.0  # between the ankle and the foot
     base = 45.0, 45.0  # from the center of body
 
-    def __init__(self, hip, knee, ankle, hip_offset=0):
+    def __init__(self, hip, knee, ankle, front=True):
         self.hip = hip
         self.knee = knee
         self.ankle = ankle
-        self.hip_offset = hip_offset  # angle that points outward
         self.position = 30.0, 30.0, -35.0
+        self.front = front
+        if self.front:
+            self.y_direction = 1
+        else:
+            self.y_direction = -1
+        self.hip_offset = 45 * self.y_direction
 
     def __iter__(self):
         """Iterate over all joints."""
@@ -99,12 +104,18 @@
 
     def rotate(self, degrees):
         """Rotate the leg around the center of the body horizontally."""
+        radians = math.radians(degrees)
         x, y, z = self.position
-        bx, by = self.base[0] + x, self.base[1] + y
+        print 'starting pos: %r, %r' % (x, y)
+        bx, by = self.base[0] + x, self.base[1] - y * self.y_direction
         r = _length(bx, by)
-        x = r * math.cos(math.radians(degrees)) - self.base[0]
-        y = r * math.sin(math.radians(degrees)) - self.base[1]
-        self.move_to((x, y, None))
+        a = math.atan2(bx, by)
+        print 'arc coords: %r, %r' % (math.degrees(a), r)
+        print 'move by: %r' % radians
+        x = r * math.sin(a + radians) - self.base[0]
+        y = r * math.cos(a + radians) - self.base[1]
+        print 'end pos: %r, %r' % (x, y)
+        self.move_to((x, -y * self.y_direction, None))
 
     def update(self):
         hip, knee, ankle = self._inverse_kinematics(self.position)
@@ -123,27 +134,25 @@
             Leg(HipServo(m, 1, trim=250),
                 KneeServo(m, 2, trim=-200),
                 AnkleServo1(m, 0, trim=0, center=1500 * 4),
-                45),
+                False),
             Leg(HipServo(m, 4, trim=-100),
                 KneeServo(m, 5, reverse=True, trim=-200),
                 AnkleServo2(m, 3, reverse=True, trim=-400, center=1500 * 4),
-                -45),
+                True),
             Leg(HipServo(m, 7, reverse=True, trim=150),
                 KneeServo(m, 8, trim=100),
                 AnkleServo1(m, 6, trim=0, center=1500 * 4),
-                -45),
+                True),
             Leg(HipServo(m, 10, reverse=True, trim=-250),
                 KneeServo(m, 11, reverse=True, trim=-100),
                 AnkleServo2(m, 9, reverse=True, trim=100, center=1500 * 4),
-                45),
+                False),
         ]
 
     def home(self):
         """Move all legs to their initial position."""
-        self.legs[0].move_to((30, -30, -35))
-        self.legs[1].move_to((30, 30, -35))
-        self.legs[2].move_to((30, 30, -35))
-        self.legs[3].move_to((30, -30, -35))
+        for leg in self.legs:
+            leg.move_to((30, -30 * leg.y_direction, -35))
 
     def update(self):
         for leg in self.legs:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rotate.py	Tue Jul 08 20:52:32 2014 +0000
@@ -0,0 +1,25 @@
+import kubik
+
+FPS = 12
+
+
+def rotate(leg, step):
+    for angle in range(0, 30, 1):
+        leg.rotate(-step)
+        yield
+    for angle in range(0, 60, 1):
+        leg.rotate(step)
+        yield
+
+
+
+if __name__ == '__main__':
+    robot = kubik.Robot()
+    animations = [
+        rotate(robot.legs[0], 1),
+        rotate(robot.legs[1], -1),
+        rotate(robot.legs[2], 1),
+        rotate(robot.legs[3], -1),
+    ]
+    robot.home()
+    robot.run_animations(animations, FPS)