首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >让马达前进一段时间?覆盆子皮

让马达前进一段时间?覆盆子皮
EN

Stack Overflow用户
提问于 2015-08-22 16:08:54
回答 3查看 891关注 0票数 0

我最近购买了4 4tronix的Pi2go lite。作为提供的库的一部分,您可以向前或向后移动,但这是永远发生的,我希望,在键盘上按下就会发生,然后当按钮不再按下时,就不会发生任何动作。在做了一些研究后,这并不是一个众所周知的过程,或者根本可以做到,但是我的解决办法是按下键,操作只需半秒钟,就可以模拟按住键的过程。如何才能做到这一点?谢谢你的进阶。下面是由GitHub中的raspberry Pi Guy提供的代码,但是一旦按下'W‘键,就很难控制它,因为当您松开键时,它不会停止。

代码语言:javascript
复制
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()
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-08-08 10:33:29

这是我的密码。

代码语言:javascript
复制
elif key == 'w':
        speed = min(100, speed+10)
        pi2go.forward(speed)
        print 'Forward', speed
        time.sleep(.100)
    pi2go.stop()
elif key == 's':
    speed = min(100, speed+10)
        pi2go.reverse(speed)
        print 'Reverse', speed
        time.sleep(.100)
    pi2go.stop()
elif key == 'd':
    speed = min(100, speed+10)
        pi2go.spinRight(speed)
        print 'Spin Right', speed
        time.sleep(.100)
    pi2go.stop()
elif key == 'a':
    speed = min(100, speed+10)
        pi2go.spinLeft(speed)
        print 'Spin Left', speed
    time.sleep(.100)
    pi2go.stop()
    elif key == 'b':
        speed = min(100, speed+10)
        print 'Speed+', speed
    elif key == 'v':
        speed = max (0, speed-10)
        print 'Speed-', speed
    elif key == ' ':
        pi2go.stop()
        print 'Stop'
    elif ord(key) == 3:
    break
票数 0
EN

Stack Overflow用户

发布于 2015-08-22 16:20:27

最简单的方法是使用检测按键的Gtk gui制作一个简单的python程序。

代码语言:javascript
复制
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()
票数 0
EN

Stack Overflow用户

发布于 2015-09-02 14:31:41

我有从同一个站点购买的初始化,您可以在pi2go.forward(速度)下的行中添加一个pi2go.forward(0.5秒),或者在键按下或在pi2go.py中的实际前向函数中添加一个

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32158154

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档