最近在做ios的一个东西,后端用的websocket
今天把树莓派的小车又翻出来了
使用tornode做了个遥控器,python的websocket和python的gpio,天生的好基友啊
调速度
http://bbs.elecfans.com/jishu_439995_1_1.html
http://www.geekfan.net/9926/
这个time.sleep(delay)来控制
比如
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
enable_pin = 18
coil_A_1_pin = 4
coil_A_2_pin = 17
coil_B_1_pin = 23
coil_B_2_pin = 24
GPIO.setup(enable_pin, GPIO.OUT)
GPIO.setup(coil_A_1_pin, GPIO.OUT)
GPIO.setup(coil_A_2_pin, GPIO.OUT)
GPIO.setup(coil_B_1_pin, GPIO.OUT)
GPIO.setup(coil_B_2_pin, GPIO.OUT)
GPIO.output(enable_pin, 1)
def forward(delay, steps):
for i in range(0, steps):
setStep(1, 0, 1, 0)
time.sleep(delay)
setStep(0, 1, 1, 0)
time.sleep(delay)
setStep(0, 1, 0, 1)
time.sleep(delay)
setStep(1, 0, 0, 1)
time.sleep(delay)
def backwards(delay, steps):
for i in range(0, steps):
setStep(1, 0, 0, 1)
time.sleep(delay)
setStep(0, 1, 0, 1)
time.sleep(delay)
setStep(0, 1, 1, 0)
time.sleep(delay)
setStep(1, 0, 1, 0)
time.sleep(delay)
def setStep(w1, w2, w3, w4):
GPIO.output(coil_A_1_pin, w1)
GPIO.output(coil_A_2_pin, w2)
GPIO.output(coil_B_1_pin, w3)
GPIO.output(coil_B_2_pin, w4)
while True:
delay = raw_input("Delay between steps (milliseconds)?")
steps = raw_input("How many steps forward? ")
forward(int(delay) / 1000.0, int(steps))
steps = raw_input("How many steps backwards? ")
backwards(int(delay) / 1000.0, int(steps))
tornode参看http://haoningabc.iteye.com/blog/2164973
apt-get install python-virtualenv
virtualenv tonado
. tornado/bin/activate
apt-get install python-dev
wget http://jaist.dl.sourceforge.net/project/raspberry-gpio-python/RPi.GPIO-0.5.8.tar.gz
python setup.py install
root@raspberrypi:~# cat a.py
#!/usr/bin/env python
import RPi.GPIO as GPIO
import curses
import time
from curses import wrapper
GPIO.setmode(GPIO.BCM)
GPIO.setup(27,GPIO.OUT)
GPIO.setup(18,GPIO.OUT)
GPIO.setup(22,GPIO.OUT)
GPIO.setup(23,GPIO.OUT)
stdscr=curses.initscr()
stdscr.clear()
while True:
ch=stdscr.getkey()
#time.sleep(0.001)
#调速度是这里吧,没测试
if ch =='s':
curses.endwin()
GPIO.output(27,False)
GPIO.output(18,False)
GPIO.output(22,False)
GPIO.output(23,False)
break
if ch == 'w':
GPIO.output(27,False)
GPIO.output(18,True)
GPIO.output(22,False)
GPIO.output(23,True)
if ch == 'x':
GPIO.output(27,True)
GPIO.output(18,False)
GPIO.output(22,True)
GPIO.output(23,False)
if ch == 'a':
GPIO.output(27,False)
GPIO.output(18,True)
GPIO.output(22,False)
GPIO.output(23,False)
if ch == 'd':
GPIO.output(27,False)
GPIO.output(18,False)
GPIO.output(22,False)
GPIO.output(23,True)
root@raspberrypi:~#
这个ssh作为控制方向
http://blog.csdn.net/xukai871105/article/details/12684617
http://raspberry-gpio-python.googlecode.com
#----------------------------------------------------------------------------
tonardo的websocket作为遥控器
python aa.py
#!/usr/bin/python
#coding:utf-8
import os.path
import tornado.httpserver
import tornado.web
import tornado.ioloop
import tornado.options
import tornado.httpclient
import tornado.websocket
import json
import RPi.GPIO as GPIO
import time
#GPIO.setwarnings(False)
#GPIO.setmode(GPIO.BCM)
#GPIO.setup(17,GPIO.OUT)
#GPIO.setup(18,GPIO.OUT)
#GPIO.setup(22,GPIO.OUT)
#GPIO.setup(23,GPIO.OUT)
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html")
class SocketHandler(tornado.websocket.WebSocketHandler):
"""docstring for SocketHandler"""
clients = set()
@staticmethod
def send_to_all(message):
for c in SocketHandler.clients:
c.write_message(json.dumps(message))
def open(self):
# GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.OUT)
GPIO.setup(18,GPIO.OUT)
GPIO.setup(22,GPIO.OUT)
GPIO.setup(23,GPIO.OUT)
self.write_message(json.dumps({
'type': 'sys',
'message': 'Welcome to WebSocket',
}))
SocketHandler.send_to_all({
'type': 'sys',
'message': str(id(self)) + ' has joined',
})
SocketHandler.clients.add(self)
def on_close(self):
SocketHandler.clients.remove(self)
SocketHandler.send_to_all({
'type': 'sys',
'message': str(id(self)) + ' has left',
})
def on_message(self, message):
if(message == 'w'):
GPIO.output(17,False)
GPIO.output(18,True)
GPIO.output(22,False)
GPIO.output(23,True)
elif(message == 'a'):
GPIO.output(17,False)
GPIO.output(18,True)
GPIO.output(22,False)
GPIO.output(23,False)
elif(message == 'd'):
GPIO.output(17,False)
GPIO.output(18,False)
GPIO.output(22,False)
GPIO.output(23,True)
elif(message == 's'):
GPIO.output(17,True)
GPIO.output(18,False)
GPIO.output(22,True)
GPIO.output(23,False)
else:
GPIO.output(17,False)
GPIO.output(18,False)
GPIO.output(22,False)
GPIO.output(23,False)
SocketHandler.send_to_all({
'type': 'user',
'id': id(self),
'message': message,
})
##MAIN
if __name__ == '__main__':
app = tornado.web.Application(
handlers=[
(r"/", IndexHandler),
(r"/chat", SocketHandler)
],
debug = True,
# template_path = os.path.join(os.path.dirname(__file__), "templates"),
# static_path = os.path.join(os.path.dirname(__file__), "static")
)
app.listen(8000)
tornado.ioloop.IOLoop.instance().start()
客户端
index.html
<html>
<head>
<script type="text/javascript">
var ws = new WebSocket("ws://192.168.0.112:8000/chat");
ws.onmessage = function(event) {
console.log(event.data);
}
ws.onopen = function() {
console.log('open');
};
ws.onclose = function() {
console.log('Closed! ');
}
function sendto() {
ws.send(document.getElementById('chat').value );
}
function send(str){
ws.send(str);
}
</script>
</head>
<body>
<div>
hello
<input id="chat">
<button onclick="sendto()">send</button>
<br/>
<pre>
<button onclick="send('w')">up</button>
<button onclick="send('a')">left</button><button onclick="send('d')">right</button>
<button onclick="send('s')">down</button>
<button onclick="send('x')">stop</button>
</pre>
</div>
</body>
</html>
python aa.py
http://192.168.0.112:8000
摄像头
http://liyao.me/raspberry-pi-wifi-camera/