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

Scpi psu reconnect #726

Merged
merged 20 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
35f0a5a
Created a new agent.py which dynamically reconnects sometimes, but st…
simonscryo Mar 30, 2024
d47fa8c
Fixed the code to dynamically reconnect. See the relevant DAQ discuss…
simonscryo Aug 10, 2024
9971cca
Removed numpy
simonscryo Aug 10, 2024
ba51bfb
Removed the commented out call for psu.test() as it ended up being un…
simonscryo Aug 10, 2024
988e592
Removed status messages and added logging as per daq-discussions#106
simonscryo Aug 14, 2024
416bc37
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 15, 2024
0030097
Made the easy changes
simonscryo Sep 7, 2024
750da62
Made the easy changes
simonscryo Sep 7, 2024
6cb267b
Made some of the changes requested by Brian
simonscryo Sep 7, 2024
e2d6921
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 7, 2024
4580932
Merge branch 'main' of https://github.com/simonsobs/socs into scpi_ps…
simonscryo Sep 7, 2024
4567a0b
Merged main
simonscryo Sep 7, 2024
cc73d09
Merge branch 'scpi_psu_reconnect' of https://github.com/simonsobs/soc…
simonscryo Sep 7, 2024
8b0ab3a
Changed the nested logic to use elif
simonscryo Sep 7, 2024
737343c
Finished requested changes
simonscryo Sep 7, 2024
a51c156
Changed the reconnect logic again.
simonscryo Sep 7, 2024
4f1fc11
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 7, 2024
719ea14
Uncommented the correct drivers.
agthomas-uc Sep 7, 2024
0583384
Remove extra local drivers import
BrianJKoopman Sep 9, 2024
c9d7a6b
Remove extra word added to comment
BrianJKoopman Sep 9, 2024
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
67 changes: 42 additions & 25 deletions socs/agents/scpi_psu/agent.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import argparse
import socket
import time
from typing import Optional

from ocs import ocs_agent, site_config
from ocs.ocs_twisted import TimeoutLock
Expand All @@ -19,7 +20,7 @@ def __init__(self, agent, ip_address, gpib_slot):
self.gpib_slot = gpib_slot
self.monitor = False

self.psu = None
self.psu: Optional[ScpiPsuAgent] = None

# Registers Temperature and Voltage feeds
agg_params = {
Expand All @@ -41,16 +42,25 @@ def init(self, session, params=None):
if not acquired:
return False, "Could not acquire lock"

try:
self.psu = PsuInterface(self.ip_address, self.gpib_slot)
self.idn = self.psu.identify()
except socket.timeout as e:
self.log.error(f"PSU timed out during connect: {e}")
return False, "Timeout"
self.log.info("Connected to psu: {}".format(self.idn))

while not self._initialize_module():
time.sleep(5)
return True, 'Initialized PSU.'

def _initialize_module(self):
"""Initialize the ScpiPsu module."""
try:
self.psu = PsuInterface(self.ip_address, self.gpib_slot)
except (socket.timeout, OSError) as e:
self.log.warn(f"Error establishing connection: {e}")
self.psu = None
return False

self.idn = self.psu.identify()
self.log.info("Connected to psu: {}".format(self.idn))
self.log.info("Clearing event registers and error queue")
self.psu.clear()
return True

@ocs_agent.param('wait', type=float, default=1)
@ocs_agent.param('channels', type=list, default=[1, 2, 3])
@ocs_agent.param('test_mode', type=bool, default=False)
Expand All @@ -71,29 +81,36 @@ def monitor_output(self, session, params=None):
self.monitor = True

while self.monitor:
time.sleep(params['wait'])
with self.lock.acquire_timeout(1) as acquired:
if acquired:
data = {
'timestamp': time.time(),
'block_name': 'output',
'data': {}
}
if not acquired:
self.log.warn("Could not acquire in monitor_current")
continue

if not self.psu:
self._initialize_module()
continue

data = {
'timestamp': time.time(),
'block_name': 'output',
'data': {}
}

try:
for chan in params['channels']:
data['data']["Voltage_{}".format(chan)] = self.psu.get_volt(chan)
data['data']["Current_{}".format(chan)] = self.psu.get_curr(chan)
except socket.timeout as e:
self.log.warn(f"TimeoutError: {e}")
self.log.info("Attempting to reconnect")
self.psu = None
continue

# self.log.info(str(data))
# print(data)
self.agent.publish_to_feed('psu_output', data)

# Allow this process to be queried to return current data
session.data = data
self.agent.publish_to_feed('psu_output', data)

else:
self.log.warn("Could not acquire in monitor_current")

time.sleep(params['wait'])
# Allow this process to be queried to return current data
session.data = data

if params['test_mode']:
break
Expand Down
6 changes: 6 additions & 0 deletions socs/agents/scpi_psu/drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,9 @@ def get_curr(self, ch):
self.write('MEAS:CURR? CH' + str(ch))
current = float(self.read())
return current

def clear(self):
# Clear all the event registers and error queue, using a query such as *ESR? or MEAS:X?
# instead of *CLS can confuse the PSU
self.write('*CLS')
return True