Skip to content
Snippets Groups Projects
Commit 15b353e0 authored by SirWalross's avatar SirWalross
Browse files

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
parent d2f8b86a
No related branches found
No related tags found
No related merge requests found
...@@ -4,4 +4,3 @@ CHANGELOG.md ...@@ -4,4 +4,3 @@ CHANGELOG.md
data/data data/data
logs/log logs/log
profiling.log profiling.log
profiling_trace.json
\ No newline at end of file
...@@ -3,6 +3,7 @@ import yaml ...@@ -3,6 +3,7 @@ import yaml
import time import time
from typing import Any, List, Optional from typing import Any, List, Optional
import serial import serial
import serial.serialutil
from pathlib import Path from pathlib import Path
import logging import logging
import logging.handlers import logging.handlers
...@@ -62,7 +63,7 @@ def convert(data) -> str: ...@@ -62,7 +63,7 @@ def convert(data) -> str:
def get_offset() -> np.ndarray: 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: if files:
for file in files[::-1]: for file in files[::-1]:
...@@ -94,7 +95,7 @@ def setup_loggers(config: Any) -> None: ...@@ -94,7 +95,7 @@ def setup_loggers(config: Any) -> None:
TimedRotatingFileHandlerWithHeader( 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", 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']}", filename=f"{Path(__file__).parent}/data/{config['DataLogger']['filename']}",
when='h', when="h",
interval=25, interval=25,
backupCount=config["DataLogger"]["backupCount"], backupCount=config["DataLogger"]["backupCount"],
) )
...@@ -142,6 +143,7 @@ def main(config: Any) -> None: ...@@ -142,6 +143,7 @@ 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])}" f"Factors: {', '.join(f'{factor:.3f}' for factor in factors[:4])}, Offset: {', '.join(f'{offset:.3f}' for offset in offsets[:4])}"
) )
try:
with serial.Serial("/dev/ttyACM0", 9600, timeout=3) as con1, serial.Serial("/dev/ttyACM1", 9600, timeout=3) as con2: 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") logger.warning("Connected to serial ports")
last_write = time.time() last_write = time.time()
...@@ -149,14 +151,13 @@ def main(config: Any) -> None: ...@@ -149,14 +151,13 @@ def main(config: Any) -> None:
n = 0 n = 0
recv1, recv2 = None, None recv1, recv2 = None, None
off1, off2 = None, None off1, off2 = None, None
while datetime.datetime.now() - datetime.timedelta(seconds=delta_time) < end_time: while datetime.datetime.now() - datetime.timedelta(seconds=4 * delta_time) < end_time:
try: try:
new_data = data.copy() new_data = data.copy()
# offsets for writing data of each arduino in correct column # offsets for writing data of each arduino in correct column
con1.write(1) con1.write(1)
off1 = 0 if int(convert(con1.readline())) == 1.0 else 4 off1 = 0 if int(convert(con1.readline())) == 1.0 else 4
...@@ -187,6 +188,9 @@ def main(config: Any) -> None: ...@@ -187,6 +188,9 @@ def main(config: Any) -> None:
n = 0 n = 0
data = np.zeros((8,)) data = np.zeros((8,))
last_write = time.time() last_write = time.time()
except serial.serialutil.SerialException:
logger.exception("SerialException was caught, shutting down.")
sys.exit(15)
fh[0].doRollover() fh[0].doRollover()
......
...@@ -136,7 +136,7 @@ def loop(con1: serial.Serial, con2: serial.Serial): ...@@ -136,7 +136,7 @@ def loop(con1: serial.Serial, con2: serial.Serial):
def main() -> None: def main() -> None:
with serial.Serial("/dev/ttyACM0", 9600, timeout=3) as con1, serial.Serial("/dev/ttyACM1", 9600, timeout=3) as con2: 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) loop(con1, con2)
......
#!/bin/bash #!/bin/bash
ls -la .
ls -la data
ls -la log
pip install multiprocessing_logging
python3 measure.py
git add . git add .
git commit -m "New data" && git push --force git commit -m "New data" && git push --force
echo "" echo -e "Starting python script:\n"
loop_count = 1
while : ; do
python3 main.py 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment