HTML

finch.py

Nov 18th, 2016
190
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.77 KB | None | 0 0
  1. 3# Functions to control Finch robot.
  2.  
  3. # The Finch is a robot for computer science education. Its design is the result
  4. # of a four year study at Carnegie Mellon's CREATE lab.
  5.  
  6. # http://www.finchrobot.com
  7. # See included examples and documentation for how to use the API
  8.  
  9. import time
  10. import finchconnection
  11.  
  12. class Finch():
  13.  
  14.     def __init__(self):
  15.         self.connection = finchconnection.ThreadedFinchConnection()
  16.         self.connection.open()
  17.        
  18.     def led(self, *args):
  19.         """Control three LEDs (orbs).
  20.      
  21.          - hex triplet string: led('#00FF8B') or
  22.            0-255 RGB values: led(0, 255, 139)
  23.        """
  24.         if len(args) == 3:
  25.             r, g, b = [int(x) % 256 for x in args]
  26.         elif (len(args) == 1 and isinstance(args[0], str)):
  27.             color = args[0].strip()
  28.             if len(color) == 7 and color.startswith('#'):
  29.                 r = int(color[1:3], 16)
  30.                 g = int(color[3:5], 16)
  31.                 b = int(color[5:7], 16)
  32.         else:
  33.             return
  34.         self.connection.send(b'O', [r, g, b])
  35.  
  36.     def buzzer(self, duration, frequency):
  37.         """ Outputs sound. Does not wait until a note is done beeping.
  38.  
  39.        duration - duration to beep, in seconds (s).
  40.        frequency - integer frequency, in hertz (HZ).
  41.        """
  42.         millisec = int(duration * 1000)
  43.         self.connection.send(b'B',
  44.                 [(millisec & 0xff00) >> 8, millisec & 0x00ff,
  45.                  (frequency & 0xff00) >> 8, frequency & 0x00ff])
  46.  
  47.     def buzzer_with_delay(self, duration, frequency):
  48.         """ Outputs sound. Waits until a note is done beeping.
  49.  
  50.        duration - duration to beep, in seconds (s).
  51.        frequency - integer frequency, in hertz (HZ).
  52.        """
  53.         millisec = int(duration * 1000)
  54.         self.connection.send(b'B',
  55.                 [(millisec & 0xff00) >> 8, millisec & 0x00ff,
  56.                  (frequency & 0xff00) >> 8, frequency & 0x00ff])
  57.         time.sleep(duration*1.05)
  58.  
  59.     def light(self):
  60.         """ Get light sensor readings. The values ranges from 0.0 to 1.0.
  61.  
  62.            returns - a tuple(left, right) of two real values
  63.         """
  64.         self.connection.send(b'L')
  65.         data = self.connection.receive()
  66.         if data is not None:
  67.             left = data[0] / 255.0
  68.             right = data[1] / 255.0
  69.             return left, right
  70.  
  71.     def obstacle(self):
  72.         """Get obstacle sensor readings.
  73.  
  74.        returns - a tuple(left,right) of two boolean values
  75.        """
  76.         self.connection.send(b'I')
  77.         data = self.connection.receive()
  78.         if data is not None:
  79.             left = data[0] != 0
  80.             right = data[1] != 0
  81.             return left, right
  82.  
  83.     def temperature(self):
  84.         """ Returns temperature in degrees Celcius. """
  85.        
  86.         self.connection.send(b'T')
  87.         data = self.connection.receive()
  88.         if data is not None:
  89.             return (data[0] - 127) / 2.4 + 25;
  90.  
  91.     def convert_raw_accel(self, a):
  92.         """Converts the raw acceleration obtained from the hardware into G's"""
  93.        
  94.         if a > 31:
  95.             a -= 64
  96.         return a * 1.6 / 32.0
  97.  
  98.     def acceleration(self):
  99.         """ Returns the (x, y, z, tap, shake).  x, y, and z, are
  100.            the acceleration readings in units of G's, and range
  101.            from -1.5 to 1.5.
  102.            When the finch is horisontal, z is close to 1, x, y close to 0.
  103.            When the finch stands on its tail, y, z are close to 0,
  104.            x is close to -1.
  105.            When the finch is held with its left wing down, x, z are close to 0,
  106.            y is close to 1.
  107.            tap, shake are boolean values -- true if the correspondig event has
  108.            happened.
  109.        """
  110.        
  111.         self.connection.send(b'A')
  112.         data = self.connection.receive()
  113.         if data is not None:
  114.             x = self.convert_raw_accel(data[1])
  115.             y = self.convert_raw_accel(data[2])
  116.             z = self.convert_raw_accel(data[3])
  117.             tap = (data[4] & 0x20) != 0
  118.             shake = (data[4] & 0x80) != 0
  119.             return (x, y, z, tap, shake)
  120.  
  121.     def wheels(self, left, right):
  122.         """ Controls the left and right wheels.
  123.  
  124.        Values must range from -1.0 (full throttle reverse) to
  125.        1.0 (full throttle forward).
  126.        use left = right = 0.0 to stop.
  127.        """
  128.        
  129.         dir_left = int(left < 0)
  130.         dir_right = int(right < 0)
  131.         left = min(abs(int(left * 255)), 255)
  132.         right = min(abs(int(right * 255)), 255)
  133.         self.connection.send(b'M', [dir_left, left, dir_right, right])
  134.  
  135.     def halt(self):
  136.         """ Set all motors and LEDs to off. """
  137.         self.connection.send(b'X', [0])
  138.  
  139.     def close(self):
  140.         self.connection.close()
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment