lili821 发表于 2018-2-8 15:23:14

基于树莓派制作手机遥控小车, Python实现程序代码

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



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")

(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>
    <linkrel="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>


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)以上三个文件需放置到同一文件路径下。


蟋蟀的帅 发表于 2018-3-27 11:00:13

用的几代树莓板?

youzipai 发表于 2018-3-22 16:14:44

大神有能加你qq吗

youzipai 发表于 2018-3-22 11:20:14

两个py代码文件好好啦,就html打开网页不是***就几个方块,编码用的UTF-8呀

youzipai 发表于 2018-3-22 09:17:59

大神在吗,我代码见好文件,说,标识符中的字符无效,请教

jwdxu2009 发表于 2018-2-8 17:49:20

上上原理图,学习和参考一下,好产品。
页: [1]
查看完整版本: 基于树莓派制作手机遥控小车, Python实现程序代码

Baidu
map