-
Notifications
You must be signed in to change notification settings - Fork 0
/
机器人控制运动界面(后端代码))
121 lines (105 loc) · 3.86 KB
/
机器人控制运动界面(后端代码))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from flask import Flask, render_template, Response, request
from picamera2 import Picamera2
import cv2
from base_ctrl import BaseController
app = Flask(__name__)
# 设置机器人控制器(使用树莓派特定的代码)
def is_raspberry_pi5():
with open('/proc/cpuinfo', 'r') as file:
for line in file:
if 'Model' in line:
if 'Raspberry Pi 5' in line:
return True
else:
return False
# 根据树莓派型号设置正确的控制器
if is_raspberry_pi5():
base = BaseController('/dev/ttyAMA0', 115200)
else:
base = BaseController('/dev/serial0', 115200)
# 摄像头设置,生成视频流
def gen_frames():
picam2 = Picamera2()
picam2.configure(picam2.create_video_configuration(main={"format": 'XRGB8888', "size": (640, 480)}))
picam2.start()
while True:
frame = picam2.capture_array()
# 转换 BGR 到 RGB,避免颜色问题
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# 将图像编码为JPEG格式
ret, buffer = cv2.imencode('.jpg', frame_rgb)
frame = buffer.tobytes()
# 生成视频流
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
# 首页路由,处理动作
@app.route("/", methods=['GET', 'POST'])
def index():
if request.method == 'POST':
if request.form.get('action1') == 'VALUE1':
pass # 执行某个动作
elif request.form.get('action2') == 'VALUE2':
pass # 执行另一个动作
else:
pass # 未知的动作
elif request.method == 'GET':
return render_template('1.html') # 渲染模板
# 视频流路由,返回实时视频流
@app.route('/video_feed')
def video_feed():
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
# 运动控制路由
@app.route('/move/<direction>')
def move(direction):
if direction == 'forward':
base.send_command({"T": 1, "L": 0.2, "R": 0.2})
elif direction == 'backward':
base.send_command({"T": 1, "L": -0.2, "R": -0.2})
elif direction == 'left':
base.send_command({"T": 1, "L": -0.2, "R": 0.2})
elif direction == 'right':
base.send_command({"T": 1, "L": 0.2, "R": -0.2})
elif direction == 'stop':
base.send_command({"T": 1, "L": 0, "R": 0})
return '', 204 # 返回204响应,表示无内容
# 云台控制路由
@app.route('/gimbal/<direction>')
def gimbal(direction):
if direction == 'up':
base.gimbal_ctrl(0, 45, 0, 0)
elif direction == 'down':
base.gimbal_ctrl(0, -45, 0, 0)
elif direction == 'left':
base.gimbal_ctrl(-45, 0, 0, 0)
elif direction == 'right':
base.gimbal_ctrl(45, 0, 0, 0)
elif direction == 'stop':
base.gimbal_ctrl(0, 0, 0, 0)
elif direction == 'reset': # 回到原点
base.gimbal_ctrl(0, 0, 0, 0) # 回到原点,假设原点是(0, 0)
print("云台已回到原点")
return '', 204
# LED控制路由
@app.route('/led/<action>')
def led_control(action):
if action == 'on':
# 设置LED为打开状态
base.lights_ctrl(255, 0) # IO4_PWM = 255, IO5_PWM = 0 表示打开LED
elif action == 'off':
# 设置LED为关闭状态
base.lights_ctrl(0, 0) # 关闭LED
return '', 204 # 返回204响应,表示无内容
# LED闪烁控制路由
@app.route('/led/blink')
def led_blink():
# 让LED闪烁的简单实现:间隔一段时间打开和关闭
import time
for _ in range(5): # 闪烁5次
base.lights_ctrl(255, 0) # 开灯
time.sleep(0.5) # 等待0.5秒
base.lights_ctrl(0, 0) # 关灯
time.sleep(0.5) # 等待0.5秒
return '', 204 # 返回204响应,表示无内容
# 启动应用
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)