Exemplo n.º 1
0
def main():
    """ Main function """
    ps1 = cpx.CPX400DPDriver(1, device='/dev/ttyACM0', interface='serial')
    ps2 = cpx.CPX400DPDriver(2, device='/dev/ttyACM0', interface='serial')
    isotech = ips.IPS('/dev/serial/by-id/' +
                      'usb-Prolific_Technology_Inc._USB-Serial_Controller-if00-port0')

    pullsocket = DateDataPullSocket('vhp_temp_control',
                                    ['setpoint', 'dutycycle', 'pid_p', 'pid_i', 'pid_e'],
                                    timeouts=[999999, 3.0, 3.0, 3.0, 3.0],
                                    port=9001)
    pullsocket.start()

    pushsocket = DataPushSocket('vhp_push_control', action='store_last')
    pushsocket.start()

    power_calc = PowerCalculatorClass(pullsocket, pushsocket)
    power_calc.daemon = True
    power_calc.start()

    heater = HeaterClass(power_calc, pullsocket, ps1, ps2, isotech)
    heater.start()

    tui = CursesTui(heater, ps1)
    tui.daemon = True
    tui.start()
Exemplo n.º 2
0
def main():
    """ Main function """
    power_supplies = {}
    for i in range(1, 3):
        power_supplies[i] = cpx.CPX400DPDriver(
            i,
            interface='lan',
            hostname='cinf-palle-heating-ps',
            tcp_port=9221)
        power_supplies[i].set_voltage(0)
        power_supplies[i].output_status(True)

    codenames = [
        'setpoint', 'wanted_voltage', 'actual_voltage_1', 'actual_voltage_2',
        'actual_current_1', 'actual_current_2', 'power', 'temperature'
    ]
    pullsocket = DateDataPullSocket(
        'palle_temp_control',
        codenames,
        timeouts=[999999, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0])
    pullsocket.start()

    pushsocket = DataPushSocket('mgw_push_control', action='store_last')
    pushsocket.start()

    power_calculator = PowerCalculatorClass(pullsocket, pushsocket)
    power_calculator.daemon = True
    power_calculator.start()

    heater = HeaterClass(power_calculator, pullsocket, power_supplies)
    heater.start()

    tui = CursesTui(heater)
    tui.daemon = True
    tui.start()
Exemplo n.º 3
0
def main():
    """ Main function """
    valve_names = [0] * 20
    for i in range(0, 20):
        valve_names[i] = str(i + 1)

    try:
        name = chr(0x03BC) # Python 3
    except ValueError:
        name = unichr(0x03BC) # Python 2

    pullsocket = DateDataPullSocket(name + '-reacor Valvecontrol',
                                    valve_names, timeouts=[2]*20)
    pullsocket.start()

    pushsocket = DataPushSocket(name + '-reactor valve control',
                                action='enqueue')
    pushsocket.start()

    valve_controller = valve_control.ValveControl(valve_names, pullsocket, pushsocket)
    valve_controller.start()

    while True:
        time.sleep(1)

    valve_controller.running = False
Exemplo n.º 4
0
def main():
    """ Main function """
    power_supplies = {}
    for i in range(1, 3):
        power_supplies[i] = cpx.CPX400DPDriver(i, interface='lan',
                                               hostname='cinf-palle-heating-ps',
                                               tcp_port=9221)
        power_supplies[i].set_voltage(0)
        power_supplies[i].output_status(True)

    codenames = ['setpoint', 'wanted_voltage', 'actual_voltage_1', 'actual_voltage_2',
                 'actual_current_1', 'actual_current_2', 'power', 'temperature']
    pullsocket = DateDataPullSocket('palle_temp_control', codenames,
                                    timeouts=[999999, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0])
    pullsocket.start()

    pushsocket = DataPushSocket('mgw_push_control', action='store_last')
    pushsocket.start()

    power_calculator = PowerCalculatorClass(pullsocket, pushsocket)
    power_calculator.daemon = True
    power_calculator.start()

    heater = HeaterClass(power_calculator, pullsocket, power_supplies)
    heater.start()

    tui = CursesTui(heater)
    tui.daemon = True
    tui.start()
Exemplo n.º 5
0
class FlowControl(threading.Thread):
    """ Keep updated values of the current flow """
    def __init__(self, mks_instance, mfcs, devices, name):
        threading.Thread.__init__(self)
        self.mfcs = mfcs
        self.mks = mks_instance
        self.pullsocket = DateDataPullSocket(name, devices, timeouts=3.0, port=9000)
        self.pullsocket.start()

        self.pushsocket = DataPushSocket(name, action='enqueue')
        self.pushsocket.start()

        self.livesocket = LiveSocket(name, devices)
        self.livesocket.start()
        self.running = True

    def run(self):
        while self.running:
            time.sleep(0.1)
            qsize = self.pushsocket.queue.qsize()
            while qsize > 0:
                element = self.pushsocket.queue.get()
                mfc = list(element.keys())[0]
                print(element[mfc])
                print('Queue: ' + str(qsize))
                self.mks.set_flow(element[mfc], self.mfcs[mfc])
                qsize = self.pushsocket.queue.qsize()

            for mfc in self.mfcs:
                print('!!!')
                flow = self.mks.read_flow(self.mfcs[mfc])
                print(mfc + ': ' + str(flow))
                self.pullsocket.set_point_now(mfc, flow)
                self.livesocket.set_point_now(mfc, flow)
Exemplo n.º 6
0
def main():
    port_brooks = '/dev/serial/by-id/usb-FTDI_USB-RS485_Cable_FTWDN166-if00-port0'
    devices = ['3F2320902001', '3F2320901001']
    datasocket = DateDataPullSocket('palle_mfc_control', devices,
                                    timeouts=[3.0, 3.0], port=9000)
    datasocket.start()

    pushsocket = DataPushSocket('palle_brooks_push_control', action='enqueue')
    pushsocket.start()

    i = 0
    mfcs = {}
    for i in range(0, 2):
        device = devices[i]
        mfcs[device] = brooks.Brooks(device, port=port_brooks)
        print(mfcs[device].long_address)
        print(mfcs[device].read_flow())

    fc = FlowControl(mfcs, datasocket, pushsocket)
    fc.start()

    while fc.running:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            fc.running = False
            print('stopping, waiting for 2 sek')
            time.sleep(2)
    print('stopped')
Exemplo n.º 7
0
class GC_start_stop_socket(object):

    def __init__(self):
        self.state = 'alive'
        self.injection_number = 0
        self.relay = Relay()
        
        self.name = 'Sigrun_GC_start_stop'
        self.socket = DataPushSocket(self.name,
                                     action='callback_direct',
                                     callback=self.callback,
                                     return_format='json',
                                     port=8502)
        self.socket.start()

    def callback(self, data):
        method_name = data.pop('method')
        method = self.__getattribute__(method_name)
        return method(**data)

    def stop(self):
        self.socket.stop()

    def start_run(self):
        self.relay.start_GC()
        print('Starting GC on '+time.ctime())
        self.run_in_progress = True
    
    def stop_run(self):
        self.relay.stop_GC()
        print('Stopping GC on '+time.ctime())
        self.run_in_progress = False
Exemplo n.º 8
0
def main():
    """ Main function """
    port = '/dev/serial/by-id/usb-FTDI_USB-RS485_Cable_FTWGRR44-if00-port0'
    devices = ['F25600004', 'F25600005', 'F25600006', 'F25600001', 'F25600002',
               'F25600003', 'F25698001']
    datasocket = DateDataPullSocket('vhp_mfc_control',
                                    devices,
                                    timeouts=[3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0],
                                    port=9000)
    datasocket.start()

    pushsocket = DataPushSocket('vhp_push_control', action='enqueue')
    pushsocket.start()

    i = 0
    mfcs = {}
    for device in devices:
        mfcs[device] = brooks.Brooks(device, port=port)
        print(mfcs[device].read_flow())

    fc = FlowControl(mfcs, datasocket, pushsocket)
    fc.start()

    while True:
        time.sleep(0.5)
Exemplo n.º 9
0
def main():
    """ Main function """
    wp.wiringPiSetup()
    datasocket = DateDataPullSocket('furnaceroom_controller',
                                    ['temperature', 'setpoint', 'dutycycle', 'pid_p', 'pid_i'],
                                    timeouts=999999, port=9000)
    datasocket.start()

    pushsocket = DataPushSocket('furnaceroom_push_control', action='store_last')
    pushsocket.start()

    power_calculator = PowerCalculatorClass(datasocket, pushsocket)
    power_calculator.daemon = True
    power_calculator.start()

    heater = HeaterClass(power_calculator, datasocket)
    heater.start()

    tui = CursesTui(heater)
    tui.daemon = True
    tui.start()
    # make sure tui close down properly and the T setpoint is put low.
    try:
        while not heater.quit:
            time.sleep(1)
    except KeyboardInterrupt:
        print("Keyboard Interrupt detected, closing program")
        heater.quit = True
    finally:
        power_calculator.quit = True
        time.sleep(0.1)
        tui.stop()
Exemplo n.º 10
0
def main():
    """ Main function """
    valve_names = [0] * 20
    for i in range(0, 20):
        valve_names[i] = str(i + 1)

    try: # Python 3
        name = chr(0x03BC)
    except ValueError:  # Python 2
        name = unichr(0x03BC) # pylint: disable=undefined-variable

    pullsocket = DateDataPullSocket(name + '-reacorNG valve control',
                                    valve_names, timeouts=[2]*20)
    pullsocket.start()

    pushsocket = DataPushSocket(name + '-reactorNG valve control',
                                action='enqueue')
    pushsocket.start()

    valve_controller = valve_control.ValveControl(valve_names, pullsocket, pushsocket)
    valve_controller.start()

    while True:
        time.sleep(1)

    valve_controller.running = False
Exemplo n.º 11
0
def main():
    """ Main function """
    ps1 = cpx.CPX400DPDriver(1, device='/dev/ttyACM0', interface='serial')
    ps2 = cpx.CPX400DPDriver(2, device='/dev/ttyACM0', interface='serial')
    isotech = ips.IPS(
        '/dev/serial/by-id/' +
        'usb-Prolific_Technology_Inc._USB-Serial_Controller-if00-port0')

    pullsocket = DateDataPullSocket(
        'vhp_temp_control',
        ['setpoint', 'dutycycle', 'pid_p', 'pid_i', 'pid_e'],
        timeouts=[999999, 3.0, 3.0, 3.0, 3.0],
        port=9001)
    pullsocket.start()

    pushsocket = DataPushSocket('vhp_push_control', action='store_last')
    pushsocket.start()

    power_calc = PowerCalculatorClass(pullsocket, pushsocket)
    power_calc.daemon = True
    power_calc.start()

    heater = HeaterClass(power_calc, pullsocket, ps1, ps2, isotech)
    heater.start()

    tui = CursesTui(heater, ps1)
    tui.daemon = True
    tui.start()
