Загрузка данных


import serial
import tkinter as tk

port = 'COM3'  # замените на свой порт
ser = serial.Serial(port, 9600, timeout=0.01)

def send(cmd):
    ser.write(cmd.encode())

def update_dist():
    while ser.in_waiting:
        line = ser.readline().decode().strip()
        if line.startswith("DIST:"):
            d = line.split(":")[1]
            dist_label.config(text=d + " см")
    root.after(50, update_dist)

def set_manual():
    send('M')
    status.config(text="Ручной")

def set_auto():
    send('A')
    status.config(text="Автомат")

root = tk.Tk()
root.title("Управление дроном")
root.geometry("300x400")

tk.Label(root, text="Расстояние:").pack(pady=(10,0))
dist_label = tk.Label(root, text="0 см", font=("Arial", 20))
dist_label.pack()

mode_frame = tk.Frame(root)
mode_frame.pack(pady=10)
tk.Button(mode_frame, text="Ручной", command=set_manual, width=10).pack(side="left", padx=5)
tk.Button(mode_frame, text="Автомат", command=set_auto, width=10).pack(side="left", padx=5)
status = tk.Label(root, text="Ручной")
status.pack()

btn_frame = tk.Frame(root)
btn_frame.pack(pady=20)
tk.Button(btn_frame, text="Вперёд", command=lambda: send('F'), width=8).grid(row=0, column=1, padx=5, pady=5)
tk.Button(btn_frame, text="Назад",  command=lambda: send('B'), width=8).grid(row=2, column=1, padx=5, pady=5)
tk.Button(btn_frame, text="Влево",  command=lambda: send('L'), width=8).grid(row=1, column=0, padx=5, pady=5)
tk.Button(btn_frame, text="Вверх",  command=lambda: send('U'), width=8).grid(row=1, column=1, padx=5, pady=5)
tk.Button(btn_frame, text="Вниз",   command=lambda: send('D'), width=8).grid(row=2, column=1, padx=5, pady=5)
tk.Button(btn_frame, text="Вправо", command=lambda: send('R'), width=8).grid(row=1, column=2, padx=5, pady=5)

root.after(100, update_dist)
root.mainloop()

#include <Servo.h>

Servo servoYaw;
Servo servoPitch;

const int motorPin = 5;
const int trig = 12, echo = 13;

int motorSpeed = 150;
int yaw = 90, pitch = 90;
char mode = 'M';

void setup() {
  Serial.begin(9600);
  servoYaw.attach(9);
  servoPitch.attach(6);
  pinMode(motorPin, OUTPUT);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  servoYaw.write(yaw);
  servoPitch.write(pitch);
  analogWrite(motorPin, motorSpeed);
}

void loop() {
  digitalWrite(trig, LOW); delayMicroseconds(2);
  digitalWrite(trig, HIGH); delayMicroseconds(10); digitalWrite(trig, LOW);
  long dur = pulseIn(echo, HIGH);
  int dist = constrain(dur * 0.034 / 2, 0, 150);
  Serial.print("DIST:");
  Serial.println(dist);

  if (Serial.available()) {
    char cmd = Serial.read();
    if (cmd == 'M') mode = 'M';
    if (cmd == 'A') mode = 'A';
    
    if (mode == 'M') {
      if (cmd == 'U') motorSpeed = min(255, motorSpeed + 20);
      if (cmd == 'D') motorSpeed = max(0, motorSpeed - 20);
      if (cmd == 'L') yaw = max(0, yaw - 15);
      if (cmd == 'R') yaw = min(180, yaw + 15);
      if (cmd == 'F') pitch = min(180, pitch + 15);
      if (cmd == 'B') pitch = max(0, pitch - 15);
      servoYaw.write(yaw);
      servoPitch.write(pitch);
      analogWrite(motorPin, motorSpeed);
    }
  }

  if (mode == 'A') {
    if (dist < 30) {
      analogWrite(motorPin, 200);
      servoYaw.write(120);
      servoPitch.write(110);
    } else {
      analogWrite(motorPin, 150);
      servoYaw.write(90);
      servoPitch.write(90);
    }
  }
  delay(50);
}