-
Notifications
You must be signed in to change notification settings - Fork 0
/
backward.py
43 lines (35 loc) · 1.23 KB
/
backward.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
import time
# Attempt to create a serial connection
try:
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1) # Adjust as needed
print(f"Connected to {ser.portstr}") # Confirm connection
except serial.SerialException as e:
print(f"Error: {e}")
exit(1)
def send_command(command):
try:
# Ensure command is sent as a single byte
command_byte = bytes([command])
ser.write(command_byte)
print(f"Command sent: {command} (0x{command:02X})") # Show command in hex for clarity
except Exception as e:
print(f"Failed to send command: {e}")
# Move motor forward
send_command(64 + 43) # Move motor 1 forward at medium speed
send_command(192 + 43) # Move motor 2 forward at medium speed
time.sleep(2) # Simulate running for 2 seconds
# Stop motors
send_command(64) # Stop motor 1
send_command(192) # Stop motor 2
time.sleep(1) # Simulate pause for a second
# Move motor backward
send_command(64 - 43) # Move motor 1 backward at medium speed
send_command(192 - 43) # Move motor 2 backward at medium speed
time.sleep(2)
# Stop motors
send_command(64) # Stop motor 1
send_command(192) # Stop motor 2
# Close the serial connection
ser.close()
print("Serial connection closed.")