Exemplo n.º 12
0
 def test_init_enqueue(self):
     """Test initialization with when action is enqueue"""
     dps = DataPushSocket(NAME, action='enqueue')
     dps.start()
     self.init_common_tests(dps, 'enqueue')
     assert(isinstance(dps.queue, Queue.Queue))
     assert(DATA[PORT]['queue'] is dps.queue)
     dps.stop()
Exemplo n.º 13
0
class I2CComm(object):
    """Handle I2C communication with DAC and ADC"""
    def __init__(self):
        self.measurements = {}
        self.mcp3428 = MCP3428()
        self.ad5667 = AD5667()
        self.dps = DataPushSocket(
            'large_CO2_mea_push_socket',
            action='callback_direct',
            callback=self.communicate,
            return_format='json',
        )

        # These two queues form a pair, that is used to interrupt the
        # continuous measurement of the values from the ADC to allow
        # setting the DAC and return copy the values from the ADC
        self.comm_flag = Queue()
        self.comm_return = Queue()

        self.dps.start()

    def measure(self):
        """Main measure loop (busy)"""
        while True:
            for channel in range(1, 5):
                self.measurements[channel] = self.mcp3428.read_sample(channel)
            print("Measured:", self.measurements)

            try:
                values_to_set = self.comm_flag.get(block=False)
            except Empty:
                continue

            # If we escaped the comm_flag test, we should set and enqueue current values
            if "no_voltages_to_set" not in values_to_set:
                print("Asked to set DAC values", values_to_set)
                sleep(0.01)
                self.ad5667.set_channel_A(values_to_set['A'])
                self.ad5667.set_channel_B(values_to_set['B'])
                sleep(0.01)
            self.comm_return.put(dict(self.measurements))

    def run(self):
        """Run method used to allow keyboard interrupts"""
        try:
            self.measure()
        except KeyboardInterrupt:
            self.dps.stop()

    def communicate(self, data):
        """Send the received values to be written on the DAC and return values
        from the ADC

        """
        self.comm_flag.put(data)
        # Wait until we get values back
        result = self.comm_return.get()
        return result
class IonOpticsControl(threading.Thread):
    """ Main optics control """
    def __init__(self, port, name, lenses):
        threading.Thread.__init__(self)
        name = name + '_ion_optics'
        self.pullsocket = DateDataPullSocket(name, lenses, timeouts=20.0)
        self.pullsocket.start()
        self.pushsocket = DataPushSocket(name, action='enqueue')
        self.pushsocket.start()
        self.ion_optics = stahl_hv_400.StahlHV400(port)
        self.lenses = lenses
        self.set_voltages = {}
        self.actual_voltages = {}
        for lens in self.lenses:
            self.set_voltages[lens] = 0
            self.actual_voltages[lens] = 0
        self.status = {}
        self.status['channel_status'] = {}
        for i in range(1, len(self.lenses) + 1):
            self.status['channel_status'][i] = False
        self.status['temperature'] = None
        self.status['output_error'] = None
        self.quit = False

    def run(self):
        current_lens = 1
        while not self.quit:
            self.status['temperature'] = self.ion_optics.read_temperature()
            if self.status['temperature'] > 50:
                for lens in self.lenses:
                    self.set_voltages[lens] = 0

            self.status[
                'channel_status'] = self.ion_optics.check_channel_status()
            self.status['output_error'] = False in self.status[
                'channel_status']

            actual_voltage = self.ion_optics.query_voltage(current_lens)
            self.actual_voltages[self.lenses[current_lens -
                                             1]] = actual_voltage
            self.pullsocket.set_point_now(self.lenses[current_lens - 1],
                                          actual_voltage)

            if current_lens == len(self.lenses):
                current_lens = 1
            else:
                current_lens += 1

            qsize = self.pushsocket.queue.qsize()
            while qsize > 0:
                element = self.pushsocket.queue.get()
                lens = str(list(element.keys())[0])
                value = element[lens]
                self.set_voltages[lens] = value
                channel_number = self.lenses.index(lens) + 1
                self.ion_optics.set_voltage(channel_number, value)
                qsize = self.pushsocket.queue.qsize()
            time.sleep(0.1)
Exemplo n.º 15
0
 def test_init_callback_direct_default(self):
     """Test initialization when action is callback_direct"""
     # Test init of callback
     dps = DataPushSocket(NAME, action='callback_direct', callback=dir)
     dps.start()
     self.init_common_tests(dps, 'callback_direct')
     assert(DATA[PORT]['callback'] is dir)
     assert(DATA[PORT]['return_format'] == 'json')
     dps.stop()
Exemplo n.º 16
0
 def test_init_custom_queue(self):
     """Test initialization when action is enqueue and use custom queue"""
     queue = Queue.Queue()
     dps = DataPushSocket(NAME, action='enqueue', queue=queue)
     dps.start()
     self.init_common_tests(dps, 'enqueue')
     assert(dps.queue is queue)
     assert(DATA[PORT]['queue'] is queue)
     dps.stop()
Exemplo n.º 17
0
def main():
    """ Main function """
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.settimeout(1)
    try:
        network_adress = 'rasppi12'
        command = 'microreactorng_temp_sample#raw'.encode()
        sock.sendto(command, (network_adress, 9000))
        received = sock.recv(1024)
        received = received.decode('ascii')
        if received == 'OLD_DATA':
            raise RuntimeError(
                "Received OLD_DATA from rasppi12. Please check it.")
        start_temp = float(received[received.find(',') + 1:])
        agilent_hostname = '10.54.6.56'
        rtd_reader = RtdReader(agilent_hostname, start_temp)
    except socket.timeout:
        print('Could not find rasppi12')
        exit()
    rtd_reader.daemon = True
    rtd_reader.start()
    time.sleep(1)

    power_supply = {}
    for k in range(1, 3):
        power_supply[k] = cpx.CPX400DPDriver(
            k,
            interface='lan',
            hostname='surfcat-stm312-heating-ps',
            tcp_port=9221)
        power_supply[k].set_voltage(0)
        power_supply[k].output_status(True)

    codenames = [
        'setpoint', 'wanted_voltage', 'actual_voltage_1', 'actual_voltage_2',
        'actual_current_1', 'actual_current_2', 'power', 'temperature'
    ]
    pullsocket = DateDataPullSocket(
        MICRO + '-reactorng_temp_control',
        codenames,
        timeouts=[999999, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0])
    pullsocket.start()

    pushsocket = DataPushSocket(MICRO + '-reactorng_push_control',
                                action='store_last')
    pushsocket.start()

    power_calculator = PowerCalculatorClass(pullsocket, pushsocket, rtd_reader)
    power_calculator.daemon = True
    power_calculator.start()

    heater = HeaterClass(power_calculator, pullsocket, power_supply)
    heater.start()

    tui_class = CursesTui(heater)
    tui_class.start()
    LOGGER.info('script ended')
 def test_init_callback_direct_raw(self):
     """Test initialization when action is callback_direct"""
     # Test init of callback
     dps_ = DataPushSocket(NAME, action='callback_direct', callback=dir,
                          return_format='raw')
     dps_.start()
     self.init_common_tests(dps_, 'callback_direct')
     assert DATA[PORT]['callback'] is dir
     assert DATA[PORT]['return_format'] == 'raw'
     dps_.stop()
Exemplo n.º 19
0
def main():
    """ Main function """
    wp.wiringPiSetup()
    datasocket = DateDataPullSocket(
        'furnaceroom_controller',
        ['temperature', 'setpoint', 'dutycycle', 'pid_p', 'pid_i'],
        timeouts=999999,
        port=9000)
    datasocket.start()

    pushsocket = DataPushSocket('furnaceroom_push_control',
                                action='store_last')
    pushsocket.start()

    power_calculator = PowerCalculatorClass(datasocket, pushsocket)
    power_calculator.daemon = True
    power_calculator.start()

    codenames = [
        'fr307_furnace_1_dutycycle', 'fr307_furnace_1_S',
        'fr307_furnace_1_pid_p', 'fr307_furnace_1_pid_i'
    ]
    db_logger = ContinuousDataSaver(
        continuous_data_table='dateplots_furnaceroom307',
        username=credentials.user,
        password=credentials.passwd,
        measurement_codenames=codenames)
    db_logger.start()

    # Criterium checker
    criterium_checker = LoggingCriteriumChecker(
        codenames=codenames,
        types=['lin'] * len(codenames),
        criteria=[0.1, 0.99, 1., 1.],
        time_outs=[60, 600, 300, 300],
    )

    heater = HeaterClass(power_calculator, datasocket, db_logger,
                         criterium_checker)
    heater.start()

    tui = CursesTui(heater)
    tui.daemon = True
    tui.start()
    # make sure tui close down properly and the T setpoint is put low.
    try:
        while not heater.quit:
            time.sleep(1)
    except KeyboardInterrupt:
        print("Keyboard Interrupt detected, closing program")
        heater.quit = True
    finally:
        power_calculator.quit = True
        time.sleep(0.1)
        tui.stop()
Exemplo n.º 20
0
def dps(request):
    """DataPushSocket fixture, if requested in a class that has dps_kwargs
    class variable, use those in init
    """
    if hasattr(request, 'cls') and hasattr(request.cls, 'dps_kwargs'):
        dps = DataPushSocket(NAME, **request.cls.dps_kwargs)
    else:
        dps = DataPushSocket(NAME)
    dps.start()
    yield dps
    dps.stop()
Exemplo n.º 21
0
 def test_init_callback_async(self):
     """Test initialization when action is callback_async"""
     # Test init of callback
     dps = DataPushSocket(NAME, action='callback_async', callback=dir)
     dps.start()
     self.init_common_tests(dps, 'callback_async')
     assert(isinstance(dps.queue, Queue.Queue))
     assert(DATA[PORT]['queue'] is dps.queue)  
     assert(isinstance(dps._callback_thread, CallBackThread))
     assert(dps._callback_thread.callback is dir)
     dps.stop()
