Documente Academic
Documente Profesional
Documente Cultură
com/2013/12/construye-tu-
propio-rover-teledirigido-con-rpi-webiopi.html
Después conectamos los motores del coche a los pines de las salidas
correspondientes: Motor A o izquierdo y Motor B o derecho e
interconectamos los pines IN1, 2, 3 y 4 a las salidas digitales de la GPIO
de la RPi (17, 27 y 10, 9 respectivamente).
pi@escorial ~ $ uname -a
Linux escorial 3.6.11+ #538 PREEMPT Fri Aug 30 20:42:08 BST 2013 armv6l
GNU/Linux
pi@escorial ~ $ lsusb
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 148f:5370 Ralink Technology, Corp. RT5370 Wireless
Adapter
network={
ssid="WLAN_AB"
psk="****************"
}
#!/bin/sh
PLUGINPATH=/usr/src/mjpg-streamer-code/mjpg-streamer
STREAMER=$PLUGINPATH/mjpg_streamer
DEVICE=/dev/video0
RESOLUTION=320x240
FRAMERATE=25
HTTP_PORT=8001
#PLUGINPATH=/usr/local/lib
$ wget http://webiopi.googlecode.com/files/WebIOPi-0.6.0.tar.gz
$ tar xvzf WebIOPi-0.6.0.tar.gz
$ cd WebIOPi-0.6.0
$ sudo ./setup.sh
$ sudo webiopi [-h] [-c config] [-l log] [-s script] [-d] [port]
Options:
-h, --help Display this help
-c, --config file Load config from file
-l, --log file Log to file
-s, --script file Load script from file
-d, --debug Enable DEBUG
Arguments:
port Port to bind the HTTP Server
#imports
import webiopi
# Libreria GPIO
GPIO = webiopi.GPIO
# -------------------------------------------------- #
# Definicion constantes #
# -------------------------------------------------- #
# -------------------------------------------------- #
# Funciones motor izquierdo #
# -------------------------------------------------- #
def left_stop():
GPIO.output(L1, GPIO.LOW)
GPIO.output(L2, GPIO.LOW)
def left_forward():
GPIO.output(L1, GPIO.HIGH)
GPIO.output(L2, GPIO.LOW)
def left_backward():
GPIO.output(L1, GPIO.LOW)
GPIO.output(L2, GPIO.HIGH)
# -------------------------------------------------- #
# Funciones motor derecho #
# -------------------------------------------------- #
def right_stop():
GPIO.output(R1, GPIO.LOW)
GPIO.output(R2, GPIO.LOW)
def right_forward():
GPIO.output(R1, GPIO.HIGH)
GPIO.output(R2, GPIO.LOW)
def right_backward():
GPIO.output(R1, GPIO.LOW)
GPIO.output(R2, GPIO.HIGH)
# -------------------------------------------------- #
# Definicion macros #
# -------------------------------------------------- #
@webiopi.macro
def go_forward():
left_forward()
@webiopi.macro
def go_backward():
left_backward()
@webiopi.macro
def turn_left():
right_forward()
@webiopi.macro
def turn_right():
right_backward()
@webiopi.macro
def stop():
left_stop()
right_stop()
# -------------------------------------------------- #
# Iniciacializacion #
# -------------------------------------------------- #
def setup():
# Instalacion GPIOs
GPIO.setFunction(L1, GPIO.OUT)
GPIO.setFunction(L2, GPIO.OUT)
GPIO.setFunction(R1, GPIO.OUT)
GPIO.setFunction(R2, GPIO.OUT)
def destroy():
# Resetea las funciones GPIO
GPIO.setFunction(L1, GPIO.IN)
GPIO.setFunction(L2, GPIO.IN)
GPIO.setFunction(R1, GPIO.IN)
GPIO.setFunction(R2, GPIO.IN)
Para que este script sea llamado al iniciar el servidor webiopi tendremos
que especificarlo en el fichero de configuración /etc/webiopi/config.
Para ello vamos al fichero config y especificamos la ruta en la sección de
scripts:
[SCRIPTS]
# Load custom scripts syntax :
# name = sourcefile
# each sourcefile may have setup, loop and destroy functions and macros
#myscript = /home/pi/webiopi/examples/scripts/macros/script.py
cambot = /home/pi/cambot/cambot.py
[HTTP]
# HTTP Server configuration
enabled = true
port = 8000
$ sudo webiopi-passwd
WebIOPi passwd file generator
Enter Login: webiopi
Enter Password:
Confirm password:
Hash: e70c940a189251e9cd4515b3a1a6c6f02aa05c744a456ce360fe14bf2c5c0353
Saved to /etc/webiopi/passwd
function go_forward() {
webiopi().callMacro("go_forward");
}
function go_backward() {
webiopi().callMacro("go_backward");
}
function turn_right() {
webiopi().callMacro("turn_right");
}
function turn_left() {
webiopi().callMacro("turn_left");
}
function stop() {
webiopi().callMacro("stop");
}
webiopi().ready(init);
</script>
<style type="text/css">
button {
margin: 5px 5px 5px 5px;
width: 50px;
height: 50px;
font-size: 24pt;
font-weight: bold;
color: black;
}
</style>
</head>
<body>
<div id="content" align="center">
<img width="320" height="240"
src="http://192.168.1.37:8001/?action=stream"><br/>
<div id="up"></div>
<div id="middle"></div>
<div id="down"></div>
</div>
</body>
</html>
¡A jugar!
http://raspberry:8000/app/gpio-header
Como podéis comprobar es posible cambiar el estado de cada pin sólo
mediante un clic.
http://raspberry:8000/
Y bueno, aquí os dejo un breve vídeo con el resultado: