Permalink
Browse files

created a cfg module to hold configuration data and segregate it into…

… namespaces.
  • Loading branch information...
1 parent 9f6b7d5 commit 885981d8c703504ff1a376438c181d4279d00305 @pkropf committed Feb 5, 2011
Showing with 174 additions and 116 deletions.
  1. +72 −0 cfg.py
  2. +17 −14 commands.py
  3. +50 −26 exuro.cfg
  4. +26 −50 eye.py
  5. +3 −14 move.py
  6. +6 −12 track.py
View
@@ -0,0 +1,72 @@
+# Copyright (c) 2011 Peter Kropf. All rights reserved.
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+
+
+import ConfigParser
+
+
+config = ConfigParser.RawConfigParser()
+config.read('exuro.cfg')
+
+class servo(object):
+ pause = config.getfloat('servo', 'pause')
+
+
+class arduino(object):
+ reset_time = config.getfloat('arduino', 'reset_time')
+ baud = config.getint('arduino', 'baud')
+ port = config.get('arduino', 'port')
+
+
+class general(object):
+ debug = config.getboolean('general', 'debug')
+
+
+class eye(object):
+
+ class left(object):
+ hpin = config.getint('left eye', 'horizontal')
+ vpin = config.getint('left eye', 'vertical')
+ hmin = config.getint('left eye', 'hmin')
+ hmax = config.getint('left eye', 'hmax')
+ vmin = config.getint('left eye', 'vmin')
+ vmax = config.getint('left eye', 'vmax')
+
+ offset = config.getfloat('left eye', 'offset')
+ height = config.getfloat('left eye', 'height')
+
+ class right(object):
+ hpin = config.getint('right eye', 'horizontal')
+ vpin = config.getint('right eye', 'vertical')
+ hmin = config.getint('right eye', 'hmin')
+ hmax = config.getint('right eye', 'hmax')
+ vmin = config.getint('right eye', 'vmin')
+ vmax = config.getint('right eye', 'vmax')
+
+ offset = config.getfloat('right eye', 'offset')
+ height = config.getfloat('right eye', 'height')
+
+
+class kinect(object):
+ height = config.getfloat('kinect', 'height')
+ x = config.getint('kinect', 'x')
+ y = config.getint('kinect', 'y')
+ hfield = config.getint('kinect', 'hfield')
+ vfield = config.getint('kinect', 'vfield')
View
@@ -33,13 +33,15 @@
import serial
import time
-import ConfigParser
+import cfg
-config = ConfigParser.RawConfigParser()
-config.read('exuro.cfg')
-_pause = config.getfloat('servo', 'pause')
-_reset_time = config.getfloat('arduino', 'reset_time')
+class cmd(object):
+ blink = 0 # blink an led
+ servo = 1 # move a servo
+ register_switch = 2 # register a switch on a pin
+ read_switch = 3 # read the status of a switch, open / closed
+ pin = 4 # set a pin to the specified voltage
class Command(object):
@@ -55,19 +57,20 @@ def __init__(self, command, pin, port):
def reset_serial(self):
- self.serial = serial.Serial(self.port, 9600, timeout=1)
+ self.serial = serial.Serial(self.port, cfg.arduino.baud, timeout=1)
+ # todo: last_reset should be a singeton across the same self.port
self.last_reset = time.time()
#print self.serial, self.last_reset
def send(self, parm):
if not self.check(parm):
- raise ValueError, self.check_msg
+ raise ValueError, (parm, self.check_msg)
try:
duration = time.time() - self.last_reset # it takes around 2 seconds for the arduino be become ready after
- if duration < _reset_time: # serial communications have been established. so make sure it's
- time.sleep(_reset_time - duration) # been at least 2 seconds before the last reset.
+ if duration < cfg.arduino.reset_time: # serial communications have been established. so make sure it's
+ time.sleep(cfg.arduino.reset_time - duration)# been at least 2 seconds before the last reset.
self.serial.write(chr(255)) # header, start of a new command set
self.serial.write(chr(self.command)) # with our command code
@@ -76,7 +79,7 @@ def send(self, parm):
self.last_message = time.time()
except serial.serialutil.SerialException:
- time.sleep(_reset_time)
+ time.sleep(cfg.arduino.reset_time)
self.reset_serial()
def check(self, parm):
@@ -86,13 +89,13 @@ def check(self, parm):
class Blink(Command):
def __init__(self, pin, port):
- super(Blink, self).__init__(0, pin, port)
+ super(Blink, self).__init__(cmd.blink, pin, port)
class Servo(Command):
- def __init__(self, pin, min, max, port, pause = _pause):
- super(Servo, self).__init__(1, pin, port)
+ def __init__(self, pin, min, max, port, pause = cfg.servo.pause):
+ super(Servo, self).__init__(cmd.servo, pin, port)
self.min = min
self.max = max
self.check_msg = 'Servo angle must be an integer between %d and %d.' % (self.min, self.max)
@@ -115,4 +118,4 @@ def check(self, parm):
class Pin(Command):
def __init__(self, pin, port):
- super(Pin, self).__init__(4, pin, port)
+ super(Pin, self).__init__(cmd.pin, pin, port)
View
@@ -6,9 +6,13 @@
# reset_time - how long in seconds to wait for the arduino to reset
# when there's a communications failure.
#
+# baud - communication speed used when talking with the arduino
+#
port = /dev/ttyACM0
reset_time = 2.0
+baud = 9600
+
[kinect]
#
@@ -30,51 +34,71 @@ hfield = 57
vfield = 43
-[eye]
+[left eye]
#
-# left_horizontal - the arduino port used to control the left eye
+# horizontal - the arduino port used to control the left eye
# horizontal servo.
#
-# left_vertical - the arduino port used to control the left eye
+# vertical - the arduino port used to control the left eye
# vertical servo.
#
-# right_horizontal - the arduino port used to control the right eye
+# offset - the distance in meters from the center axis to the center
+# of the eye.
+#
+# height - the distance in meters from the center of the eye to the
+# floor.
+#
+# hmin - minimum allowed value for the left eye horizontal movement
+#
+# hmax - maximum allowed value for the left eye horizontal movement
+#
+# vmin - minimum allowed value for the left eye vertical movement
+#
+# vmax - maximum allowed value for the left eye vertical movement
+#
+
+horizontal = 10
+vertical = 9
+hmin = 0
+hmax = 140
+vmin = 0
+vmax = 140
+offset = -0.10636
+height = 1.753
+
+
+[right eye]
+#
+# horizontal - the arduino port used to control the right eye
# horizontal servo.
#
-# right_vertical - the arduino port used to control the right eye
-# vertical servo.
+# vertical - the arduino port used to control the right eye vertical
+# servo.
#
# offset - the distance in meters from the center axis to the center
# of either eye.
#
# height - the distance in meters from the center of an eye to the
# floor.
#
-# left_hmin - minimum allowed value for the left eye horizontal
-# movement
+# hmin - minimum allowed value for the left eye horizontal movement
#
-# left_hmax - maximum allowed value for the left eye horizontal
-# movement
+# hmax - maximum allowed value for the left eye horizontal movement
#
-# left_vmin - minimum allowed value for the left eye vertical movement
+# vmin - minimum allowed value for the left eye vertical movement
#
-# left_vmax - maximum allowed value for the left eye vertical movement
+# vmax - maximum allowed value for the left eye vertical movement
#
-left_horizontal = 10
-left_vertical = 9
-left_hmin = 0
-left_hmax = 140
-left_vmin = 0
-left_vmax = 140
-right_horizontal = 11
-right_vertical = 12
-right_hmin = 40
-right_hmax = 170
-right_vmin = 20
-right_vmax = 160
-offset = 0.10636
-height = 1.753
+horizontal = 11
+vertical = 12
+hmin = 40
+hmax = 170
+vmin = 20
+vmax = 160
+offset = 0.10636
+height = 1.753
+
[servo]
#
View
@@ -22,45 +22,17 @@
from commands import Servo
+import cfg
from time import sleep
-import ConfigParser
import math
-config = ConfigParser.RawConfigParser()
-config.read('exuro.cfg')
-
-_debug = config.getboolean('general', 'debug')
-_port = config.get('arduino', 'port')
-
-_lhpin = config.getint('eye', 'left_horizontal')
-_lvpin = config.getint('eye', 'left_vertical')
-_lhmin = config.getint('eye', 'left_hmin')
-_lhmax = config.getint('eye', 'left_hmax')
-_lvmin = config.getint('eye', 'left_vmin')
-_lvmax = config.getint('eye', 'left_vmax')
-_rhpin = config.getint('eye', 'right_horizontal')
-_rvpin = config.getint('eye', 'right_vertical')
-_rhmin = config.getint('eye', 'right_hmin')
-_rhmax = config.getint('eye', 'right_hmax')
-_rvmin = config.getint('eye', 'right_vmin')
-_rvmax = config.getint('eye', 'right_vmax')
-
-_offset = config.getfloat('eye', 'offset')
-_height = config.getfloat('eye', 'height')
-
-_kinect_x = float(config.getint('kinect', 'x'))
-_kinect_y = float(config.getint('kinect', 'y'))
-_kinect_hfield = config.getint('kinect', 'hfield')
-_kinect_vfield = config.getint('kinect', 'vfield')
-
-
class Eye(object):
def __init__(self, name,
horizontal_pin, vertical_pin,
hmin, hmax, vmin, vmax,
port, offset,
- height=_height):
+ height):
self.name = name
self.hpin = horizontal_pin
self.vpin = vertical_pin
@@ -75,7 +47,7 @@ def __init__(self, name,
self.height = height
self.move(90, 90)
- if _debug:
+ if cfg.general.debug:
print self.name, self.hpin, self.vpin, self.port, self.offset
print self.hservo
print self.vservo
@@ -86,7 +58,7 @@ def str(self):
def move(self, horizontal, vertical):
- if _debug:
+ if cfg.general.debug:
print 'move', self.name, 'to', horizontal, vertical
self.hservo.send(horizontal)
self.vservo.send(vertical)
@@ -96,46 +68,50 @@ def focus(self, distance, point):
"""distance in meters.
point is x, y tuple for location in grid space.
- angle of focus for x axis is x % of _kinect_x % of _kinect_hfield
- angle of focus for y axis is y % of _kinect_y % of _kinect_vfield
+ angle of focus for x axis is x % of cfg.kinect.x % of cfg.kinect.hfield
+ angle of focus for y axis is y % of cfg.kinect.y % of cfg.kinect.vfield
"""
- x = point[0] / _kinect_x * _kinect_hfield
- y = point[1] / _kinect_y * _kinect_vfield
+ x = point[0] / cfg.kinect.x * cfg.kinect.hfield
+ y = point[1] / cfg.kinect.y * cfg.kinect.vfield
print distance, point, x, y
self.move(x, y)
Left = Eye('left eye',
- _lhpin, _lvpin,
- _lhmin, _lhmax, _lvmin, _lvmax,
- _port, -_offset)
+ cfg.eye.left.hpin, cfg.eye.left.vpin,
+ cfg.eye.left.hmin, cfg.eye.left.hmax,
+ cfg.eye.left.vmin, cfg.eye.left.vmax,
+ cfg.arduino.port,
+ cfg.eye.left.offset, cfg.eye.left.height)
+
Right = Eye('right eye',
- _rhpin, _rvpin,
- _rhmin, _rhmax, _rvmin, _rvmax,
- _port, _offset)
+ cfg.eye.right.hpin, cfg.eye.right.vpin,
+ cfg.eye.right.hmin, cfg.eye.right.hmax,
+ cfg.eye.right.vmin, cfg.eye.right.vmax,
+ cfg.arduino.port,
+ cfg.eye.left.offset, cfg.eye.right.height)
def random_eyes():
from random import Random
- lhrange = (_lhmin, _lhmax)
- lvrange = (_lvmin, _lvmax)
- rhrange = (_rhmin, _rhmax)
- rvrange = (_rvmin, _rvmax)
r = Random()
for x in range(100000):
- Left.move(r.randrange(*lhrange), r.randrange(*lvrange))
- Right.move(r.randrange(*rhrange), r.randrange(*rvrange))
+ Left.move(r.randrange(cfg.eye.left.hmin, cfg.eye.left.hmax),
+ r.randrange(cfg.eye.left.vmin, cfg.eye.left.vmax))
+
+ Right.move(r.randrange(cfg.eye.right.hmin, cfg.eye.right.hmax),
+ r.randrange(cfg.eye.right.vmin, cfg.eye.right.vmax))
sleep(0.25)
def random_focus():
from random import Random
- hrange = (0, _kinect_x -1)
- vrange = (0, _kinect_y - 1)
+ hrange = (0, cfg.kinect.x - 1)
+ vrange = (0, cfg.kinect.y - 1)
drange = (0, 3000)
r = Random()
Oops, something went wrong.

0 comments on commit 885981d

Please sign in to comment.