modify the motor sleep time, to make it more efficient

This commit is contained in:
2019-06-02 12:48:14 +10:00
parent c184821b69
commit 74f318a2af
+31 -23
View File
@@ -3,26 +3,26 @@ import usb.util
import time
import RPi.GPIO as GPIO
USB_IF = 0 # Interface
USB_TIMEOUT = 5 # Timeout in MS
USB_IF = 0 # Interface
USB_TIMEOUT = 5 # Timeout in MS
SLEEP_TIME = 0.05
SLEEP_TIME = 0.01
BTN_LEFT = 4
BTN_LEFT = 4
BTN_RIGHT = 8
BTN_DOWN = 2
BTN_UP = 1
BTN_STOP = 44 # Space
BTN_EXIT = 32 # ESC
BTN_DOWN = 2
BTN_UP = 1
BTN_STOP = 44 # Space
BTN_EXIT = 32 # ESC
USB_VENDOR = 0x045e # Rii
USB_PRODUCT = 0x028e # Mini Wireless Keyboard
USB_VENDOR = 0x045E # Rii
USB_PRODUCT = 0x028E # Mini Wireless Keyboard
ForwardRight=20
BackwardRight=26
ForwardRight = 20
BackwardRight = 26
ForwardLeft=18
BackwardLeft=17
ForwardLeft = 18
BackwardLeft = 17
GPIO.setmode(GPIO.BCM)
@@ -37,30 +37,35 @@ def forward():
GPIO.output(ForwardRight, GPIO.HIGH)
time.sleep(SLEEP_TIME)
def backward():
GPIO.output(BackwardRight, GPIO.HIGH)
GPIO.output(BackwardLeft, GPIO.HIGH)
time.sleep(SLEEP_TIME)
def turn_left():
GPIO.output(ForwardLeft, GPIO.HIGH)
GPIO.output(BackwardRight, GPIO.HIGH)
time.sleep(SLEEP_TIME)
def turn_right():
GPIO.output(ForwardRight, GPIO.HIGH)
GPIO.output(BackwardLeft, GPIO.HIGH)
time.sleep(SLEEP_TIME)
def clear():
GPIO.output(ForwardLeft, GPIO.LOW)
GPIO.output(ForwardRight, GPIO.LOW)
GPIO.output(BackwardRight, GPIO.LOW)
GPIO.output(BackwardLeft, GPIO.LOW)
dev = usb.core.find(idVendor=USB_VENDOR, idProduct=USB_PRODUCT)
endpoint = dev[0][(0,0)][0]
endpoint = dev[0][(0, 0)][0]
if dev.is_kernel_driver_active(USB_IF) is True:
dev.detach_kernel_driver(USB_IF)
@@ -71,36 +76,39 @@ while True:
control = None
try:
control = dev.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize, USB_TIMEOUT)
control = dev.read(
endpoint.bEndpointAddress, endpoint.wMaxPacketSize, USB_TIMEOUT
)
# print(control)
if control[0] == 0:
# print('clear')
clear()
except:
pass
if control != None:
if control is not None:
if BTN_DOWN in control:
print('-')
print("-")
backward()
if BTN_UP in control:
print('+')
print("+")
forward()
if BTN_LEFT in control:
print('<-')
print("<-")
turn_left()
if BTN_RIGHT in control:
print('->')
print("->")
turn_right()
if BTN_STOP in control:
print('stop')
print("stop")
if BTN_EXIT in control:
clear()
exit()
else:
clear()
time.sleep(SLEEP_TIME) # Let CTRL+C actually exit
# Let CTRL+C actually exit
time.sleep(SLEEP_TIME)