Adjusted to new pycomedi setup.
authorW. Trevor King <wking@drexel.edu>
Mon, 15 Dec 2008 15:21:49 +0000 (10:21 -0500)
committerW. Trevor King <wking@drexel.edu>
Mon, 15 Dec 2008 15:21:49 +0000 (10:21 -0500)
Also added workaround for the pycomedi extra-point hack.

x_piezo.py
z_piezo.py
z_piezo_utils.py

index 6e87591f666d6c39456c0c572bf73f3203ea3319..9405b6569e77a6d4e92bb23fcd40d940c611d108 100644 (file)
@@ -1,5 +1,4 @@
-import comedi_single_aio
-#import sngAO
+from pycomedi.single_aio import AO
 
 class x_piezoError (Exception) :
     pass
@@ -10,7 +9,7 @@ class x_piezo :
         self.xpSensitivity = 34.0 # nm/Volt
         self.gain = 20 # Vpiezo / Voutput
 
-        self.AO = comedi_single_aio.ao_obj(chan=(xp_chan,))
+        self.AO = AO(chan=(xp_chan,))
         self.AO.close()
        #self.AO = sngAO.sngAOobj([xp_chan], -10, 10)
 
index 6b1e5391e6f0824cba75f1b3f4df2d747e635ca9..3c2166e614c0aaa1b7329d0a5bd752292c779acd 100644 (file)
@@ -220,39 +220,24 @@ class _z_piezo_jump :
     def __init__(self, chan_info) :
         self.chan_info = chan_info
         self.reserved = False
-        if USE_COMEDI :
-            self.AI = comedi_single_aio.ai_obj(chan=self.chan_info.inChan)
-            self.AI.close()
-            self.AO = comedi_single_aio.ao_obj(chan=self.chan_info.outChan)
-            self.AO.close()
-        else :
-            self.AI = sngAI.sngAIobj(self.chan_info.inChan, -10, 10)
-            self.AO = sngAO.sngAOobj(self.chan_info.outChan, -10, 10)
+        self.AI = single_aio.AI(chan=self.chan_info.inChan)
+        self.AI.close()
+        self.AO = single_aio.AO(chan=self.chan_info.outChan)
+        self.AO.close()
     def reserve(self) :
-        if USE_COMEDI :
-            self.AI.open()
-            self.AO.open()
-        else :
-            self.AI.reserve()
-            self.AO.reserve()
+        self.AI.open()
+        self.AO.open()
         self.reserved = True
     def release(self) :
-        if USE_COMEDI :
-            self.AI.close()
-            self.AO.close()
-        else :
-            self.AI.unreserve()
-            self.AO.unreserve()
+        self.AI.close()
+        self.AO.close()
         self.reserved = False
     def updateInputs(self) :
         if self.reserved == False :
             self.AI.open()
             #self.reserve()
             self.reserved = False
-        if USE_COMEDI :
-            In = self.AI.read()
-        else :
-            (er, In) = self.AI.read()
+        In = self.AI.read()
         if self.reserved == False :
             self.AI.close()
             #self.release()
@@ -266,18 +251,16 @@ class _z_piezo_jump :
             self.release()
         return self.updateInputs()
     def out_to_phys(self, output) :
-        if USE_COMEDI :
-            return self.AO.comedi_to_phys(0, output)
+        return self.AO.comedi_to_phys(0, output)
     def phys_to_out(self, physical) :
-        if USE_COMEDI :
-            return self.AO.phys_to_comedi(0, physical)
+        return self.AO.phys_to_comedi(0, physical)
 
 class _z_piezo_ramp :
     def __init__(self, chan_info) :
         self.chan_info = chan_info
-        self.verbose = True
         self.attempts=0
         self.failures=0
+        self.verbose = True
     def ramp(self, outArray, freq) :
         # allocate and run the task
         npoints = int(len(outArray)/self.chan_info.numOut)
@@ -288,38 +271,65 @@ class _z_piezo_ramp :
                 out_data[i] = outArray[i]
         else :
             out_data = outArray
+        if simult_aio.DONT_OUTPUT_LAST_SAMPLE_HACK == True:
+            # duplicate the last output point
+            npoints += 1
+            out_hack = array([0]*npoints*self.chan_info.numOut, dtype=uint16)
+            for i in range(npoints-1) :
+                out_hack[i] = out_data[i]
+            out_hack[-1] = out_data[-1]
+            out_data = out_hack
+            in_data = array([0]*npoints*self.chan_info.numIn, dtype=uint16)
 
         correlated = False
         ramp={}
         while not correlated :
