-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforce_logging.py
More file actions
85 lines (72 loc) · 2.5 KB
/
Copy pathforce_logging.py
File metadata and controls
85 lines (72 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import serial
import threading
import sys
import time
SERIAL_PORT = '/dev/ttyACM1'
BAUD_RATE = 9600
# log file names with current date and time
LOG_FILE = f'arduino_log_{time.strftime("%Y%m%d_%H%M%S")}.txt'
# log summary file with current date and time
SUMMARY_FILE = f'summary_{time.strftime("%Y%m%d_%H%M%S")}.txt'
recording = False
running = True
def read_serial(ser, stats):
with open(LOG_FILE, 'w') as f:
while running:
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8', errors='replace').strip()
print(line)
if recording:
try:
value = float(line)
f.write(f"{value}\n")
f.flush()
if value > 0:
stats['total'] += 1
if value < 20:
stats['below_20'] += 1
except ValueError:
pass # Ignore non-numeric lines
def main():
global recording, running
stats = {'total': 0, 'below_20': 0}
try:
ser = serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=1)
except Exception as e:
print(f"Failed to open serial port: {e}")
sys.exit(1)
print("Connected to Arduino.")
print("Press 'r' to start recording, 's' to stop, 'q' to quit.")
t = threading.Thread(target=read_serial, args=(ser, stats), daemon=True)
t.start()
try:
while True:
cmd = input().strip().lower()
if cmd == 'r':
ser.write(b'r')
recording = True
print("[Recording started]")
elif cmd == 's':
ser.write(b's')
recording = False
print("[Recording stopped]")
elif cmd == 'q':
ser.write(b's')
running = False
break
except KeyboardInterrupt:
running = False
finally:
ser.close()
print("Serial port closed.")
# Calculate and write summary
if stats['total'] > 0:
percent = 100.0 * stats['below_20'] / stats['total']
else:
percent = 0.0
with open(SUMMARY_FILE, 'w') as sf:
sf.write(f"Percentage of force readings > 0 N that were < 20 N: {percent:.2f}%\n")
sf.write(f"Total valid samples: {stats['total']}\n")
print(f"Saved summary to {SUMMARY_FILE}\nExiting...")
if __name__ == '__main__':
main()