diff options
Diffstat (limited to 'examples/magpi-9-cambot2/cambot.py')
| -rw-r--r-- | examples/magpi-9-cambot2/cambot.py | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/examples/magpi-9-cambot2/cambot.py b/examples/magpi-9-cambot2/cambot.py new file mode 100644 index 0000000..f1013ab --- /dev/null +++ b/examples/magpi-9-cambot2/cambot.py | |||
| @@ -0,0 +1,117 @@ | |||
| 1 | # This version uses new-style automatic setup/destroy/mapping | ||
| 2 | # Need to change /etc/webiopi | ||
| 3 | |||
| 4 | # Imports | ||
| 5 | import webiopi | ||
| 6 | |||
| 7 | # Retrieve GPIO lib | ||
| 8 | GPIO = webiopi.GPIO | ||
| 9 | |||
| 10 | # -------------------------------------------------- # | ||
| 11 | # Constants definition # | ||
| 12 | # -------------------------------------------------- # | ||
| 13 | |||
| 14 | # Left motor GPIOs | ||
| 15 | L1=17 # H-Bridge 1 | ||
| 16 | L2=18 # H-Bridge 2 | ||
| 17 | LS=21 # H-Bridge 1,2EN | ||
| 18 | |||
| 19 | # Right motor GPIOs | ||
| 20 | R1=23 # H-Bridge 3 | ||
| 21 | R2=24 # H-Bridge 4 | ||
| 22 | RS=25 # H-Bridge 3,4EN | ||
| 23 | |||
| 24 | # -------------------------------------------------- # | ||
| 25 | # Convenient PWM Function # | ||
| 26 | # -------------------------------------------------- # | ||
| 27 | |||
| 28 | # Set the speed of two motors | ||
| 29 | def set_speed(speed): | ||
| 30 | GPIO.pulseRatio(LS, speed) | ||
| 31 | GPIO.pulseRatio(RS, speed) | ||
| 32 | |||
| 33 | # -------------------------------------------------- # | ||
| 34 | # Left Motor Functions # | ||
| 35 | # -------------------------------------------------- # | ||
| 36 | |||
| 37 | def left_stop(): | ||
| 38 | GPIO.output(L1, GPIO.LOW) | ||
| 39 | GPIO.output(L2, GPIO.LOW) | ||
| 40 | |||
| 41 | def left_forward(): | ||
| 42 | GPIO.output(L1, GPIO.HIGH) | ||
| 43 | GPIO.output(L2, GPIO.LOW) | ||
| 44 | |||
| 45 | def left_backward(): | ||
| 46 | GPIO.output(L1, GPIO.LOW) | ||
| 47 | GPIO.output(L2, GPIO.HIGH) | ||
| 48 | |||
| 49 | # -------------------------------------------------- # | ||
| 50 | # Right Motor Functions # | ||
| 51 | # -------------------------------------------------- # | ||
| 52 | def right_stop(): | ||
| 53 | GPIO.output(R1, GPIO.LOW) | ||
| 54 | GPIO.output(R2, GPIO.LOW) | ||
| 55 | |||
| 56 | def right_forward(): | ||
| 57 | GPIO.output(R1, GPIO.HIGH) | ||
| 58 | GPIO.output(R2, GPIO.LOW) | ||
| 59 | |||
| 60 | def right_backward(): | ||
| 61 | GPIO.output(R1, GPIO.LOW) | ||
| 62 | GPIO.output(R2, GPIO.HIGH) | ||
| 63 | |||
| 64 | # -------------------------------------------------- # | ||
| 65 | # Macro definition part # | ||
| 66 | # -------------------------------------------------- # | ||
| 67 | @webiopi.macro | ||
| 68 | def go_forward(): | ||
| 69 | left_forward() | ||
| 70 | right_forward() | ||
| 71 | |||
| 72 | @webiopi.macro | ||
| 73 | def go_backward(): | ||
| 74 | left_backward() | ||
| 75 | right_backward() | ||
| 76 | |||
| 77 | @webiopi.macro | ||
| 78 | def turn_left(): | ||
| 79 | left_backward() | ||
| 80 | right_forward() | ||
| 81 | |||
| 82 | @webiopi.macro | ||
| 83 | def turn_right(): | ||
| 84 | left_forward() | ||
| 85 | right_backward() | ||
| 86 | |||
| 87 | @webiopi.macro | ||
| 88 | def stop(): | ||
| 89 | left_stop() | ||
| 90 | right_stop() | ||
| 91 | |||
| 92 | # Called by WebIOPi at script loading | ||
| 93 | def setup(): | ||
| 94 | # Setup GPIOs | ||
| 95 | GPIO.setFunction(LS, GPIO.PWM) | ||
| 96 | GPIO.setFunction(L1, GPIO.OUT) | ||
| 97 | GPIO.setFunction(L2, GPIO.OUT) | ||
| 98 | |||
| 99 | GPIO.setFunction(RS, GPIO.PWM) | ||
| 100 | GPIO.setFunction(R1, GPIO.OUT) | ||
| 101 | GPIO.setFunction(R2, GPIO.OUT) | ||
| 102 | |||
| 103 | set_speed(0.5) | ||
| 104 | stop() | ||
| 105 | |||
| 106 | |||
| 107 | # Called by WebIOPi at server shutdown | ||
| 108 | def destroy(): | ||
| 109 | # Reset GPIO functions | ||
| 110 | GPIO.setFunction(LS, GPIO.IN) | ||
| 111 | GPIO.setFunction(L1, GPIO.IN) | ||
| 112 | GPIO.setFunction(L2, GPIO.IN) | ||
| 113 | |||
| 114 | GPIO.setFunction(RS, GPIO.IN) | ||
| 115 | GPIO.setFunction(R1, GPIO.IN) | ||
| 116 | GPIO.setFunction(R2, GPIO.IN) | ||
| 117 | |||
