summaryrefslogtreecommitdiffstats
path: root/examples/magpi-9-cambot
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2013-12-25 13:25:16 +0100
committermanuel <manuel@mausz.at>2013-12-25 13:25:16 +0100
commit0c8c9ad976879f7c90f9915a60845ccb0cdb337d (patch)
tree162951b4713f3836f4114958a423e2c90ecf9c6b /examples/magpi-9-cambot
downloadwebiopi-0c8c9ad976879f7c90f9915a60845ccb0cdb337d.tar.gz
webiopi-0c8c9ad976879f7c90f9915a60845ccb0cdb337d.tar.bz2
webiopi-0c8c9ad976879f7c90f9915a60845ccb0cdb337d.zip
initial commit
Diffstat (limited to 'examples/magpi-9-cambot')
-rw-r--r--examples/magpi-9-cambot/cambot.py139
-rw-r--r--examples/magpi-9-cambot/index.html70
-rwxr-xr-xexamples/magpi-9-cambot/stream.sh11
3 files changed, 220 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
diff --git a/examples/magpi-9-cambot/index.html b/examples/magpi-9-cambot/index.html
new file mode 100644
index 0000000..db69b41
--- /dev/null
+++ b/examples/magpi-9-cambot/index.html
@@ -0,0 +1,70 @@
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta name="viewport" content = "height = device-height, width = 420, user-scalable = no" />
6 <title>CamBot</title>
7 <script type="text/javascript" src="/webiopi.js"></script>
8 <script type="text/javascript">
9 function init() {
10 var button;
11
12 button = webiopi().createButton("bt_up", "/\\", go_forward, stop);
13 $("#up").append(button);
14
15 button = webiopi().createButton("bt_left", "<", turn_left, stop);
16 $("#middle").append(button);
17
18 button = webiopi().createButton("bt_stop", "X", stop);
19 $("#middle").append(button);
20
21 button = webiopi().createButton("bt_right", ">", turn_right, stop);
22 $("#middle").append(button);
23
24 button = webiopi().createButton("bt_down", "\\/", go_backward, stop);
25 $("#down").append(button);
26 }
27
28 function go_forward() {
29 webiopi().callMacro("go_forward");
30 }
31
32 function go_backward() {
33 webiopi().callMacro("go_backward");
34 }
35
36 function turn_right() {
37 webiopi().callMacro("turn_right");
38 }
39
40 function turn_left() {
41 webiopi().callMacro("turn_left");
42 }
43
44 function stop() {
45 webiopi().callMacro("stop");
46 }
47
48 webiopi().ready(init);
49
50 </script>
51 <style type="text/css">
52 button {
53 margin: 5px 5px 5px 5px;
54 width: 50px;
55 height: 50px;
56 font-size: 24pt;
57 font-weight: bold;
58 color: black;
59 }
60 </style>
61</head>
62<body>
63 <div id="content" align="center">
64 <img width="320" height="240" src="http://raspberrypi:8001/?action=stream"><br/>
65 <div id="up"></div>
66 <div id="middle"></div>
67 <div id="down"></div>
68 </div>
69</body>
70</html>
diff --git a/examples/magpi-9-cambot/stream.sh b/examples/magpi-9-cambot/stream.sh
new file mode 100755
index 0000000..12d6579
--- /dev/null
+++ b/examples/magpi-9-cambot/stream.sh
@@ -0,0 +1,11 @@
1#!/bin/sh
2
3STREAMER=mjpg_streamer
4DEVICE=/dev/video0
5RESOLUTION=320x240
6FRAMERATE=25
7HTTP_PORT=8001
8
9PLUGINPATH=/usr/local/lib
10
11$STREAMER -i "$PLUGINPATH/input_uvc.so -n -d $DEVICE -r $RESOLUTION -f $FRAMERATE" -o "$PLUGINPATH/output_http.so -n -p $HTTP_PORT"