Exemplo n.º 22
0
class IonOpticsControl(threading.Thread):
    """ Main optics control """
    def __init__(self, port, name, lenses):
        threading.Thread.__init__(self)
        name = name + '_ion_optics'
        self.pullsocket = DateDataPullSocket(name, lenses, timeouts=20.0)
        self.pullsocket.start()
        self.pushsocket = DataPushSocket(name, action='enqueue')
        self.pushsocket.start()
        self.ion_optics = stahl_hv_400.StahlHV400(port)
        self.lenses = lenses
        self.set_voltages = {}
        self.actual_voltages = {}
        for lens in self.lenses:
            self.set_voltages[lens] = 0
            self.actual_voltages[lens] = 0
        self.status = {}
        self.status['channel_status'] = {}
        for i in range(1, len(self.lenses)+1):
            self.status['channel_status'][i] = False
        self.status['temperature'] = None
        self.status['output_error'] = None
        self.quit = False

    def run(self):
        current_lens = 1
        while not self.quit:
            self.status['temperature'] = self.ion_optics.read_temperature()
            if self.status['temperature'] > 50:
                for lens in self.lenses:
                    self.set_voltages[lens] = 0

            self.status['channel_status'] = self.ion_optics.check_channel_status()
            self.status['output_error'] = False in self.status['channel_status']

            actual_voltage = self.ion_optics.query_voltage(current_lens)
            self.actual_voltages[self.lenses[current_lens-1]] = actual_voltage
            self.pullsocket.set_point_now(self.lenses[current_lens-1], actual_voltage)

            if current_lens == len(self.lenses):
                current_lens = 1
            else:
                current_lens += 1

            qsize = self.pushsocket.queue.qsize()
            while qsize > 0:
                element = self.pushsocket.queue.get()
                lens = str(list(element.keys())[0])
                value = element[lens]
                self.set_voltages[lens] = value
                channel_number = self.lenses.index(lens) + 1
                self.ion_optics.set_voltage(channel_number, value)
                qsize = self.pushsocket.queue.qsize()
            time.sleep(0.1)
def main():
    """ Main function """
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.settimeout(1)
    try:
        temperature_string = 'mr_sample_tc_temperature#raw'
        sock.sendto(temperature_string.encode('ascii'), ('rasppi12', 9000))
        received = sock.recv(1024).decode('ascii')
        start_temp = float(received[received.find(',') + 1:])
    except socket.gaierror:
        print('Could not find rasppi12')
        exit()
    except ValueError:
        print('Bad reply from rasppi12')
        exit()
    rtd_reader = RtdReader(start_temp)

    rtd_reader.daemon = True
    rtd_reader.start()
    time.sleep(1)

    power_supply = {}
    for k in range(1, 3):
        power_supply[k] = cpx.CPX400DPDriver(k,
                                             interface='serial',
                                             device='/dev/ttyACM0')
        power_supply[k].set_voltage(0)
        power_supply[k].output_status(True)

    codenames = [
        'setpoint', 'wanted_voltage', 'actual_voltage_1', 'actual_voltage_2',
        'actual_current_1', 'actual_current_2', 'power', 'temperature'
    ]
    pullsocket = DateDataPullSocket(
        MICRO + '-reactor_temp_control',
        codenames,
        timeouts=[999999, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0])
    pullsocket.start()

    pushsocket = DataPushSocket(MICRO + '-reactor push control',
                                action='store_last')
    pushsocket.start()

    power_calculator = PowerCalculatorClass(pullsocket, pushsocket, rtd_reader)
    power_calculator.daemon = True
    power_calculator.start()

    heater = HeaterClass(power_calculator, pullsocket, power_supply)
    heater.start()

    tui = CursesTui(heater)
    tui.daemon = True
    tui.start()
def main():
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.settimeout(1)
    try:
        temperature_string = 'mr_sample_tc_temperature#raw'
        sock.sendto(temperature_string.encode('ascii'), ('rasppi12', 9000))
        received = sock.recv(1024).decode('ascii')
        start_temp = float(received[received.find(',') + 1:])
        agilent_hostname = '10.54.6.79'
        rtd_reader = RtdReader(agilent_hostname, start_temp)
    except:
        print('Could not find rasppi12')
        exit()

    rtd_reader.daemon = True
    rtd_reader.start()
    time.sleep(1)

    PS = {}
    for k in range(1, 3):
        PS[k] = cpx.CPX400DPDriver(k,
                                   interface='serial',
                                   device='/dev/ttyACM0')
        PS[k].set_voltage(0)
        PS[k].output_status(True)

    try:
        micro = chr(0x03BC)  # Python 3
    except ValueError:
        micro = unichr(0x03BC)  # Python 2
    Pullsocket = DateDataPullSocket(micro + 'reactor temp_control',
                                    ['setpoint', 'voltage', 'temperature'],
                                    timeouts=[999999, 3.0, 3.0],
                                    port=9000)
    Pullsocket.start()

    Pushsocket = DataPushSocket(micro + '-reactor push control',
                                action='store_last')
    Pushsocket.start()

    pcc = PowerCalculatorClass(Pullsocket, Pushsocket, rtd_reader)
    pcc.daemon = True
    pcc.start()

    heater = HeaterClass(P, Pullsocket, PS)
    heater.start()

    tui = CursesTui(H)
    tui.daemon = True
    tui.start()
Exemplo n.º 25
0
def main():
    """ Main function """
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.settimeout(1)
    try:
        network_adress = 'rasppi12'
        command = 'microreactorng_temp_sample#raw'.encode()
        sock.sendto(command, (network_adress, 9000))
        received = sock.recv(1024)
        received = received.decode('ascii')
        if received == 'OLD_DATA':
            raise RuntimeError("Received OLD_DATA from rasppi12. Please check it.")
        start_temp = float(received[received.find(',') + 1:])
        agilent_hostname = '10.54.6.56'
        rtd_reader = RtdReader(agilent_hostname, start_temp)
    except socket.timeout:
        print('Could not find rasppi12')
        exit()
    rtd_reader.daemon = True
    rtd_reader.start()
    time.sleep(1)

    power_supply = {}
    for k in range(1, 3):
        power_supply[k] = cpx.CPX400DPDriver(k, interface='lan',
                                             hostname='surfcat-stm312-heating-ps',
                                             tcp_port=9221)
        power_supply[k].set_voltage(0)
        power_supply[k].output_status(True)

    codenames = ['setpoint', 'wanted_voltage', 'actual_voltage_1', 'actual_voltage_2',
                 'actual_current_1', 'actual_current_2', 'power', 'temperature']
    pullsocket = DateDataPullSocket(MICRO + '-reactorng_temp_control', codenames,
                                    timeouts=[999999, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0])
    pullsocket.start()
    
    pushsocket = DataPushSocket(MICRO + '-reactorng_push_control', action='store_last')
    pushsocket.start()

    power_calculator = PowerCalculatorClass(pullsocket, pushsocket, rtd_reader)
    power_calculator.daemon = True
    power_calculator.start()

    heater = HeaterClass(power_calculator, pullsocket, power_supply)
    heater.start()

    tui_class = CursesTui(heater)
    tui_class.start()
    LOGGER.info('script ended')
Exemplo n.º 26
0
def main():
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.settimeout(1)
    try:
        temperature_string = 'mr_sample_tc_temperature#raw'
        sock.sendto(temperature_string.encode('ascii'), ('rasppi12', 9000))
        received = sock.recv(1024).decode('ascii')
        start_temp = float(received[received.find(',') + 1:])
        agilent_hostname = '10.54.6.79'
        rtd_reader = RtdReader(agilent_hostname, start_temp)
    except:
        print('Could not find rasppi12')
        exit()

    rtd_reader.daemon = True
    rtd_reader.start()
    time.sleep(1)

    PS = {}
    for k in range(1, 3):
        PS[k] = cpx.CPX400DPDriver(k, interface='serial', device='/dev/ttyACM0')
        PS[k].set_voltage(0)
        PS[k].output_status(True)

    try:
        micro = chr(0x03BC) # Python 3
    except ValueError:
        micro = unichr(0x03BC) # Python 2
    Pullsocket = DateDataPullSocket(micro + 'reactor temp_control',
                                    ['setpoint', 'voltage', 'temperature'], 
                                    timeouts=[999999, 3.0, 3.0],
                                    port=9000)
    Pullsocket.start()

    Pushsocket = DataPushSocket(micro + '-reactor push control', action='store_last')
    Pushsocket.start()

    pcc = PowerCalculatorClass(Pullsocket, Pushsocket, rtd_reader)
    pcc.daemon = True
    pcc.start()

    heater = HeaterClass(pcc, Pullsocket, PS)
    heater.start()

    tui = CursesTui(heater)
    tui.daemon = True
    tui.start()
Exemplo n.º 27
0
def main():
    """ Main function """
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.settimeout(1)
    try:
        temperature_string = 'mr_sample_tc_temperature#raw'
        sock.sendto(temperature_string.encode('ascii'), ('rasppi12', 9000))
        received = sock.recv(1024).decode('ascii')
        start_temp = float(received[received.find(',') + 1:])
    except socket.gaierror:
        print('Could not find rasppi12')
        exit()
    except ValueError:
        print('Bad reply from rasppi12')
        exit()
    rtd_reader = RtdReader(start_temp)

    rtd_reader.daemon = True
    rtd_reader.start()
    time.sleep(1)

    power_supply = {}
    for k in range(1, 3):
        power_supply[k] = cpx.CPX400DPDriver(k, interface='serial', device='/dev/ttyACM0')
        power_supply[k].set_voltage(0)
        power_supply[k].output_status(True)

    codenames = ['setpoint', 'wanted_voltage', 'actual_voltage_1', 'actual_voltage_2',
                 'actual_current_1', 'actual_current_2', 'power', 'temperature']
    pullsocket = DateDataPullSocket(MICRO + '-reactor_temp_control', codenames,
                                    timeouts=[999999, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0])
    pullsocket.start()

    pushsocket = DataPushSocket(MICRO + '-reactor push control', action='store_last')
    pushsocket.start()

    power_calculator = PowerCalculatorClass(pullsocket, pushsocket, rtd_reader)
    power_calculator.daemon = True
    power_calculator.start()

    heater = HeaterClass(power_calculator, pullsocket, power_supply)
    heater.start()

    tui = CursesTui(heater)
    tui.daemon = True
    tui.start()
Exemplo n.º 28
0
def class_dps(request):
    """DataPushSocket fixture

    If requested in a class that has dps_kwargs class variable, use those in init

    .. note:: This fixture lasts for the duration of a test class. It has been setup like this
       because the sockets take a long time to shut down. So in order to save time on the test
       run, we only initiate the socket once per class, but reset it in the dps fixture. It is
       the dps fixture that should be used.

    """
    if hasattr(request, 'cls') and hasattr(request.cls, 'dps_kwargs'):
        dps_ = DataPushSocket(NAME, **request.cls.dps_kwargs)
    else:
        dps_ = DataPushSocket(NAME)
    dps_.start()
    yield dps_
    dps_.stop()
