0
- When I press the keyboard, for example the "w" to go forward, the robot goes forward and sometimes another motor turns on together.
- When I press a command and keep pressed, when stopping pressing the engine it is still running, due to the time set(accumulates), a solution is to define a keydown/keyup, how can I do this?
ADDITIONAL INFORMATION: I am using a Raspberry and a bridge h L298N, to start two engines (I am doing tests for a robot).
I expect suggestions for improvements and thank you in advance.
Code below:
import RPi.GPIO as gpio
import time
import sys
import Tkinter as tk
def init():
gpio.setmode(gpio.BOARD)
gpio.setup(7, gpio.OUT)
gpio.setup(11, gpio.OUT)
gpio.setup(13, gpio.OUT)
gpio.setup(15, gpio.OUT)
def frente(tf):
gpio.output(7, False)
gpio.output(11, True)
gpio.output(13, True)
gpio.output(15, True)
time.sleep(tf)
gpio.cleanup()
def tras(tf):
gpio.output(7, True)
gpio.output(11, False)
gpio.output(13, False)
gpio.output(15, False)
time.sleep(tf)
gpio.cleanup()
def esq(tf):
gpio.output(7, True)
gpio.output(11, True)
gpio.output(13, False)
gpio.output(15, True)
time.sleep(tf)
gpio.cleanup()
def dir(tf):
gpio.output(7, False)
gpio.output(11, False)
gpio.output(13, True)
gpio.output(15, False)
time.sleep(tf)
gpio.cleanup()
def key_input(event):
init()
print 'Key:', event.char
key_press = event.char
sleep_time = (0.02)
if key_press.lower() == 'w':
frente(sleep_time)
elif key_press.lower() == 's':
tras(sleep_time)
elif key_press.lower() == 'a':
esq(sleep_time)
elif key_press.lower() == 'd':
dir(sleep_time)
janela = tk.Tk()
janela.bind('<KeyPress>', key_input)
janela.mainloop()
That one link should help you.
– Caio de Paula Silva