slitaz-arm view rpi/piborg/TurtleBorg.py @ rev 188
Add support for PiBorg (A PiCy will be used at Paris)
author | Christophe Lincoln <pankso@slitaz.org> |
---|---|
date | Thu May 15 18:57:11 2014 +0200 (2014-05-15) |
parents | |
children |
line source
1 #!/usr/bin/env python
2 # coding: Latin-1
4 # Load library functions we want
5 import sys
6 import time
7 import RPi.GPIO as GPIO
8 GPIO.setmode(GPIO.BCM)
9 GPIO.setwarnings(False)
11 # Set which GPIO pins the drive outputs are connected to
12 DRIVE_1 = 4
13 DRIVE_2 = 18
14 DRIVE_3 = 8
15 DRIVE_4 = 7
17 # Set all of the drive pins as output pins
18 GPIO.setup(DRIVE_1, GPIO.OUT)
19 GPIO.setup(DRIVE_2, GPIO.OUT)
20 GPIO.setup(DRIVE_3, GPIO.OUT)
21 GPIO.setup(DRIVE_4, GPIO.OUT)
23 # Map of functions to drive pins
24 leftDrive = DRIVE_1 # Drive number for left motor
25 rightDrive = DRIVE_4 # Drive number for right motor
26 penDrive = DRIVE_3 # Drive number for pen solenoid
28 # Functions for the robot to perform
29 def MoveForward(n):
30 """Move forward for 'n' seconds"""
31 GPIO.output(leftDrive, GPIO.HIGH)
32 GPIO.output(rightDrive, GPIO.HIGH)
33 time.sleep(n)
34 GPIO.output(leftDrive, GPIO.LOW)
35 GPIO.output(rightDrive, GPIO.LOW)
37 def MoveLeft(n):
38 """Move left for 'n' seconds"""
39 GPIO.output(leftDrive, GPIO.HIGH)
40 GPIO.output(rightDrive, GPIO.LOW)
41 time.sleep(n)
42 GPIO.output(leftDrive, GPIO.LOW)
44 def MoveRight(n):
45 """Move right for 'n' seconds"""
46 GPIO.output(leftDrive, GPIO.LOW)
47 GPIO.output(rightDrive, GPIO.HIGH)
48 time.sleep(n)
49 GPIO.output(rightDrive, GPIO.LOW)
51 def PenUp(n):
52 """Lift the pen up"""
53 GPIO.output(penDrive, GPIO.LOW)
55 def PenDown(n):
56 """Place the pen down"""
57 GPIO.output(penDrive, GPIO.HIGH)
59 def HelpMessage(n):
60 """Display a list of available commands"""
61 print ''
62 print 'Available commands:'
63 commands = dCommands.keys()
64 commands.sort()
65 for command in commands:
66 print '% 10s - %s, %s' % (command, dCommands[command].func_name, dCommands[command].__doc__)
67 print ''
69 # Map of command names to functions
70 dCommands = {
71 'FORWARD':MoveForward,
72 'FD':MoveForward,
73 'LEFT':MoveLeft,
74 'LT':MoveLeft,
75 'RIGHT':MoveRight,
76 'RT':MoveRight,
77 'PENUP':PenUp,
78 'PU':PenUp,
79 'PENDOWN':PenDown,
80 'PD':PenDown,
81 'HELP':HelpMessage,
82 '?':HelpMessage
83 }
85 # If we have been run directly then look at command line
86 if __name__ == "__main__":
87 # Process command
88 if len(sys.argv) > 1:
89 # Extract the command name and value (if there is any)
90 command = sys.argv[1].upper()
91 if len(sys.argv) > 2:
92 sValue = sys.argv[2].upper()
93 else:
94 sValue = '0'
95 try:
96 fValue = float(sValue)
97 except:
98 fValue = 0.0
100 # Select the appropriate function and call it
101 if dCommands.has_key(command):
102 dCommands[command](fValue)
103 else:
104 print 'Command "%s" not recognised' % (command)
105 HelpMessage(fValue)
106 else:
107 # No command, display the help message
108 print 'Usage: %s command [n]' % (sys.argv[0])
109 HelpMessage(0)