changeset 89:61d88bb9cb9a

Distance sensor
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Fri, 26 Dec 2014 12:17:36 +0100
parents c38532de6cbc
children da950915298d
files distance.py joy.py robot.py servo.py ukubik/distance.ino ukubik/ukubik.ino
diffstat 6 files changed, 41 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/distance.py	Fri Dec 26 12:17:36 2014 +0100
@@ -0,0 +1,15 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import serial
+import kubik
+
+
+def show_battery(robot):
+    while True:
+        print robot.get_distance()
+        yield
+
+
+robot = kubik.Robot(serial.Serial('/dev/rfcomm0', 115200))
+robot.run_animations([show_battery(robot)], 6)
--- a/joy.py	Fri Dec 26 12:17:07 2014 +0100
+++ b/joy.py	Fri Dec 26 12:17:36 2014 +0100
@@ -52,7 +52,7 @@
                         else:
                             self.gait = 'right'
                     elif event.key == pygame.K_RETURN:
-                        robot.beep(440, 100)
+                        self.robot.beep(440, 100)
                 elif event.type == pygame.KEYUP:
                     if event.key in (pygame.K_RSHIFT, pygame.K_RCTRL):
                         self.gait = ''
@@ -68,7 +68,7 @@
                         else:
                             self.gait = 'right'
                     elif event.button == 0:
-                        robot.beep(440, 100)
+                        self.robot.beep(440, 100)
                     else:
                         print "Button %d" % event.button
                 elif event.type == pygame.JOYBUTTONUP:
--- a/robot.py	Fri Dec 26 12:17:07 2014 +0100
+++ b/robot.py	Fri Dec 26 12:17:36 2014 +0100
@@ -64,6 +64,8 @@
             sensors[4:7], # gyroscope
             sensors[3],   # temperature
         ]
+    def get_distance(self):
+        return self.legs[0].hip.get_distance()
 
     def get_attitude(self):
         ((ax, ay, az), (gx, gy, gz), tmp) = self.get_accel()
--- a/servo.py	Fri Dec 26 12:17:07 2014 +0100
+++ b/servo.py	Fri Dec 26 12:17:36 2014 +0100
@@ -52,6 +52,13 @@
             pass
         return [_read_word(self.serial) for w in range(7)]
 
+    def get_distance(self):
+        command = '\xf4'
+        self.serial.write(command)
+        while self.serial.read(1) != '\xff':
+            pass
+        return ord(self.serial.read(1))
+
     def beep(self, frequency, duration):
         command = '\xf1'
         self.serial.write(command)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ukubik/distance.ino	Fri Dec 26 12:17:36 2014 +0100
@@ -0,0 +1,11 @@
+void distance_setup() {
+    pinMode(A0, INPUT);
+}
+
+void do_distance() {
+    double reading = analogRead(A0);
+    unsigned char distance = min(255, 61.573 * pow(reading * 0.00488, -1.1068));
+
+    Serial.write(0xff);
+    Serial.write(distance);
+}
--- a/ukubik/ukubik.ino	Fri Dec 26 12:17:07 2014 +0100
+++ b/ukubik/ukubik.ino	Fri Dec 26 12:17:36 2014 +0100
@@ -57,6 +57,10 @@
             /* Fold up and shut down the robot. */
             do_shutdown();
             break;
+        case 0xf4:
+            /* Read the distance sensor. */
+            do_distance();
+            break;
     }
     serial_command = 0;
 }