Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

… namespaces.
  • Loading branch information...
commit 885981d8c703504ff1a376438c181d4279d00305 1 parent 9f6b7d5
Peter Kropf authored

Showing 6 changed files with 174 additions and 116 deletions. Show diff stats Hide diff stats

  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
72 cfg.py
... ... @@ -0,0 +1,72 @@
  1 +# Copyright (c) 2011 Peter Kropf. All rights reserved.
  2 +#
  3 +# Permission is hereby granted, free of charge, to any person obtaining a copy
  4 +# of this software and associated documentation files (the "Software"), to deal
  5 +# in the Software without restriction, including without limitation the rights
  6 +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7 +# copies of the Software, and to permit persons to whom the Software is
  8 +# furnished to do so, subject to the following conditions:
  9 +#
  10 +# The above copyright notice and this permission notice shall be included in
  11 +# all copies or substantial portions of the Software.
  12 +#
  13 +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14 +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15 +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16 +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17 +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18 +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19 +# THE SOFTWARE.
  20 +
  21 +
  22 +import ConfigParser
  23 +
  24 +
  25 +config = ConfigParser.RawConfigParser()
  26 +config.read('exuro.cfg')
  27 +
  28 +class servo(object):
  29 + pause = config.getfloat('servo', 'pause')
  30 +
  31 +
  32 +class arduino(object):
  33 + reset_time = config.getfloat('arduino', 'reset_time')
  34 + baud = config.getint('arduino', 'baud')
  35 + port = config.get('arduino', 'port')
  36 +
  37 +
  38 +class general(object):
  39 + debug = config.getboolean('general', 'debug')
  40 +
  41 +
  42 +class eye(object):
  43 +
  44 + class left(object):
  45 + hpin = config.getint('left eye', 'horizontal')
  46 + vpin = config.getint('left eye', 'vertical')
  47 + hmin = config.getint('left eye', 'hmin')
  48 + hmax = config.getint('left eye', 'hmax')
  49 + vmin = config.getint('left eye', 'vmin')
  50 + vmax = config.getint('left eye', 'vmax')
  51 +
  52 + offset = config.getfloat('left eye', 'offset')
  53 + height = config.getfloat('left eye', 'height')
  54 +
  55 + class right(object):
  56 + hpin = config.getint('right eye', 'horizontal')
  57 + vpin = config.getint('right eye', 'vertical')
  58 + hmin = config.getint('right eye', 'hmin')
  59 + hmax = config.getint('right eye', 'hmax')
  60 + vmin = config.getint('right eye', 'vmin')
  61 + vmax = config.getint('right eye', 'vmax')
  62 +
  63 + offset = config.getfloat('right eye', 'offset')
  64 + height = config.getfloat('right eye', 'height')
  65 +
  66 +
  67 +class kinect(object):
  68 + height = config.getfloat('kinect', 'height')
  69 + x = config.getint('kinect', 'x')
  70 + y = config.getint('kinect', 'y')
  71 + hfield = config.getint('kinect', 'hfield')
  72 + vfield = config.getint('kinect', 'vfield')
31 commands.py
@@ -33,13 +33,15 @@
33 33
34 34 import serial
35 35 import time
36   -import ConfigParser
  36 +import cfg
37 37
38   -config = ConfigParser.RawConfigParser()
39   -config.read('exuro.cfg')
40 38
41   -_pause = config.getfloat('servo', 'pause')
42   -_reset_time = config.getfloat('arduino', 'reset_time')
  39 +class cmd(object):
  40 + blink = 0 # blink an led
  41 + servo = 1 # move a servo
  42 + register_switch = 2 # register a switch on a pin
  43 + read_switch = 3 # read the status of a switch, open / closed
  44 + pin = 4 # set a pin to the specified voltage
43 45
44 46
45 47 class Command(object):
@@ -55,19 +57,20 @@ def __init__(self, command, pin, port):
55 57
56 58
57 59 def reset_serial(self):
58   - self.serial = serial.Serial(self.port, 9600, timeout=1)
  60 + self.serial = serial.Serial(self.port, cfg.arduino.baud, timeout=1)
  61 + # todo: last_reset should be a singeton across the same self.port
59 62 self.last_reset = time.time()
60 63 #print self.serial, self.last_reset
61 64
62 65
63 66 def send(self, parm):
64 67 if not self.check(parm):
65   - raise ValueError, self.check_msg
  68 + raise ValueError, (parm, self.check_msg)
