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