Skip to content

Commit

Permalink
Small change to make vantage agent more robust to hardware dropouts (#…
Browse files Browse the repository at this point in the history
…524)

* Small change to make vantage agent more robust to hardware dropouts

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Remove 30s sleep after timeout

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
jlashner and pre-commit-ci[bot] authored Sep 12, 2023
1 parent 79b92b5 commit 04a39a4
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions socs/agents/vantagepro2/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def __init__(self, agent, port="/dev/ttyUSB0", sample_freq=0.5):
sample_freq = 0.5
self.sample_freq = sample_freq

self.initialized = False
self.take_data = False

# Registers weather data feed
Expand All @@ -48,6 +47,17 @@ def __init__(self, agent, port="/dev/ttyUSB0", sample_freq=0.5):
record=True,
agg_params=agg_params)

def _initialize_module(self):
"""Initialize the VantagePro2 module."""
try:
self.module = VantagePro2(self.port)
self.log.info(f"Initialized Vantage Pro2 module: {self.module}")
return True
except Exception as e:
self.log.error(f"Failed to initialize Vantage Pro2 module: {e}")
self.module = None
return False

@ocs_agent.param('auto_acquire', default=False, type=bool)
def init(self, session, params=None):
"""init(auto_acquire=False)
Expand All @@ -59,7 +69,7 @@ def init(self, session, params=None):
initialization if True. Defaults to False.
"""
if self.initialized:
if self.module is not None:
return True, "Already Initialized Module"

with self.lock.acquire_timeout(0, job='init') as acquired:
Expand All @@ -70,11 +80,7 @@ def init(self, session, params=None):

session.set_status('starting')

self.module = VantagePro2(self.port)
print("Initialized Vantage Pro2 module: {!s}".format(
self.module))

self.initialized = True
self._initialize_module()

# Start data acquisition if requested
if params['auto_acquire']:
Expand Down Expand Up @@ -120,12 +126,24 @@ def acq(self, session, params=None):

while self.take_data:
pm.sleep()

if self.module is None: # Try to re-initialize module if it fails
if not self._initialize_module():
time.sleep(30) # wait 30 sec before trying to re-initialize
continue

data = {
'timestamp': time.time(),
'block_name': 'weather',
'data': {}
}
data['data'] = self.module.weather_daq()
try:
data['data'] = self.module.weather_daq()
except TimeoutError as e:
self.log.warn(f"TimeoutError: {e}")
self.module = None
continue

self.agent.publish_to_feed('weather_data', data)
time.sleep(wait_time)

Expand Down

0 comments on commit 04a39a4

Please sign in to comment.