diff options
Diffstat (limited to 'examples/magpi-9-cambot/cambot.py')
| -rw-r--r-- | examples/magpi-9-cambot/cambot.py | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/examples/magpi-9-cambot/cambot.py b/examples/magpi-9-cambot/cambot.py new file mode 100644 index 0000000..ed2e499 --- /dev/null +++ b/examples/magpi-9-cambot/cambot.py | |||
| @@ -0,0 +1,139 @@ | |||
| 1 | # Imports | ||
| 2 | import webiopi | ||
| 3 | |||
| 4 | # Retrieve GPIO lib | ||
| 5 | GPIO = webiopi.GPIO | ||
| 6 | |||
| 7 | # -------------------------------------------------- # | ||
| 8 | # Constants definition # | ||
| 9 | # -------------------------------------------------- # | ||
| 10 | |||
| 11 | # Left motor GPIOs | ||
| 12 | L1=9 # H-Bridge 1 | ||
| 13 | L2=10 # H-Bridge 2 | ||
| 14 | LS=11 # H-Bridge 1,2EN | ||
| 15 | |||
| 16 | # Right motor GPIOs | ||
| 17 | R1=23 # H-Bridge 3 | ||
| 18 | R2=24 # H-Bridge 4 | ||
| 19 | RS=25 # H-Bridge 3,4EN | ||
| 20 | |||
| 21 | # -------------------------------------------------- # | ||
| 22 | # Convenient PWM Function # | ||
| 23 | # -------------------------------------------------- # | ||
| 24 | |||
| 25 | # Set the speed of two motors | ||
| 26 | def set_speed(speed): | ||
| 27 | GPIO.pulseRatio(LS, speed) | ||
| 28 | GPIO.pulseRatio(RS, speed) | ||
| 29 | |||
| 30 | # -------------------------------------------------- # | ||
| 31 | # Left Motor Functions # | ||
| 32 | # -------------------------------------------------- # | ||
| 33 | |||
| 34 | def left_stop(): | ||
| 35 | GPIO.output(L1, GPIO.LOW) | ||
| 36 | GPIO.output(L2, GPIO.LOW) | ||
| 37 | |||
| 38 | def left_forward(): | ||
| 39 | GPIO.output(L1, GPIO.HIGH) | ||
| 40 | GPIO.output(L2, GPIO.LOW) | ||
| 41 | |||
| 42 | def left_backward(): | ||
| 43 | GPIO.output(L1, GPIO.LOW) | ||
| 44 | GPIO.output(L2, GPIO.HIGH) | ||
| 45 | |||
| 46 | # -------------------------------------------------- # | ||
| 47 | # Right Motor Functions # | ||
| 48 | # -------------------------------------------------- # | ||
| 49 | def right_stop(): | ||
| 50 | GPIO.output(R1, GPIO.LOW) | ||
| 51 | GPIO.output(R2, GPIO.LOW) | ||
| 52 | |||
| 53 | def right_forward(): | ||
| 54 | GPIO.output(R1, GPIO.HIGH) | ||
| 55 | GPIO.output(R2, GPIO.LOW) | ||
| 56 | |||
| 57 | def right_backward(): | ||
| 58 | GPIO.output(R1, GPIO.LOW) | ||
| 59 | GPIO.output(R2, GPIO.HIGH) | ||
| 60 | |||
| 61 | # -------------------------------------------------- # | ||
| 62 | # Macro definition part # | ||
| 63 | # -------------------------------------------------- # | ||
| 64 | |||
| 65 | def go_forward(): | ||
| 66 | left_forward() | ||
| 67 | right_forward() | ||
| 68 | |||
| 69 | def go_backward(): | ||
| 70 | left_backward() | ||
| 71 | right_backward() | ||
| 72 | |||
| 73 | def turn_left(): | ||
| 74 | left_backward() | ||
| 75 | right_forward() | ||
| 76 | |||
| 77 | def turn_right(): | ||
| 78 | left_forward() | ||
| 79 | right_backward() | ||
| 80 | |||
| 81 | def stop(): | ||
| 82 | left_stop() | ||
| 83 | right_stop() | ||
| 84 | |||
| 85 | # -------------------------------------------------- # | ||
| 86 | # Initialization part # | ||
| 87 | # -------------------------------------------------- # | ||
| 88 | |||
| 89 | # Setup GPIOs | ||
| 90 | GPIO.setFunction(LS, GPIO.PWM) | ||
| 91 | GPIO.setFunction(L1, GPIO.OUT) | ||
| 92 | GPIO.setFunction(L2, GPIO.OUT) | ||
| 93 | |||
| 94 | GPIO.setFunction(RS, GPIO.PWM) | ||
| 95 | GPIO.setFunction(R1, GPIO.OUT) | ||
| 96 | GPIO.setFunction(R2, GPIO.OUT) | ||
| 97 | |||
| 98 | set_speed(0.5) | ||
| 99 | stop() | ||
| 100 | |||
| 101 | # -------------------------------------------------- # | ||
| 102 | # Main server part # | ||
| 103 | # -------------------------------------------------- # | ||
| 104 | |||
| 105 | |||
| 106 | # Instantiate the server on the port 8000, it starts immediately in its own thread | ||
| 107 | server = webiopi.Server(port=8000, login="cambot", password="cambot") | ||
| 108 | |||
| 109 | # Register the macros so you can call it with Javascript and/or REST API | ||
| 110 | |||
| 111 | server.addMacro(go_forward) | ||
| 112 | server.addMacro(go_backward) | ||
| 113 | server.addMacro(turn_left) | ||
| 114 | server.addMacro(turn_right) | ||
| 115 | server.addMacro(stop) | ||
| 116 | |||
| 117 | # -------------------------------------------------- # | ||
| 118 | # Loop execution part # | ||
| 119 | # -------------------------------------------------- # | ||
| 120 | |||
| 121 | # Run our loop until CTRL-C is pressed or SIGTERM received | ||
| 122 | webiopi.runLoop() | ||
| 123 | |||
| 124 | # -------------------------------------------------- # | ||
| 125 | # Termination part # | ||
| 126 | # -------------------------------------------------- # | ||
| 127 | |||
| 128 | # Stop the server | ||
| 129 | server.stop() | ||
| 130 | |||
| 131 | # Reset GPIO functions | ||
| 132 | GPIO.setFunction(LS, GPIO.IN) | ||
| 133 | GPIO.setFunction(L1, GPIO.IN) | ||
| 134 | GPIO.setFunction(L2, GPIO.IN) | ||
| 135 | |||
| 136 | GPIO.setFunction(RS, GPIO.IN) | ||
| 137 | GPIO.setFunction(R1, GPIO.IN) | ||
| 138 | GPIO.setFunction(R2, GPIO.IN) | ||
| 139 | |||
