【问题标题】:Getting a motor to move forward for a time duration? Raspberry Pi让电机向前移动一段时间?树莓派
【发布时间】:2015-08-22 16:08:54
【问题描述】:

我最近购买了 4tronix 的 Pi2go lite。作为提供的库的一部分,您可以前后移动,但我希望这会永远发生,在键盘按下时会发生这种情况,然后当不再按下按钮时不会发生任何操作。做了一些研究后,这似乎不是一个众所周知的过程,或者根本可以完成,但是我的解决方案是按键,该操作只发生半秒钟,以模仿按住键的过程。这怎么可能实现?提前谢谢你。下面是 GitHub 上的树莓派小哥提供的代码,但是一旦按下“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()

【问题讨论】:

  • 您可以编辑问题以添加您遇到问题的代码示例吗?
  • 您可能需要监控按键事件并在按键按下时触发“开始”功能,在按键按下时触发“停止”功能。
  • 如果键盘通过 USB 连接到 Pi,您可以读取 /dev/input/event0,可能使用 some evdev library。还是你是通过 ssh 登录的?
  • 查看this answer 了解使用 evdev 的更简单方法。

标签: python raspberry-pi


【解决方案1】:

这是我的代码。

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

【讨论】:

    【解决方案2】:

    最简单的方法是用 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()
    

    【讨论】:

    • 感谢您的回复,不幸的是没有运气,当我运行文件时它显示错误说 GTK 警告无法打开显示,也许这是我的错在某个地方?
    • 你在控制台模式吗?
    • 它在我的控制台/终端上的 pi 上,是的,我完全复制了代码
    • 哦,我以为你的树莓派是GUI模式,我给你找解决办法
    【解决方案3】:

    我有从同一站点购买的 initio,您可以在 pi2go.forward(speed) 下方的行中添加 time.sleep(0.5) 半秒,无论是在按键中还是在 pi2go.py 中的实际转发功能中

    【讨论】:

      猜你喜欢
      • 2022-12-05
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 1970-01-01
      • 2019-02-28
      • 1970-01-01
      • 2018-08-17
      • 1970-01-01
      相关资源
      最近更新 更多