我最近购买了4tronix的Pi2go lite。作为提供的库的一部分,您可以前进和后退,但这种情况永远发生,我希望,在键盘按下发生这种情况,然后当按钮不再按下时不会发生任何操作。做过一些研究之后,这看起来并不是一个众所周知的过程,或者根本无法完成,但是我的解决方案是按键操作,操作仅发生半秒钟,模仿持有钥匙的过程。怎么能实现这一目标?先谢谢你。下面是GitHub中覆盆子Pi Guy提供的代码,但是一旦' W'按下按键很难控制,因为当你松开按键时它不会停止。
import pi2go, time
# Reading a button press from your keyboard, don't worry about this too much!
import sys
import tty
import termios
UP = 0
DOWN = 1
RIGHT = 2
LEFT = 3
def readchar():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
if ch == '0x03':
raise KeyboardInterrupt
return ch
def readkey(getchar_fn=None):
getchar = getchar_fn or readchar
c1 = getchar()
if ord(c1) != 0x1b:
return c1
c2 = getchar()
if ord(c2) != 0x5b:
return c1
c3 = getchar()
return ord(c3) - 65 # 0=Up, 1=Down, 2=Right, 3=Left arrows
speed = 30
pi2go.init()
try:
while True:
keyp = readkey()
if keyp == 'w' or keyp == UP:
pi2go.forward(speed)
print 'Forward', speed
elif keyp == 's' or keyp == DOWN:
pi2go.reverse(speed)
print 'Backward', speed
elif keyp == 'd' or keyp == RIGHT:
pi2go.spinRight(speed)
print 'Spin Right', speed
elif keyp == 'a' or keyp == LEFT:
pi2go.spinLeft(speed)
print 'Spin Left', speed
elif keyp == '.' or keyp == '>':
speed = min(100, speed+10)
print 'Speed+', speed
elif keyp == ',' or keyp == '<':
speed = max (0, speed-10)
print 'Speed-', speed
elif keyp == ' ':
pi2go.stop()
print 'Stop'
elif ord(keyp) == 3:
break
except KeyboardInterrupt:
pi2go.cleanup()
答案 0 :(得分:0)
最简单的方法是用一个检测按键的Gtk gui制作一个简单的python程序
import gtk
class DK:
def __init__(self):
window = gtk.Window()
window.add_events(gtk.gdk.KEY_PRESS_MASK)
window.connect("key-press-event", self.forward)
window.show()
def forward(self, widget, event):
if event.keyval == 119: #W key
print "do what you want with this"
forward(100)
else:
stop()
DK()
gtk.main()
答案 1 :(得分:0)
我有从同一网站购买的initio,您可以在pi2go.forward(速度)下面的行上添加time.sleep(0.5)半秒,无论是在按键还是在pi2go.py中的实际前向功能< / p>
答案 2 :(得分:0)
这是我的代码。
data != null && data.getData() != null