From 15b353e03894d87a03a98dd3bf5a80a4c06f7954 Mon Sep 17 00:00:00 2001 From: SirWalross <27106448+SirWalross@users.noreply.github.com> Date: Sat, 11 Jun 2022 16:30:20 +0200 Subject: [PATCH] Various updates - Add measurements of communication with measure.py - Add multiple retries in case of a SerialException - Add various debug information - Update end time for main.py to combat script not ending before midnight --- .gitignore | 3 +- main.py | 98 ++++++++++++++++++++++++----------------------- measure.py | 2 +- scripts/push.bash | 26 ++++++++++++- 4 files changed, 77 insertions(+), 52 deletions(-) diff --git a/.gitignore b/.gitignore index 67c91ae..6d0e360 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,4 @@ out/* CHANGELOG.md data/data logs/log -profiling.log -profiling_trace.json \ No newline at end of file +profiling.log \ No newline at end of file diff --git a/main.py b/main.py index 76b4d1d..b89bfdf 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ import yaml import time from typing import Any, List, Optional import serial +import serial.serialutil from pathlib import Path import logging import logging.handlers @@ -62,7 +63,7 @@ def convert(data) -> str: def get_offset() -> np.ndarray: - files = sorted(glob.glob(str(Path.joinpath(Path(__file__).parent, "data", "log.*.log")))) + files = sorted(glob.glob(str(Path.joinpath(Path(__file__).parent, "data", "data.*.log")))) if files: for file in files[::-1]: @@ -94,7 +95,7 @@ def setup_loggers(config: Any) -> None: TimedRotatingFileHandlerWithHeader( header=f"Timestamp,{','.join([f'dms{i+1}' for i in range(4)])},{','.join([f'temp{i+1}' for i in range(4)])},n", filename=f"{Path(__file__).parent}/data/{config['DataLogger']['filename']}", - when='h', + when="h", interval=25, backupCount=config["DataLogger"]["backupCount"], ) @@ -142,51 +143,54 @@ def main(config: Any) -> None: f"Factors: {', '.join(f'{factor:.3f}' for factor in factors[:4])}, Offset: {', '.join(f'{offset:.3f}' for offset in offsets[:4])}" ) - with serial.Serial("/dev/ttyACM0", 9600, timeout=3) as con1, serial.Serial("/dev/ttyACM1", 9600, timeout=3) as con2: - logger.warning("Connected to serial ports") - last_write = time.time() - data = np.zeros((8,)) - n = 0 - recv1, recv2 = None, None - off1, off2 = None, None - while datetime.datetime.now() - datetime.timedelta(seconds=delta_time) < end_time: - - try: - new_data = data.copy() - - # offsets for writing data of each arduino in correct column - - - con1.write(1) - off1 = 0 if int(convert(con1.readline())) == 1.0 else 4 - - # read data - for i in range(4): - recv1 = con1.readline() - new_data[i + off1] += float(convert(recv1)) - recv1 = None - - con2.write(2) - off2 = 4 if int(convert(con2.readline())) == 2.0 else 0 - - for i in range(4): - recv2 = con2.readline() - new_data[i + off2] += float(convert(recv2)) - recv2 = None - - n += 1 - data = new_data - except (TypeError, ValueError): - # may occur if no data was read over serial - logger.info(f"Didn't receive data from arduino, off1: {off1}, off2: {off2}, recv1: {recv1}, recv2: {recv2}") - - if time.time() - last_write > delta_time: - # write data - data_logger.info(",".join([f"{(value/n) * factors[i]:.5f}" for i, value in enumerate(data)]) + f",{n}") - logger.debug("Wrote data") - n = 0 - data = np.zeros((8,)) - last_write = time.time() + try: + with serial.Serial("/dev/ttyACM0", 9600, timeout=3) as con1, serial.Serial("/dev/ttyACM1", 9600, timeout=3) as con2: + logger.warning("Connected to serial ports") + last_write = time.time() + data = np.zeros((8,)) + n = 0 + recv1, recv2 = None, None + off1, off2 = None, None + while datetime.datetime.now() - datetime.timedelta(seconds=4 * delta_time) < end_time: + + try: + new_data = data.copy() + + # offsets for writing data of each arduino in correct column + + con1.write(1) + off1 = 0 if int(convert(con1.readline())) == 1.0 else 4 + + # read data + for i in range(4): + recv1 = con1.readline() + new_data[i + off1] += float(convert(recv1)) + recv1 = None + + con2.write(2) + off2 = 4 if int(convert(con2.readline())) == 2.0 else 0 + + for i in range(4): + recv2 = con2.readline() + new_data[i + off2] += float(convert(recv2)) + recv2 = None + + n += 1 + data = new_data + except (TypeError, ValueError): + # may occur if no data was read over serial + logger.info(f"Didn't receive data from arduino, off1: {off1}, off2: {off2}, recv1: {recv1}, recv2: {recv2}") + + if time.time() - last_write > delta_time: + # write data + data_logger.info(",".join([f"{(value/n) * factors[i]:.5f}" for i, value in enumerate(data)]) + f",{n}") + logger.debug("Wrote data") + n = 0 + data = np.zeros((8,)) + last_write = time.time() + except serial.serialutil.SerialException: + logger.exception("SerialException was caught, shutting down.") + sys.exit(15) fh[0].doRollover() diff --git a/measure.py b/measure.py index c9b174a..8604a21 100644 --- a/measure.py +++ b/measure.py @@ -136,7 +136,7 @@ def loop(con1: serial.Serial, con2: serial.Serial): def main() -> None: with serial.Serial("/dev/ttyACM0", 9600, timeout=3) as con1, serial.Serial("/dev/ttyACM1", 9600, timeout=3) as con2: - for _ in range(100): + for _ in range(50): loop(con1, con2) diff --git a/scripts/push.bash b/scripts/push.bash index f2fc007..98fd588 100755 --- a/scripts/push.bash +++ b/scripts/push.bash @@ -1,6 +1,28 @@ #!/bin/bash +ls -la . +ls -la data +ls -la log + +pip install multiprocessing_logging +python3 measure.py + git add . git commit -m "New data" && git push --force -echo "" -python3 main.py \ No newline at end of file +echo -e "Starting python script:\n" +loop_count = 1 +while : ; do + python3 main.py + return_code=$? + if [[ "$return_code" -eq 0 ]]; then + echo -e "\nPython script ended" + break + fi + if [[ "$loop_count" -eq 10 ]]; then + echo -e "\nPython script failed too many times, trying again tomorrow" + break + fi + echo -e "Ended with error, waiting and trying again" + loop_count=$((loop_count + 1)) + sleep $((loop_count * 60)) +done \ No newline at end of file -- GitLab