From 85da7661789db698c83b158366119de119fc1efb Mon Sep 17 00:00:00 2001 From: lab1 Date: Thu, 18 Feb 2021 17:48:40 -0500 Subject: [PATCH 1/3] Updates to Pfeiffer agent. It now catches when the monitor channels are turned off and prints a statement to the logs. Also sets pressure value to zero if the channel was off. Prior to this, it threw an error when querying pressure values when channels are off --- .../pfeiffer_tpg366/pfeiffer_tpg366_agent.py | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/agents/pfeiffer_tpg366/pfeiffer_tpg366_agent.py b/agents/pfeiffer_tpg366/pfeiffer_tpg366_agent.py index cc1837ad5..35af357f3 100644 --- a/agents/pfeiffer_tpg366/pfeiffer_tpg366_agent.py +++ b/agents/pfeiffer_tpg366/pfeiffer_tpg366_agent.py @@ -34,6 +34,26 @@ def __init__(self, ip_address, port, timeout=10, self.comm.connect((self.ip_address, self.port)) self.comm.settimeout(timeout) + def channel_power(self): + ''' + Function to check the power status of a given channel + + Args: + None + Returns: + print statement warning about channels being off + ''' + msg = 'SEN\r\n' + self.comm.send(msg.encode()) + status = self.comm.recv(BUFF_SIZE).decode() + self.comm.send(ENQ.encode()) + read_str = self.comm.recv(BUFF_SIZE).decode() + power_str = read_str.split('\r') + power_states = np.array(power_str[0].split(','), dtype=int) + if any(chan == 0 for chan in power_states): + print("Something is off!") + return + def read_pressure(self, ch_no): """ Function to measure the pressure of one given channel @@ -48,7 +68,6 @@ def read_pressure(self, ch_no): """ msg = 'PR%d\r\n' % ch_no self.comm.send(msg.encode()) - # Can use this to catch exemptions, for troubleshooting status = self.comm.recv(BUFF_SIZE).decode() self.comm.send(ENQ.encode()) read_str = self.comm.recv(BUFF_SIZE).decode() @@ -74,10 +93,15 @@ def read_pressure_all(self): self.comm.send(ENQ.encode()) read_str = self.comm.recv(BUFF_SIZE).decode() pressure_str = read_str.split('\r')[0] - #gauge_states = pressure_str.split(',')[::2] - #gauge_states = np.array(gauge_states, dtype=int) + gauge_states = pressure_str.split(',')[::2] + gauge_states = np.array(gauge_states, dtype=int) pressures = pressure_str.split(',')[1::2] pressures = [float(p) for p in pressures] + if any(state != 0 for state in gauge_states): + index = np.where(gauge_states != 0) + for j in range(len(index[0])): + channel = np.int(index[0][j]) + pressures[channel] = 0. return pressures def close(self): @@ -109,7 +133,6 @@ def start_acq(self, session, params=None): Args: sampling_frequency- defaults to 2.5 Hz - """ if params is None: params = {} @@ -136,6 +159,7 @@ def start_acq(self, session, params=None): 'block_name': 'pressures', 'data': {} } + self.gauge.channel_power() pressure_array = self.gauge.read_pressure_all() # Loop through all the channels on the device for channel in range(len(pressure_array)): From 3e0d35bcad8dc5ccde4fefd77140ed7acf6e9a67 Mon Sep 17 00:00:00 2001 From: lab1 Date: Thu, 27 May 2021 12:47:26 -0400 Subject: [PATCH 2/3] Changes to the pfeiffer agent, trying to save some work and then update SOCS/OCS --- .../pfeiffer_tpg366/pfeiffer_tpg366_agent.py | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/agents/pfeiffer_tpg366/pfeiffer_tpg366_agent.py b/agents/pfeiffer_tpg366/pfeiffer_tpg366_agent.py index 35af357f3..58b5beb1a 100644 --- a/agents/pfeiffer_tpg366/pfeiffer_tpg366_agent.py +++ b/agents/pfeiffer_tpg366/pfeiffer_tpg366_agent.py @@ -7,7 +7,8 @@ from ocs import ocs_agent, site_config from ocs.ocs_twisted import TimeoutLock import time - +import txaio +txaio.use_twisted() BUFF_SIZE = 128 ENQ = '\x05' @@ -33,15 +34,16 @@ def __init__(self, ip_address, port, timeout=10, self.comm = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.comm.connect((self.ip_address, self.port)) self.comm.settimeout(timeout) + self.log = txaio.make_logger() def channel_power(self): ''' - Function to check the power status of a given channel + Function to check the power status of all channels Args: None Returns: - print statement warning about channels being off + None ''' msg = 'SEN\r\n' self.comm.send(msg.encode()) @@ -50,9 +52,11 @@ def channel_power(self): read_str = self.comm.recv(BUFF_SIZE).decode() power_str = read_str.split('\r') power_states = np.array(power_str[0].split(','), dtype=int) - if any(chan == 0 for chan in power_states): - print("Something is off!") - return + print(power_states) + if any(chan != 2 for chan in power_states): + channel_states = [index+1 for index, state in enumerate(power_states) if state !=2] + self.log.info("The following channels are off:",channel_states) + print("The following channels are off:",channel_states) def read_pressure(self, ch_no): """ @@ -99,9 +103,8 @@ def read_pressure_all(self): pressures = [float(p) for p in pressures] if any(state != 0 for state in gauge_states): index = np.where(gauge_states != 0) - for j in range(len(index[0])): - channel = np.int(index[0][j]) - pressures[channel] = 0. + for j in index[0]: + pressures[j] = 0. return pressures def close(self): @@ -184,6 +187,9 @@ def stop_acq(self, session, params=None): if __name__ == '__main__': + # Start logging + #txaio.start_logging(level=Pfeiffer.get("LOGLEVEL", "info")) + parser = site_config.add_arguments() pgroup = parser.add_argument_group('Agent Options') From fc8173074be118950d5edf48db38e0bedf3560bd Mon Sep 17 00:00:00 2001 From: lab1 Date: Mon, 14 Jun 2021 09:27:34 -0400 Subject: [PATCH 3/3] Adding caching method to pfieffer class --- .../pfeiffer_tpg366/pfeiffer_tpg366_agent.py | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/agents/pfeiffer_tpg366/pfeiffer_tpg366_agent.py b/agents/pfeiffer_tpg366/pfeiffer_tpg366_agent.py index 58b5beb1a..c64083678 100644 --- a/agents/pfeiffer_tpg366/pfeiffer_tpg366_agent.py +++ b/agents/pfeiffer_tpg366/pfeiffer_tpg366_agent.py @@ -4,6 +4,7 @@ import socket import numpy as np +from os import environ from ocs import ocs_agent, site_config from ocs.ocs_twisted import TimeoutLock import time @@ -11,8 +12,7 @@ txaio.use_twisted() BUFF_SIZE = 128 ENQ = '\x05' - - +LOG = txaio.make_logger() class Pfeiffer: """CLASS to control and retrieve data from the pfeiffer tpg366 pressure gauge controller @@ -28,14 +28,13 @@ class Pfeiffer: close closes the socket """ def __init__(self, ip_address, port, timeout=10, - f_sample=2.5): + f_sample=1.): self.ip_address = ip_address self.port = port self.comm = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.comm.connect((self.ip_address, self.port)) self.comm.settimeout(timeout) self.log = txaio.make_logger() - def channel_power(self): ''' Function to check the power status of all channels @@ -52,11 +51,28 @@ def channel_power(self): read_str = self.comm.recv(BUFF_SIZE).decode() power_str = read_str.split('\r') power_states = np.array(power_str[0].split(','), dtype=int) - print(power_states) if any(chan != 2 for chan in power_states): channel_states = [index+1 for index, state in enumerate(power_states) if state !=2] - self.log.info("The following channels are off:",channel_states) - print("The following channels are off:",channel_states) + LOG.info("The following channels are off:{}".format(channel_states)) + return channel_states + + def check_channel_stage_changes(self, old_state): + ''' + Function to check whether a channel has been turned off and on. + Args: + old_state: The last known record of channel states + ''' + msg = 'SEN\r\n' + self.comm.send(msg.encode()) + status = self.comm.recv(BUFF_SIZE).decode() + self.comm.send(ENQ.encode()) + read_str = self.comm.recv(BUFF_SIZE).decode() + power_str = read_str.split('\r') + power_states = np.array(power_str[0].split(','), dtype=int) + if any(chan != 2 for chan in power_states): + channel_states = [index+1 for index, state in enumerate(power_states) if state !=2] + if channel_states != old_state: + print("Something changed!") def read_pressure(self, ch_no): """ @@ -114,7 +130,7 @@ def close(self): class PfeifferAgent: - def __init__(self, agent, ip_address, port, f_sample=2.5): + def __init__(self, agent, ip_address, port, f_sample=1.): self.active = True self.agent = agent self.log = agent.log @@ -123,7 +139,6 @@ def __init__(self, agent, ip_address, port, f_sample=2.5): self.take_data = False self.gauge = Pfeiffer(ip_address, int(port)) agg_params = {'frame_length': 60, } - self.agent.register_feed('pressures', record=True, agg_params=agg_params, @@ -155,7 +170,6 @@ def start_acq(self, session, params=None): session.set_status('running') self.take_data = True - while self.take_data: data = { 'timestamp': time.time(), @@ -163,6 +177,7 @@ def start_acq(self, session, params=None): 'data': {} } self.gauge.channel_power() + self.gauge.check_channel_stage_changes(self.gauge.channel_power()) pressure_array = self.gauge.read_pressure_all() # Loop through all the channels on the device for channel in range(len(pressure_array)): @@ -187,9 +202,9 @@ def stop_acq(self, session, params=None): if __name__ == '__main__': - # Start logging - #txaio.start_logging(level=Pfeiffer.get("LOGLEVEL", "info")) + # Start logging + txaio.start_logging(level=environ.get("LOGLEVEL", "info")) parser = site_config.add_arguments() pgroup = parser.add_argument_group('Agent Options')