Exemplo n.º 29
0
class FlowControl(threading.Thread):
    """ Keep updated values of the current flow or pressure """
    def __init__(self, mfcs, name):
        threading.Thread.__init__(self)
        self.daemon = True
        self.mfcs = mfcs
        print(mfcs)
        devices = list(self.mfcs.keys())
        self.values = {}
        for device in devices:
            self.values[device] = None

        self.pullsocket = DateDataPullSocket(name + '_analog_control', devices,
                                             timeouts=[3.0] * len(devices))
        self.pullsocket.start()

        self.pushsocket = DataPushSocket(name + '_analog_pc_control', action='enqueue')
        self.pushsocket.start()

        self.livesocket = LiveSocket(name + '_analog_mfc_control', devices)
        self.livesocket.start()
        self.running = True

    def value(self, device):
        """ Return the current value of a device """
        return self.values[device]

    def run(self):
        while self.running:
            time.sleep(0.1)
            qsize = self.pushsocket.queue.qsize()
            while qsize > 0:
                print('queue-size: ' + str(qsize))
                element = self.pushsocket.queue.get()
                mfc = list(element.keys())[0]
                self.mfcs[mfc].set_flow(element[mfc])
                qsize = self.pushsocket.queue.qsize()

            for mfc in self.mfcs:
                flow = self.mfcs[mfc].read_flow()
                self.values[mfc] = flow
                print(flow)
                self.pullsocket.set_point_now(mfc, flow)
                self.livesocket.set_point_now(mfc, flow)
Exemplo n.º 30
0
def main():
    """ Main function """
    valve_names = [0] * 20
    for i in range(0, 20):
        valve_names[i] = str(i + 1)

    pullsocket = DateDataPullSocket('VHP Valvecontrol', valve_names, timeouts=[2]*20)
    pullsocket.start()

    pushsocket = DataPushSocket('VHP calve control', action='enqueue')
    pushsocket.start()

    valve_controller = valve_control.ValveControl(valve_names, pullsocket, pushsocket)
    valve_controller.start()

    while True:
        time.sleep(1)

    valve_controller.running = False
Exemplo n.º 31
0
def main():
    """ Main function """
    wp.wiringPiSetup()
    datasocket = DateDataPullSocket('furnaceroom_controller',
                                    ['temperature', 'setpoint', 'dutycycle', 'pid_p', 'pid_i'],
                                    timeouts=999999, port=9000)
    datasocket.start()

    pushsocket = DataPushSocket('furnaceroom_push_control', action='store_last')
    pushsocket.start()

    power_calculator = PowerCalculatorClass(datasocket, pushsocket)
    power_calculator.daemon = True
    power_calculator.start()

    heater = HeaterClass(power_calculator, datasocket)
    heater.start()

    tui = CursesTui(heater)
    tui.daemon = True
    tui.start()
Exemplo n.º 32
0
class FlowControl(threading.Thread):
    """ Keep updated values of the current flow """
    def __init__(self, mks_instance, mfcs, devices, name):
        threading.Thread.__init__(self)
        self.mfcs = mfcs
        self.mks = mks_instance
        self.pullsocket = DateDataPullSocket(name,
                                             devices,
                                             timeouts=3.0,
                                             port=9000)
        self.pullsocket.start()

        self.pushsocket = DataPushSocket(name, action='enqueue')
        self.pushsocket.start()

        self.livesocket = LiveSocket('palle_mks_flows', devices)
        self.livesocket.start()
        self.running = True

    def run(self):
        while self.running:
            time.sleep(0.1)
            qsize = self.pushsocket.queue.qsize()
            while qsize > 0:
                element = self.pushsocket.queue.get()
                mfc = list(element.keys())[0]
                print(element[mfc])
                print('Queue: ' + str(qsize))
                self.mks.set_flow(element[mfc], self.mfcs[mfc])
                qsize = self.pushsocket.queue.qsize()

            for mfc in self.mfcs:
                print('!!!')
                flow = self.mks.read_flow(self.mfcs[mfc])
                print(mfc + ': ' + str(flow))
                self.pullsocket.set_point_now(mfc, flow)
                self.livesocket.set_point_now(mfc, flow)
Exemplo n.º 33
0
def main():
    """ Main function """
    power_supply = cpx.CPX400DPDriver(1, device='/dev/ttyACM0', interface='serial')
    power_supply.set_dual_output(False) # Synchronize outputs
    power_supply.set_current_limit(2.4)
    pullsocket = DateDataPullSocket('vhp_temp_control',
                                    ['setpoint', 'pid_p', 'pid_i', 'pid_e', 'voltage'],
                                    timeouts=[999999, 3.0, 3.0, 3.0, 3.0],
                                    port=9000)
    pullsocket.start()

    pushsocket = DataPushSocket('vhp_push_control', action='store_last')
    pushsocket.start()

    power_calc = PowerCalculatorClass(pullsocket, pushsocket)
    power_calc.daemon = True
    power_calc.start()

    heater = HeaterClass(power_calc, pullsocket, power_supply)
    heater.start()

    tui = CursesTui(heater, power_supply)
    tui.daemon = True
    tui.start()
Exemplo n.º 34
0
# pylint: disable=R0913,W0142,C0103

import time
import PyExpLabSys.common.valve_control as valve_control
from PyExpLabSys.common.sockets import DateDataPullSocket
from PyExpLabSys.common.sockets import DataPushSocket

valve_names = [0] * 20
for i in range(0, 20):
    valve_names[i] = str(i + 1)
Pullsocket = DateDataPullSocket('Palle, Valve control',
                                valve_names,
                                timeouts=[2] * 20)
Pullsocket.start()

Pushsocket = DataPushSocket('Palle, push control', action='enqueue')
Pushsocket.start()

vc = valve_control.ValveControl(valve_names, Pullsocket, Pushsocket)
vc.start()

while True:
    time.sleep(1)

vc.running = False
Exemplo n.º 35
0
class LaserControl(object):
    """Class that controls the giant laser laser on the moon"""
    def __init__(self):
        self.settings = {'power': 100, 'focus': 100, 'target': None}
        self._state = 'idle'
        self._stop = False
        self._temperature_meas = Queue.Queue()

        name = 'Laser control, callback socket, for giant laser on the moon'
        self.dps = DataPushSocket(name,
                                  action='callback_direct',
                                  callback=self.callback,
                                  return_format='json')
        self.dps.start()

    def callback(self, data):
        """Callback and central control function"""
        method_name = data.pop('method')  # Don't pass the method name on
        method = self.__getattribute__(method_name)
        return method(**data)

    def update_settings(self, **kwargs):
        """Update settings"""
        for key in kwargs.keys():
            if key not in self.settings.keys():
                message = 'Not settings for key: {}'.format(key)
                raise ValueError(message)
        self.settings.update(kwargs)
        print 'Updated settings with: {}'.format(kwargs)
        return 'Updated settings with: {}'.format(kwargs)

    def state(self, state):
        """Set state"""
        self._state = state
        print 'State set to: {}'.format(state)
        return 'State set to: {}'.format(state)

    def run(self):
        """Monitor state and run temperature monitorint"""
        print 'Run started'
        while not self._stop:
            if self._state == 'active':
                self.monitor_temperature()
            time.sleep(1)
        self.dps.stop()
        print 'Run ended'

    def stop(self):
        """Stop the laser controller"""
        self._state = 'idle'
        self._stop = True
        print 'Stopped'
        return 'Stopped'

    def monitor_temperature(self):
        """Monitor the temperature"""
        while self._state == 'active':
            item = [time.strftime('%Y-%m-%d %X'), random.randint(30, 300)]
            self._temperature_meas.put(item)
            print 'At {} temperature is {}'.format(*item)
            time.sleep(1)

    def get_temperature(self):
        """Get the temperature measurements available"""
        out = []
        for _ in range(self._temperature_meas.qsize()):
            out.append(self._temperature_meas.get())
        return out
Exemplo n.º 36
0
class I2CComm(object):
    """Handle I2C communication with DAC and ADC"""

    def __init__(self):
        self.measurements = {}
        self.mcp3428 = MCP3428()
        #self.ad5667 = AD5667()
        self.dps = DataPushSocket(
            'large_CO2_mea_push_socket',
            action='callback_direct',
            callback=self.communicate,
            return_format='json',
        )

        # These two queues form a pair, that is used to interrupt the
        # continuous measurement of the values from the ADC to allow
        # setting the DAC and return copy the values from the ADC
        self.comm_flag = Queue()
        self.comm_return = Queue()

        self.dps.start()

    def measure(self):
        """Main measure loop (busy)"""
        last_print = 0
        while True:
            for channel in range(1, 5):
                self.measurements[channel] = self.mcp3428.read_sample(channel)

            now = time()
            if (now - last_print) > 10:
                print(now, "Measured:", self.measurements)
                last_print = now

            try:
                values_to_set = self.comm_flag.get(block=False)
            except Empty:
                continue

            # If we escaped the comm_flag test, we should set and enqueue current values
            if "no_voltages_to_set" not in values_to_set:
                print("Asked to set DAC values", values_to_set)
                sleep(0.01)
                #self.ad5667.set_channel_A(values_to_set['A'])
                #self.ad5667.set_channel_B(values_to_set['B'])
                sleep(0.01)
            self.comm_return.put(dict(self.measurements))

    def run(self):
        """Run method used to allow keyboard interrupts"""
        try:
            self.measure()
        except KeyboardInterrupt:
            self.dps.stop()

    def communicate(self, data):
        """Send the received values to be written on the DAC and return values
        from the ADC

        """
        print("Received comm request")
        # Make sure there are no results hanging in the result queue
        while True:
            try:
                self.comm_return.get(block=False)
            except Empty:
                break

        print("Set comm flag")
        self.comm_flag.put(data)

        # Wait until we get values back
        result = self.comm_return.get()
        print("Got result, return")
        return result
