最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python3實(shí)現(xiàn)網(wǎng)頁版raspberry pi(樹莓派)小車控制

 更新時(shí)間:2020年02月12日 09:38:31   作者:weixin_42534940  
這篇文章主要為大家詳細(xì)介紹了python3實(shí)現(xiàn)網(wǎng)頁版raspberry pi(樹莓派)小車控制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

關(guān)于樹莓派四驅(qū)小車的運(yùn)動(dòng)方向控制、攝像頭方向控制已經(jīng)在前面的兩篇博文中介紹過。有需要的可以參考。本文也是基于上述兩個(gè)python文件就緒的情況進(jìn)行的。

本文主要講述我是如何實(shí)現(xiàn)通過網(wǎng)頁實(shí)現(xiàn)小車控制的。當(dāng)前的實(shí)現(xiàn)方式比較簡陋,只能支持控制網(wǎng)頁和樹莓派在同一個(gè)局域網(wǎng)中的場景。如果以后還有精力,可能會(huì)進(jìn)行一些改進(jìn)。

1. 基本思路

2. 服務(wù)端控制程序server.py

# --coding:utf-8--
from http.server import BaseHTTPRequestHandler, HTTPServer
import time
import socket
import urllib
from car_controler import FourWheelDriveCar
from 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. 服務(wù)端返回的頁面control.html 

幾點(diǎn)說明:

  • html文件中有兩個(gè)地址,我是在server.py中做了替換的,所以client請(qǐng)求之后會(huì)有實(shí)際的地址給到瀏覽器,最終都是使用的樹莓派的ip
  • 有個(gè)顯示監(jiān)控視頻的區(qū)域,可以直接用我給出的示例使用即可,前提是你也用的MJPG-Streamer來獲取攝像頭監(jiān)控
  • 小車控制我只給來前后左右運(yùn)動(dòng),沒有給后退的轉(zhuǎn)向控制,有需要可以自己添加
  • 比較重要的是點(diǎn)擊按鈕之后發(fā)送請(qǐng)求到服務(wù)端,參考文件<script>中的代碼
<html>
<script>
  function directionBtnDown(direction) {
    var url = "requestAddress"
    var request = new XMLHttpRequest();
    request.open("POST", url);
 
    request.send(direction)
  }
 
  function directionBtnUp() {
    var url = "requestAddress"
    var request = new XMLHttpRequest();
    request.open("POST", url);
    request.send("S")
  }
</script>
<style type="text/css">
  span.car {
    position: absolute;
    margin-top: 30%;
    height: 480px;  
  }
 
  span.camera {
    position: absolute;
    margin-top: 5%;
    margin-left: 290px;
    height: 480px;
    width: 640px;
    background-color: blue
  }
 
  span.camera_control {
    position: absolute;
    margin-top: 30%;
    margin-left: 950px;
    height: 480px;
    background-color: blue
  }
 
 
  button.top {
    position: absolute;
    height: 50px;
    width: 90px;
    margin-left: 90px
  }
 
  button.left {
    position: absolute;
    height: 50px;
    width: 90px;
    margin-top: 50px;
  }
 
  button.right {
    position: absolute;
    height: 50px;
    width: 90px;
    margin-top: 50px;
    margin-left: 180px
  }
 
  button.bottom {
    position: absolute;
    height: 50px;
    width: 90px;
    margin-top: 100px;
    margin-left: 90px
  }
</style>
 
<head>
  <title>control page</title>
</head>
 
<body>
  <span id="car_control" class="car">
    <button class="top drectionBtn" id="F" οnmοusedοwn="directionBtnDown('F')" οnmοuseup="directionBtnUp()">F</button>
    <button class="left drectionBtn" id="L" οnmοusedοwn="directionBtnDown('L')" οnmοuseup="directionBtnUp()">L</button>
    <button class="right drectionBtn" id="R" οnmοusedοwn="directionBtnDown('R')" οnmοuseup="directionBtnUp()">R</button>
    <button class="bottom drectionBtn" id="B" οnmοusedοwn="directionBtnDown('B')" οnmοuseup="directionBtnUp()">B</button>
  </span>
  <span id="camera_view" class="camera">
    <img id="view" src="cameraAddress?action=stream" />
  </span>
  <span id="camera_control" class="camera_control">
    <button class="top drectionBtn" id="VU" οnmοusedοwn="directionBtnDown('VU')">Up</button>
    <button class="left drectionBtn" id="HL" οnmοusedοwn="directionBtnDown('HL')">Left</button>
    <button class="right drectionBtn" id="HR" οnmοusedοwn="directionBtnDown('HR')">Right</button>
    <button class="bottom drectionBtn" id="VD" οnmοusedοwn="directionBtnDown('VD')">Down</button>
  </span>
 
