-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathservo_control_module.py
More file actions
29 lines (25 loc) · 1.05 KB
/
servo_control_module.py
File metadata and controls
29 lines (25 loc) · 1.05 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
import serial
import time
class ServoControlModule:
def __init__(self, port='/dev/ttyUSB0', baudrate=9600):
try:
self.serial_connection = serial.Serial(port, baudrate, timeout=1)
time.sleep(2) # Wait for the connection to establish
except Exception as e:
raise RuntimeError(f"Failed to initialize serial connection: {str(e)}")
def execute(self, commands):
responses = []
try:
for servo, angle in commands.items():
command = f"{servo} {angle}\n"
self.serial_connection.write(command.encode('utf-8'))
response = self.serial_connection.readline().decode('utf-8').strip()
responses.append(response)
except Exception as e:
responses.append(f"Error executing servo command: {str(e)}")
return responses
def close(self):
try:
self.serial_connection.close()
except Exception as e:
raise RuntimeError(f"Failed to close serial connection: {str(e)}")