-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdriver.py
91 lines (74 loc) · 2.62 KB
/
driver.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
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
86
87
88
89
90
91
from audio_utils import run_threads
from arduino_controller import create_commands, connect_arduino, sendCommand, setupTurntable, BUTTON_DELAY
from contextlib import closing
from time import sleep
from SignalProcessing import process_files, NDArray
# default serial settings
BAUD = 115200
def main(play_file: str, rec_dir: str, angleStep: int, freq: int, port: str, baud = BAUD) -> NDArray:
'''
Main driver that handles rotation, audio playback, audio recording, and plotting results.
Parameters
----------
play_file : str
Path to file to send to speaker
rec_dir : str
Path to directory to store recorded data
angleStep : int
Angle increment in degrees
freq : int
Frequency to plot in Hz
port : str
Serial port that Arduino is connected to
baud : str, optional
Baud rate of Arduino Serial connection. This is always set to 115,200
'''
commands, batch_size = create_commands(angleStep)
with closing(connect_arduino(port, baud)) as arduino:
setupTurntable(arduino)
for idx, (sleepTime, command) in enumerate(commands):
# status of angle
if idx != 0 and is_int(360 / idx):
print(f'Current angle: {idx} degrees')
# only execute audio if done rotating
if idx % batch_size == 0:
run_threads(play_file, rec_dir)
sendCommand(sleepTime, command, arduino)
print('test complete, beginning processing')
return process_files(angleStep, freq, rec_dir)
def rotate_only(angleStep: int, port: str, baud = BAUD):
'''
Debug function that only performs the rotation component of the test.
Parameters
----------
angleStep : int
Angle increment in degrees
port : str
Serial port that Arduino is connected to
baud : str, optional
Baud rate of Arduino Serial connection. This is always set to 115,200
'''
commands, _ = create_commands(angleStep)
with closing(connect_arduino(port, baud)) as arduino:
setupTurntable(arduino)
for sleepTime, command in commands:
sleep(max(0, BUTTON_DELAY - sleepTime))
sendCommand(sleepTime, command, arduino)
print('test complete')
def is_int(val) -> bool:
'''
Checks if a value is an integer.
Parameters
----------
val
Value to test as an integer
Returns
-------
True if val is an integer, False otherwise
'''
try:
return int(val) == float(val)
except:
return False
if __name__ == '__main__':
rotate_only(30, 'COM7')