0

I have recently purchase the Pi2go lite by 4tronix. As part of the provided library, you can move forward and back, but this takes place forever, I want, on a keyboard press for that to happen, then when the button is no longer pressed no action occurs. Having done some research this does not see to be a well known process or can be done at all, however my solution would be on a key press, the operation to only occur for half a second, to imitate the process of holding a key. How could this be achieved? Thank you in advanced. Below is the code supplied by the raspberry Pi Guy in GitHub, however once the 'W' key is pressed it is hard to control as it doesn't stop when you let go of the key.

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()
  • Can you edit the question to add a sample of the code you are having a problem with? – Martin Evans Aug 22 '15 at 16:15
  • You will probably need to monitor key events and trigger a 'go' fucntion on key down and a 'stop' function on key up. – kylieCatt Aug 22 '15 at 16:20
  • If the keyboard is connected via USB to the Pi you could read /dev/input/event0, maybe using [some evdev library](http://python-evdev.readthedocs.org/en/latest/tutorial.html). Or are you logged in via ssh? – maxy Aug 22 '15 at 17:16
  • See [this answer](http://stackoverflow.com/a/16682549/235548) for a simpler way to use evdev. – maxy Aug 24 '15 at 13:08

3 Answers3

0

The easiest way is to make a simple python program with a Gtk gui that detects keypress

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

i have the initio i bought from same site you can add a time.sleep(0.5) half second on the line below the pi2go.forward(speed) either in the keypress or the actual forward function in the pi2go.py

0

This is my code.

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
OmamArmy
  • 120
  • 1
  • 6