def SetBField(self): try: xyz.fine_field_cart(self.xValue, self.yValue, self.zValue, self.handle) print("Magnetic Field Set") except Error as err: xyz.closePorts(self.handle) # close the ports before raising the error. raise err
def pid(setpoint, position, handle): """ hello this is a doc string """ kP = 0.004 kD = 0.005 kI = 0.4 #kP = 0.019 #kD = 0.007 #kI = 0.4 global prePos global integral global t0 global initialOutput # calculate dt and save t0 for the next call. t1 = time.time() dt = t1 - t0 t0 = t1 offset = position - setpoint preOffset = prePos - setpoint # only set the integral if it is not saturated if (integral > -5 and offset > 0) or (integral < 5 and offset <= 0): integral += (offset * dt) derivative = (offset - preOffset) / dt prePos = position # save the position for the next call. output = ((kP * offset) + (kI * integral) + (kD * derivative)) * -1e-7 + initialOutput # now set the field with the output #print('output = %14.11f\toffset = %14.11f\tdt = %14.11f\tintegral = %14.11f' % (output, offset, position, dt, integral)) print( 'offset = %14.11f\tintegral = %14.11f\tderivative = %14.11f\tdt = %14.11f' % (offset, integral, derivative, dt)) xyz.fine_field_cart(xyz.xCoil.appliedMaxField, output, handle) return
nominalFieldOffset = 41.5e-6 outputYField = nominalFieldOffset try: while True: # querry the labjack t1 = time.time() sumSignal, leftMinusRight = xyz.ljm.eReadNames(handle, 2, ['AIN0', 'AIN1']) # run the control loop output = pid(setpoint, leftMinusRight) outputYField += output*1e-6 print('output = %s' % output) # set the field xyz.fine_field_cart(xyz.xCoil.coilField, outputYField, xyz.zCoil.largeCoilField, handle) # save the (now) old leftMinusRight value for the next loop lastLeft = leftMinusRight dt = time.time() - t1 # keep track of the timestep for consistancy sake. print('time step = %s' % dt) if sumSignal < 3: raise exception('sum signal LOW!') except Exception as e: # helpful to close the ports on except when debugging the code! xyz.closePorts(handle) print('closed all the ports') print(e) # print the exception raise
handle = xyz.openPorts() # configure the analog register # Setup and call eWriteNames to configure the AIN on the LabJack. numFrames = 3 names = ["AIN0_NEGATIVE_CH", "AIN0_RANGE", "AIN0_RESOLUTION_INDEX"] aValues = [199, 0, 10] # setup the analog register values for the labjack xyz.ljm.eWriteNames(handle, numFrames, names, aValues) # open ports handle = xyz.openPorts() try: while True: input('setLow') xyz.fine_field_cart(10e-6, 10e-6, handle) input('setHigh') xyz.fine_field_cart(20e-6, 20e-6, handle) except KeyboardInterrupt: time.sleep(.5) print('\n') xyz.closePorts(handle) print('closed all the ports') #print('Keyboard Interrupt') # print the exception #raise except Exception as e: # helpful to close the ports on except when debugging the code! time.sleep(.5) xyz.closePorts(handle) print('closed all the ports')
import xyzFieldControl as xyz import numpy as np import time as time # open the all ports and get the labjack handle handle = xyz.openPorts() #lock in the z because we know what it is (don't change it) zCurrent = (xyz.zCoil.largeCoilCurrent) # xyz.zCoil.supply.current(439.5) yField = 41.92e-6 try: while True: yField += float(input('offset yField by (nano Tesla) -->')) * 1e-9 # print('yField = %f (micro Tesla)' % (yField * 1e6)) xyz.fine_field_cart(xyz.xCoil.appliedMaxField, yField, xyz.zCoil.largeCoilField, handle) ''' # try something a bit more simple xyz.field_cart(xyz.xCoil.appliedMaxField, xyz.yCoil.appliedMaxField, xyz.zCoil.largeCoilField) ''' pass except Exception as e: # helpful to close the ports on except when debugging the code! xyz.closePorts(handle) print('closed all the ports') print(e) # print the exception raise
backwardsBy = By[::-1] minSumSignal = 3.0 # analog voltage goes between 0 and 6 volts roughly sumSignal = np.array([0.0,0.0]) # open the all ports and get the labjack handle handle - xyz.openPorts() #lock in the z because we know what it is (don't change it) zCurrent = (xyz.zCoil.largeCoilCurrent) # xyz.zCoil.supply.current(439.5) try: for i, xField in enumerate(Bx): if i % 2: for j, yField in enumerate(By): xyz.fine_field_cart(xField, yField, xyz.zCoil.largeCoilField, handle) #time.sleep(0.1) result = float(xyz.ljm.eReadName(handle,'AIN0')) if result > minSumSignal: sumSignal = np.vstack((sumSignal, np.array([xField, yField]))) else: for j, yField in enumerate(backwardsBy): xyz.fine_field_cart(xField, yField, xyz.zCoil.largeCoilField, handle) #time.sleep(0.1) result = float(xyz.ljm.eReadName(handle,'AIN0')) if result > minSumSignal: sumSignal = np.vstack((sumSignal, np.array([xField, yField]))) ''' # try something a bit more simple xyz.field_cart(xyz.xCoil.appliedMaxField, xyz.yCoil.appliedMaxField, xyz.zCoil.largeCoilField)
setpoint = 0 lastSum = 0 lastLeft = 0 # make some dque objects for leftMinusRight and sumSignal? try: while True: # querry the labjack time = time.time() sumSignal, leftMinusRight = xyz.ljm.eReadNames(handle, 2, ['AIN0', 'AIN1']) # run the control loop output = pid(setpoint, leftMinusRight, lastLeft, 1) + # set the field xyz.fine_field_cart(output, xyz.yCoil.largeCoilField, xyz.zCoil.largeCoilField, handle) # save the (now) old leftMinusRight value for the next loop lastLeft = leftMinusRight dt = time.time() - time # keep track of the timestep for consistancy sake. print('times step = ' + dt) except Exception as e: # helpful to close the ports on except when debugging the code! xyz.closePorts(handle) print('closed all the ports') print(e) # print the exception raise # work in the optical zero space so we are always adusting perpenductular to the optical zero.
zCurrent = (xyz.zCoil.largeCoilCurrent) # xyz.zCoil.supply.current(439.5) xField = 0 yField = 0 zField = 0 try: while True: xInField = float('0' + input('xField in micro Tesla -->')) * 1e-6 yInField = float('0' + input('yField in micro Tesla -->')) * 1e-6 #zInField = float('0'+input('zField in micro Tesla -->'))*1e-6 if xInField != 0: xField = xInField if yInField != 0: yField = yInField zField = xyz.zCoil.getLargeCoilField( ) # keep the z at whatever it's set to print('setting field to:\n(xField, yField, zField)\n(%f, %f, %f)' % ((xField * 1e6), (yField * 1e6), (zField * 1e6))) xyz.fine_field_cart(xField, yField, zField, handle) except Exception as e: # helpful to close the ports on except when debugging the code! xyz.closePorts(handle) print('closed all the ports') print(e) # print the exception raise