Merge pull request '小车控制补全' (#1) from zyp into main

chang^2
pa2ltb7y5 4 weeks ago
commit 23f1f757a0

@ -19,6 +19,8 @@ from car import CAR
import threading import threading
import cv2 import cv2
app = Flask(__name__, static_url_path='') app = Flask(__name__, static_url_path='')
# 照片保存路径
PHOTO_PATH = "photo.jpg"
def gen_frames(): # generate frame by frame from camera def gen_frames(): # generate frame by frame from camera
picam2 = Picamera2() picam2 = Picamera2()
@ -42,6 +44,18 @@ def index():
def video_feed(): def video_feed():
#Video streaming route. Put this in the src attribute of an img tag #Video streaming route. Put this in the src attribute of an img tag
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame') return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
#---------------------------------------------------------------------------------
@app.route('/take_photo')
def take_photo():
picam2 = Picamera2()
picam2.configure(picam2.create_video_configuration(main={"format": 'XRGB8888', "size": (640, 480)}))
picam2.start()
frame = picam2.capture_array() # Capture a single frame
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
cv2.imwrite(PHOTO_PATH, frame) # Save the frame as a JPEG file
return send_file(PHOTO_PATH, as_attachment=True) # Send the file to download
#---------------------------------------------------------------------------------------
# def gen(camera): # def gen(camera):
# """Video streaming generator function.""" # """Video streaming generator function."""
@ -69,9 +83,6 @@ car = CAR()
@app.route('/control/') @app.route('/control/')
def control_index(): def control_index():
word = """指令:\n word = """指令:\n
/led: 灯光闪烁\n
/led_light: 打开全部灯光\n
/led_dark: 关闭全部灯光\n
/stop(/Q): 小车停止运动\n /stop(/Q): 小车停止运动\n
/forward(/W): 小车开始运动\n /forward(/W): 小车开始运动\n
/back(/S): 小车向后运动\n /back(/S): 小车向后运动\n

@ -3,11 +3,11 @@ import os
import time import time
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
from base_ctrl import BaseController from base_ctrl import BaseController
#######################################
#############信号引脚定义##############
#######################################
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False) GPIO.setwarnings(False)
#底盘连接
def is_raspberry_pi5(): def is_raspberry_pi5():
with open('/proc/cpuinfo', 'r') as file: with open('/proc/cpuinfo', 'r') as file:
for line in file: for line in file:
@ -17,75 +17,13 @@ def is_raspberry_pi5():
else: else:
return False return False
#云台连接
base = BaseController('/dev/ttyAMA0', 115200) base = BaseController('/dev/ttyAMA0', 115200)
class CAR: class CAR:
def __init__(self):
self.LED0 = 10 #LED0的IO口定义
self.LED1 = 9 #LED1的IO口定义
self.LED2 = 25 #LED2的IO口定义
self.ENA = 13 #//L298 使能A
self.ENB = 20 #//L298 使能B
self.IN1 = 19 #//电机接口1
self.IN2 = 16 #//电机接口2
self.IN3 = 21 #//电机接口3
self.IN4 = 26 #//电机接口4
GPIO.setup(self.LED0, GPIO.OUT, initial=GPIO.HIGH) ##led0初始化为高电平
GPIO.setup(self.LED1, GPIO.OUT, initial=GPIO.HIGH) ##led1初始化为高电平
GPIO.setup(self.LED2, GPIO.OUT, initial=GPIO.HIGH) ##led2初始化为高电平
GPIO.setup(self.ENA, GPIO.OUT, initial=GPIO.LOW) ##ENA初始化为低电平
GPIO.setup(self.ENB, GPIO.OUT, initial=GPIO.LOW) ##ENB初始化为低电平
GPIO.setup(self.IN1, GPIO.OUT, initial=GPIO.LOW) ##IN1初始化为低电平
GPIO.setup(self.IN2, GPIO.OUT, initial=GPIO.LOW) ##IN2初始化为低电平
GPIO.setup(self.IN3, GPIO.OUT, initial=GPIO.LOW) ##IN3初始化为低电平
GPIO.setup(self.IN4, GPIO.OUT, initial=GPIO.LOW) ##IN4初始化为低电平
def led(self):
GPIO.output(self.LED0,False)
GPIO.output(self.LED1,False)
GPIO.output(self.LED2,False)###LED0,LED1,LED2 = 亮 亮 亮
time.sleep(0.5)
GPIO.output(self.LED0,True)
GPIO.output(self.LED1,False)
GPIO.output(self.LED2,False)###LED0,LED1,LED2 = 灭 亮 亮
time.sleep(0.5)
GPIO.output(self.LED0,False)
GPIO.output(self.LED1,True)
GPIO.output(self.LED2,False)###LED0,LED1,LED2 = 亮 灭 亮
time.sleep(0.5)
GPIO.output(self.LED0,False)
GPIO.output(self.LED1,False)
GPIO.output(self.LED2,True)###LED0,LED1,LED2 = 亮 亮 灭
time.sleep(0.5)
GPIO.output(self.LED0,False)
GPIO.output(self.LED1,False)
GPIO.output(self.LED2,False)###LED0,LED1,LED2 = 亮 亮 亮
time.sleep(0.5)
GPIO.output(self.LED0,True)
GPIO.output(self.LED1,True)
GPIO.output(self.LED2,True)###LED0,LED1,LED2 = 灭 灭 灭
time.sleep(0.5)
print("run: led")
def led_light(self):
GPIO.output(self.LED0,False)
GPIO.output(self.LED1,False)
GPIO.output(self.LED2,False)###LED0,LED1,LED2 = 亮 亮 亮
print("run: led_light")
def led_dark(self):
GPIO.output(self.LED0,True)
GPIO.output(self.LED1,True)
GPIO.output(self.LED2,True)###LED0,LED1,LED2 = 灭 灭 灭
print("run: led_dark")
def stop(self): # 停止运行 def stop(self): # 停止运行
GPIO.output(self.ENA,False) base.send_command({"T":1,"L":0,"R":0})
GPIO.output(self.ENB,False)
GPIO.output(self.IN1,False)
GPIO.output(self.IN2,False)
GPIO.output(self.IN3,False)
GPIO.output(self.IN4,False)
print("run: stop move") print("run: stop move")
def Q(self): # 停止的快捷键 def Q(self): # 停止的快捷键
@ -95,7 +33,7 @@ class CAR:
def forward(self): # 前进 def forward(self): # 前进
base.send_command({"T":1,"L":0.2,"R":0.2}) base.send_command({"T":1,"L":0.2,"R":0.2})
time.sleep(2) time.sleep(1)
base.send_command({"T":1,"L":0,"R":0}) base.send_command({"T":1,"L":0,"R":0})
print("run: move !!!!forward") print("run: move !!!!forward")
@ -105,12 +43,9 @@ class CAR:
self.forward() self.forward()
def back(self): # 后退 def back(self): # 后退
GPIO.output(self.ENA,True) base.send_command({"T":1,"L":-0.2,"R":-0.2})
GPIO.output(self.ENB,True) time.sleep(1)
GPIO.output(self.IN1,True) base.send_command({"T":1,"L":0,"R":0})
GPIO.output(self.IN2,False)
GPIO.output(self.IN3,True)
GPIO.output(self.IN4,False)
print("run: move back") print("run: move back")
def S(self): # 后退的快捷键 def S(self): # 后退的快捷键
@ -119,12 +54,9 @@ class CAR:
self.back() self.back()
def left(self): # 左转 def left(self): # 左转
GPIO.output(self.ENA,True) base.send_command({"T":1,"L":-0.2,"R":0.2})
GPIO.output(self.ENB,True) time.sleep(0.5)
GPIO.output(self.IN1,False) base.send_command({"T":1,"L":0,"R":0})
GPIO.output(self.IN2,True)
GPIO.output(self.IN3,True)
GPIO.output(self.IN4,False)
print("run: move left") print("run: move left")
def A(self): # 左转的快捷键 def A(self): # 左转的快捷键
@ -133,12 +65,9 @@ class CAR:
self.left() self.left()
def right(self): # 右转 def right(self): # 右转
GPIO.output(self.ENA,True) base.send_command({"T":1,"L":0.2,"R":-0.2})
GPIO.output(self.ENB,True) time.sleep(0.5)
GPIO.output(self.IN1,True) base.send_command({"T":1,"L":0,"R":0})
GPIO.output(self.IN2,False)
GPIO.output(self.IN3,False)
GPIO.output(self.IN4,True)
print("run: move right") print("run: move right")
def D(self): # 右转的快捷键 def D(self): # 右转的快捷键

