Skip to content

Instantly share code, notes, and snippets.

@jamie23
Created February 2, 2015 17:08
Show Gist options
  • Save jamie23/bbf56059883e195e76c4 to your computer and use it in GitHub Desktop.
Save jamie23/bbf56059883e195e76c4 to your computer and use it in GitHub Desktop.
Amazebots py code
# Pi2Go Motor Test
# Moves: Forward, Reverse, turn Right, turn Left, Stop - then repeat
# Press Ctrl-C to stop
#
# Also demonstrates writing to the LEDs
#
# To check wiring is correct ensure the order of movement as above is correct
# Run using: sudo python motorTest.py
import pi2go, time
# Reading single character by forcing stdin to raw mode
import sys
import tty
import termios
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 chr(0x10 + ord(c3) - 65) # 16=Up, 17=Down, 18=Right, 19=Left arrows
# End of single character reading
speed = 30
pi2go.init()
# main loop
try:
while True:
# Take the user input as a string
# Split the input into two parts. The first being the direction of travel, and the second being the duration of time
commandString = raw_input()
commandDirection = commandString.strip()
commandTime = commandString.split(" ")
keyp = readkey()
if commandDirection.lower() == 'forward':
pi2go.forward(speed)
print 'Forward', commandTime
time.sleep(commandTime)
pi2go.stop()
elif commandDirection.lower() == 'reverse':
pi2go.reverse(speed)
print 'Reverse', commandTime
time.sleep(commandTime)
pi2go.stop()
elif commandDirection.lower() == 'left':
pi2go.spinLeft(speed)
print 'Spin Left', commandTime
time.sleep(commandTime)
pi2go.stop()
elif commandDirection.lower() == 'right':
pi2go.spinRight(speed)
print 'Spin Right', commandTime
time.sleep(commandTime)
pi2go.stop()
elif keyp == ' ':
pi2go.stop()
print 'Stop'
elif ord(keyp) == 3:
break
except KeyboardInterrupt:
# pass
pi2go.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment