Загрузка данных
#include <Servo.h>
Servo servoYaw;
const int motorPin = 5;
const int trigPin = 12;
const int echoPin = 13;
int motorSpeed = 150;
int yawAngle = 90;
char currentMode = 'M';
void setup() {
Serial.begin(9600);
servoYaw.attach(9);
pinMode(motorPin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
servoYaw.write(yawAngle);
analogWrite(motorPin, motorSpeed);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
int distance = constrain(duration * 0.034 / 2, 0, 150);
Serial.print("DIST:");
Serial.println(distance);
if (Serial.available()) {
char command = Serial.read();
if (command == 'M') currentMode = 'M';
if (command == 'A') currentMode = 'A';
if (currentMode == 'M') {
if (command == 'U') motorSpeed = min(255, motorSpeed + 20);
if (command == 'D') motorSpeed = max(0, motorSpeed - 20);
if (command == 'L') yawAngle = max(0, yawAngle - 15);
if (command == 'R') yawAngle = min(180, yawAngle + 15);
servoYaw.write(yawAngle);
analogWrite(motorPin, motorSpeed);
}
}
delay(50);
}
import serial
import tkinter as tk
from PIL import Image, ImageTk # если используешь PNG
serial_port = serial.Serial('COM3', 9600, timeout=0.01)
def send_command(command):
serial_port.write(command.encode())
def update_distance():
while serial_port.in_waiting:
line = serial_port.readline().decode().strip()
if line.startswith("DIST:"):
distance_value = line.split(":")[1]
distance_label.config(text=distance_value + " см")
root.after(50, update_distance)
def set_manual():
send_command('M')
mode_status.config(text="Ручной")
def set_auto():
send_command('A')
mode_status.config(text="Автомат")
root = tk.Tk()
root.title("Управление дроном")
root.geometry("350x450")
tk.Label(root, text="Расстояние до препятствия:").pack(pady=(10,0))
distance_label = tk.Label(root, text="0 см", font=("Arial", 20))
distance_label.pack()
mode_frame = tk.Frame(root)
mode_frame.pack(pady=10)
tk.Button(mode_frame, text="Ручной режим", command=set_manual, width=12).pack(side="left", padx=5)
tk.Button(mode_frame, text="Автомат режим", command=set_auto, width=12).pack(side="left", padx=5)
mode_status = tk.Label(root, text="Ручной")
mode_status.pack()
# Загрузка картинок-стрелок (файлы должны лежать в той же папке)
try:
img_up = ImageTk.PhotoImage(Image.open("вверх.png"))
img_down = ImageTk.PhotoImage(Image.open("вниз.png"))
img_left = ImageTk.PhotoImage(Image.open("влево.png"))
img_right = ImageTk.PhotoImage(Image.open("вправо.png"))
except:
# Если картинок нет – используем текст
img_up = img_down = img_left = img_right = None
buttons_frame = tk.Frame(root)
buttons_frame.pack(pady=20)
btn_up = tk.Button(buttons_frame, text="⬆" if img_up is None else "", image=img_up, command=lambda: send_command('U'), width=8)
btn_up.grid(row=0, column=1, padx=5, pady=5)
btn_down = tk.Button(buttons_frame, text="⬇" if img_down is None else "", image=img_down, command=lambda: send_command('D'), width=8)
btn_down.grid(row=1, column=1, padx=5, pady=5)
btn_left = tk.Button(buttons_frame, text="⬅" if img_left is None else "", image=img_left, command=lambda: send_command('L'), width=8)
btn_left.grid(row=1, column=0, padx=5, pady=5)
btn_right = tk.Button(buttons_frame, text="➡" if img_right is None else "", image=img_right, command=lambda: send_command('R'), width=8)
btn_right.grid(row=1, column=2, padx=5, pady=5)
root.after(100, update_distance)
root.mainloop()