Skip to content

Commit

Permalink
Abr asair sensor (#14445)
Browse files Browse the repository at this point in the history
<!--
Thanks for taking the time to open a pull request! Please make sure
you've read the "Opening Pull Requests" section of our Contributing
Guide:


https://github.com/Opentrons/opentrons/blob/edge/CONTRIBUTING.md#opening-pull-requests

To ensure your code is reviewed quickly and thoroughly, please fill out
the sections below to the best of your ability!
-->

# Overview

This PR adds a script to the hardware-testing folder that will allow the
robot to connect to an asair temperature and humidity sensor and record
continuously to a google spread sheet.


# Test Plan

I tested this script on robot DVT10 on software v7.1.1. It successfully
measured the temperature and humidity and uploaded to the google sheet.

# Changelog

Added abr-asair-sensor script
Script:

1. Asks user for robot name
2. Asks user for duration of recording (min)
3. Asks user for frequency of recording (min)
4. Robot reads ports and connects to asair sensor
5. robot collects data and appends to a list at the given frequency
6. At the end of the duration the robot saves a .csv file of the data to
the testing-data folder within the robot and to the google sheet
I also updated the analyze-abr script to improve ease of analysis.


IMPORTANT: MAKE SURE ANY BREAKING CHANGES ARE PROPERLY COMMUNICATED
-->

# Review requests

<!--
Describe any requests for your reviewers here.
-->

# Risk assessment

- for the script to run successfully on the robot, google_sheets_tool.py
and a google sheets credentials file needs to be uploaded manually onto
the robot's jupyter notebook.
- for google_sheets_tool.py to run successfully within the script python
libraries: httplib2, oauth2client, gspread, and pprintpp need to be
installed onto the robots
  • Loading branch information
rclarke0 authored Feb 28, 2024
1 parent d080d04 commit 2c5932f
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 7 deletions.
129 changes: 126 additions & 3 deletions hardware-testing/hardware_testing/scripts/abr_asair_sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,129 @@
"""ABR Temperature Humidity Sensors."""
# from hardware_testing import data
# from hardware_testing.drivers import asair_sensor

from hardware_testing import data
from hardware_testing.drivers import asair_sensor
import datetime
import sys
import time as t
from typing import List
import os

# if __name__ == "__main__":

def _get_user_input(lst: List[str], some_string: str) -> str:
variable = input(some_string)
while variable not in lst:
print(
f"Your input was {variable}. Expected input is one of the following: {lst}"
)
variable = input(some_string)
return variable


class _ABRAsairSensor:
def __init__(self, robot: str, duration: int, frequency: int) -> None:
try:
sys.path.insert(0, "/var/lib/jupyter/notebooks")
import google_sheets_tool # type: ignore[import]

credentials_path = "/var/lib/jupyter/notebooks/abr.json"
except ImportError:
raise ImportError(
"Run on robot. Make sure google_sheets_tool.py is in jupyter notebook."
)
print(os.path.exists(credentials_path))
test_name = "ABR-Environment-Monitoring"
run_id = data.create_run_id()
file_name = data.create_file_name(test_name, run_id, robot)
sensor = asair_sensor.BuildAsairSensor(False, True)
print(sensor)
env_data = sensor.get_reading()
header = [
"Robot",
"Timestamp",
"Date",
"Time",
"Temp (oC)",
"Relative Humidity (%)",
]
header_str = ",".join(header) + "\n"
data.append_data_to_file(test_name, run_id, file_name, header_str)
# Upload to google has passed
try:
google_sheet = google_sheets_tool.google_sheet(
credentials_path, "ABR Ambient Conditions", tab_number=0
)
print("Connected to the google sheet.")
except FileNotFoundError:
print(
"There is no google sheets credentials. Make sure credentials in jupyter notebook."
)
results_list = [] # type: List
start_time = datetime.datetime.now()
while True:
env_data = sensor.get_reading()
timestamp = datetime.datetime.now()
# Time adjustment for ABR robot timezone
new_timestamp = timestamp - datetime.timedelta(hours=5)
date = new_timestamp.date()
time = new_timestamp.time()
temp = env_data.temperature
print(temp)
rh = env_data.relative_humidity
print(rh)
row = [
robot,
str(new_timestamp),
str(date),
str(time),
temp,
rh,
]
results_list.append(row)
# Check if duration elapsed
elapsed_time = datetime.datetime.now() - start_time
if elapsed_time.total_seconds() >= duration * 60:
break
# write to google sheet
try:
google_sheet.write_header(header)
google_sheet.update_row_index()
google_sheet.write_to_row(row)
print("Wrote row")
except RuntimeError:
print("Did not write row.")
# Delay for desired frequency minutes before the next iteration
t.sleep(frequency * 60) # seconds

# Upload to robot testing data folder
for sublist in results_list:
row_str = ", ".join(map(str, sublist)) + "\n" # type: str
save_file_path = data.append_data_to_file(
test_name, run_id, file_name, row_str
)
print(f"Saved to robot: f{save_file_path}.")
print(
f"Done. Ran for {duration} minutes and collected every {frequency} minutes."
)


if __name__ == "__main__":
robot_list = [
"DVT1ABR1",
"DVT1ABR2",
"DVT1ABR3",
"DVT1ABR4",
"DVT2ABR5",
"DVT2ABR6",
"PVT1ABR7",
"PVT1ABR8",
"PVT1ABR9",
"PVT1ABR10",
"PVT1ABR11",
"PVT1ABR12",
"ROOM_339",
"Room_340",
] # type: List
robot = _get_user_input(robot_list, "Robot/Room: ")
duration = int(input("Duration (min): "))
frequency = int(input("Frequency (min): "))
_ABRAsairSensor(robot, duration, frequency)
2 changes: 2 additions & 0 deletions hardware-testing/hardware_testing/scripts/abr_scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
"PVT1ABR10",
"PVT1ABR11",
"PVT1ABR12",
"ROOM_339",
"ROOM_340",
]
# Labware per Robot
labware_DVT1ABR4 = [
Expand Down
11 changes: 7 additions & 4 deletions hardware-testing/hardware_testing/scripts/analyze_abr.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@ def _get_user_input(list: List, some_string: str) -> str:
dir_2 = os.path.join(current_dir, folder_of_interest)
new_csv_file_path = os.path.join(current_dir, results_file_name)
file_list_2 = os.listdir(dir_2) # LIST OF individual run folders
# WRITE HEADER
with open(new_csv_file_path, "w", newline="") as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerow(header)
for file2 in file_list_2:
raw_data_folder = os.path.join(dir_2, file2)
raw_data_file_csv = os.listdir(raw_data_folder)[0]
plate_state = raw_data_file_csv.split("_")[-1].split("-")[1].split(".")[0]
sample = raw_data_file_csv.split("_")[-1].split("-")[0]
raw_data_file_csv_path = os.path.join(raw_data_folder, raw_data_file_csv)
results_list = []
try:
with open(raw_data_file_csv_path, "r") as f:
for line in f:
Expand All @@ -54,13 +59,11 @@ def _get_user_input(list: List, some_string: str) -> str:
stable_value,
sample,
)
results_list.append(row_data)

pass
except Exception as e:
print(f"Error opening file: {e}")
# WRITE HEADER
with open(new_csv_file_path, "w", newline="") as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerow(header)
with open(new_csv_file_path, "a", newline="") as csv_file:
csv_writer = csv.writer(csv_file)
# Write data
Expand Down

0 comments on commit 2c5932f

Please sign in to comment.