Exemplo n.º 37
0
class LaserControl(object):
    """Class that controls the giant laser laser on the moon"""

    def __init__(self):
        self.settings = {'power': 100, 'focus': 100, 'target': None}
        self._state = 'idle'
        self._stop = False
        self._temperature_meas = Queue.Queue()
        
        name = 'Laser control, callback socket, for giant laser on the moon'
        self.dps = DataPushSocket(name, action='callback_direct',
                                  callback=self.callback,
                                  return_format='json')
        self.dps.start()

    def callback(self, data):
        """Callback and central control function"""
        method_name = data.pop('method')  # Don't pass the method name on
        method = self.__getattribute__(method_name)
        return method(**data)

    def update_settings(self, **kwargs):
        """Update settings"""
        for key in kwargs.keys():
            if key not in self.settings.keys():
                message = 'Not settings for key: {}'.format(key)
                raise ValueError(message)
        self.settings.update(kwargs)
        print 'Updated settings with: {}'.format(kwargs)
        return 'Updated settings with: {}'.format(kwargs)

    def state(self, state):
        """Set state"""
        self._state = state
        print 'State set to: {}'.format(state)
        return 'State set to: {}'.format(state)

    def run(self):
        """Monitor state and run temperature monitorint"""
        print 'Run started'
        while not self._stop:
            if self._state == 'active':
                self.monitor_temperature()
            time.sleep(1)
        self.dps.stop()
        print 'Run ended'

    def stop(self):
        """Stop the laser controller"""
        self._state = 'idle'
        self._stop = True
        print 'Stopped'
        return 'Stopped'

    def monitor_temperature(self):
        """Monitor the temperature"""
        while self._state == 'active':
            item = [time.strftime('%Y-%m-%d %X'), random.randint(30, 300)]
            self._temperature_meas.put(item)
            print 'At {} temperature is {}'.format(*item)
            time.sleep(1)

    def get_temperature(self):
        """Get the temperature measurements available"""
        out= []
        for _ in range(self._temperature_meas.qsize()):
            out.append(self._temperature_meas.get())
        return out
Exemplo n.º 38
0
class EmissionControl(threading.Thread):
    """ Control the emission of a filament. """
    def __init__(self):
        threading.Thread.__init__(self)
        channels = ['setpoint', 'emission', 'ionenergy']
        self.datasocket = DateDataPullSocket('emission_tof', channels,
                                             timeouts=[99999999, 1.0, 99999999])
        self.datasocket.start()
        self.pushsocket = DataPushSocket('tof-emission-push_control', action='enqueue')
        self.pushsocket.start()
        self.livesocket = LiveSocket('tof-emission', channels)
        self.livesocket.start()
        self.filament = {}
        port = '/dev/serial/by-id/usb-TTI_CPX400_Series_PSU_C2F952E5-if00'
        self.filament['device'] = CPX.CPX400DPDriver(1, interface='serial', device=port)
        self.filament['voltage'] = 0
        self.filament['current'] = 0
        self.filament['idle_voltage'] = 1.7
        self.filament['device'].set_current_limit(7)
        self.filament['device'].output_status(True)
        self.bias = {}
        port = '/dev/serial/by-id/'
        port += 'usb-Prolific_Technology_Inc._USB-Serial_Controller_D-if00-port0'
        self.keithley_port = port
        self.bias['device'] = smu.KeithleySMU(interface='serial', device=self.keithley_port)
        self.bias['grid_voltage'] = 0
        self.bias['grid_current'] = 0
        self.bias['device'].output_state(True)
        self.pid = pid.PID(0.01, 0.1, 0, 4)
        self.looptime = 0
        self.setpoint = 0.05
        self.pid.update_setpoint(self.setpoint)
        self.running = True
        self.wanted_voltage = 0
        self.emission_current = 999

    def set_bias(self, bias):
        """ Set the bias-voltage """
        if self.datasocket is not None:
            self.datasocket.set_point_now('ionenergy', bias)
        if bias > -1:
            self.bias['device'].set_voltage(bias)

    def update_setpoint(self, setpoint):
        """ Update the setpoint """
        self.setpoint = setpoint
        self.datasocket.set_point_now('setpoint', setpoint)
        self.livesocket.set_point_now('setpoint', setpoint)

    def set_filament_voltage(self, voltage):
        """ Set the filament voltage """
        return self.filament['device'].set_voltage(voltage)

    def read_filament_voltage(self):
        """ Read the filament voltage """
        return self.filament['device'].read_actual_voltage()

    def read_filament_current(self):
        """ Read the filament current """
        return self.filament['device'].read_actual_current()

    def read_grid_voltage(self):
        """Read the actual grid voltage """
        return self.bias['device'].read_voltage()

    def read_emission_current(self):
        """ Read the grid current as measured by power supply """
        emission_current = self.bias['device'].read_current()
        if emission_current is None:
            self.bias['device'] = smu.KeithleySMU(
                interface='serial',
                device=self.keithley_port,
                )
            time.sleep(0.1)
            emission_current = self.value()
            LOGGER.error('Emission current not read correctly - reset device')
        else:
            emission_current = emission_current * 1000
        return emission_current

    def read_grid_current(self):
        """ Read the actual emission current """
        return self.bias['device'].read_current()

    def value(self):
        """ Return the curren emission value """
        return self.emission_current

    def run(self):
        while self.running:
            #time.sleep(0.1)
            start_time = time.time()
            qsize = self.pushsocket.queue.qsize()
            LOGGER.debug('qsize: ' + str(qsize))
            while qsize > 0:
                element = self.pushsocket.queue.get()
                LOGGER.debug('Element: ' + str(element))
                param = list(element.keys())[0]
                if param == 'setpoint':
                    value = element[param]
                    self.update_setpoint(value)
                if param == 'bias':
                    value = element[param]
                    self.set_bias(value)
                qsize = self.pushsocket.queue.qsize()

            self.emission_current = self.read_emission_current()
            self.wanted_voltage = (self.pid.wanted_power(self.emission_current) +
                                   self.filament['idle_voltage'])
            self.pid.update_setpoint(self.setpoint)
            self.set_filament_voltage(self.wanted_voltage)
            self.filament['voltage'] = self.read_filament_voltage()
            self.filament['current'] = self.read_filament_current()
            self.bias['grid_voltage'] = self.read_grid_voltage()
            self.bias['grid_current'] = self.read_grid_current()
            self.datasocket.set_point_now('ionenergy', self.bias['grid_voltage'])
            self.datasocket.set_point_now('emission', self.emission_current)
            self.livesocket.set_point_now('emission', self.emission_current)
            self.looptime = time.time() - start_time
        self.setpoint = 0
        self.set_filament_voltage(0)
Exemplo n.º 39
0
class EmissionControl(threading.Thread):
    """ Control the emission of a filament. """
    def __init__(self):
        threading.Thread.__init__(self)
        channels = ['setpoint', 'emission', 'ionenergy']
        self.datasocket = DateDataPullSocket(
            'emission_tof', channels, timeouts=[99999999, 1.0, 99999999])
        self.datasocket.start()
        self.pushsocket = DataPushSocket('tof-emission-push_control',
                                         action='enqueue')
        self.pushsocket.start()
        self.livesocket = LiveSocket('tof-emission', channels, 1)
        self.livesocket.start()
        self.filament = {}
        port = '/dev/serial/by-id/usb-TTI_CPX400_Series_PSU_C2F952E5-if00'
        self.filament['device'] = CPX.CPX400DPDriver(1,
                                                     interface='serial',
                                                     device=port)
        self.filament['voltage'] = 0
        self.filament['current'] = 0
        self.filament['idle_voltage'] = 1.7
        self.filament['device'].set_current_limit(7)
        self.filament['device'].output_status(True)
        self.bias = {}
        port = '/dev/serial/by-id/'
        port += 'usb-Prolific_Technology_Inc._USB-Serial_Controller_D-if00-port0'
        self.keithley_port = port
        self.bias['device'] = smu.KeithleySMU(interface='serial',
                                              device=self.keithley_port)
        self.bias['grid_voltage'] = 0
        self.bias['grid_current'] = 0
        self.bias['device'].output_state(True)
        self.pid = pid.PID(0.01, 0.1, 0, 4)
        self.looptime = 0
        self.setpoint = 0.05
        self.pid.update_setpoint(self.setpoint)
        self.running = True
        self.wanted_voltage = 0
        self.emission_current = 999

    def set_bias(self, bias):
        """ Set the bias-voltage """
        if self.datasocket is not None:
            self.datasocket.set_point_now('ionenergy', bias)
        if bias > -1:
            self.bias['device'].set_voltage(bias)

    def update_setpoint(self, setpoint):
        """ Update the setpoint """
        self.setpoint = setpoint
        self.datasocket.set_point_now('setpoint', setpoint)
        self.livesocket.set_point_now('setpoint', setpoint)

    def set_filament_voltage(self, voltage):
        """ Set the filament voltage """
        return self.filament['device'].set_voltage(voltage)

    def read_filament_voltage(self):
        """ Read the filament voltage """
        return self.filament['device'].read_actual_voltage()

    def read_filament_current(self):
        """ Read the filament current """
        return self.filament['device'].read_actual_current()

    def read_grid_voltage(self):
        """Read the actual grid voltage """
        return self.bias['device'].read_voltage()

    def read_emission_current(self):
        """ Read the grid current as measured by power supply """
        emission_current = self.bias['device'].read_current()
        if emission_current is None:
            self.bias['device'] = smu.KeithleySMU(
                interface='serial',
                device=self.keithley_port,
            )
            time.sleep(0.1)
            emission_current = self.value()
            LOGGER.error('Emission current not read correctly - reset device')
        else:
            emission_current = emission_current * 1000
        return emission_current

    def read_grid_current(self):
        """ Read the actual emission current """
        return self.bias['device'].read_current()

    def value(self):
        """ Return the curren emission value """
        return self.emission_current

    def run(self):
        while self.running:
            #time.sleep(0.1)
            start_time = time.time()
            qsize = self.pushsocket.queue.qsize()
            LOGGER.debug('qsize: ' + str(qsize))
            while qsize > 0:
                element = self.pushsocket.queue.get()
                LOGGER.debug('Element: ' + str(element))
                param = list(element.keys())[0]
                if param == 'setpoint':
                    value = element[param]
                    self.update_setpoint(value)
                if param == 'bias':
                    value = element[param]
                    self.set_bias(value)
                qsize = self.pushsocket.queue.qsize()

            self.emission_current = self.read_emission_current()
            self.wanted_voltage = (
                self.pid.wanted_power(self.emission_current) +
                self.filament['idle_voltage'])
            self.pid.update_setpoint(self.setpoint)
            self.set_filament_voltage(self.wanted_voltage)
            self.filament['voltage'] = self.read_filament_voltage()
            self.filament['current'] = self.read_filament_current()
            self.bias['grid_voltage'] = self.read_grid_voltage()
            self.bias['grid_current'] = self.read_grid_current()
            self.datasocket.set_point_now('ionenergy',
                                          self.bias['grid_voltage'])
            self.datasocket.set_point_now('emission', self.emission_current)
            self.livesocket.set_point_now('emission', self.emission_current)
            self.looptime = time.time() - start_time
        self.setpoint = 0
        self.set_filament_voltage(0)
            self.datasocket.set_point_now("dutycycle", self.dutycycle)
            for i in range(0, self.beatsteps):
                time.sleep(1.0 * self.beatperiod / self.beatsteps)
                if i < self.beatsteps * self.dutycycle:
                    wp.digitalWrite(self.pinnumber, 1)
                else:
                    wp.digitalWrite(self.pinnumber, 0)
        wp.digitalWrite(self.pinnumber, 0)


