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

Small change to make vantage agent more robust to hardware dropouts #524

Merged
merged 3 commits into from
Sep 12, 2023
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
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