111

发布于 2021-09-23  206 次阅读


https://blog.csdn.net/weixin_47390133/article/details/113730925

# coding:utf-8
import RPi.GPIO as GPIO
import time
import sys
import tornado.ioloop
import tornado.web
import tornado.httpserver
import tornado.options
from tornado.options import define,options

#GPIO.setmode(GPIO.BOARD)
#端口号定义
define("port",default=8080,help="run on the given port",type=int)

#IO口定义
IN1 = 11
IN2 = 12
IN3 = 13
IN4 = 15

#IO口初始化
def init():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    GPIO.setup(IN1,GPIO.OUT)
    GPIO.setup(IN2,GPIO.OUT)
    GPIO.setup(IN3,GPIO.OUT)
    GPIO.setup(IN4,GPIO.OUT)
#向右
def right(tf):
    GPIO.output(IN1,GPIO.HIGH)
    GPIO.output(IN2,GPIO.LOW)
    GPIO.output(IN3,False)
    GPIO.output(IN4,False)
    time.sleep(tf)
    #GPIO.cleanup()
#向左
def left(tf):
    GPIO.output(IN1,GPIO.LOW)
    GPIO.output(IN2,GPIO.HIGH)
    GPIO.output(IN3,False)
    GPIO.output(IN4,False)
    time.sleep(tf)
    #GPIO.cleanup()
#向前
def before(tf):
    GPIO.output(IN1,False)
    GPIO.output(IN2,False)
    GPIO.output(IN3,GPIO.HIGH)
    GPIO.output(IN4,GPIO.LOW)
    time.sleep(tf)
    #GPIO.cleanup()
#向后
def cabk(tf):
    GPIO.output(IN1,False)
    GPIO.output(IN2,False)
    GPIO.output(IN3,GPIO.LOW)
    GPIO.output(IN4,GPIO.HIGH)
    time.sleep(tf)
    #GPIO.cleanup()
