#!/usr/bin/env python # coding: Latin-1 # Load library functions we want import sys import time import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) # Set which GPIO pins the drive outputs are connected to DRIVE_1 = 4 DRIVE_2 = 18 DRIVE_3 = 8 DRIVE_4 = 7 # Set all of the drive pins as output pins GPIO.setup(DRIVE_1, GPIO.OUT) GPIO.setup(DRIVE_2, GPIO.OUT) GPIO.setup(DRIVE_3, GPIO.OUT) GPIO.setup(DRIVE_4, GPIO.OUT) # Map of functions to drive pins leftDrivePos = DRIVE_1 # Drive number for left motor positive relay leftDriveNeg = DRIVE_2 # Drive number for left motor negative relay rightDrivePos = DRIVE_3 # Drive number for right motor positive relay rightDriveNeg = DRIVE_4 # Drive number for right motor negative relay # Functions for the robot to perform def MoveForward(n): """Move forward for 'n' seconds""" GPIO.output(leftDriveNeg, GPIO.LOW) GPIO.output(rightDriveNeg, GPIO.LOW) GPIO.output(leftDrivePos, GPIO.HIGH) GPIO.output(rightDrivePos, GPIO.HIGH) time.sleep(n) GPIO.output(leftDrivePos, GPIO.LOW) GPIO.output(rightDrivePos, GPIO.LOW) def MoveLeft(n): """Move left for 'n' seconds""" GPIO.output(leftDrivePos, GPIO.LOW) GPIO.output(rightDriveNeg, GPIO.LOW) GPIO.output(leftDriveNeg, GPIO.HIGH) GPIO.output(rightDrivePos, GPIO.HIGH) time.sleep(n) GPIO.output(leftDriveNeg, GPIO.LOW) GPIO.output(rightDrivePos, GPIO.LOW) def MoveRight(n): """Move right for 'n' seconds""" GPIO.output(leftDriveNeg, GPIO.LOW) GPIO.output(rightDrivePos, GPIO.LOW) GPIO.output(leftDrivePos, GPIO.HIGH) GPIO.output(rightDriveNeg, GPIO.HIGH) time.sleep(n) GPIO.output(leftDrivePos, GPIO.LOW) GPIO.output(rightDriveNeg, GPIO.LOW) def MoveBackward(n): """Move backward for 'n' seconds""" GPIO.output(leftDrivePos, GPIO.LOW) GPIO.output(rightDrivePos, GPIO.LOW) GPIO.output(leftDriveNeg, GPIO.HIGH) GPIO.output(rightDriveNeg, GPIO.HIGH) time.sleep(n) GPIO.output(leftDriveNeg, GPIO.LOW) GPIO.output(rightDriveNeg, GPIO.LOW) def HelpMessage(n): """Display a list of available commands""" print '' print 'Available commands:' commands = dCommands.keys() commands.sort() for command in commands: print '% 10s - %s, %s' % (command, dCommands[command].func_name, dCommands[command].__doc__) print '' # Map of command names to functions dCommands = { 'FORWARD':MoveForward, 'FD':MoveForward, 'LEFT':MoveLeft, 'LT':MoveLeft, 'RIGHT':MoveRight, 'RT':MoveRight, 'BACKWARD':MoveBackward, 'BD':MoveBackward, 'HELP':HelpMessage, '?':HelpMessage } # If we have been run directly then look at command line if __name__ == "__main__": # Process command if len(sys.argv) > 1: # Extract the command name and value (if there is any) command = sys.argv[1].upper() if len(sys.argv) > 2: sValue = sys.argv[2].upper() else: sValue = '0' try: fValue = float(sValue) except: fValue = 0.0 # Select the appropriate function and call it if dCommands.has_key(command): dCommands[command](fValue) else: print 'Command "%s" not recognised' % (command) HelpMessage(fValue) else: # No command, display the help message print 'Usage: %s command [n]' % (sys.argv[0]) HelpMessage(0)