技术类文章收集大杂烩树莓派玩转树莓派

Python+树莓派 制作手机遥控小车

2017-07-30  本文已影响603人  高杆python

今天跟大家分享一个利用树莓派和L298N模块制作手机遥控小车的程序。具体小车的硬件链接我将不再这里赘述,网上会搜索到很多教程。在这只和大家交流分享利用Python实现的程序代码。先上一张效果图。


树莓派小车.jpg

1.程序思路:
(1)在树莓派上编写一个控制小车运行的文件index.py。
(2)在树莓派上利用Python的bottle库建立web应用服务,运行main.py。
(3)在手机浏览器访问树莓派上的遥控器网页index.html,实现与index.py程序交互,进而控制小车运行。

2.程序文件:
(1)控制小车运行的Python文件:index.py

#!/usr/bin/env python3
from bottle import get,post,run,request,template
import RPi.GPIO as GPIO
import time
IN1 = 11
IN2 = 12
IN3 = 13
IN4 = 15
def init():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(IN1,GPIO.OUT)
    GPIO.setup(IN2,GPIO.OUT)
    GPIO.setup(IN3,GPIO.OUT)
    GPIO.setup(IN4,GPIO.OUT)
# 前进
def forward(tf):
    GPIO.output(IN1,GPIO.HIGH)
    GPIO.output(IN2,GPIO.LOW)
    GPIO.output(IN3,GPIO.HIGH)
    GPIO.output(IN4,GPIO.LOW)
    time.sleep(tf)
    GPIO.cleanup()
# 后退
def down(tf):
    GPIO.output(IN1,GPIO.LOW)
    GPIO.output(IN2,GPIO.HIGH)
    GPIO.output(IN3,GPIO.LOW)
    GPIO.output(IN4,GPIO.HIGH)
    time.sleep(tf)
    GPIO.cleanup()
# 左转弯
def left(tf):
    GPIO.output(IN1,GPIO.LOW)
    GPIO.output(IN2,GPIO.LOW)
    GPIO.output(IN3,GPIO.HIGH)
    GPIO.output(IN4,GPIO.LOW)
    time.sleep(tf)
    GPIO.cleanup()
# 右转弯
def right(tf):
    GPIO.output(IN1,GPIO.HIGH)
    GPIO.output(IN2,GPIO.LOW)
    GPIO.output(IN3,GPIO.LOW)
    GPIO.output(IN4,GPIO.LOW)
    time.sleep(tf)
    GPIO.cleanup()
# 停止
def stop():
    GPIO.output(IN1,False)
    GPIO.output(IN2,False)
    GPIO.output(IN3,False)
    GPIO.output(IN4,False)
    GPIO.cleanup()
@get("/")
def index():
    return template("index")
@post("/cmd")
def cmd():
    print("按下了按钮: "+request.body.read().decode())
    init()
    sleep_time = 1
    arg = request.body.read().decode()
    if(arg=='up'):
        forward(sleep_time)
    elif(arg=='down'):
        down(sleep_time)
    elif(arg=='left'):
        left(sleep_time)
    elif(arg=='right'):
        right(sleep_time)
    elif(arg=='stop'):
        stop()   
    else:
        return False
    #return "OK"
run(host="0.0.0.0",port="8080")

(2)建立Web服务的Python文件:main.py

#!/usr/bin/env python3
from bottle import get,post,run,request,template
@get("/")
def index():
    return template("index")
@post("/cmd")
def cmd():
    print("按下了按钮: "+request.body.read().decode())
    #return "OK"
run(host="0.0.0.0",port="8080")
main.py

(3)控制小车的网页html文件:index.html

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>树莓派遥控小车</title>
    <link href="http://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" media="screen">
    <script src="http://code.jquery.com/jquery.js"></script>
    <style type="text/css">
        #up {
            margin-left: 55px;
            margin-bottom: 3px;
        }
        #down {
            margin-top: 3px;
            margin-left: 55px;
        }
    </style>
    <script>
        $(function(){
            $("button").click(function(){
                $.post("/cmd",this.id,function(data,status){});
            });
        });
    </script>
</head>
<body>
<div id="container" class="container">
    <div>
        <button id="up" class="btn btn-lg btn-primary glyphicon glyphicon-circle-arrow-up"></button>
    </div>
    <div>
        <button id='left' class="btn btn-lg btn-primary glyphicon glyphicon-circle-arrow-left"></button>
        <button id='stop' class="btn btn-lg btn-primary glyphicon glyphicon-stop"></button>
        <button id='right' class="btn btn-lg btn-primary glyphicon glyphicon-circle-arrow-right"></button>
    </div>
    <div>
        <button id='down' class="btn btn-lg btn-primary glyphicon glyphicon-circle-arrow-down"></button>
    </div>
</div>
<script src="http://cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</body>
</html>
网页遥控器.png

3.程序原理及代码解析
(1)main.py 文件中通过使用Python最轻量级的web框架bottle在树莓派上建立web应用服务,并设置服务开放的端口号为8080,进而实现通过手机等客户端访问树莓派上的html网页文件。
(2)index.py 文件中通过引用Python的GPIO库,实现通过树莓派的11、12、13、14号GPIO接口链接控制L298N模块,进而控制小车的马达实现小车的运动。
(3)index.html通过使用js的click事件,检测网页中的button点击情况并反馈给index.py文件,从而实现通过点击按钮实现对小车的控制。
(4)以上三个文件需放置到同一文件路径下。
在这里推荐一篇bottle框架学习文章:Bottle手册(0.13-dev)中文翻译r

上一篇 下一篇

猜你喜欢

热点阅读