66 69
67 70 try:
68 71 duration = time.time() - self.last_reset # it takes around 2 seconds for the arduino be become ready after
69   - if duration < _reset_time: # serial communications have been established. so make sure it's
70   - time.sleep(_reset_time - duration) # been at least 2 seconds before the last reset.
  72 + if duration < cfg.arduino.reset_time: # serial communications have been established. so make sure it's
  73 + time.sleep(cfg.arduino.reset_time - duration)# been at least 2 seconds before the last reset.
71 74
72 75 self.serial.write(chr(255)) # header, start of a new command set
73 76 self.serial.write(chr(self.command)) # with our command code
@@ -76,7 +79,7 @@ def send(self, parm):
76 79 self.last_message = time.time()
77 80
78 81 except serial.serialutil.SerialException:
79   - time.sleep(_reset_time)
  82 + time.sleep(cfg.arduino.reset_time)
80 83 self.reset_serial()
81 84
82 85 def check(self, parm):
@@ -86,13 +89,13 @@ def check(self, parm):
86 89
87 90 class Blink(Command):
88 91 def __init__(self, pin, port):
89   - super(Blink, self).__init__(0, pin, port)
  92 + super(Blink, self).__init__(cmd.blink, pin, port)
90 93
91 94
92 95
93 96 class Servo(Command):
94   - def __init__(self, pin, min, max, port, pause = _pause):
95   - super(Servo, self).__init__(1, pin, port)
  97 + def __init__(self, pin, min, max, port, pause = cfg.servo.pause):
  98 + super(Servo, self).__init__(cmd.servo, pin, port)
96 99 self.min = min
97 100 self.max = max
98 101 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):
115 118
116 119 class Pin(Command):
117 120 def __init__(self, pin, port):
118   - super(Pin, self).__init__(4, pin, port)
  121 + super(Pin, self).__init__(cmd.pin, pin, port)
76 exuro.cfg
@@ -6,9 +6,13 @@
6 6 # reset_time - how long in seconds to wait for the arduino to reset
7 7 # when there's a communications failure.
8 8 #
  9 +# baud - communication speed used when talking with the arduino
  10 +#
9 11
10 12 port = /dev/ttyACM0
11 13 reset_time = 2.0
  14 +baud = 9600
  15 +
12 16
13 17 [kinect]
14 18 #
@@ -30,19 +34,46 @@ hfield = 57
30 34 vfield = 43
31 35
32 36
33   -[eye]
  37 +[left eye]
34 38 #
35   -# left_horizontal - the arduino port used to control the left eye
  39 +# horizontal - the arduino port used to control the left eye
36 40 # horizontal servo.
37 41 #
38   -# left_vertical - the arduino port used to control the left eye
  42 +# vertical - the arduino port used to control the left eye
39 43 # vertical servo.
40 44 #
41   -# right_horizontal - the arduino port used to control the right eye
  45 +# offset - the distance in meters from the center axis to the center
  46 +# of the eye.
  47 +#
  48 +# height - the distance in meters from the center of the eye to the
  49 +# floor.
  50 +#
  51 +# hmin - minimum allowed value for the left eye horizontal movement
  52 +#
  53 +# hmax - maximum allowed value for the left eye horizontal movement
  54 +#
  55 +# vmin - minimum allowed value for the left eye vertical movement
  56 +#
  57 +# vmax - maximum allowed value for the left eye vertical movement
  58 +#
  59 +
  60 +horizontal = 10
  61 +vertical = 9
  62 +hmin = 0
  63 +hmax = 140
  64 +vmin = 0
  65 +vmax = 140
  66 +offset = -0.10636
  67 +height = 1.753
  68 +
  69 +
  70 +[right eye]
  71 +#
  72 +# horizontal - the arduino port used to control the right eye
42 73 # horizontal servo.
43 74 #
44   -# right_vertical - the arduino port used to control the right eye
45   -# vertical servo.
  75 +# vertical - the arduino port used to control the right eye vertical
  76 +# servo.
46 77 #
47 78 # offset - the distance in meters from the center axis to the center
48 79 # of either eye.
@@ -50,31 +81,24 @@ vfield = 43
50 81 # height - the distance in meters from the center of an eye to the
51 82 # floor.
52 83 #
53   -# left_hmin - minimum allowed value for the left eye horizontal
54   -# movement
  84 +# hmin - minimum allowed value for the left eye horizontal movement
55 85 #
56   -# left_hmax - maximum allowed value for the left eye horizontal
57   -# movement
  86 +# hmax - maximum allowed value for the left eye horizontal movement
58 87 #
59   -# left_vmin - minimum allowed value for the left eye vertical movement
  88 +# vmin - minimum allowed value for the left eye vertical movement
60 89 #
61   -# left_vmax - maximum allowed value for the left eye vertical movement
  90 +# vmax - maximum allowed value for the left eye vertical movement
