Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to Pfeiffer agent. #145

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 59 additions & 6 deletions socs/agents/pfeiffer_tpg366/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
import argparse
import socket
import time
from os import environ

import numpy as np
import txaio
from ocs import ocs_agent, site_config
from ocs.ocs_twisted import TimeoutLock

txaio.use_twisted()

BUFF_SIZE = 128
ENQ = '\x05'
LOG = txaio.make_logger()


class Pfeiffer:
Expand All @@ -29,12 +35,52 @@ 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:
None
'''
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 != 2 for chan in power_states):
channel_states = [index + 1 for index, state in enumerate(power_states) if state != 2]
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())
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):
"""
Expand All @@ -50,7 +96,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()
Expand All @@ -76,9 +121,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):
Expand All @@ -88,7 +138,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
Expand All @@ -97,7 +147,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,
Expand Down Expand Up @@ -130,13 +179,14 @@ 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': {}
}
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)):
Expand Down Expand Up @@ -180,6 +230,9 @@ def make_parser(parser=None):


def main(args=None):
# Start logging
txaio.start_logging(level=environ.get("LOGLEVEL", "info"))

parser = make_parser()
args = site_config.parse_args(agent_class='PfeifferAgent',
parser=parser,
Expand Down
Loading