wp.wiringPiSetup()
datasocket = DateDataPullSocket("furnaceroom_controller", ["setpoint", "dutycycle"], timeouts=[999999, 3.0], port=9000)
datasocket.start()

pushsocket = DataPushSocket("furnaceroom_push_control", action="store_last")
pushsocket.start()

P = PowerCalculatorClass(datasocket, pushsocket)
# print P.ramp_calculator(2)
# print P.ramp_calculator(2000)
P.daemon = True
P.start()

H = HeaterClass(P, datasocket)
# H.daemon = True
H.start()

T = CursesTui(H)
T.daemon = True
T.start()
Exemplo n.º 41
0
class EmissionControl(threading.Thread):
    """ Control the emission of a filament. """
    def __init__(self):
        threading.Thread.__init__(self)
        channels = ['setpoint', 'emission', 'ionenergy']
        self.datasocket = DateDataPullSocket(
            'emission_tof', channels, timeouts=[99999999, 1.0, 99999999])
        self.datasocket.start()
        self.pushsocket = DataPushSocket('tof-emission-push_control',
                                         action='enqueue')
        self.pushsocket.start()
        self.livesocket = LiveSocket('tof-emission', channels)
        self.livesocket.start()
        self.filament = {}
        port = '/dev/serial/by-id/usb-TTI_CPX400_Series_PSU_C2F952E5-if00'
        self.filament['device'] = CPX.CPX400DPDriver(1,
                                                     interface='serial',
                                                     device=port)
        self.filament['voltage'] = 0
        self.filament['current'] = 0
        self.filament['idle_voltage'] = 1.7
        self.filament['device'].set_current_limit(4.0)
        self.filament['device'].output_status(True)
        self.bias = {}
        self.bias['device'] = CPX.CPX400DPDriver(2,
                                                 interface='serial',
                                                 device=port)
        self.bias['grid_voltage'] = 0
        self.bias['grid_current'] = 0
        self.bias['device'].output_status(True)
        cns = 'USB0::0x0957::0x0607::MY45000583::INSTR'
        self.bias['current_reader'] = a34410A.Agilent34410ADriver(
            interface='usbtmc', connection_string=cns)

        self.bias['current_reader'].select_measurement_function('CURRENT')
        self.pid = pid.PID(0.01, 0.1, 0, 4)
        self.looptime = 0
        self.setpoint = 0.05
        self.pid.update_setpoint(self.setpoint)
        self.running = True
        self.wanted_voltage = 0
        self.emission_current = 999

    def set_bias(self, bias):
        """ Set the bias-voltage """
        if self.datasocket is not None:
            self.datasocket.set_point_now('ionenergy', bias)
        if bias > -1:
            self.bias['device'].set_voltage(bias)

    def update_setpoint(self, setpoint):
        """ Update the setpoint """
        self.setpoint = setpoint
        self.datasocket.set_point_now('setpoint', setpoint)
        self.livesocket.set_point_now('setpoint', setpoint)

    def set_filament_voltage(self, voltage):
        """ Set the filament voltage """
        return self.filament['device'].set_voltage(voltage)

    def read_filament_voltage(self):
        """ Read the filament voltage """
        return self.filament['device'].read_actual_voltage()

    def read_filament_current(self):
        """ Read the filament current """
        return self.filament['device'].read_actual_current()

    def read_grid_voltage(self):
        """Read the actual grid voltage """
        return self.bias['device'].read_actual_voltage()

    def read_emission_current(self):
        """ Read the grid current as measured by power supply """
        emission_current = self.bias['current_reader'].read()
        emission_current = emission_current * -1000
        return emission_current

    def read_grid_current(self):
        """ Read the actual emission current """
        return self.bias['device'].read_actual_current()

    def value(self):
        """ Return the curren emission value """
        return self.emission_current

    def run(self):
        while self.running:
            #time.sleep(0.1)
            start_time = time.time()
            qsize = self.pushsocket.queue.qsize()
            LOGGER.debug('qsize: %d', qsize)
            while qsize > 0:
                element = self.pushsocket.queue.get()
                LOGGER.debug('Element: %s', element)
                param = list(element.keys())[0]
                if param == 'setpoint':
                    value = element[param]
                    self.update_setpoint(value)
                if param == 'bias':
                    value = element[param]
                    self.set_bias(value)
                qsize = self.pushsocket.queue.qsize()

            self.emission_current = self.read_emission_current()
            self.wanted_voltage = (
                self.pid.wanted_power(self.emission_current) +
                self.filament['idle_voltage'])
            self.pid.update_setpoint(self.setpoint)
            self.set_filament_voltage(self.wanted_voltage)
            self.filament['voltage'] = self.read_filament_voltage()
            self.filament['current'] = self.read_filament_current()
            self.bias['grid_voltage'] = self.read_grid_voltage()
            self.bias['grid_current'] = self.read_grid_current()
            self.datasocket.set_point_now('ionenergy',
                                          self.bias['grid_voltage'])
            self.datasocket.set_point_now('emission', self.emission_current)
            self.livesocket.set_point_now('emission', self.emission_current)
            self.looptime = time.time() - start_time
        self.setpoint = 0
        self.set_filament_voltage(0)
        self.bias['device'].set_voltage(0)
class FlowControl(threading.Thread):
    """ Keep updated values of the current flow """
    def __init__(self, ranges, devices, name):
        threading.Thread.__init__(self)
        self.devices = devices
        name = {}
        mfcs = {}
        print('!')
        for i in range(0, 8):
            print('----------------')
            print('Cheking port number: {}'.format(i))
            error = 0
            name[i] = ''
            while (error < 3) and (name[i] == ''):
                # Pro forma-range will be update in a few lines
                ioerror = 0
                while ioerror < 10:
                    time.sleep(0.5)
                    print(ioerror)
                    try:
                        bronk = bronkhorst.Bronkhorst('/dev/ttyUSB' + str(i), 1)
                        print('MFC Found')
                        break
                    except:
                        ioerror = ioerror + 1
                if ioerror == 10:
                    print('No MFC found on this port')
                    break
                print('Error count before identification: {}'.format(ioerror))
                name[i] = bronk.read_serial()
                print('MFC Name: {}'.format(name[i]))
                name[i] = name[i].strip()
                error = error + 1
            if name[i] in devices:
                ioerror = 0
                if ioerror < 10:
                    print(ioerror)
                    try:
                        mfcs[name[i]] = bronkhorst.Bronkhorst('/dev/ttyUSB' + str(i),
                                                              ranges[name[i]])
                        mfcs[name[i]].set_control_mode() #Accept setpoint from rs232
                    except IOError:
                        ioerror = ioerror + 1
                if ioerror == 10:
                    print('Found MFC but could not set range')

        self.mfcs = mfcs
        self.pullsocket = DateDataPullSocket(name, devices,
                                             timeouts=3.0, port=9000)
        self.pullsocket.start()

        self.pushsocket = DataPushSocket(name, action='enqueue')
        self.pushsocket.start()
        self.livesocket = LiveSocket(name, devices)
        self.livesocket.start()
        self.running = True
        self.reactor_pressure = float('NaN')

    def value(self):
        """ Helper function for the reactor logger functionality """
        return self.reactor_pressure

    def run(self):
        while self.running:
            time.sleep(0.1)
            qsize = self.pushsocket.queue.qsize()
            print("Qsize: " + str(qsize))
            while qsize > 0:
                element = self.pushsocket.queue.get()
                mfc = list(element.keys())[0]
                self.mfcs[mfc].set_flow(str(element[mfc]))
                qsize = self.pushsocket.queue.qsize()

            for mfc in self.mfcs:
                flow = self.mfcs[mfc].read_flow()
                self.pullsocket.set_point_now(mfc, flow)
                self.livesocket.set_point_now(mfc, flow)
                if mfc == self.devices[0]: # First device is considered pressure controller
                    print("Pressure: " + str(flow))
                    self.reactor_pressure = flow
Exemplo n.º 43
0
class I2CComm(object):
    """Handle I2C communication with DAC and ADC and use GPIO to control a valve relay"""

    def __init__(self):
        self.measurements = {}
        self.mcp3428 = MCP3428()
        #self.ad5667 = AD5667()
        self.dps = DataPushSocket(
            'large_CO2_mea_push_socket',
            action='callback_direct',
            callback=self.communicate,
            return_format='json',
        )
        
        # These two queues form a pair, that is used to interrupt the
        # continuous measurement of the values from the ADC to allow
        # setting the DAC and return copy the values from the ADC
        self.comm_flag = Queue()
        self.comm_return = Queue()

        self.relay_channel = [7]
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(self.relay_channel,GPIO.OUT, initial=GPIO.HIGH)

        self.dps.start()

    def measure(self):
        """Main measure loop (busy)"""
        last_print = 0
        while True:
            for channel in range(1, 5):
                self.measurements[channel] = self.mcp3428.read_sample(channel)

            now = time()
            if (now - last_print) > 10:
                print(now, "Measured:", self.measurements)
                last_print = now

            try:
                values_to_set = self.comm_flag.get(block=False)
            except Empty:
                continue

            # If we escaped the comm_flag test, we should set and enqueue current values
            # if "no_voltages_to_set" not in values_to_set:
            #     print("Asked to set DAC values", values_to_set)
            #     sleep(0.01)
            #     #self.ad5667.set_channel_A(values_to_set['A'])
            #     #self.ad5667.set_channel_B(values_to_set['B'])
            #     sleep(0.01)
            # Reads the commands about switching set the GPIO output



            if "switch anode" in values_to_set:
                print("switching to anode")
                # setup the GPIO board to use the correct channel
#                GPIO.setmode(GPIO.BOARD)
#                GPIO.setup(self.relay_channel,GPIO.OUT, initial=GPIO.HIGH)
                GPIO.output(self.relay_channel[0],GPIO.LOW)
            elif "switch cathode" in values_to_set:
                print("switching to cathode")
                GPIO.output(self.relay_channel[0],GPIO.HIGH)
                
            self.comm_return.put(dict(self.measurements))
            
    def run(self):
        """Run method used to allow keyboard interrupts"""
        try:
            self.measure()
        except KeyboardInterrupt:
            self.dps.stop()
            self.destroy()

    def destroy(self):
        GPIO.output(self.relay_channel,GPIO.HIGH)
        GPIO.cleanup()
        
    def communicate(self, data):
        """Send the received values to be written on the DAC and return values
        from the ADC

        """
        print("Received comm request")
        # Make sure there are no results hanging in the result queue
        while True:
            try:
                self.comm_return.get(block=False)
            except Empty:
                break

        print("Set comm flag")
        self.comm_flag.put(data)

        # Wait until we get values back
        result = self.comm_return.get()
        print("Got result, return")
        return result