@ -22,6 +22,7 @@
</style> </style>
</head> </head>
<body> <body>
<h5>实时视频流</h5>
<img src="{{ url_for('video_feed') }}"> <img src="{{ url_for('video_feed') }}">
<p> 小车控制界面</p> <p> 小车控制界面</p>
<bodylink = "red"> <bodylink = "red">
@ -29,19 +30,21 @@
<div id="container" class="container"> <div id="container" class="container">
<div> <div>
<button id="front" type="button" onclick="forward()" class="btn btn-lg btn-primary glyphicon glyphicon-circle-arrow-up"> </button> <button id="front" type="button" onclick="forward()" class="btn btn-lg btn-primary glyphicon glyphicon-circle-arrow-up" style="margin-left: 56px;"> </button>
</div> </div>
<div> <!-- <div>
<button id="printButton" type="button" onclick="printClicked()" class="btn btn-lg btn-primary">打印点击</button> <button id="printButton" type="button" onclick="printClicked()" class="btn btn-lg btn-primary">打印点击</button>
</div> </div> -->
<div> <div>
<button type="button" onclick="left()" class="btn btn-lg btn-primary glyphicon glyphicon-circle-arrow-left"> </button> <button type="button" onclick="left()" class="btn btn-lg btn-primary glyphicon glyphicon-circle-arrow-left" > </button>
<button type="button" onclick="stop()" class="btn btn-lg btn-primary glyphicon glyphicon-stop"> </button> <button type="button" onclick="stop()" class="btn btn-lg btn-primary glyphicon glyphicon-stop"> </button>
<button type="button" onclick="right()" class="btn btn-lg btn-primary glyphicon glyphicon-circle-arrow-right"> </button> <button type="button" onclick="right()" class="btn btn-lg btn-primary glyphicon glyphicon-circle-arrow-right" > </button>
<!-- ---------------------------------------- -->
<button type="button" onclick="takePhoto()">拍照</button>
<!-- ---------------------------------------- -->
</div> </div>
<div> <button id="rear" type="button" onclick="back()" class="btn btn-lg btn-primary glyphicon glyphicon-circle-arrow-down" style="margin-left: 56px;"> </button>
<button id="rear" type="button" onclick="back()" class="btn btn-lg btn-primary glyphicon glyphicon-circle-arrow-down"> </button>
</div> </div>
</div> </div>
@ -75,8 +78,13 @@
<script> <script>
function printClicked() { function printClicked() {
console.log("132465789891651354684"); // console.log("132465789891651354684");
}
// ---------------------------------------
function takePhoto() {
window.location.href = "{{ url_for('take_photo') }}"; // 重定向到拍照请求
} }
// ---------------------------------------
function forward() { function forward() {
console.log("132465789891651354684"); console.log("132465789891651354684");
$.ajax({ $.ajax({

Loading…
Cancel
Save