changeset 12:da7ac7636281

Add tv remote control
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Sat, 13 Sep 2014 16:47:20 +0200
parents e934ad2bdc17
children 5ec3593bb005
files katka/katka.ino
diffstat 1 files changed, 52 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/katka/katka.ino	Sat Sep 13 13:39:47 2014 +0200
+++ b/katka/katka.ino	Sat Sep 13 16:47:20 2014 +0200
@@ -1,7 +1,7 @@
 /* Katka creeping around autonomously. */
 
 #include <Servo.h>
-// #include <m.h>
+#include <IRremote.h>
 
 
 // Number of servos in legs.
@@ -14,6 +14,11 @@
 
 #define NaN (0.0/0.0)
 const double _2PI = PI * 2;
+const double PI2 = PI / 2;
+
+
+// TV remote support
+IRrecv irrecv(2);
 
 const char JOINTS[MAX_SERVO][2] {
 //  reverse, trim
@@ -77,7 +82,7 @@
     // Calculate angles for knee and ankle, and put them in those variables.
     // Return true on success, and false if x and y are out of range.
     double d = _norm(x, y);
-    *knee = PI / 2 - (atan2(y, x) + _solve_triangle(FEMUR, d, TIBIA));
+    *knee = PI2 - (atan2(y, x) + _solve_triangle(FEMUR, d, TIBIA));
     if (isnan(*knee)) { return false; }
     *ankle = _solve_triangle(FEMUR, TIBIA, d) - 0.75 * PI;
     if (isnan(*ankle)) { return false; }
@@ -86,7 +91,7 @@
 
 void _move_joint(char joint, double rads) {
     // Move a joint to a position in radians between -PI and PI.
-    rads = ((rads * JOINTS[joint][0]) + PI / 2);
+    rads = ((rads * JOINTS[joint][0]) + PI2);
     while (rads > _2PI) {
         rads -= _2PI;
     }
@@ -134,9 +139,43 @@
     }
 }
 
+void ir_setup() {
+    irrecv.enableIRIn();
+}
+
+void ir() {
+    static unsigned long last = 0;
+    decode_results ir_results;
+
+    if (irrecv.decode(&ir_results)) {
+        if (ir_results.value == 0xFFFFFF) {
+            ir_results.value = last;
+        }
+        switch (ir_results.value) {
+            case 0xFF02FD:
+                creep_speed = min(1.0, creep_speed + 0.25);
+                break;
+            case 0xFF096F:
+                creep_speed = max(-1.0, creep_speed - 0.25);
+                break;
+            case 0xFFE01E:
+                creep_rotation = min(1.0, creep_rotation + 0.25);
+                break;
+            case 0xFF906E:
+                creep_rotation = max(-1.0, creep_rotation - 0.25);
+                break;
+            case 0xFFA857:
+                creep_speed = 0.0;
+                creep_rotation = 0.0;
+                break;
+        }
+        irrecv.resume();
+    }
+}
+
 void creep_setup() {
     for (char leg = 0; leg < 4; ++leg) {
-        move_leg(leg, (HOME_X - 15) * LEGS[leg][2] + 5, HOME_Y - 8);
+        move_leg(leg, (HOME_X - 15) * LEGS[leg][2] + 15, HOME_Y - 8);
         tick(82);
     }
 }
@@ -149,12 +188,18 @@
                      HOME_Y - 8 + creep_tilt * LEGS[leg][3]);
         }
     }
+    ir();
     tick(82); // 12 FPS
 }
 
 void creep() {
     char leg = CREEP_ORDER[creep_current];
 
+    if ((creep_speed > -0.01 && creep_speed < 0.01) &&
+        (creep_rotation > -0.01 && creep_rotation < 0.01)) {
+        creep_frame();
+        return;
+    }
     if (LEGS[leg][3] > 0) {
         while (creep_tilt < 4) {
             creep_tilt += 2;
@@ -171,8 +216,8 @@
     creep_frame();
     creep_frame();
     move_leg(leg,
-             (HOME_X - 10) * LEGS[leg][2] +
-             (25 * creep_speed + creep_rotation * LEGS[leg][2]),
+             (HOME_X - 10) * LEGS[leg][2] + 15 +
+             (10 * creep_speed + creep_rotation * LEGS[leg][2]),
              legs_y[leg]);
     creep_frame();
     creep_frame();
@@ -188,6 +233,7 @@
 void setup() {
     servo_setup();
     creep_setup();
+    ir_setup();
 }
 
 void loop() {