Exemple #1
0
    def initialiseParameters(self):
        # Communiate with the analog powermeter
        # self.apm = AnalogComm(analogpm_add)
        # self.APM_CHANNEL = 1

        # # Communicate with the usb counter
        self.counter = Countercomm(counter_add)
        self.counter.set_gate_time(30)

        # Create a variable to store the average value & number of averages of the analog powermeter
        self.average_apm = 0
        self.NUM_OF_AVG = 20

        # Creating the objects voltage_handler
        self.voltage_handler = [0, 0, 0, 0, 0, 0, 0]
        for i in range(1, 7):
            self.voltage_handler[i] = VoltageHandler(MAX_STEP_VOLTAGE[i], i)

        # Initialise the variable set_voltage: These are the values that we want to set the DAC to.
        # Note: In voltage 6, it is the set_value (from GUI) + the fine adjustment for locking
        self.set_voltage = [0, 0, 0, 0, 0, 0, 0]

        # Initialise the offset adj voltage & correction direction
        self.offset_adj_voltage = [0, 0]
        self.offset_adj_direction = 1  #1 for positive, -1 for negative direction
        self.count_adj_steps = 0

        # Lock request variable (1: asking for lock, 0: nothing)
        self.lock_request = 0

        # Create the locking delay variable & locking mode
        self.LOCKING_DELAY = 5
        self.lock_delay_counter = 0
        self.locking_mode = 1  # Starts locking from voltage 6
        self.locking_mode_max = self.locking_mode + MAX_LOCKING_TRIES
