-
Notifications
You must be signed in to change notification settings - Fork 0
/
motorcontrol_pico_class.py
42 lines (31 loc) · 1.33 KB
/
motorcontrol_pico_class.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
import serial
COM_PORT = "COM6" # Instrument port location
TIMEOUT = 1
class StepperValueError(Exception):
pass
class SerialInstrument:
def __init__(self,
port: str,
timeout: float | None = 1,
**serial_kwargs) -> None:
self._connection = serial.Serial(
port=port,
timeout=timeout,
write_timeout=timeout,
**serial_kwargs
)
def write(self , direct : str , freq : int, degree: int):
if freq < 0 or freq > 500:
raise StepperValueError("Frequency out of range - must be between 0 and 500")
elif direct != "left" and direct != "right":
raise StepperValueError('Direction must be "left" or "right"')
else:
command = f'{{"direction": "{direct}", "freq": {freq}, "degree": {degree}}}'+"\n"
self._connection.write(command.encode())
return self._connection.readline().decode()
def disconnect(self) -> None:
self._connection.close()
if __name__ == "__main__":
instrument = SerialInstrument(COM_PORT, TIMEOUT) # example - object Init, open Port
print(instrument.write("left",8,60)) # send instructions : direction, speed(Hz), angle in Deg
instrument.disconnect() # close Port