国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python3實現網頁版raspberry pi(樹莓派)小車控制

2020-02-15 21:15:02
字體:
來源:轉載
供稿:網友

關于樹莓派四驅小車的運動方向控制、攝像頭方向控制已經在前面的兩篇博文中介紹過。有需要的可以參考。本文也是基于上述兩個python文件就緒的情況進行的。

本文主要講述我是如何實現通過網頁實現小車控制的。當前的實現方式比較簡陋,只能支持控制網頁和樹莓派在同一個局域網中的場景。如果以后還有精力,可能會進行一些改進。

1. 基本思路

2. 服務端控制程序server.py

# --coding:utf-8--from http.server import BaseHTTPRequestHandler, HTTPServerimport timeimport socketimport urllibfrom car_controler import FourWheelDriveCarfrom camera_controler import Camera  class CarServer(BaseHTTPRequestHandler):    carControler = FourWheelDriveCar()  cameraControler = Camera()   def get_host_ip(self):    '''    This method is used for getting local ip address    The car server will deploy on this ip    '''    try:      serverSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)      serverSocket.connect(("8.8.8.8", 80))      localIP = serverSocket.getsockname()[0]    finally:      return localIP   def do_GET(self):    '''    Define the car control GUI for client    For the first edition, it will only return direction contol GUI    '''    localIP = CarServer.get_host_ip(self)     # When this GET method is called, then should init the car    self.carControler.reset()     # Read control page html file from control.html    controlPageFile = open("control.html")    controlPageGUI = controlPageFile.read()    controlPageFile.close()    controlPageGUI = controlPageGUI.replace(      "requestAddress", "http://" + localIP + ":9090/")    controlPageGUI = controlPageGUI.replace(      "cameraAddress", "http://" + localIP + ":8080/")     self.send_response(200)    self.send_header("Content-type", "text/html")    self.end_headers()    self.wfile.write(controlPageGUI.encode())   def do_POST(self):    length = int(self.headers['Content-Length'])    qs = self.rfile.read(length)    direction = qs.decode()    print(direction)     cameraDirection = ['HR', 'HL', 'VU', 'VD', 'RESET']    if direction in cameraDirection:      # This is used to control the camera      self.cameraControler.cameraRotate(direction)    else:      # This is used to control the car      self.carControler.carMove(direction)     self.send_response(200)  if __name__ == "__main__":  raspCarServer = CarServer  hostIP = raspCarServer.get_host_ip(raspCarServer)  hostPort = 9090  myServer = HTTPServer((hostIP, hostPort), raspCarServer)   print(time.asctime(), "Server Starts - %s:%s" % (hostIP, hostPort))   try:    myServer.serve_forever()  except KeyboardInterrupt:    pass

3. 服務端返回的頁面control.html 

幾點說明:

html文件中有兩個地址,我是在server.py中做了替換的,所以client請求之后會有實際的地址給到瀏覽器,最終都是使用的樹莓派的ip 有個顯示監控視頻的區域,可以直接用我給出的示例使用即可,前提是你也用的MJPG-Streamer來獲取攝像頭監控 小車控制我只給來前后左右運動,沒有給后退的轉向控制,有需要可以自己添加 比較重要的是點擊按鈕之后發送請求到服務端,參考文件<script>中的代碼
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 资溪县| 普安县| 凤城市| 定兴县| 达拉特旗| 舟山市| 东乡族自治县| 南昌县| 屏山县| 靖远县| 玛曲县| 雅江县| 华蓥市| 华安县| 二连浩特市| 九寨沟县| 新闻| 恭城| 比如县| 济源市| 佳木斯市| 武汉市| 福泉市| 丹阳市| 纳雍县| 黄龙县| 鹰潭市| 三亚市| 岐山县| 成都市| 泾川县| 长治市| 海城市| 英德市| 大邑县| 汶上县| 卓资县| 靖远县| 宁强县| 越西县| 西宁市|