Commits

tlatorre committed b6cd4d4

initial commit

Comments (0)

Files changed (5)

+How to install Phidgets software for this machine
+-------------------------------------------------
+
+Download phidget21 software
+---------------------------
+
+http://www.phidgets.com/downloads/libraries/libphidget_2.1.7.20110203.tar.gz
+
+./configure
+./make
+./make install
+
+Install python API
+------------------
+
+http://www.phidgets.com/downloads/libraries/PhidgetsPython_2.1.7.20110203.zip
+
+sudo apt-get install python-setuptools
+
+How to chmod stepper device
+---------------------------
+
+lsusb (then find Phidgets Device)
+
+sudo chmod a+rwx /dev/bus/usb/005/006
+from stepper import *
+#!/usr/bin/env python
+import time
+import string
+import network
+from lecrunch import fetch
+
+def main(root, events, runs):
+    # settings for sno pmt
+    network.send('setdegree', -71)
+
+    for angle in range(-69,70,3):
+        print 'angle: %i' % angle
+
+        # set position
+        network.send('setdegree', angle)
+
+        # wait 5 seconds
+        time.sleep(5)
+
+        for i in range(runs):
+            timestr = string.replace(time.asctime(time.localtime()), ' ', '-')
+            filename = '%s_angle_%i_%s.hdf5' % (root, angle, timestr)
+
+            try:
+                fetch(filename, events)
+            except KeyboardInterrupt:
+                print 'ctrl-c caught; exiting...'
+                return -1
+            except Exception, e:
+                print '%s: %s' % (type(e).__name__, e)
+                print 'trying to recover.'
+                print 'waiting for 5 minutes...'
+                time.sleep(60*5)
+                print 'continuing...'
+                continue
+                
+if __name__ == '__main__':
+    import sys
+    import optparse
+
+    parser = optparse.OptionParser('%prog basename')
+    parser.add_option('-n', type='int', dest='events', default=100000)
+    parser.add_option('-r', type='int', dest='runs', default=1)
+    parser.add_option('-s', type='int', dest='scans', default=1)
+    options, args = parser.parse_args()
+
+    if len(args) < 1:
+        sys.exit(parser.format_help())
+
+    for i in range(options.scans):
+        retval = main(args[0], options.events, options.runs)
+
+        if retval == -1:
+            break
+
+    network.close()
+import execnet
+
+gw = execnet.makegateway('ssh=atom')
+
+def stepper_server(channel):
+    import sys
+    sys.path.append('/home/tlatorre/projects')
+
+    import stepper
+
+    s = stepper.Stepper()
+
+    while not channel.isclosed():
+        key, value = channel.receive()
+
+        if key == 'setposition':
+            s.setposition(value)
+            channel.send(s.getposition())
+        if key == 'setdegree':
+            s.setdegree(value)
+            channel.send(s.getdegree())
+        if key == 'close':
+            s.close()
+        if key == 'getposition':
+            channel.send(s.getposition())
+        if key == 'getdegree':
+            channel.send(s.getdegree())
+
+    s.close()
+        
+channel = gw.remote_exec(stepper_server)
+
+def recv():
+    return channel.receive()
+
+def send(key, value=None):
+    channel.send((key, value))
+    return recv()
+
+def close():
+    channel.send(('close', None))
+    channel.close()
+    
+#!/usr/bin/env python
+"""Simple API to the Phidget python library for a stepper motor"""
+import time
+import Phidgets.Devices.Stepper
+
+# microsteps per revolution
+microsteps = 18285
+
+# max and min positions allowed
+posmin = -int(microsteps/4.0)
+posmax = +int(microsteps/4.0)
+
+class Stepper(Phidgets.Devices.Stepper.Stepper):
+    def __init__(self):
+        Phidgets.Devices.Stepper.Stepper.__init__(self)
+        self.openPhidget()
+        self.waitForAttach(10000)
+
+        # set maximum current to 500 mA
+        self.setCurrentLimit(0, 0.5)
+        # set maximum velocity to .005 rev/sec
+        self.setVelocityLimit(0, microsteps/200.0)
+
+        self.setEngaged(0, True)
+        time.sleep(1)
+
+    def close(self):
+        self.closePhidget()
+
+    def setcurrent(self, x):
+        self.setCurrentLimit(0, x) 
+
+    def zero(self):
+        self.setCurrentPosition(0, 0)
+
+    def getposition(self):
+        return self.getCurrentPosition(0)
+
+    def setposition(self, pos):
+        mod = abs(pos) % 16
+        if mod <= 8:
+            if pos >= 0:
+                mpos = pos - mod
+            else:
+                mpos = pos + mod
+        else:
+            if pos >= 0:
+                mpos = pos + (16 - mod)
+            else:
+                mpos = pos - (16 - mod)
+
+        self.setcurrent(1.7)
+        time.sleep(1)
+        if pos >= posmin and pos <= posmax:
+            self.setTargetPosition(0, pos)
+            while not self.getStopped(0):
+                pass
+        else:
+            raise Exception("Position out of bounds.")
+        time.sleep(1)
+        self.setcurrent(0.5)
+
+    def setdegree(self, degree):
+        self.setposition(int(degree*microsteps/360.0))
+
+    def getdegree(self):
+        return int(self.getposition()*360/microsteps)