Exemple #2
0
class ThreadedClient:
    """
    Launch the main part of the GUI and the worker thread. periodicCall and
    endApplication could reside in the GUI part, but putting them here
    means that you have all the thread controls in a single place.
    """
    def __init__(self, master):
        """
        Start the GUI and the asynchronous threads. We are in the main
        (original) thread of the application, which will later be used by
        the GUI as well. We spawn a new thread for the worker (I/O).
        """
        self.master = master

        # Create the queue
        self.queue = Queue.Queue()

        # Set up the GUI part
        self.gui = GuiPart(master, self.queue, self.endApplication)
        master.protocol("WM_DELETE_WINDOW",
                        self.endApplication)  # About the silly exit button

        # Start the procedure regarding the initialisation of experimental parameters and objects
        self.initialiseParameters()

        # Initialising the zmq server
        self.context = zmq.Context()
        self.socket = self.context.socket(zmq.REP)
        self.socket.bind("tcp://127.0.0.1:5556")
        print "The server is up. Ready to receive messages"

        # Set up the thread to do asynchronous I/O
        # More threads can also be created and used, if necessary
        self.running = 1
        self.thread1 = threading.Thread(target=self.workerThread1_APM)
        self.thread1.start()
        self.thread2 = threading.Thread(target=self.workerThread2_zmq)
        self.thread2.start()

        # Start the periodic call in the GUI to check if the queue contains
        # anything
        self.periodicCall()

    def initialiseParameters(self):
        # Communiate with the analog powermeter
        # self.apm = AnalogComm(analogpm_add)
        # self.APM_CHANNEL = 1

        # # Communicate with the usb counter
        self.counter = Countercomm(counter_add)
        self.counter.set_gate_time(30)

        # Create a variable to store the average value & number of averages of the analog powermeter
        self.average_apm = 0
        self.NUM_OF_AVG = 20

        # Creating the objects voltage_handler
        self.voltage_handler = [0, 0, 0, 0, 0, 0, 0]
        for i in range(1, 7):
            self.voltage_handler[i] = VoltageHandler(MAX_STEP_VOLTAGE[i], i)

        # Initialise the variable set_voltage: These are the values that we want to set the DAC to.
        # Note: In voltage 6, it is the set_value (from GUI) + the fine adjustment for locking
        self.set_voltage = [0, 0, 0, 0, 0, 0, 0]

        # Initialise the offset adj voltage & correction direction
        self.offset_adj_voltage = [0, 0]
        self.offset_adj_direction = 1  #1 for positive, -1 for negative direction
        self.count_adj_steps = 0

        # Lock request variable (1: asking for lock, 0: nothing)
        self.lock_request = 0

        # Create the locking delay variable & locking mode
        self.LOCKING_DELAY = 5
        self.lock_delay_counter = 0
        self.locking_mode = 1  # Starts locking from voltage 6
        self.locking_mode_max = self.locking_mode + MAX_LOCKING_TRIES

    def periodicCall(self):
        """
        Check every 100 ms if there is something new in the queue.
        """
        self.gui.processIncoming()

        # Setting a refresh rate for periodic call
        self.master.after(100, self.periodicCall)

        # Check the lock status and perform locking/unlocking if necessary
        if self.gui.set_lock_status == 0:
            self.gui.lock_process = 0
            self.offset_adj_voltage[0] = 0
            self.offset_adj_voltage[1] = 0
        elif self.gui.set_lock_status == 1 and self.lock_request == 0:
            if self.average_apm < self.gui.set_value[0]:
                self.gui.lock_process = 4
        elif self.gui.set_lock_status == 1 and self.lock_request == 1:
            # Check for whether display APM is in locked condition
            if self.gui.lock_process == 4:
                self.gui.lock_process = 1
            if self.average_apm > self.gui.lock_target:
                self.gui.lock_process = 2

        # Simple slow locking mechanism. Note: the value of display_apm in GUI is delayed by 100ms from average_apm
        if self.gui.lock_process == 1:
            self.lock_delay_counter += 1
            self.try_mode = self.locking_mode % 2
            if self.lock_delay_counter >= self.LOCKING_DELAY:  #Delay for the correction to set in
                if self.average_apm < self.gui.display_apm:
                    self.offset_adj_direction = -1 * self.offset_adj_direction  # Reverse the process if the correction does not become better
                self.offset_adj_voltage[
                    self.
                    try_mode] += STEP_OFFSET_ADJ * self.offset_adj_direction
                self.lock_delay_counter = 0
            self.count_adj_steps += 1
            if self.count_adj_steps >= MAX_LOCKING_STEP[self.try_mode]:
                self.locking_mode += 1
                self.count_adj_steps = 0
                print "Trying locking in other direction"
        if self.locking_mode >= self.locking_mode_max:
            self.gui.lock_process = 3

        # Update the display status of the lock
        self.gui.updateLockProcess()

        # Check the insanity of the offset adj voltage and refresh periodically the offset_adj_voltage display
        for i in range(2):
            self.offset_adj_voltage[i] = insanity_check(
                self.offset_adj_voltage[i], MIN_OFFSET_ADJ, MAX_OFFSET_ADJ)
            self.gui.display_offset_adj[i] = self.offset_adj_voltage[i]
            self.gui.label_offset_adj[i]['text'] = '%.3f' % round(
                self.gui.display_offset_adj[i], 3)

        # Updating the set voltage based on the gui set value. For voltage 5 and 6, need to add the offset adj.
        for i in range(1, 5):
            self.set_voltage[i] = self.gui.set_value[i]
        for i in range(2):
            self.set_voltage[
                i + 5] = self.gui.set_value[i + 5] + self.offset_adj_voltage[i]

        # Voltage "insanity check" for the final time before being processed by the program
        for i in range(1, 7):
            self.set_voltage[i] = insanity_check(self.set_voltage[i],
                                                 MIN_VALUE_VOLTAGE,
                                                 MAX_VALUE_VOLTAGE)

        # Updating the voltage handler objects & GUI
        for i in range(1, 7):
            self.voltage_handler[i].change_set_voltage(self.set_voltage[i])
            output = self.voltage_handler[i].update()
            self.gui.output_value[i] = output
            self.gui.label_output[i]['text'] = 'Output : ' + '%.3f' % round(
                self.gui.output_value[i], 3)

        # Updating the display value of analog powermeter
        self.gui.display_apm = self.average_apm
        self.gui.label_display_apm['text'] = '%.2f' % round(
            self.gui.display_apm, 2)

        # Shutting down the program
        if not self.running:
            # Check whether the voltages has been switched off
            for i in range(1, 7):
                if self.gui.output_value[i] == 0:
                    pass
                else:
                    return 0
            print "Shutting Down"
            import sys
            sys.exit()

    def workerThread1_APM(self):
        """
        This is where we handle the asynchronous I/O. For example, it may be
        a 'select(  )'. One important thing to remember is that the thread has
        to yield control pretty regularly, by select or otherwise.
        """
        while self.running:
            # To simulate asynchronous I/O, we create a random number at
            # random intervals. Replace the following two lines with the real
            # thing.
            try:
                # Analog Powermeter
                # now = float(self.apm.get_voltage(self.APM_CHANNEL))
                # Usb Counter
                now = float(self.counter.get_counts(0))

                self.average_apm = (
                    self.NUM_OF_AVG - 1
                ) * self.average_apm / self.NUM_OF_AVG + now / self.NUM_OF_AVG
            except:
                pass

    def workerThread2_zmq(self):
        """
        This is where we handle the asynchronous I/O. For example, it may be
        a 'select(  )'. One important thing to remember is that the thread has
        to yield control pretty regularly, by select or otherwise.
        """
        while self.running:
            try:
                self.message = self.socket.recv()
                print "Received message from the other side :", self.message

                try:
                    self.message_a, self.message_b = self.message.split(" ")
                except:
                    print "MESSAGE ILL DEFINED"
                    # Tell Boss the message is ill defined
                    self.socket.send("Speak Properly")

                # The reply if it does not satisfy anything below
                self.message_back = "Whatdahell Boss"

                try:
                    # A big try

                    if self.message_a == "Please":
                        # Dealing with lock
                        if self.message_b == "Lock":
                            self.lock_request = 1
                            self.locking_mode = 1  # Start locking from V6 again
                            self.count_adj_steps = 0
                            self.message_back = "Okay Boss"
                            if self.gui.lock_process == 3:
                                print "Trying to lock again"
                                self.gui.lock_process = 1
                            # Check whether the lock is set
                            if self.gui.set_lock_status == 0:
                                print "Lock is not set. Tell the other side"
                                self.message_back = "Lock Nonexistent"
                                self.lock_request = 0
                        # Dealing with shutdown
                        if self.message_b == "Annihilate":
                            self.message_back = "Okay Boss"
                            self.endApplication()

                    if self.message_a == "Check":
                        if self.message_b == "Lock":
                            # ----- DEALING WITH LOCK ----- #
                            # Check if the lock is obtained (or error)
                            if self.gui.lock_process == 2:
                                print "Locked successful, tell the good news to the other side"
                                self.message_back = "Lock Successful"
                                self.lock_request = 0
                            elif self.gui.lock_process == 3:
                                print "Locked not successful, tell the bad news to the other side"
                                self.message_back = "Lock Unsuccessful"
                            # Misc
                            elif self.gui.lock_process == 1:
                                print "Still Locking"
                                self.message_back = "Still Locking"
                            else:
                                print "Request for check lock is probably not at the correct moment"
                                self.message_back = "Something Wrong"
                            # ----- END ----- #

                    if self.message_a == "CheckVolt":
                        # Check voltage for a specific channel
                        channel = int(float(self.message_b))
                        if channel > 0 and channel < 7:
                            value = '%.3f' % round(
                                self.gui.output_value[channel], 3)
                            self.message_back = "Volt" + str(
                                channel) + " " + value
                        else:
                            self.message_back = "Volt" + str(
                                channel) + " " + "Undefined"

                    if self.message_a[:-1] == "SetVolt":
                        channel = int(self.message_a[-1])
                        if channel > 0 and channel < 7:
                            value = round(float(self.message_b), 3)
                            value = insanity_check(value, MIN_VALUE_VOLTAGE,
                                                   MAX_VALUE_VOLTAGE)
                            self.gui.set_value[channel] = value
                            self.message_back = "SetVolt" + str(
                                channel) + " " + str(value)
                        else:
                            self.message_back = "SetVolt" + str(
                                channel) + " " + "Undefined"

                    if self.message_a[:-1] == "ShiftVolt":
                        channel = int(self.message_a[-1])
                        if channel > 0 and channel < 7:
                            if self.message_b == "Up":
                                self.gui.set_value[
                                    channel] += self.gui.fine_step[channel]
                                self.message_back = "ShiftVolt" + str(
                                    channel) + " " + "Up"
                            if self.message_b == "Down":
                                self.gui.set_value[
                                    channel] -= self.gui.fine_step[channel]
                                self.message_back = "ShiftVolt" + str(
                                    channel) + " " + "Down"
                        else:
                            self.message_back = "ShiftVolt" + str(
                                channel) + " " + "Undefined"

                except:
                    # The message is ill defined
                    self.message_back = "Speak Properly"

                # Finally send the message back
                self.socket.send(self.message_back)

            except:
                pass

    def endApplication(self):
        # Cleaning up before shutting down
        for i in range(1, 7):
            self.gui.set_value[i] = 0

        # Kill and wait for the processes to be killed
        self.running = 0
        time.sleep(0.1)
