From 10b48d193e845b8ef7a1c5c6ae498c6bb56b7e3a Mon Sep 17 00:00:00 2001 From: tanaybhandarkar Date: Fri, 22 Nov 2024 08:56:59 -0500 Subject: [PATCH] Handle disabled gagues in Pfeiffer agent (#145) * Set pressure value to zero if the channel was off. Prior to this, it threw an error when querying pressure values when channels are off * Changes to the pfeiffer agent, trying to save some work and then update SOCS/OCS * Adding caching method to pfieffer class * Make os import consistent with other agents * Fix sphinx builds by removing global logger The problem line was `LOG = txaio.make_logger()`, though I'm not sure why, that same usage is in plenty of other agents. * Check strictly for gauges that are offline This counts gauges that cannot be switched on/off as 'on', where it was previously counting them as 'off'. * Move disabled channel log message to debug logs * Clean up docstring for channel_power method * Remove check_channel_stage_changes method * Remove call to channel_power method during acquisition --------- Co-authored-by: Brian Koopman --- socs/agents/pfeiffer_tpg366/agent.py | 50 ++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/socs/agents/pfeiffer_tpg366/agent.py b/socs/agents/pfeiffer_tpg366/agent.py index 6b363a31f..8b2896dc4 100644 --- a/socs/agents/pfeiffer_tpg366/agent.py +++ b/socs/agents/pfeiffer_tpg366/agent.py @@ -3,12 +3,18 @@ # Zhilei Xu, Tanay Bhandarkar import argparse +import os import socket import time +import numpy as np +import txaio from ocs import ocs_agent, site_config from ocs.ocs_twisted import TimeoutLock +# For logging +txaio.use_twisted() + BUFF_SIZE = 128 ENQ = '\x05' @@ -29,12 +35,36 @@ class Pfeiffer: """ 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. + + Args: + None + + Returns: + List of channel states. + + """ + msg = 'SEN\r\n' + self.comm.send(msg.encode()) + 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 == 1 for chan in power_states): + channel_states = [index + 1 for index, state in enumerate(power_states) if state == 1] + self.log.debug("The following channels are off: {}".format(channel_states)) + return channel_states def read_pressure(self, ch_no): """ @@ -50,7 +80,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 self.comm.recv(BUFF_SIZE).decode() self.comm.send(ENQ.encode()) read_str = self.comm.recv(BUFF_SIZE).decode() @@ -76,9 +105,14 @@ 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 = 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 index[0]: + pressures[j] = 0. return pressures def close(self): @@ -88,7 +122,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 @@ -97,7 +131,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, @@ -130,13 +163,15 @@ def acq(self, session, params=None): return False, "Could not acquire lock." self.take_data = True - while self.take_data: data = { 'timestamp': time.time(), 'block_name': 'pressures', 'data': {} } + # Useful for debugging, but should separate to a task to cut + # down on queries in the main acq() loop. + # 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)): @@ -180,6 +215,9 @@ def make_parser(parser=None): def main(args=None): + # Start logging + txaio.start_logging(level=os.environ.get("LOGLEVEL", "info")) + parser = make_parser() args = site_config.parse_args(agent_class='PfeifferAgent', parser=parser,