-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadFSRs.py
59 lines (46 loc) · 2.09 KB
/
ReadFSRs.py
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
import serial
import csv
import time
def initialize_serial(port, baud_rate):
"""Initialize serial communication with Arduino."""
ser = serial.Serial(port, baud_rate, timeout=1)
return ser
def write_data(ser, csv_filename, num_channels=8):
"""Read data from Arduino and write to CSV file."""
with open(csv_filename, 'w', newline='') as csvfile:
csv_writer = csv.writer(csvfile)
# Create header with sensor indices
header = ['Timestamp'] + [f'FSR {i+1} (mV)' for i in range(num_channels)]
csv_writer.writerow(header)
try:
while True:
# Read data from Arduino
data = ser.readline().decode().strip()
if data:
# Get current timestamp
timestamp = time.strftime('%Y-%m-%d %H:%M:%S')
# Split the data into individual FSR readings
readings = data.split()
if len(readings) == num_channels:
# Convert readings to integers
readings = [int(value) for value in readings]
# Print data to console
print(f'Timestamp: {timestamp}, Readings: {readings}')
# Write data to CSV file
csv_writer.writerow([timestamp] + readings)
else:
print("Warning: Received incomplete data.")
except KeyboardInterrupt:
print('Data collection stopped.')
ser.close()
if __name__ == "__main__":
serial_port = '/dev/cu.usbmodem142101' # Change as needed
baud_rate = 9600 # Match with Arduino
csv_filename = 'FSR_Test_Data.csv' # Change for each test run
arduino_serial = initialize_serial(serial_port, baud_rate)
try:
write_data(arduino_serial, csv_filename)
except Exception as e:
print(f"An error occurred: {e}")
finally:
arduino_serial.close()