Skip to content

Commit

Permalink
Changed connection protocal from an infinite loop into a set number o…
Browse files Browse the repository at this point in the history
…f iterations. Added reconnection functionality into the agent acq process
  • Loading branch information
Bryce Bixler committed Nov 17, 2023
1 parent fa26efa commit f939874
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 24 deletions.
18 changes: 11 additions & 7 deletions socs/agents/hwp_pid/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,17 @@ def acq(self, session, params):
data = {'timestamp': time.time(),
'block_name': 'HWPPID', 'data': {}}

current_freq = self.pid.get_freq()
target_freq = self.pid.get_target()
direction = self.pid.get_direction()

data['data']['current_freq'] = current_freq
data['data']['target_freq'] = target_freq
data['data']['direction'] = direction
try:
current_freq = self.pid.get_freq()
target_freq = self.pid.get_target()
direction = self.pid.get_direction()

data['data']['current_freq'] = current_freq
data['data']['target_freq'] = target_freq
data['data']['direction'] = direction
except:
time.sleep(1)
continue

self.agent.publish_to_feed('hwppid', data)

Expand Down
5 changes: 4 additions & 1 deletion socs/agents/hwp_pid/drivers/pid_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ def _establish_connection(ip, port, timeout=5):
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.settimeout(timeout)
# unit tests might fail on first connection attempt
while True:
attempts = 3
for attempt in range(attempts):
try:
conn.connect((ip, port))
break
except (ConnectionRefusedError, OSError) as e:
print(f"Failed to connect to device at {ip}:{port}")
time.sleep(5)
else:
raise RuntimeError('Could not connect to PID controller')
return conn

@staticmethod
Expand Down
35 changes: 20 additions & 15 deletions socs/agents/hwp_pmx/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,26 +281,31 @@ def acq(self, session, params):
'block_name': 'hwppmx',
'data': {}
}
msg, curr = self.dev.meas_current()
data['data']['current'] = curr

msg, volt = self.dev.meas_voltage()
data['data']['voltage'] = volt
try:
msg, curr = self.dev.meas_current()
data['data']['current'] = curr

msg, code = self.dev.check_error()
data['data']['err_code'] = code
data['data']['err_msg'] = msg
msg, volt = self.dev.meas_voltage()
data['data']['voltage'] = volt

prot_code = self.dev.check_prot()
if prot_code != 0:
self.prot = prot_code
msg, code = self.dev.check_error()
data['data']['err_code'] = code
data['data']['err_msg'] = msg

prot_msg = self.dev.get_prot_msg(self.prot)
data['data']['prot_code'] = self.prot
data['data']['prot_msg'] = prot_msg
prot_code = self.dev.check_prot()
if prot_code != 0:
self.prot = prot_code

msg, src = self.dev.check_source()
data['data']['source'] = src
prot_msg = self.dev.get_prot_msg(self.prot)
data['data']['prot_code'] = self.prot
data['data']['prot_msg'] = prot_msg

msg, src = self.dev.check_source()
data['data']['source'] = src
except:
time.sleep(sleep_time)
continue

self.agent.publish_to_feed('hwppmx', data)
session.data = {'curr': curr,
Expand Down
5 changes: 4 additions & 1 deletion socs/agents/hwp_pmx/drivers/PMX_ethernet.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ def __init__(self, ip, port):
def _establish_connection(self, ip, port, timeout=5):
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.settimeout(timeout)
while True:
attempts = 3
for attempt in range(attempts):
try:
conn.connect((ip, port))
break
except (ConnectionRefusedError, OSError) as e:
print(f"Failed to connect to device at {ip}:{port}")
time.sleep(5)
else:
raise RuntimeError('Could not connect to PID controller')
return conn

def close(self):
Expand Down

0 comments on commit f939874

Please sign in to comment.