-            if USE_COMEDI :
-                AIO = comedi_simult_aio.aio_obj(in_chan=self.chan_info.inChan,
-                                                out_chan=self.chan_info.outChan)
-                if self.verbose :
-                    print "setup AIO"
-                AIO.setup(freq, out_data)
-                if self.verbose :
-                    print "arm AIO"
-                AIO.arm()
-                if self.verbose :
-                    print "read AIO"
-                AIO.start_read(in_data)
-                if self.verbose :
-                    print "close AIO"
-                AIO.close()
-                if self.verbose :
-                    print "finished AIO"
+            AIO = simult_aio.AIO(in_chan=self.chan_info.inChan,
+                                 out_chan=self.chan_info.outChan)
+            if self.verbose :
+                print "setup AIO"
+            AIO.setup(freq=freq, out_buffer=out_data)
+            if self.verbose :
+                print "arm AIO"
+            AIO.arm()
+            if self.verbose :
+                print "read AIO"
+            AIO.start_read(in_data)
+            if self.verbose :
+                print "close AIO"
+            AIO.close()
+            if self.verbose :
+                print "finished AIO"
             self.attempts += 1
             ramp["Z piezo output"] = out_data[self.chan_info.zp_ind::self.chan_info.numOut]
             ramp["Z piezo input"] = in_data[self.chan_info.zp_mon_ind::self.chan_info.numIn]
+            ramp["Deflection input"] = in_data[self.chan_info.def_ind::self.chan_info.numIn]
             failed = False
             gradient, intercept, r_value, p_value, std_err = linregress(ramp["Z piezo output"],
                                                                         ramp["Z piezo input"])
             rnge = ramp["Z piezo output"].max()-ramp["Z piezo output"].min()
             if gradient < .8 and rnge > 100 :
+                if PYLAB_INTERACTIVE_VERBOSE :
+                    figure(BASE_FIG_NUM+3)
+                    subplot(211)
+                    title('ramp (def_in vs zp_out)')
+                    timestamp = time.strftime('%H%M%S')
+                    plot(ramp["Z piezo output"], ramp["Deflection input"], '.', label=timestamp)
+                    subplot(212)
+                    title('zp_in vs zp_out')
+                    plot(ramp["Z piezo output"], ramp["Z piezo input"], '.', label=timestamp)
+                    legend(loc='best')
                 print "ramp failed slope (%g, %g), try again" % (gradient, rnge)
                 failed = True
             if not failed :
+                if simult_aio.DONT_OUTPUT_LAST_SAMPLE_HACK == True:
+                    # remove the duplicated last output point
+                    out_data = out_data[:-self.chan_info.numOut]
+                    in_data = in_data[:-self.chan_info.numIn]
+                ramp["Z piezo output"] = out_data[self.chan_info.zp_ind::self.chan_info.numOut]
+                ramp["Z piezo input"] = in_data[self.chan_info.zp_mon_ind::self.chan_info.numIn]
+                ramp["Deflection input"] = in_data[self.chan_info.def_ind::self.chan_info.numIn]
+
                 if USE_ABCD_DEFLECTION :
                     ramp["Deflection segment"] = []
                     for c_ind in self.chan_info.def_ind :
@@ -329,8 +339,7 @@ class _z_piezo_ramp :
                     C = ramp["Deflection segment"][2].astype(float)
                     D = ramp["Deflection segment"][3].astype(float)
                     ramp["Deflection input"] = (((A+B)-(C+D))/(A+B+C+D) * 2**15).astype(uint16)
-                    if USE_COMEDI :
-                        ramp["Deflection input"] += 2**15 # HACK, comedi uses unsigned ints...
+                    ramp["Deflection input"] += 2**15 # HACK, comedi uses unsigned ints...
                 else :
                     ramp["Deflection input"] = in_data[self.chan_info.def_ind::self.chan_info.numIn]
                 correlated = True
index b5a2a5497ed8013e2fc73e59a66d0204a4ac40d9..8fa74e3c232b101ec85bee98df4e730ce2ea5045 100644 (file)
@@ -106,7 +106,7 @@ def wiggleForInterferenceMin(zpiezo, wig_amp=20000, wig_freq=100) :
     title('wiggle for interference')
     hold(False)
     p = plot(out, out, 'b.-')
-    c = curses_check_for_keypress.check_for_keypress()
+    c = curses_check_for_keypress.check_for_keypress(test_mode=False)
     while c.input() == None :
         data = zpiezo.ramp(out, scanfreq)
         p[0].set_ydata(data["Deflection input"])