Project

General

Profile

Desarrollo del Prototipo » History » Version 74

antonella butron, 11/17/2024 10:43 PM

1 33 antonella butron
----
2 33 antonella butron
3 34 antonella butron
h1=. %{color: purple}Desarrollo del Prototipo%
4 1 antonella butron
5 32 bastian cruz
h1=. %{color: purple}Avance del robot EV3%
6 8 antonella butron
7 72 antonella butron
h1=. "Robot se mueve solo":https://youtube.com/shorts/gr5waMOEi_Q?si=b0sV5mOU094Sfs_I (video)
8 71 antonella butron
9 72 antonella butron
h1=. "Movimiento de la Garra":https://youtube.com/shorts/DefEWpvIxXI?si=5R5jll9lt9r1wHsJ (video)
10 69 antonella butron
11 69 antonella butron
12 1 antonella butron
13 64 antonella butron
table{border-collapse: collapse; width: 100%; text-align:center}.
14 62 antonella butron
|{border: 1px solid black; background-color:#D8BFD8; font-size: 15px; text-align:center}. *Versión*    |{border: 1px solid black; background-color:#D8BFD8; font-size: 15px; text-align:center}. *Imagen* |
15 66 antonella butron
|{border: 1px solid black;text-align:center; background-color: #F7F2F7;}. _Versión Beta_ |{border: 1px solid black; text-align:center; background-color: #F7F2F7;}. !vprueba.jpg! |
16 67 antonella butron
|{border: 1px solid black;text-align:center; background-color: #F7F2F7;}. _Versión 1_    |{border: 1px solid black;text-align:center; background-color: #F7F2F7;}. !V1.jpg!      |
17 66 antonella butron
|{border: 1px solid black;text-align:center; background-color: #F7F2F7;}. _Versión 2_    |{border: 1px solid black;text-align:center; background-color: #F7F2F7;}. !V2.jpg!      |
18 66 antonella butron
|{border: 1px solid black;text-align:center; background-color: #F7F2F7;}. _Versión 3_    |{border: 1px solid black;text-align:center; background-color: #F7F2F7;}. !V3.jpg!      |
19 66 antonella butron
|{border: 1px solid black;text-align:center; background-color: #F7F2F7;}. _Versión 4_    |{border: 1px solid black;text-align:center; background-color: #F7F2F7;}. !V4.jpg!      |
20 66 antonella butron
|{border: 1px solid black;text-align:center; background-color: #F7F2F7;}. _Versión 5_    |{border: 1px solid black;text-align:center; background-color: #F7F2F7;}. !V5.jpg!      |
21 66 antonella butron
|{border: 1px solid black;text-align:center; background-color: #F7F2F7;}. _Versión 6_    |{border: 1px solid black;text-align:center; background-color: #F7F2F7;}. !V6A.jpg!     |
22 66 antonella butron
|{border: 1px solid black;text-align:center; background-color: #F7F2F7;}. _Versión 7_    |{border: 1px solid black;text-align:center; background-color: #F7F2F7;}. !V7final.jpg!     |
23 57 antonella butron
24 54 antonella butron
25 54 antonella butron
26 54 antonella butron
27 27 antonella butron
h1=. **Código Utilizado**
28 33 antonella butron
29 73 antonella butron
Interfaz Gráfica 
30 73 antonella butron
31 73 antonella butron
{{python}}
32 73 antonella butron
import socket
33 73 antonella butron
import tkinter as tk
34 73 antonella butron
from tkinter import font, messagebox
35 73 antonella butron
import threading
36 73 antonella butron
37 73 antonella butron
class EV3ControlApp:
38 73 antonella butron
    contador_subir = 0
39 73 antonella butron
    contador_bajar = 1
40 73 antonella butron
    contador_abrir = 1
41 73 antonella butron
    contador_cerrar = 0
42 73 antonella butron
43 73 antonella butron
    def __init__(self, master):
44 73 antonella butron
        self.master = master
45 73 antonella butron
        self.master.title("EV3 Control Panel")
46 73 antonella butron
        self.master.configure(bg='#1a1a1a')
47 73 antonella butron
        self.master.geometry('600x400')
48 73 antonella butron
49 73 antonella butron
        self.sock = None
50 73 antonella butron
51 73 antonella butron
        # Configuración de columnas y filas
52 73 antonella butron
        self.master.grid_columnconfigure(0, weight=1)
53 73 antonella butron
        self.master.grid_columnconfigure(1, weight=1)
54 73 antonella butron
        self.master.grid_columnconfigure(2, weight=1)
55 73 antonella butron
        self.master.grid_columnconfigure(3, weight=1)
56 73 antonella butron
        self.master.grid_rowconfigure(10, weight=1)
57 73 antonella butron
        self.master.grid_rowconfigure(11, weight=1)
58 73 antonella butron
        self.master.grid_rowconfigure(12, weight=1)
59 73 antonella butron
        self.master.grid_rowconfigure(13, weight=1)
60 73 antonella butron
        self.master.grid_rowconfigure(14, weight=1)
61 73 antonella butron
        self.master.grid_rowconfigure(15, weight=1)
62 73 antonella butron
63 73 antonella butron
        # Configuración de fuentes y estilo
64 73 antonella butron
        self.btn_font = font.Font(family='Helvetica', size=14, weight='bold')
65 73 antonella butron
        self.bg_color = '#2e2e2e'
66 73 antonella butron
        self.fg_color = 'white'
67 73 antonella butron
        self.active_bg = '#4d4d4d'
68 73 antonella butron
        self.padx = 15
69 73 antonella butron
        self.pady = 15
70 73 antonella butron
71 73 antonella butron
        # Crear widgets
72 73 antonella butron
        self.create_widgets()
73 73 antonella butron
74 73 antonella butron
    def create_widgets(self):
75 73 antonella butron
        self.ip_label = tk.Label(self.master, text="IP del EV3:", font=self.btn_font, bg=self.master['bg'], fg=self.fg_color)
76 73 antonella butron
        self.ip_label.grid(row=10, column=1, padx=self.padx, pady=self.pady)
77 73 antonella butron
78 73 antonella butron
        self.ip_entry = tk.Entry(self.master, font=self.btn_font, bg=self.bg_color, fg=self.fg_color)
79 73 antonella butron
        self.ip_entry.grid(row=10, column=2, padx=self.padx, pady=self.pady)
80 73 antonella butron
81 73 antonella butron
        self.btn_connect = tk.Button(self.master, text="Conectar", font=self.btn_font, bg=self.bg_color, fg=self.fg_color,
82 73 antonella butron
                                      activebackground=self.active_bg, command=self.start_connect_thread)
83 73 antonella butron
        self.btn_connect.grid(row=10, column=3, padx=self.padx, pady=self.pady)
84 73 antonella butron
85 73 antonella butron
        # Botones de control de movimiento
86 73 antonella butron
        self.btn_up = tk.Button(self.master, text="▲", font=self.btn_font, bg=self.bg_color, fg=self.fg_color,
87 73 antonella butron
                                activebackground=self.active_bg, command=self.move_up)
88 73 antonella butron
        self.btn_up.grid(row=11, column=1, padx=self.padx, pady=self.pady, sticky="nsew")
89 73 antonella butron
90 73 antonella butron
        self.btn_left = tk.Button(self.master, text="", font=self.btn_font, bg=self.bg_color, fg=self.fg_color,
91 73 antonella butron
                                  activebackground=self.active_bg, command=self.move_left)
92 73 antonella butron
        self.btn_left.grid(row=12, column=0, padx=self.padx, pady=self.pady, sticky="nsew")
93 73 antonella butron
94 73 antonella butron
        self.btn_stop = tk.Button(self.master, text="■", font=self.btn_font, bg=self.bg_color, fg=self.fg_color,
95 73 antonella butron
                                  activebackground=self.active_bg, command=self.stop)
96 73 antonella butron
        self.btn_stop.grid(row=12, column=1, padx=self.padx, pady=self.pady, sticky="nsew")
97 73 antonella butron
98 73 antonella butron
        self.btn_right = tk.Button(self.master, text="", font=self.btn_font, bg=self.bg_color, fg=self.fg_color,
99 73 antonella butron
                                   activebackground=self.active_bg, command=self.move_right)
100 73 antonella butron
        self.btn_right.grid(row=12, column=2, padx=self.padx, pady=self.pady, sticky="nsew")
101 73 antonella butron
102 73 antonella butron
        self.btn_down = tk.Button(self.master, text="▼", font=self.btn_font, bg=self.bg_color, fg=self.fg_color,
103 73 antonella butron
                                  activebackground=self.active_bg, command=self.move_down)
104 73 antonella butron
        self.btn_down.grid(row=13, column=1, padx=self.padx, pady=self.pady, sticky="nsew")
105 73 antonella butron
106 73 antonella butron
        self.btn_claw_updown = tk.Button(self.master, text="Subir/Bajar garra", font=self.btn_font, bg=self.bg_color, fg=self.fg_color,
107 73 antonella butron
                                          activebackground=self.active_bg, command=self.handle_updown)
108 73 antonella butron
        self.btn_claw_updown.grid(row=12, column=3, padx=self.padx, pady=self.pady, sticky="nsew")
109 73 antonella butron
110 73 antonella butron
        self.btn_claw_openclose = tk.Button(self.master, text="Abrir/Cerrar garra", font=self.btn_font, bg=self.bg_color, fg=self.fg_color,
111 73 antonella butron
                                             activebackground=self.active_bg, command=self.handle_openclose)
112 73 antonella butron
        self.btn_claw_openclose.grid(row=13, column=3, padx=self.padx, pady=self.pady, sticky="nsew")
113 73 antonella butron
114 73 antonella butron
        self.btn_stop_handle = tk.Button(self.master, text="Parar garra", font=self.btn_font, bg=self.bg_color, fg=self.fg_color,
115 73 antonella butron
                                          activebackground='#ff4c4c', command=self.stop_handle)
116 73 antonella butron
        self.btn_stop_handle.grid(row=14, column=3, columnspan=2, padx=self.padx, pady=self.pady, sticky="nsew")
117 73 antonella butron
118 73 antonella butron
        self.btn_quit = tk.Button(self.master, text="Cerrar servidor", font=self.btn_font, bg='#ff5c5c', fg=self.fg_color,
119 73 antonella butron
                                  activebackground='#ff4c4c', command=self.quit_app)
120 73 antonella butron
        self.btn_quit.grid(row=15, column=1, columnspan=2, padx=self.padx, pady=self.pady, sticky="nsew")
121 73 antonella butron
122 73 antonella butron
    def start_connect_thread(self):
123 73 antonella butron
        """Inicia un hilo para la conexión, manteniendo la interfaz responsiva."""
124 73 antonella butron
        ip = self.ip_entry.get()
125 73 antonella butron
        if self.is_valid_ip(ip):
126 73 antonella butron
            threading.Thread(target=self.connect, args=(ip,), daemon=True).start()
127 73 antonella butron
        else:
128 73 antonella butron
            messagebox.showerror("Error de entrada", "La dirección IP ingresada no es válida. Por favor, intente de nuevo.")
129 73 antonella butron
130 73 antonella butron
    def is_valid_ip(self, ip):
131 73 antonella butron
        """Verifica si la IP ingresada es válida."""
132 73 antonella butron
        parts = ip.split('.')
133 73 antonella butron
        if len(parts) != 4:
134 73 antonella butron
            return False
135 73 antonella butron
        for part in parts:
136 73 antonella butron
            if not part.isdigit() or not (0 <= int(part) < 256):
137 73 antonella butron
                return False
138 73 antonella butron
        return True
139 73 antonella butron
140 73 antonella butron
    def connect(self, ip):
141 73 antonella butron
        try:
142 73 antonella butron
            if self.sock is not None:
143 73 antonella butron
                self.sock.close()
144 73 antonella butron
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
145 73 antonella butron
            self.sock.connect((ip, 8080))
146 73 antonella butron
            messagebox.showinfo("Conexión", "Conectado correctamente al EV3.")
147 73 antonella butron
            self.enable_controls(True)
148 73 antonella butron
        except Exception as e:
149 73 antonella butron
            messagebox.showerror("Error de conexión", f"No se pudo conectar a {ip}. \n{str(e)}")
150 73 antonella butron
            self.enable_controls(False)
151 73 antonella butron
152 73 antonella butron
    def enable_controls(self, state):
153 73 antonella butron
        """Habilita o deshabilita los botones de control según el estado proporcionado."""
154 73 antonella butron
        self.btn_up.config(state=tk.NORMAL if state else tk.DISABLED)
155 73 antonella butron
        self.btn_left.config(state=tk.NORMAL if state else tk.DISABLED)
156 73 antonella butron
        self.btn_stop.config(state=tk.NORMAL if state else tk.DISABLED)
157 73 antonella butron
        self.btn_right.config(state=tk.NORMAL if state else tk.DISABLED)
158 73 antonella butron
        self.btn_down.config(state=tk.NORMAL if state else tk.DISABLED)
159 73 antonella butron
        self.btn_claw_updown.config(state=tk.NORMAL if state else tk.DISABLED)
160 73 antonella butron
        self.btn_claw_openclose.config(state=tk.NORMAL if state else tk.DISABLED)
161 73 antonella butron
        self.btn_stop_handle.config(state=tk.NORMAL if state else tk.DISABLED)
162 73 antonella butron
        self.btn_quit.config(state=tk.NORMAL if state else tk.DISABLED)
163 73 antonella butron
164 73 antonella butron
    def send_command(self, command):
165 73 antonella butron
        if self.sock:
166 73 antonella butron
            self.sock.sendall(command.encode('utf-8'))
167 73 antonella butron
168 73 antonella butron
    def move_up(self):
169 73 antonella butron
        self.send_command('w')
170 73 antonella butron
171 73 antonella butron
    def move_down(self):
172 73 antonella butron
        self.send_command('s')
173 73 antonella butron
174 73 antonella butron
    def move_left(self):
175 73 antonella butron
        self.send_command('a')
176 73 antonella butron
177 73 antonella butron
    def move_right(self):
178 73 antonella butron
        self.send_command('d')
179 73 antonella butron
180 73 antonella butron
    def stop(self):
181 73 antonella butron
        self.send_command('x')
182 73 antonella butron
183 73 antonella butron
    def handle_updown(self):
184 73 antonella butron
        if EV3ControlApp.contador_bajar == 0:
185 73 antonella butron
            self.send_command('g')
186 73 antonella butron
            EV3ControlApp.contador_bajar += 1
187 73 antonella butron
            EV3ControlApp.contador_subir = 0
188 73 antonella butron
        else:
189 73 antonella butron
            self.send_command('t')
190 73 antonella butron
            EV3ControlApp.contador_subir += 1
191 73 antonella butron
            EV3ControlApp.contador_bajar = 0
192 73 antonella butron
193 73 antonella butron
    def handle_openclose(self):
194 73 antonella butron
        if EV3ControlApp.contador_cerrar == 0:
195 73 antonella butron
            self.send_command('f')
196 73 antonella butron
            EV3ControlApp.contador_cerrar += 1
197 73 antonella butron
            EV3ControlApp.contador_abrir = 0
198 73 antonella butron
        else:
199 73 antonella butron
            self.send_command('r')
200 73 antonella butron
            EV3ControlApp.contador_abrir += 1
201 73 antonella butron
            EV3ControlApp.contador_cerrar = 0
202 73 antonella butron
203 73 antonella butron
    def stop_handle(self):
204 73 antonella butron
        self.send_command('c')
205 73 antonella butron
206 73 antonella butron
    def quit_app(self):
207 73 antonella butron
        if EV3ControlApp.contador_abrir == 0:
208 73 antonella butron
            self.send_command('r')
209 73 antonella butron
        if EV3ControlApp.contador_bajar == 0:
210 73 antonella butron
            self.send_command('g')
211 73 antonella butron
        self.send_command('c')
212 73 antonella butron
        self.send_command('q')
213 73 antonella butron
        self.sock.close()
214 73 antonella butron
        self.master.quit()
215 73 antonella butron
216 73 antonella butron
if __name__ == "__main__":
217 73 antonella butron
    root = tk.Tk()
218 73 antonella butron
    app = EV3ControlApp(root)
219 73 antonella butron
    root.mainloop()
220 74 antonella butron
{{python}}
221 73 antonella butron
222 73 antonella butron
223 73 antonella butron
224 73 antonella butron
225 47 bastian cruz
---