Exemple #3
0
        return (pac)


def return_initial():
    print(
        'Setting the Piezo back to the initial values before the measurement')
    setV(V0x, V0y)
    print('Setting rf power to 810AOM back to originial values')
    dds_810.set_power(init_power)


##############################################################################################

#initial miniusb counter device
miniusb_port = '/dev/serial/by-id/usb-Centre_for_Quantum_Technologies_USB_Counter_Ucnt-QO10-if00'
miniusb = Countercomm(miniusb_port)
miniusb.set_gate_time(30)

#initiate analogI0
analogIO_port = '/dev/serial/by-id/usb-Centre_for_Quantum_Technologies_Analog_Mini_IO_Unit_MIO-QO13-if00'
analogIO = AnalogComm(analogIO_port)

#filename
filename = 'landscape_ext_810_c2.dat'

#Parmeters defnition
#Retrieve current voltage set to PZT as the initial VX0,VY0
socket.send("CheckVolt 5")
V0y = float((socket.recv()).split()[-1])
socket.send("CheckVolt 6")
V0x = float((socket.recv()).split()[-1])
def scan_table(table,DDS_address,freq=None,detector='pm',directo='probescan/settest', duration=5,umc_average=10):
    '''
    To scan probe freq by 2 aom with pre-defined calibrated rf power.

    The calibrated values OF 2 AOMs stored in the table obtained from the polishing
    process

    Scan the whole table if freq is not specified


    '''
    average=200

    wavelength=780
    if detector=='pm':
        Power_meter_address = '/dev/serial/by-id/usb-Centre_for_Quantum_Technologies_Optical_Power_Meter_OPM-QO04-if00'
        pm = PowerMeterComm(Power_meter_address)
        pm.set_range(2)
    if detector=='analog_pm':
        analogpm_add='/dev/serial/by-id/usb-Centre_for_Quantum_Technologies_Analog_Mini_IO_Unit_MIO-QO13-if00'
        apm=AnalogComm(analogpm_add)
    if detector=='usbmini-counter':
        counter_add='/dev/serial/by-id/usb-Centre_for_Quantum_Technologies_USB_Counter_Ucnt-QO10-if00'
        counter=Countercomm(counter_add)
        counter.set_gate_time(30)
    f=table[:,0]
    fup=table[:,1]
    fdown=table[:,2]
    pup=table[:,5]
    pd=table[:,6]
    if freq!=None:
        print('Freq: ',freq)
        
        i=np.where(table[:,0]==freq)[0][0]
        fu=fup[i]
        fd=fdown[i]
        print('fup fd',fu,fd)
        ampup=pup[i]
        ampd=pd[i]

        dds = DDSComm(DDS_address,0)
        dds.set_freq(fd)
        dds.set_power(ampd)

        dds1= DDSComm(DDS_address,1)
        dds1.set_freq(fu)
        dds1.set_power(ampup)
        time.sleep(2)
        if detector=='pm':
                value=[]
                for m in range(average):
                    power = pm.get_power(wavelength)
                    value.append(power)
                    time.sleep(1e-1) # delay between asking for next value
                p=np.mean(value)

        elif detector=='analog_pm':
                for m in range(average):
                    power = apm.get_voltage(0)
                    value.append(float(power))
                    time.sleep(1e-2) # delay between asking for next value

                p=np.mean(value)
                powers.append(p)
                print('Power: ',p)
        elif detector=="apd":
                name=tsc.filename(str(freq),directo)
                tsc.start(name=name)
                time.sleep(duration)
                tsc.stop()
                time.sleep(0.5)
                return name
        elif detector=="usbmini-counter":
                value=[]

                for m in range(umc_average):
                    value.append((counter.get_counts(0)))
                p=np.mean(value)
                return(freq,value)

    if freq==None:
        #Scan the whole table
        print('scan the whole table')
        powers=[]
        #for i in range(len(f)):
        fscan=[]
        for i in range(len(f)):
            fscan.append(f[i])
            fu=fup[i]
            fd=fdown[i]
            ampup=pup[i]
            ampd=pd[i]
            print(fu,fd)
            dds = DDSComm(DDS_address,0)
            dds.set_freq(fd)
            dds.set_power(ampd)

            dds1= DDSComm(DDS_address,1)
            dds1.set_freq(fu)
            dds1.set_power(ampup)
            time.sleep(1)
            value=[]
            std=[]
            if detector=='pm':

                for m in range(average):
                    power = pm.get_power(wavelength)
                    value.append(power)
                    time.sleep(1e-1) # delay between asking for next value
                p=np.mean(value)
                std=np.std(value)
                print('Freq ',f[i])
                print(p,std)
                powers.append(p)

            elif detector=="apd":
                directo='probescan/set2'
                name=tsc.filename(str(f[i]),directo)
                tsc.start(name=name)
                time.sleep(0.5)
                tsc.stop()
                time.sleep(2)
            elif detector=='analog_pm':
                for m in range(average):
                    power = apm.get_voltage(0)
                    value.append(float(power))
                    time.sleep(1e-2) # delay between asking for next value

                p=np.mean(value)
                powers.append(p)
                print('P is: ',p)
                print('Freq:',f[i])
        return (fscan,powers)
    message = socket.recv()


