Project

General

Profile

Desarrollo del Prototipo » History » Version 77

« Previous - Version 77/80 (diff) - Next » - Current version
antonella butron, 11/17/2024 10:53 PM



Desarrollo del Prototipo

Avance del robot EV3

Robot se mueve solo (video)

Movimiento de la Garra (video)

Versión Imagen
Versión Beta
Versión 1
Versión 2
Versión 3
Versión 4
Versión 5
Versión 6
Versión 7

Código Utilizado

Interfaz Gráfica

import socket
import tkinter as tk
from tkinter import font, messagebox
import threading

class EV3ControlApp:
    contador_subir = 0
    contador_bajar = 1
    contador_abrir = 1
    contador_cerrar = 0

    def __init__(self, master):
        self.master = master
        self.master.title("EV3 Control Panel")
        self.master.configure(bg='#1a1a1a')
        self.master.geometry('600x400')

        self.sock = None

        # Configuración de columnas y filas
        self.master.grid_columnconfigure(0, weight=1)
        self.master.grid_columnconfigure(1, weight=1)
        self.master.grid_columnconfigure(2, weight=1)
        self.master.grid_columnconfigure(3, weight=1)
        self.master.grid_rowconfigure(10, weight=1)
        self.master.grid_rowconfigure(11, weight=1)
        self.master.grid_rowconfigure(12, weight=1)
        self.master.grid_rowconfigure(13, weight=1)
        self.master.grid_rowconfigure(14, weight=1)
        self.master.grid_rowconfigure(15, weight=1)

        # Configuración de fuentes y estilo
        self.btn_font = font.Font(family='Helvetica', size=14, weight='bold')
        self.bg_color = '#2e2e2e'
        self.fg_color = 'white'
        self.active_bg = '#4d4d4d'
        self.padx = 15
        self.pady = 15

        # Crear widgets
        self.create_widgets()

    def create_widgets(self):
        self.ip_label = tk.Label(self.master, text="IP del EV3:", font=self.btn_font, bg=self.master['bg'], fg=self.fg_color)
        self.ip_label.grid(row=10, column=1, padx=self.padx, pady=self.pady)

        self.ip_entry = tk.Entry(self.master, font=self.btn_font, bg=self.bg_color, fg=self.fg_color)
        self.ip_entry.grid(row=10, column=2, padx=self.padx, pady=self.pady)

        self.btn_connect = tk.Button(self.master, text="Conectar", font=self.btn_font, bg=self.bg_color, fg=self.fg_color,
                                      activebackground=self.active_bg, command=self.start_connect_thread)
        self.btn_connect.grid(row=10, column=3, padx=self.padx, pady=self.pady)

        # Botones de control de movimiento
        self.btn_up = tk.Button(self.master, text="▲", font=self.btn_font, bg=self.bg_color, fg=self.fg_color,
                                activebackground=self.active_bg, command=self.move_up)
        self.btn_up.grid(row=11, column=1, padx=self.padx, pady=self.pady, sticky="nsew")

        self.btn_left = tk.Button(self.master, text="", font=self.btn_font, bg=self.bg_color, fg=self.fg_color,
                                  activebackground=self.active_bg, command=self.move_left)
        self.btn_left.grid(row=12, column=0, padx=self.padx, pady=self.pady, sticky="nsew")

        self.btn_stop = tk.Button(self.master, text="■", font=self.btn_font, bg=self.bg_color, fg=self.fg_color,
                                  activebackground=self.active_bg, command=self.stop)
        self.btn_stop.grid(row=12, column=1, padx=self.padx, pady=self.pady, sticky="nsew")

        self.btn_right = tk.Button(self.master, text="", font=self.btn_font, bg=self.bg_color, fg=self.fg_color,
                                   activebackground=self.active_bg, command=self.move_right)
        self.btn_right.grid(row=12, column=2, padx=self.padx, pady=self.pady, sticky="nsew")

        self.btn_down = tk.Button(self.master, text="▼", font=self.btn_font, bg=self.bg_color, fg=self.fg_color,
                                  activebackground=self.active_bg, command=self.move_down)
        self.btn_down.grid(row=13, column=1, padx=self.padx, pady=self.pady, sticky="nsew")

        self.btn_claw_updown = tk.Button(self.master, text="Subir/Bajar garra", font=self.btn_font, bg=self.bg_color, fg=self.fg_color,
                                          activebackground=self.active_bg, command=self.handle_updown)
        self.btn_claw_updown.grid(row=12, column=3, padx=self.padx, pady=self.pady, sticky="nsew")

        self.btn_claw_openclose = tk.Button(self.master, text="Abrir/Cerrar garra", font=self.btn_font, bg=self.bg_color, fg=self.fg_color,
                                             activebackground=self.active_bg, command=self.handle_openclose)
        self.btn_claw_openclose.grid(row=13, column=3, padx=self.padx, pady=self.pady, sticky="nsew")

        self.btn_stop_handle = tk.Button(self.master, text="Parar garra", font=self.btn_font, bg=self.bg_color, fg=self.fg_color,
                                          activebackground='#ff4c4c', command=self.stop_handle)
        self.btn_stop_handle.grid(row=14, column=3, columnspan=2, padx=self.padx, pady=self.pady, sticky="nsew")

        self.btn_quit = tk.Button(self.master, text="Cerrar servidor", font=self.btn_font, bg='#ff5c5c', fg=self.fg_color,
                                  activebackground='#ff4c4c', command=self.quit_app)
        self.btn_quit.grid(row=15, column=1, columnspan=2, padx=self.padx, pady=self.pady, sticky="nsew")

    def start_connect_thread(self):
        ip = self.ip_entry.get()
        if self.is_valid_ip(ip):
            threading.Thread(target=self.connect, args=(ip,), daemon=True).start()
        else:
            messagebox.showerror("Error de entrada", "La dirección IP ingresada no es válida.")

    def is_valid_ip(self, ip):
        parts = ip.split('.')
        if len(parts) != 4:
            return False
        for part in parts:
            if not part.isdigit() or not (0 <= int(part) < 256):
                return False
        return True

    def connect(self, ip):
        try:
            if self.sock is not None:
                self.sock.close()
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.sock.connect((ip, 8080))
            messagebox.showinfo("Conexión", "Conectado correctamente al EV3.")
            self.enable_controls(True)
        except Exception as e:
            messagebox.showerror("Error de conexión", f"No se pudo conectar a {ip}. \n{str(e)}")
            self.enable_controls(False)

    def enable_controls(self, state):
        self.btn_up.config(state=tk.NORMAL if state else tk.DISABLED)
        self.btn_left.config(state=tk.NORMAL if state else tk.DISABLED)
        self.btn_stop.config(state=tk.NORMAL if state else tk.DISABLED)
        self.btn_right.config(state=tk.NORMAL if state else tk.DISABLED)
        self.btn_down.config(state=tk.NORMAL if state else tk.DISABLED)
        self.btn_claw_updown.config(state=tk.NORMAL if state else tk.DISABLED)
        self.btn_claw_openclose.config(state=tk.NORMAL if state else tk.DISABLED)
        self.btn_stop_handle.config(state=tk.NORMAL if state else tk.DISABLED)
        self.btn_quit.config(state=tk.NORMAL if state else tk.DISABLED)

    def send_command(self, command):
        if self.sock:
            self.sock.sendall(command.encode('utf-8'))

    def move_up(self):
        self.send_command('w')

    def move_down(self):
        self.send_command('s')

    def move_left(self):
        self.send_command('a')

    def move_right(self):
        self.send_command('d')

    def stop(self):
        self.send_command('x')

    def handle_updown(self):
        if EV3ControlApp.contador_bajar == 0:
            self.send_command('g')
            EV3ControlApp.contador_bajar += 1
            EV3ControlApp.contador_subir = 0
        else:
            self.send_command('t')
            EV3ControlApp.contador_subir += 1
            EV3ControlApp.contador_bajar = 0

    def handle_openclose(self):
        if EV3ControlApp.contador_cerrar == 0:
            self.send_command('f')
            EV3ControlApp.contador_cerrar += 1
            EV3ControlApp.contador_abrir = 0
        else:
            self.send_command('r')
            EV3ControlApp.contador_abrir += 1
            EV3ControlApp.contador_cerrar = 0

    def stop_handle(self):
        self.send_command('c')

    def quit_app(self):
        if EV3ControlApp.contador_abrir == 0:
            self.send_command('r')
        if EV3ControlApp.contador_bajar == 0:
            self.send_command('g')
        self.send_command('c')
        self.send_command('q')
        self.sock.close()
        self.master.quit()

if __name__ == "__main__":
    root = tk.Tk()
    app = EV3ControlApp(root)
    root.mainloop()

V2.jpg (34.2 KB) bastian cruz, 10/18/2024 10:22 AM

V1.jpg (57.1 KB) bastian cruz, 10/18/2024 10:22 AM

V3.jpg (141 KB) bastian cruz, 10/18/2024 10:22 AM

V4.jpg (247 KB) bastian cruz, 10/18/2024 10:22 AM

vprueba.jpg (93.6 KB) bastian cruz, 10/18/2024 10:22 AM

V5.jpg (215 KB) bastian cruz, 10/18/2024 10:22 AM

V6A.jpg (125 KB) bastian cruz, 10/20/2024 10:19 PM

V7final.jpg (174 KB) bastian cruz, 10/28/2024 09:28 AM