Exemplo n.º 44
0
            time.sleep(0.25)
        self.ps.voltage[self.ps_name] = 0
        self.ps.quit = True

PS = CommonPowerSupply()
PS.start()

Pullsocket_mai = DateDataPullSocket('pvd_temp_control_mai', ['setpoint', 'voltage'], 
                                    timeouts=[999999, 3], port=9000)
Pullsocket_mai.start()
Pullsocket_dca = DateDataPullSocket('pvd_temp_control_dca', ['setpoint', 'voltage'], 
                                    timeouts=[999999, 3], port=9002)
Pullsocket_dca.start()

Pushsocket_mai = DataPushSocket('pvd309_push_control_mai', action='store_last', port=8500)
Pushsocket_mai.start()
Pushsocket_dca = DataPushSocket('pvd309_push_control_dca', action='store_last', port=8502)
Pushsocket_dca.start()

Pid_Params = [0.6, 0.0035, 5]
P_mai = PowerCalculatorClass(Pullsocket_mai, Pushsocket_mai,
                             'pvd309_temp_mai_cell#raw', pid_params=Pid_Params)
P_mai.daemon = True
P_mai.start()

Pid_Params = [0.2, 0.001, 9]
P_dca = PowerCalculatorClass(Pullsocket_dca, Pushsocket_dca,
                             'pvd309_temp_dca_cell#raw', pid_params=Pid_Params)
P_dca.daemon = True
P_dca.start()
Exemplo n.º 45
0
class FlowControl(threading.Thread):
    """ Keep updated values of the current flow """
    def __init__(self, ranges, devices, socket_name):
        threading.Thread.__init__(self)
        self.devices = devices
        name = {}
        mfcs = {}
        print('!')
        for i in range(0, 8):
            print('----------------')
            print('Cheking port number: {}'.format(i))
            error = 0
            name[i] = ''
            while (error < 3) and (name[i] == ''):
                # Pro forma-range will be update in a few lines
                ioerror = 0
                while ioerror < 10:
                    time.sleep(0.5)
                    print(ioerror)
                    try:
                        bronk = bronkhorst.Bronkhorst('/dev/ttyUSB' + str(i), 1)
                        print('MFC Found')
                        break
                    except:  # pylint: disable=bare-except
                        ioerror = ioerror + 1
                if ioerror == 10:
                    print('No MFC found on this port')
                    break
                print('Error count before identification: {}'.format(ioerror))
                name[i] = bronk.read_serial()
                print('MFC Name: {}'.format(name[i]))
                name[i] = name[i].strip()
                error = error + 1
            if name[i] in devices:
                ioerror = 0
                if ioerror < 10:
                    print(ioerror)
                    try:
                        mfcs[name[i]] = bronkhorst.Bronkhorst('/dev/ttyUSB' + str(i),
                                                              ranges[name[i]])
                        mfcs[name[i]].set_control_mode() #Accept setpoint from rs232
                    except IOError:
                        ioerror = ioerror + 1
                if ioerror == 10:
                    print('Found MFC but could not set range')

        self.mfcs = mfcs
        self.pullsocket = DateDataPullSocket(socket_name, devices,
                                             timeouts=3.0, port=9000)
        self.pullsocket.start()

        self.pushsocket = DataPushSocket(socket_name, action='enqueue')
        self.pushsocket.start()
        self.livesocket = LiveSocket(socket_name, devices)
        self.livesocket.start()
        self.running = True
        self.reactor_pressure = float('NaN')

    def value(self):
        """ Helper function for the reactor logger functionality """
        return self.reactor_pressure

    def run(self):
        while self.running:
            time.sleep(0.1)
            qsize = self.pushsocket.queue.qsize()
            print("Qsize: " + str(qsize))
            while qsize > 0:
                element = self.pushsocket.queue.get()
                mfc = list(element.keys())[0]
                self.mfcs[mfc].set_flow(str(element[mfc]))
                qsize = self.pushsocket.queue.qsize()

            for mfc in self.mfcs:
                flow = self.mfcs[mfc].read_flow()
                self.pullsocket.set_point_now(mfc, flow)
                self.livesocket.set_point_now(mfc, flow)
                if mfc == self.devices[0]: # First device is considered pressure controller
                    print("Pressure: " + str(flow))
                    self.reactor_pressure = flow
                if i < self.beatsteps * self.dutycycle:
                    wp.digitalWrite(self.pinnumber, 1)
                else:
                    wp.digitalWrite(self.pinnumber, 0)
        wp.digitalWrite(self.pinnumber, 0)


wp.wiringPiSetup()
datasocket = DateDataPullSocket('furnaceroom_controller',
                                ['setpoint', 'dutycycle'],
                                timeouts=[999999, 3.0],
                                port=9000)
datasocket.start()

pushsocket = DataPushSocket('furnaceroom_push_control', action='store_last')
pushsocket.start()

P = PowerCalculatorClass(datasocket, pushsocket)
#print P.ramp_calculator(2)
#print P.ramp_calculator(2000)
P.daemon = True
P.start()

H = HeaterClass(P, datasocket)
#H.daemon = True
H.start()

T = CursesTui(H)
T.daemon = True
T.start()
Pullsocket_mai = DateDataPullSocket('pvd_temp_control_mai',
                                    ['setpoint', 'voltage'],
                                    timeouts=[999999, 3],
                                    port=9000)
Pullsocket_mai.start()
Pullsocket_dca = DateDataPullSocket('pvd_temp_control_dca',
                                    ['setpoint', 'voltage'],
                                    timeouts=[999999, 3],
                                    port=9002)
Pullsocket_dca.start()

Pushsocket_mai = DataPushSocket('pvd309_push_control_mai',
                                action='store_last',
                                port=8500)
Pushsocket_mai.start()
Pushsocket_dca = DataPushSocket('pvd309_push_control_dca',
                                action='store_last',
                                port=8502)
Pushsocket_dca.start()

P_mai = PowerCalculatorClass(Pullsocket_mai, Pushsocket_mai,
                             'pvd309_temp_mai_cell#raw')
P_mai.daemon = True
P_mai.start()

P_dca = PowerCalculatorClass(Pullsocket_mai, Pushsocket_dca,
                             'pvd309_temp_dca_cell#raw')
P_dca.daemon = True
P_dca.start()
port = '/dev/serial/by-id/usb-TTI_CPX400_Series_PSU_55126216-if00'
PS = {}
for i in range(1, 3):
    PS[i] = cpx.CPX400DPDriver(i, interface='lan',
                               hostname='cinf-palle-heating-ps',
                               tcp_port = 9221)
    PS[i].set_voltage(0)
    PS[i].output_status(True)

Pullsocket = DateDataPullSocket('mgw_temp_control',
                                ['setpoint', 'voltage'], 
                                timeouts=[999999, 3.0],
                                port=9000)
Pullsocket.start()

Pushsocket = DataPushSocket('mgw_push_control', action='store_last')
Pushsocket.start()

P = PowerCalculatorClass(Pullsocket, Pushsocket)
P.daemon = True
P.start()

H = HeaterClass(P, Pullsocket, PS)
#H.daemon = True
H.start()

T = CursesTui(H)
T.daemon = True
T.start()

Exemplo n.º 49
0
class XGS600Control(threading.Thread):
    """Read all pressures and control states of
        setpoints (ON/OFF/AUTO) in xgs600"""
    def __init__(self,
                 port,
                 socket_name,
                 codenames,
                 user_labels,
                 valve_properties,
                 db_saver=None):
        """
        Args:
            port (int): directory to port of USB to RS232 communication
            socket_name (str): name on net for pushsocket
            codenames (list of str): names for retrieving all pressures and states of valves
            user_labels (list of str): userlabel on each device connected to xgs600
            valve_properties (dict): A dictionarie of valve with a list of properties to
                                     each valve in the form of
                                {valve_name: [channel, device, setpoint_on, setpoint_off], }
                                each valve must have a channel in the XGS600 and a devices
                                to measure the pressure and two setpoints for the pressure
                                for which the valve is on and off
        Returns:
            PushSocket to control XGSvalve states
            PullSocket to acces state of Valves
            PullSocket to acces measured pressure in system
        """
        self.xgs600 = xgs600(port=port)
        time.sleep(0.2)
        threading.Thread.__init__(self)
        #setting the initial setup specific values
        self.codenames = codenames
        self.devices = user_labels
        self.valve_properties = valve_properties
        self.valve_names = list(self.valve_properties.keys())

        #setting the initial program specific values
        self.pressures = None
        self.setpointstates = None
        self.quit = False
        self.update_time = [time.time()] * len(self.valve_names)
        self.updated = [0] * len(self.valve_names)
        names = self.codenames + self.devices
        print(names)

        #starting push-, pull-, and live- sockets
        self.pullsocket = DateDataPullSocket(
            socket_name,
            names,
            timeouts=3.0,
            port=9000,
        )
        self.pullsocket.start()

        self.pushsocket = DataPushSocket(socket_name, action='enqueue')
        self.pushsocket.start()

        self.livesocket = LiveSocket(socket_name, names)
        self.livesocket.start()
        if db_saver is not None:
            self.db_saver = db_saver
            self.db_saver.start()

        self.queue = self.pushsocket.queue

    def value(self):
        """Return two lists
           1. list of floats which is pressures from top down on xgs600 unit
           2. list of string representing the state of each valve connnected"""
        return self.pressures, self.setpointstates

    def update_new_setpoint(self):
        """Update latest values of pressures and setpointstates on pull and live sockets"""
        try:
            for i in range(0, len(self.devices)):
                self.pullsocket.set_point_now(self.devices[i],
                                              self.pressures[i])
                self.livesocket.set_point_now(self.devices[i],
                                              self.pressures[i])
        except IndexError:
            pass
        self.pullsocket.set_point_now(self.codenames[0], self.pressures)
        self.pullsocket.set_point_now(self.codenames[1], self.setpointstates)

        self.livesocket.set_point_now(self.codenames[0], self.pressures)
        self.livesocket.set_point_now(self.codenames[1], self.setpointstates)
        print('press:', self.pressures)
        print('state:', self.setpointstates)

    def database_saver(self):
        """
        check weather it is time to update database logging.
        This happens at timeout(5min standard) or setpointstate and valve state is incorrect
        """
        for valve in self.valve_names:
            channel = self.valve_properties[valve][0]
            try:
                valve_state = self.setpointstates[channel - 1]
                valve_setpoint = self.xgs600.read_setpoint(channel)
            except TimeoutError:
                print(
                    'Oops, could not read setpoint of channel, valve_state,\
                        valve_setpoint: ', channel, valve_state,
                    valve_setpoint)

            if valve_setpoint is not 'OFF' and valve_state == False:
                print('Holy Crap it is closed and should be open')
                print('Channel, Valve_state, Valve_setpoint',\
                        channel, valve_state, valve_setpoint)
                if self.updated[channel - 1] == 0:
                    print('Saved to Database', channel, valve_state,
                          valve_setpoint)
                    self.db_saver.save_point_now(
                        'microreactorng_valve_' + valve, -1)
                    self.updated[channel - 1] = 1
                else:
                    print('Not saved to database, due to earlier being saved',\
                            channel, valve_state, valve_setpoint)
                    if time.time() - self.update_time[channel - 1] > 300:
                        self.update_time[channel - 1] = time.time()
                        self.updated[channel - 1] = 0
            else:
                if time.time() - self.update_time[channel - 1] > 300:
                    self.db_saver.save_point_now(
                        'microreactorng_valve_' + valve, -1)
                    self.update_time[channel - 1] = time.time()
                    self.updated[channel - 1] = 0
                    print('Saved to Database', channel, valve_state,
                          valve_setpoint)

                self.updated[channel - 1] = 0
                print(channel, valve_state, valve_setpoint)

    def run(self):
        while not self.quit:
            while True:
                try:
                    element = self.queue.get(timeout=0.25)
                except queue.Empty:
                    break
                valve = list(element.keys())[0]
                state = element[valve]
                channel = self.valve_properties[valve][0]
                user_label = self.valve_properties[valve][1]
                setpoint_on = self.valve_properties[valve][2]
                setpoint_off = self.valve_properties[valve][3]

                if state.lower() == 'off' or state == 0:
                    self.xgs600.set_setpoint(channel, state)

                else:
                    self.xgs600.set_setpoint_on(
                        channel,
                        sensor_code='user_label',
                        sensor_count=user_label,
                        pressure_on=setpoint_on,
                    )

                    self.xgs600.set_setpoint_off(
                        channel,
                        sensor_code='user_label',
                        sensor_count=user_label,
                        pressure_off=setpoint_off,
                    )

                    self.xgs600.set_setpoint(channel, state)

            #Read values of pressures and states of setpoint
            self.pressures = self.xgs600.read_all_pressures()
            time.sleep(0.1)
            self.setpointstates = self.xgs600.read_setpoint_state()
            time.sleep(0.1)

            self.update_new_setpoint()

            self.database_saver()