#左上        
def zuoshang(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 youshang(tf):
    GPIO.output(IN1,GPIO.HIGH)
    GPIO.output(IN2,GPIO.LOW)
    GPIO.output(IN3,GPIO.LOW)
    GPIO.output(IN4,GPIO.HIGH)
    time.sleep(tf)
    #GPIO.cleanup()
#右下
def youxia(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 zuoxia(tf):
    GPIO.output(IN1,GPIO.LOW)
    GPIO.output(IN2,GPIO.HIGH)
    GPIO.output(IN3,GPIO.HIGH)
    GPIO.output(IN4,GPIO.LOW)
    time.sleep(tf)
    #GPIO.cleanup()
#变速
def change(tf):
    GPIO.output(IN1,False)
    GPIO.output(IN2,False)
    GPIO.output(IN3,False)
    GPIO.output(IN4,False)
    time.sleep(tf)
    #GPIO.cleanup()

change_time=0.01# IO输出低电平初始时间
class IndexHandler(tornado.web.RequestHandler):
        def get(self):
                self.render("xiaoche.html")
        def post(self):
                init()
                sleep_time=0.1 # IO输出高电平时间
                global change_time # 引用上面的change_time 若直接在这里定义会导致变量一直更新在0.01 无法变速
                arg=self.get_argument('k')
                if(arg=='w'):
                        before(sleep_time)
                        change(change_time) #通过高低电平交错实现小车的变速
                        #print("1")
                elif(arg=='x'):
                        cabk(sleep_time)
                        change(change_time)
                elif(arg=='a'):
                        left(sleep_time)
                        change(change_time)
                elif(arg=='d'):
                        right(sleep_time)
                        change(change_time)
                elif(arg=='q'):
                        zuoshang(sleep_time)
                        change(change_time)
                elif(arg=='z'):
                        youshang(sleep_time)
                        change(change_time)
                elif(arg=='e'):
                        zuoxia(sleep_time)
                        change(change_time)
                elif(arg=='c'):
                        youxia(sleep_time)
                        change(change_time)
                elif(arg=='j'):
                        if(change_time>0.01):
                            change_time=change_time-0.001;
                elif(arg=='k'):
                        if(change_time<0.03):
                            change_time=change_time+0.001;
                else:
                        return False
                self.write(arg)
if __name__ == '__main__':
        tornado.options.parse_command_line()
        app = tornado.web.Application(handlers=[(r"/",IndexHandler)])
        http_server = tornado.httpserver.HTTPServer(app)
        http_server.listen(options.port)
        print("Demo is runing at 192.168.1.102:8888")
        tornado.ioloop.IOLoop.instance().start()  

<!DOCTYPE html>
<html>
 <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
        <title>小车</title>
 </head>

 <body>
 <!--keyCode 键盘码 在键盘事件发生的时候 记录对应按的哪个键-->
 <script type="text/javascript">
    function go(k){
        .post('/',{k:k},function(){});
    }(function(){
        window.document.onkeydown = abc;
        function abc(ev){
            ev = (ev) ? ev : window.event;
        // 指定方向键 ,w(上-->87),a(左-->83),s(下-->65),d(右-->67)
                if(ev.keyCode=='87'){
                        <!-- console.log('w'); -->
                        go('w');
                }
                if(ev.keyCode=='65'){
                        <!-- console.log('a'); -->
                        go('a');
                }
                if(ev.keyCode=='88'){
                        <!-- console.log('s'); -->
                        go('x');
                }
                if(ev.keyCode=='68'){
                        <!-- console.log('d'); -->
                        go('d');
                }
                if(ev.keyCode=='81'){
                        <!-- console.log('w'); -->
                        go('q');
                }
                if(ev.keyCode=='69'){
                        <!-- console.log('w'); -->
                        go('e');
                }
                if(ev.keyCode=='90'){
                        <!-- console.log('w'); -->
                        go('z');
                }
                if(ev.keyCode=='67'){
                        <!-- console.log('w'); -->
                        go('c');
                }
                if(ev.keyCode=='74'){
                        <!-- console.log('j'); -->
                        go('j');
                }
                if(ev.keyCode=='75'){
                        <!-- console.log('k'); -->
                        go('k');
                }
         }

         var i= null;
                ('.before').mousedown(function(){
                        i = setInterval(function(){
                                <!-- console.log('w'); -->
                                go('w');
                        },50);
                });('.left').mousedown(function(){
                        i = setInterval(function(){
                                <!-- console.log('a'); -->
                                go('a');
                        },50);
                });
                ('.cabk').mousedown(function(){
                        i = setInterval(function(){
                                <!-- console.log('x'); -->
                                go('x');
                        },50);
                });('.right').mousedown(function(){
                        i = setInterval(function(){
                                <!-- console.log('d'); -->
                                go('d');
                        },50);
                });
                ('.zuoshang').mousedown(function(){
                        i = setInterval(function(){
                                <!-- console.log('q'); -->
                                go('q');
                        },50);
                });('.youshang').mousedown(function(){
                        i = setInterval(function(){
                                <!-- console.log('e'); -->
                                go('z');
                        },50);
                });
                ('.zuoxia').mousedown(function(){
                        i = setInterval(function(){
                                <!-- console.log('z'); -->
                                go('e');
                        },50);
                });('.youxia').mousedown(function(){
                        i = setInterval(function(){
                                 <!-- console.log('c'); -->
                                go('c');
                        },50);
                });
                ('.jiasu').mousedown(function(){
                        i = setInterval(function(){
                                <!-- console.log('j'); -->
                                go('j');
                        },100);
                });('.jiansu').mousedown(function(){
                        i = setInterval(function(){
                                 <!-- console.log('k'); -->
                                go('k');
                        },100);
                });
                $('#main span').mouseup(function(){
                        clearInterval(i);
                });
        });

 </script>


 <style type="text/css">
    #main{width: 150px;height: 150px;background: #ccc;}
    #main span{width: 50px;height: 50px;float: left;}
    #main span.on2{background: #ff00ff;}
    #main span.on3{background: #555555;position: absolute;left: 8px;top: 8px;}
    #main span.on4{background: #555555;position: absolute;left: 108px;top: 8px;}
    #main span.on5{background: #555555;position: absolute;left: 8px;top: 108px;}
    #main span.on6{background: #555555;position: absolute;left: 108px;top: 108px;}
    #main span.on7{background: #10e62a;position: absolute;left: 300px;top: 58px;}
    #main span.on8{background: #f10606;position: absolute;left: 370px;top: 58px;}
 </style>

 <div id="main">    
        <span></span>
        <span class="on2 before"></span>
        <span></span>
        <span class="on2 left"></span>
        <span></span>
        <span class="on2 right"></span>
        <span></span>
        <span class="on2 cabk"></span>
        <span></span>
        <span class="on3 zuoshang"></span>
        <span></span>
        <span class="on4 zuoxia"></span>
        <span></span>
        <span class="on5 youshang"></span>
        <span></span>
        <span class="on6 youxia"></span>
        <span></span>
        <span class="on7 jiasu"></span>
        <span></span>
        <span class="on8 jiansu"></span>
        <span></span>
</div>
</body>
</html>


"穷且益坚,不坠青云之志"