62 91 #
63 92
64   -left_horizontal = 10
65   -left_vertical = 9
66   -left_hmin = 0
67   -left_hmax = 140
68   -left_vmin = 0
69   -left_vmax = 140
70   -right_horizontal = 11
71   -right_vertical = 12
72   -right_hmin = 40
73   -right_hmax = 170
74   -right_vmin = 20
75   -right_vmax = 160
76   -offset = 0.10636
77   -height = 1.753
  93 +horizontal = 11
  94 +vertical = 12
  95 +hmin = 40
  96 +hmax = 170
  97 +vmin = 20
  98 +vmax = 160
  99 +offset = 0.10636
  100 +height = 1.753
  101 +
78 102
79 103 [servo]
80 104 #
76 eye.py
@@ -22,45 +22,17 @@
22 22
23 23
24 24 from commands import Servo
  25 +import cfg
25 26 from time import sleep
26   -import ConfigParser
27 27 import math
28 28
29 29
30   -config = ConfigParser.RawConfigParser()
31   -config.read('exuro.cfg')
32   -
33   -_debug = config.getboolean('general', 'debug')
34   -_port = config.get('arduino', 'port')
35   -
36   -_lhpin = config.getint('eye', 'left_horizontal')
37   -_lvpin = config.getint('eye', 'left_vertical')
38   -_lhmin = config.getint('eye', 'left_hmin')
39   -_lhmax = config.getint('eye', 'left_hmax')
40   -_lvmin = config.getint('eye', 'left_vmin')
41   -_lvmax = config.getint('eye', 'left_vmax')
42   -_rhpin = config.getint('eye', 'right_horizontal')
43   -_rvpin = config.getint('eye', 'right_vertical')
44   -_rhmin = config.getint('eye', 'right_hmin')
45   -_rhmax = config.getint('eye', 'right_hmax')
46   -_rvmin = config.getint('eye', 'right_vmin')
47   -_rvmax = config.getint('eye', 'right_vmax')
48   -
49   -_offset = config.getfloat('eye', 'offset')
50   -_height = config.getfloat('eye', 'height')
51   -
52   -_kinect_x = float(config.getint('kinect', 'x'))
53   -_kinect_y = float(config.getint('kinect', 'y'))
54   -_kinect_hfield = config.getint('kinect', 'hfield')
55   -_kinect_vfield = config.getint('kinect', 'vfield')
56   -
57   -
58 30 class Eye(object):
59 31 def __init__(self, name,
60 32 horizontal_pin, vertical_pin,
61 33 hmin, hmax, vmin, vmax,
62 34 port, offset,
63   - height=_height):
  35 + height):
64 36 self.name = name
65 37 self.hpin = horizontal_pin
66 38 self.vpin = vertical_pin
@@ -75,7 +47,7 @@ def __init__(self, name,
75 47 self.height = height
76 48 self.move(90, 90)
77 49
78   - if _debug:
  50 + if cfg.general.debug:
79 51 print self.name, self.hpin, self.vpin, self.port, self.offset
80 52 print self.hservo
81 53 print self.vservo
@@ -86,7 +58,7 @@ def str(self):
86 58
87 59
88 60 def move(self, horizontal, vertical):
89   - if _debug:
  61 + if cfg.general.debug:
90 62 print 'move', self.name, 'to', horizontal, vertical
91 63 self.hservo.send(horizontal)
92 64 self.vservo.send(vertical)
@@ -96,37 +68,41 @@ def focus(self, distance, point):
96 68 """distance in meters.
97 69 point is x, y tuple for location in grid space.
98 70
99   - angle of focus for x axis is x % of _kinect_x % of _kinect_hfield
100   - angle of focus for y axis is y % of _kinect_y % of _kinect_vfield
  71 + angle of focus for x axis is x % of cfg.kinect.x % of cfg.kinect.hfield
  72 + angle of focus for y axis is y % of cfg.kinect.y % of cfg.kinect.vfield
101 73 """
102   - x = point[0] / _kinect_x * _kinect_hfield
103   - y = point[1] / _kinect_y * _kinect_vfield
  74 + x = point[0] / cfg.kinect.x * cfg.kinect.hfield
  75 + y = point[1] / cfg.kinect.y * cfg.kinect.vfield
104 76 print distance, point, x, y
105 77 self.move(x, y)
106 78
107 79
108 80 Left = Eye('left eye',
109   - _lhpin, _lvpin,
110   - _lhmin, _lhmax, _lvmin, _lvmax,
111   - _port, -_offset)
  81 + cfg.eye.left.hpin, cfg.eye.left.vpin,
  82 + cfg.eye.left.hmin, cfg.eye.left.hmax,
  83 + cfg.eye.left.vmin, cfg.eye.left.vmax,
  84 + cfg.arduino.port,
  85 + cfg.eye.left.offset, cfg.eye.left.height)
  86 +
