Yet Another WebIOPi+
 All Classes Namespaces Files Functions Variables Macros Pages
cambot.py
Go to the documentation of this file.
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 
42  GPIO.output(L1, GPIO.HIGH)
43  GPIO.output(L2, GPIO.LOW)
44 
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 
57  GPIO.output(R1, GPIO.HIGH)
58  GPIO.output(R2, GPIO.LOW)
59 
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()
71 
72 @webiopi.macro
76 
77 @webiopi.macro
78 def turn_left():
81 
82 @webiopi.macro
83 def turn_right():
84  left_forward()
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 
def right_backward
Definition: cambot.py:60
def right_stop
Definition: cambot.py:52
def destroy
Definition: cambot.py:108
def setup
Definition: cambot.py:93
def go_forward
Definition: cambot.py:68
def right_forward
Definition: cambot.py:56
def left_forward
Definition: cambot.py:41
def turn_left
Definition: cambot.py:78
def turn_right
Definition: cambot.py:83
def set_speed
Definition: cambot.py:29
def stop
Definition: cambot.py:88
def go_backward
Definition: cambot.py:73
def left_stop
Definition: cambot.py:37
def left_backward
Definition: cambot.py:45