</body>
 
</html>

4. 使用方式簡介

  • 在樹莓派上運(yùn)行MJPG-Streamer
  • 在樹莓派上運(yùn)行服務(wù)端控制程序server.py
  • 在相同局域網(wǎng)的pc上打開瀏覽器,訪問server地址,如 http://192.168.1.101:9090。其中,ip地址是樹莓派的ip,端口9090是server中設(shè)定的端口,可以自己修改

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • matplotlib作圖添加表格實(shí)例代碼

    matplotlib作圖添加表格實(shí)例代碼

    這篇文章主要介紹了matplotlib作圖添加表格實(shí)例代碼,實(shí)例繪制了一個(gè)簡單的折線圖,并且在圖中添加了一個(gè)表格,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • 詳解Python 正則表達(dá)式模塊

    詳解Python 正則表達(dá)式模塊

    這篇文章主要介紹了Python 正則表達(dá)式模塊詳解,分為兩部分,包括基礎(chǔ)語法總結(jié),re模塊的相關(guān)知識(shí),需要的朋友可以參考下
    2018-11-11
  • Python hashlib和hmac模塊使用方法解析

    Python hashlib和hmac模塊使用方法解析

    這篇文章主要介紹了Python hashlib和hmac模塊使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Python 爬蟲圖片簡單實(shí)現(xiàn)

    Python 爬蟲圖片簡單實(shí)現(xiàn)

    這篇文章主要介紹了Python 爬蟲圖片簡單實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Python 如何對(duì)文件目錄操作

    Python 如何對(duì)文件目錄操作

    這篇文章主要介紹了Python 如何對(duì)文件目錄操作,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • 淺談pytorch中stack和cat的及to_tensor的坑

    淺談pytorch中stack和cat的及to_tensor的坑

    這篇文章主要介紹了pytorch中stack和cat的及to_tensor的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • 用python的requests第三方模塊抓取王者榮耀所有英雄的皮膚實(shí)例

    用python的requests第三方模塊抓取王者榮耀所有英雄的皮膚實(shí)例

    下面小編就為大家分享一篇用python的requests第三方模塊抓取王者榮耀所有英雄的皮膚實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨想過來看看吧
    2017-12-12
  • Python中ROS和OpenCV結(jié)合處理圖像問題

    Python中ROS和OpenCV結(jié)合處理圖像問題

    ROS通過一個(gè)叫CvBridge的功能包,將獲取的圖像數(shù)據(jù)轉(zhuǎn)換成OpenCV的格式,OpenCV處理之后,傳回給ROS進(jìn)行圖像顯示(應(yīng)用),這篇文章主要介紹了Python中ROS和OpenCV結(jié)合處理圖像問題,需要的朋友可以參考下
    2022-06-06
  • python使用super()出現(xiàn)錯(cuò)誤解決辦法

    python使用super()出現(xiàn)錯(cuò)誤解決辦法

    這篇文章主要介紹了python使用super()出現(xiàn)錯(cuò)誤解決辦法的相關(guān)資料,對(duì)于TypeError: must be type, not classobj的錯(cuò)誤進(jìn)行處理,需要的朋友可以參考下
    2017-08-08
  • python+opencv實(shí)現(xiàn)文字顏色識(shí)別與標(biāo)定功能

    python+opencv實(shí)現(xiàn)文字顏色識(shí)別與標(biāo)定功能

    最近小編接了一個(gè)比較簡單的圖像處理的單子,今天小編給大家分享python+opencv實(shí)現(xiàn)文字顏色識(shí)別與標(biāo)定功能的完整思路及代碼,感興趣的朋友一起看看吧
    2021-09-09

最新評(píng)論

惠东县| 清镇市| 日喀则市| 高尔夫| 长兴县| 泰州市| 汾阳市| 新乐市| 金昌市| 襄汾县| 弥勒县| 黄陵县| 东辽县| 兴宁市| 沙湾县| 肃宁县| 九寨沟县| 故城县| 琼中| 留坝县| 鹰潭市| 漾濞| 怀来县| 苍溪县| 织金县| 紫阳县| 左贡县| 永泰县| 新晃| 虹口区| 双峰县| 临沧市| 宝清县| 拜城县| 荥阳市| 讷河市| 彝良县| 花垣县| 印江| 张家港市| 巩留县|