112 87 Right = Eye('right eye',
113   - _rhpin, _rvpin,
114   - _rhmin, _rhmax, _rvmin, _rvmax,
115   - _port, _offset)
  88 + cfg.eye.right.hpin, cfg.eye.right.vpin,
  89 + cfg.eye.right.hmin, cfg.eye.right.hmax,
  90 + cfg.eye.right.vmin, cfg.eye.right.vmax,
  91 + cfg.arduino.port,
  92 + cfg.eye.left.offset, cfg.eye.right.height)
116 93
117 94
118 95 def random_eyes():
119 96 from random import Random
120 97
121   - lhrange = (_lhmin, _lhmax)
122   - lvrange = (_lvmin, _lvmax)
123   - rhrange = (_rhmin, _rhmax)
124   - rvrange = (_rvmin, _rvmax)
125 98 r = Random()
126 99
127 100 for x in range(100000):
128   - Left.move(r.randrange(*lhrange), r.randrange(*lvrange))
129   - Right.move(r.randrange(*rhrange), r.randrange(*rvrange))
  101 + Left.move(r.randrange(cfg.eye.left.hmin, cfg.eye.left.hmax),
  102 + r.randrange(cfg.eye.left.vmin, cfg.eye.left.vmax))
  103 +
  104 + Right.move(r.randrange(cfg.eye.right.hmin, cfg.eye.right.hmax),
  105 + r.randrange(cfg.eye.right.vmin, cfg.eye.right.vmax))
130 106 sleep(0.25)
131 107
132 108
@@ -134,8 +110,8 @@ def random_eyes():
134 110 def random_focus():
135 111 from random import Random
136 112
137   - hrange = (0, _kinect_x -1)
138   - vrange = (0, _kinect_y - 1)
  113 + hrange = (0, cfg.kinect.x - 1)
  114 + vrange = (0, cfg.kinect.y - 1)
139 115 drange = (0, 3000)
140 116 r = Random()
141 117
17 move.py
@@ -23,21 +23,10 @@
23 23
24 24 from commands import Servo
25 25 from time import sleep
26   -import ConfigParser
  26 +import cfg
27 27
28   -config = ConfigParser.RawConfigParser()
29   -config.read('exuro.cfg')
30   -
31   -port = config.get('arduino', 'port')
32   -s1pin = config.getint('eye', 'left_horizontal')
33   -s2pin = config.getint('eye', 'left_vertical')
34   -s3pin = config.getint('eye', 'right_horizontal')
35   -s4pin = config.getint('eye', 'right_vertical')
36   -a1 = 10
37   -a2 = 170
38   -
39   -s1 = Servo(s1pin, port)
40   -s2 = Servo(s2pin, port)
  28 +s1 = Servo(cfg.eye.left.hpin, cfg.eye.left.hmin, cfg.eye.left.hmax, cfg.arduino.port)
  29 +s2 = Servo(cfg.eye.left.vpin, cfg.eye.left.vmin, cfg.eye.left.vmax, cfg.arduino.port)
41 30
42 31 for s, a in [(s1, 10), (s2, 10), (s1, 170), (s2, 170), (s1, 86), (s2, 86),]:
43 32 print 'moving servo on pin %d to %d' % (s.pin, a)
18 track.py
@@ -27,20 +27,14 @@
27 27 import numpy as np
28 28 import math
29 29 import eye
30   -import ConfigParser
  30 +import cfg
31 31
32   -config = ConfigParser.RawConfigParser()
33   -config.read('exuro.cfg')
34   -kinect_height = config.getfloat('kinect', 'height')
35   -kinect_x = config.getint('kinect', 'x')
36   -kinect_y = config.getint('kinect', 'y')
37 32
38   -
39   -threshold = 30 #
40   -current_depth = 750 #
41   -closest = (1,1) # the closest location
42   -distance = 1.0 # distance in meters from the kinect
43   -shape = (kinect_y, kinect_x) # assumed shape of the depth array
  33 +threshold = 30 #
  34 +current_depth = 750 #
  35 +closest = (1,1) # the closest location
  36 +distance = 1.0 # distance in meters from the kinect
  37 +shape = (cfg.kinect.y, cfg.kinect.x) # assumed shape of the depth array
44 38
45 39
46 40 def change_threshold(value):

0 comments on commit 885981d

Please sign in to comment.
Something went wrong with that request. Please try again.