def getCount(miniusb, channel=0, average=10):
    counts = []
    for i in range(average):
        counts.append(miniusb.get_counts(channel))
    average_count = np.mean(counts)
    return average_count


##############################################################################################

#initial miniusb counter device
miniusb_port = '/dev/serial/by-id/usb-Centre_for_Quantum_Technologies_USB_Counter_Ucnt-QO10-if00'
miniusb = Countercomm(miniusb_port)

#initiate analogI0
analogI0_port = '/dev/serial/by-id/usb-Centre_for_Quantum_Technologies_Analog_Mini_IO_Unit_MIO-QO13-if00'
analogI0 = AnalogComm(analogI0_port)

#initiate windfreak
wf_port = '/dev/serial/by-id/usb-Windfreak_Synth_Windfreak_CDC_Serial_014571-if00'
wf = WindFreakUsb2(wf_port)

#Parmeters defnition
#Retrieve current voltage set to PZT as the initial VX0,VY0
socket.send("CheckVolt 5")
V0y = float((socket.recv()).split()[-1])
socket.send("CheckVolt 6")
V0x = float((socket.recv()).split()[-1])
Exemple #6
0
    message = socket.recv()


def getCount(miniusb, channel=0, average=10):
    counts = []
    for i in range(average):
        counts.append(miniusb.get_counts(channel))
    average_count = np.mean(counts)
    return average_count


##############################################################################################

#initial miniusb counter device
address_port = '/dev/serial/by-id/usb-Centre_for_Quantum_Technologies_USB_Counter_Ucnt-QO10-if00'
miniusb = Countercomm(address_port)

#Parmeters defnition
#Retrieve current voltage set to PZT as the initial VX0,VY0
socket.send("CheckVolt 5")
V0y = float((socket.recv()).split()[-1])
socket.send("CheckVolt 6")
V0x = float((socket.recv()).split()[-1])
print('Initial Voltage: ', V0x, V0y)
#Scanning
rangeVx = 0.1
rangeVy = 0.1
stepV = 0.01
numStepx = int(rangeVx / stepV)
numStepy = int(rangeVy / stepV)
xgrid = np.arange(-numStepx, numStepx + 1) * stepV