Exemplo n.º 50
0
class Bakeout(threading.Thread):
    """ The actual heater """
    def __init__(self):
        threading.Thread.__init__(self)
        self.watchdog = Watchdog() 
        self.watchdog.daemon = True
        self.watchdog.start()
        time.sleep(1)

        self.setup = settings.setup
        self.quit = False
        for i in range(0, 7): #Set GPIO pins to output
            wp.pinMode(i, 1)
        self.setup = settings.setup
        self.dutycycles = [0, 0, 0, 0, 0, 0]
        channels = ['1', '2', '3', '4', '5', '6']
        self.livesocket = LiveSocket(self.setup + '-bakeout', channels, 1)
        self.livesocket.start()
        self.pullsocket = DateDataPullSocket(self.setup + '-bakeout', channels, timeouts=[2]*6)
        self.pullsocket.start()
        self.pushsocket = DataPushSocket(self.setup + '-push_control', action='enqueue')
        self.pushsocket.start()

    def activate(self, pin):
        """ Activate a pin """
        if settings.count_from_right:
            pin = pin
        else:
            pin = 7 - pin
        if self.watchdog.watchdog_safe:
            wp.digitalWrite(pin, 1)
        else:
            wp.digitalWrite(pin, 0)

    def deactivate(self, pin):
        """ De-activate a pin """
        if settings.count_from_right:
            pin = pin
        else:
            pin = 7 - pin
        wp.digitalWrite(pin, 0)

    def modify_dutycycle(self, channel, amount=None, value=None):
        """ Change the dutycycle of a channel """
        if amount is not None:
            self.dutycycles[channel-1] =  self.dutycycles[channel-1] + amount
        if value is not None:
            self.dutycycles[channel-1] = value

        if  self.dutycycles[channel-1] > 1:
            self.dutycycles[channel-1] = 1
        if self.dutycycles[channel-1] < 0.0001:
            self.dutycycles[channel-1] = 0
        self.livesocket.set_point_now(str(channel), self.dutycycles[channel-1])
        self.pullsocket.set_point_now(str(channel), self.dutycycles[channel-1])
        return self.dutycycles[channel-1]

    def run(self):
        totalcycles = settings.number_of_cycles

        self.quit = False
        cycle = 0
        while not self.quit:
            start_time = time.time()
            qsize = self.pushsocket.queue.qsize()
            LOGGER.debug('qsize: ' + str(qsize))
            while qsize > 0:
                element = self.pushsocket.queue.get()
                LOGGER.debug('Element: ' + str(element))
                channel = element.keys()[0]
                value = element[channel]
                self.modify_dutycycle(int(channel), value=value) 
                qsize = self.pushsocket.queue.qsize()

            self.watchdog.reset_ttl()
            for i in range(1, 7):
                if (1.0*cycle/totalcycles) < self.dutycycles[i-1]:
                    self.activate(i)
                else:
                    self.deactivate(i)
            cycle = cycle + 1
            cycle = cycle % totalcycles
            run_time = time.time() - start_time
            sleep_time = 1.0 * settings.cycle_time / settings.number_of_cycles
            try:
                time.sleep(sleep_time - run_time)
            except IOError:
                self.quit = True
                LOGGER.fatal('Program runs too slow to perform this operation!')
        for i in range(0, 7): # Ready to quit
            self.deactivate(i)
Exemplo n.º 51
0
class EmissionControl(threading.Thread):
    """ Control the emission of a filament. """
    def __init__(self):
        threading.Thread.__init__(self)
        channels = ['setpoint', 'emission', 'ionenergy']
        self.datasocket = DateDataPullSocket('emission_tof', channels,
                                             timeouts=[99999999, 1.0, 99999999])
        self.datasocket.start()
        self.pushsocket = DataPushSocket('tof-emission-push_control', action='enqueue')
        self.pushsocket.start()
        self.livesocket = LiveSocket('tof-emission', channels)
        self.livesocket.start()
        self.filament = {}
        port = '/dev/serial/by-id/usb-TTI_CPX400_Series_PSU_C2F952E5-if00'
        self.filament['device'] = CPX.CPX400DPDriver(1, interface='serial', device=port)
        self.filament['voltage'] = 0
        self.filament['current'] = 0
        self.filament['idle_voltage'] = 1.7
        self.filament['device'].set_current_limit(4.0)
        self.filament['device'].output_status(True)
        self.bias = {}
        self.bias['device'] = CPX.CPX400DPDriver(2, interface='serial', device=port)
        self.bias['grid_voltage'] = 0
        self.bias['grid_current'] = 0
        self.bias['device'].output_status(True)
        cns = 'USB0::0x0957::0x0607::MY45000583::INSTR'
        self.bias['current_reader'] = a34410A.Agilent34410ADriver(interface='usbtmc',
                                                                  connection_string=cns)

        self.bias['current_reader'].select_measurement_function('CURRENT')
        self.pid = pid.PID(0.01, 0.1, 0, 4)
        self.looptime = 0
        self.setpoint = 0.05
        self.pid.update_setpoint(self.setpoint)
        self.running = True
        self.wanted_voltage = 0
        self.emission_current = 999

    def set_bias(self, bias):
        """ Set the bias-voltage """
        if self.datasocket is not None:
            self.datasocket.set_point_now('ionenergy', bias)
        if bias > -1:
            self.bias['device'].set_voltage(bias)

    def update_setpoint(self, setpoint):
        """ Update the setpoint """
        self.setpoint = setpoint
        self.datasocket.set_point_now('setpoint', setpoint)
        self.livesocket.set_point_now('setpoint', setpoint)

    def set_filament_voltage(self, voltage):
        """ Set the filament voltage """
        return self.filament['device'].set_voltage(voltage)

    def read_filament_voltage(self):
        """ Read the filament voltage """
        return self.filament['device'].read_actual_voltage()

    def read_filament_current(self):
        """ Read the filament current """
        return self.filament['device'].read_actual_current()

    def read_grid_voltage(self):
        """Read the actual grid voltage """
        return self.bias['device'].read_actual_voltage()

    def read_emission_current(self):
        """ Read the grid current as measured by power supply """
        emission_current = self.bias['current_reader'].read()
        emission_current = emission_current * -1000
        return emission_current

    def read_grid_current(self):
        """ Read the actual emission current """
        return self.bias['device'].read_actual_current()

    def value(self):
        """ Return the curren emission value """
        return self.emission_current

    def run(self):
        while self.running:
            #time.sleep(0.1)
            start_time = time.time()
            qsize = self.pushsocket.queue.qsize()
            LOGGER.debug('qsize: %d', qsize)
            while qsize > 0:
                element = self.pushsocket.queue.get()
                LOGGER.debug('Element: %s', element)
                param = list(element.keys())[0]
                if param == 'setpoint':
                    value = element[param]
                    self.update_setpoint(value)
                if param == 'bias':
                    value = element[param]
                    self.set_bias(value)
                qsize = self.pushsocket.queue.qsize()

            self.emission_current = self.read_emission_current()
            self.wanted_voltage = (self.pid.wanted_power(self.emission_current) +
                                   self.filament['idle_voltage'])
            self.pid.update_setpoint(self.setpoint)
            self.set_filament_voltage(self.wanted_voltage)
            self.filament['voltage'] = self.read_filament_voltage()
            self.filament['current'] = self.read_filament_current()
            self.bias['grid_voltage'] = self.read_grid_voltage()
            self.bias['grid_current'] = self.read_grid_current()
            self.datasocket.set_point_now('ionenergy', self.bias['grid_voltage'])
            self.datasocket.set_point_now('emission', self.emission_current)
            self.livesocket.set_point_now('emission', self.emission_current)
            self.looptime = time.time() - start_time
        self.setpoint = 0
        self.set_filament_voltage(0)
        self.bias['device'].set_voltage(0)