Skip to content
Merged
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
50 changes: 44 additions & 6 deletions socs/agents/pfeiffer_tpg366/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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):
"""
Expand All @@ -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()
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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)):
Expand Down Expand Up @@ -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,
Expand Down