Codigo e Implementación » History » Version 11
Andrea Navia, 01/04/2024 11:39 PM
1 | 2 | Andrea Navia | *Índice:* |
---|---|---|---|
2 | 2 | Andrea Navia | * [[Introducción]] |
3 | 2 | Andrea Navia | * [[Organización de Personal]] |
4 | 2 | Andrea Navia | * [[Planificación del Proyecto]] |
5 | 2 | Andrea Navia | * [[Análisis y diseño]] |
6 | 2 | Andrea Navia | * [[Código e implementación]] |
7 | 2 | Andrea Navia | |
8 | 2 | Andrea Navia | --- |
9 | 2 | Andrea Navia | |
10 | 2 | Andrea Navia | {{thumbnail(KIKIS PROJECT.jpeg, size=400, title=LOGO)}} |
11 | 2 | Andrea Navia | |
12 | 2 | Andrea Navia | --- |
13 | 2 | Andrea Navia | |
14 | 1 | Andrea Navia | h1. Codigo e Implementación |
15 | 3 | Andrea Navia | |
16 | 10 | Andrea Navia | {{collapse(SERVIDOR) |
17 | 10 | Andrea Navia | <pre><code class="ruby"> |
18 | 1 | Andrea Navia | |
19 | 10 | Andrea Navia | #!/usr/bin/env python3 |
20 | 10 | Andrea Navia | import evdev |
21 | 10 | Andrea Navia | import ev3dev.auto as ev3 |
22 | 10 | Andrea Navia | import time |
23 | 10 | Andrea Navia | import socket |
24 | 10 | Andrea Navia | from ev3dev.ev3 import * |
25 | 10 | Andrea Navia | from ev3dev2.motor import LargeMotor, OUTPUT_A, OUTPUT_B, SpeedPercent, MoveTank |
26 | 10 | Andrea Navia | from ev3dev2.motor import LargeMotor |
27 | 10 | Andrea Navia | motorA=LargeMotor("outA") |
28 | 10 | Andrea Navia | motorD=LargeMotor("outD") |
29 | 10 | Andrea Navia | motorB=LargeMotor("outB") |
30 | 1 | Andrea Navia | |
31 | 10 | Andrea Navia | ## Funciones |
32 | 10 | Andrea Navia | def avanzar(): |
33 | 10 | Andrea Navia | motorA.on(-50) |
34 | 10 | Andrea Navia | motorD.on(-50) |
35 | 10 | Andrea Navia | time.sleep(1) |
36 | 10 | Andrea Navia | motorA.stop() |
37 | 10 | Andrea Navia | motorD.stop() |
38 | 10 | Andrea Navia | def golpear_1(): |
39 | 10 | Andrea Navia | motorB.on(-5) |
40 | 10 | Andrea Navia | time.sleep(0.5) |
41 | 10 | Andrea Navia | motorB.stop() |
42 | 10 | Andrea Navia | def golpear_2(): |
43 | 10 | Andrea Navia | mototB.on(70) |
44 | 10 | Andrea Navia | motorB.stop() |
45 | 10 | Andrea Navia | time.sleep(0.5) |
46 | 10 | Andrea Navia | def golpear_3(): |
47 | 10 | Andrea Navia | motorB.on(5) |
48 | 10 | Andrea Navia | time.sleep(0.5) |
49 | 10 | Andrea Navia | motorB.stop() |
50 | 10 | Andrea Navia | def retroceder(): |
51 | 10 | Andrea Navia | motorD.on(50,False) |
52 | 10 | Andrea Navia | motorA.on(50,False) |
53 | 10 | Andrea Navia | time.sleep(1) |
54 | 10 | Andrea Navia | motorA.stop() |
55 | 10 | Andrea Navia | motorD.stop() |
56 | 10 | Andrea Navia | def derecha(): |
57 | 10 | Andrea Navia | motorA.on(-20) |
58 | 10 | Andrea Navia | motorD.on(20) |
59 | 10 | Andrea Navia | time.sleep(0.5) |
60 | 10 | Andrea Navia | motorA.stop() |
61 | 10 | Andrea Navia | motorD.stop() |
62 | 10 | Andrea Navia | def izquierda(): |
63 | 10 | Andrea Navia | motorD.on(-20) |
64 | 10 | Andrea Navia | motorA.on(20) |
65 | 10 | Andrea Navia | time.sleep(0.5) |
66 | 10 | Andrea Navia | motorD.stop() |
67 | 10 | Andrea Navia | motorA.stop() |
68 | 10 | Andrea Navia | #funciones de inclinacion del angulo |
69 | 10 | Andrea Navia | def angulo_30(): |
70 | 10 | Andrea Navia | motorB.on_for_degrees(30,10) |
71 | 10 | Andrea Navia | def angulo_45(): |
72 | 10 | Andrea Navia | motorB.on_for_degrees(45.10) |
73 | 10 | Andrea Navia | def angulo_60(): |
74 | 10 | Andrea Navia | motorB.on_for_degrees(60,10) |
75 | 10 | Andrea Navia | def angulo_90(): |
76 | 10 | Andrea Navia | motorB.on_for_degrees(90,10) |
77 | 10 | Andrea Navia | #obtener ip de la conexion |
78 | 10 | Andrea Navia | def obtener_ip(): |
79 | 10 | Andrea Navia | try: |
80 | 10 | Andrea Navia | # Conectarse a un servicio que devuelva la dirección IP pública. |
81 | 10 | Andrea Navia | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
82 | 10 | Andrea Navia | s.connect(("8.8.8.8", 80)) # Conectar a un servidor DNS (Google DNS en este caso). |
83 | 10 | Andrea Navia | ip = s.getsockname()[0] |
84 | 10 | Andrea Navia | s.close() |
85 | 10 | Andrea Navia | return ip |
86 | 10 | Andrea Navia | except Exception as e: |
87 | 10 | Andrea Navia | print(f"Error al obtener la dirección IP: {e}") |
88 | 10 | Andrea Navia | return None |
89 | 10 | Andrea Navia | host = obtener_ip() |
90 | 10 | Andrea Navia | port = 12345 |
91 | 1 | Andrea Navia | |
92 | 10 | Andrea Navia | server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) |
93 | 10 | Andrea Navia | server_socket.bind((host, port)) |
94 | 1 | Andrea Navia | |
95 | 10 | Andrea Navia | server_socket.listen(1) |
96 | 10 | Andrea Navia | print("Listening at port 12345") |
97 | 10 | Andrea Navia | print(host) |
98 | 1 | Andrea Navia | |
99 | 10 | Andrea Navia | client_socket, addr = server_socket.accept() |
100 | 10 | Andrea Navia | print("Connection from: " + str(addr)) |
101 | 10 | Andrea Navia | client_socket.send("Bienvenido".encode()) |
102 | 10 | Andrea Navia | while True: |
103 | 10 | Andrea Navia | data = client_socket.recv(1024).decode() |
104 | 10 | Andrea Navia | if data=="avanzar": |
105 | 10 | Andrea Navia | avanzar() |
106 | 10 | Andrea Navia | elif data == "golpear": |
107 | 10 | Andrea Navia | golpear_2() |
108 | 10 | Andrea Navia | elif data=="atras": |
109 | 10 | Andrea Navia | golpear_3() |
110 | 10 | Andrea Navia | elif data=="adelante": |
111 | 10 | Andrea Navia | golpear_1() |
112 | 10 | Andrea Navia | elif data=="retroceder": |
113 | 10 | Andrea Navia | retroceder() |
114 | 10 | Andrea Navia | elif data=="derecha": |
115 | 10 | Andrea Navia | derecha() |
116 | 10 | Andrea Navia | elif data=="izquierda": |
117 | 10 | Andrea Navia | izquierda() |
118 | 10 | Andrea Navia | elif data=="desconectar": |
119 | 10 | Andrea Navia | server_socket.close() |
120 | 10 | Andrea Navia | print("Disconect") |
121 | 10 | Andrea Navia | elif data=="30°": |
122 | 10 | Andrea Navia | angulo_30() |
123 | 10 | Andrea Navia | elif data=="45°": |
124 | 10 | Andrea Navia | angulo_45() |
125 | 10 | Andrea Navia | elif data=="60°": |
126 | 10 | Andrea Navia | angulo_60() |
127 | 10 | Andrea Navia | elif data=="90°": |
128 | 10 | Andrea Navia | angulo_90() |
129 | 10 | Andrea Navia | |
130 | 10 | Andrea Navia | </code></pre> |
131 | 10 | Andrea Navia | }} |
132 | 10 | Andrea Navia | |
133 | 11 | Andrea Navia | {{collapse(INTERFAZ Y FUNCIONES) |
134 | 10 | Andrea Navia | <pre><code class="ruby"> |
135 | 10 | Andrea Navia | |
136 | 10 | Andrea Navia | import socket |
137 | 10 | Andrea Navia | import tkinter as tk |
138 | 10 | Andrea Navia | from tkinter import ttk |
139 | 10 | Andrea Navia | from tkinter import messagebox |
140 | 10 | Andrea Navia | import tkinter.font as font |
141 | 10 | Andrea Navia | from tkinter import PhotoImage |
142 | 10 | Andrea Navia | import os |
143 | 10 | Andrea Navia | from tkinter import * |
144 | 10 | Andrea Navia | |
145 | 10 | Andrea Navia | AVANZAR = 'avanzar' |
146 | 10 | Andrea Navia | RETROCEDER = 'retroceder' |
147 | 10 | Andrea Navia | CLOSE = 'close' |
148 | 10 | Andrea Navia | DERECHA='derecha' |
149 | 10 | Andrea Navia | IZQUIERDA='izquierda' |
150 | 10 | Andrea Navia | GOLPEAR='golpear' |
151 | 10 | Andrea Navia | ATRAS='atras' |
152 | 10 | Andrea Navia | ADELANTE='adelante' |
153 | 10 | Andrea Navia | DESCONECTAR='desconectar' |
154 | 10 | Andrea Navia | ANGULO_30='30°' |
155 | 10 | Andrea Navia | ANGULO_45='45°' |
156 | 10 | Andrea Navia | ANGULO_60='60°' |
157 | 10 | Andrea Navia | ANGULO_90='90°' |
158 | 10 | Andrea Navia | MOVER='mover' |
159 | 10 | Andrea Navia | |
160 | 10 | Andrea Navia | def conectar(ventana): |
161 | 10 | Andrea Navia | try: |
162 | 10 | Andrea Navia | port=12345 |
163 | 10 | Andrea Navia | host=direccion_ip.get() |
164 | 10 | Andrea Navia | global client |
165 | 10 | Andrea Navia | client = socket.socket() |
166 | 10 | Andrea Navia | client.connect((host, port)) |
167 | 10 | Andrea Navia | print(client.recv(1024).decode()) |
168 | 10 | Andrea Navia | estado_label.config(text="Estado: Conectado") |
169 | 10 | Andrea Navia | boton_conectar.config(state=tk.DISABLED) |
170 | 10 | Andrea Navia | ventana.destroy() |
171 | 10 | Andrea Navia | except socket.error: |
172 | 10 | Andrea Navia | estado_label.config(text="Estado: Fallo al conectar") |
173 | 10 | Andrea Navia | boton_conectar.config(state=tk.NORMAL) |
174 | 10 | Andrea Navia | ventana.destroy() |
175 | 10 | Andrea Navia | messagebox.showwarning("Conexión erronea","No se ha logrado al conexíon, verifique la IP") |
176 | 10 | Andrea Navia | |
177 | 10 | Andrea Navia | def ventana_ip(): |
178 | 10 | Andrea Navia | boton_conectar.config(state=tk.DISABLED) |
179 | 10 | Andrea Navia | ventana_2 = tk.Toplevel(ventana) |
180 | 10 | Andrea Navia | ventana_2.title("Conexión a servidor") |
181 | 10 | Andrea Navia | ventana_2.configure(bg="#ABDBD8") |
182 | 10 | Andrea Navia | ventana_2.geometry("150x120") |
183 | 10 | Andrea Navia | |
184 | 10 | Andrea Navia | etiqueta_ip = tk.Label(ventana_2, fg=a,text="Ingresar dirección de IP:", bg="#567E8B") |
185 | 10 | Andrea Navia | etiqueta_ip.pack(pady=10) |
186 | 10 | Andrea Navia | |
187 | 10 | Andrea Navia | global direccion_ip |
188 | 10 | Andrea Navia | direccion_ip= tk.Entry(ventana_2,bg=a) |
189 | 10 | Andrea Navia | direccion_ip.pack(pady=10) |
190 | 10 | Andrea Navia | estado_label.config(text="Estado: Conectando") |
191 | 10 | Andrea Navia | |
192 | 10 | Andrea Navia | boton_ingresar = tk.Button(ventana_2, text="Ingresar",fg=a, command=lambda:conectar(ventana_2),bg="#567E8B") |
193 | 10 | Andrea Navia | boton_ingresar.pack(pady=10) |
194 | 10 | Andrea Navia | |
195 | 10 | Andrea Navia | # funciones |
196 | 10 | Andrea Navia | def avanzar(): |
197 | 10 | Andrea Navia | client.send(AVANZAR.encode()) |
198 | 10 | Andrea Navia | estado_label.config(text="Estado: Avanzando") |
199 | 10 | Andrea Navia | print("Avanzando") |
200 | 10 | Andrea Navia | def retroceder(): |
201 | 10 | Andrea Navia | client.send(RETROCEDER.encode()) |
202 | 10 | Andrea Navia | print("Retrocediendo") |
203 | 10 | Andrea Navia | estado_label.config(text="Estado: Retrocediendo") |
204 | 10 | Andrea Navia | def girar_der(): |
205 | 10 | Andrea Navia | client.send(DERECHA.encode()) |
206 | 10 | Andrea Navia | estado_label.config(text="Estado: Girando hacia la Derecha") |
207 | 10 | Andrea Navia | print("Girando derecha") |
208 | 10 | Andrea Navia | def girar_izq(): |
209 | 10 | Andrea Navia | client.send(IZQUIERDA.encode()) |
210 | 10 | Andrea Navia | estado_label.config(text="Estado: Girando hacia la Izquierda") |
211 | 10 | Andrea Navia | print("Girando izquierda") |
212 | 10 | Andrea Navia | def rotar_brazo_Atras(): |
213 | 10 | Andrea Navia | client.send(ATRAS.encode()) |
214 | 10 | Andrea Navia | estado_label.config(text="Estado: Ajustando brazo hacia Atras") |
215 | 10 | Andrea Navia | def rotar_brazo_Adelante(): |
216 | 10 | Andrea Navia | client.send(ADELANTE.encode()) |
217 | 10 | Andrea Navia | estado_label.config(text="Estado: Ajustando brazo hacia Adelante") |
218 | 10 | Andrea Navia | def angulo(): |
219 | 10 | Andrea Navia | angulo_seleccinado=etiquetas_angulo.get() |
220 | 10 | Andrea Navia | if(angulo_seleccinado=="30°"): |
221 | 10 | Andrea Navia | etiquetas_angulo.set('') |
222 | 10 | Andrea Navia | client.send(ANGULO_30.encode()) |
223 | 10 | Andrea Navia | estado_label.config(text="Estado: Inclinado angulo 30°") |
224 | 10 | Andrea Navia | elif(angulo_seleccinado=="45°"): |
225 | 10 | Andrea Navia | etiquetas_angulo.set('') |
226 | 10 | Andrea Navia | client.send(ANGULO_45.encode()) |
227 | 10 | Andrea Navia | estado_label.config(text="Estado: Inclinado angulo 45°") |
228 | 10 | Andrea Navia | elif(angulo_seleccinado=="60°"): |
229 | 10 | Andrea Navia | etiquetas_angulo.set('') |
230 | 10 | Andrea Navia | client.send(ANGULO_60.encode()) |
231 | 10 | Andrea Navia | estado_label.config(text="Estado: Inclinado angulo 60°") |
232 | 10 | Andrea Navia | elif(angulo_seleccinado=="90°"): |
233 | 10 | Andrea Navia | etiquetas_angulo.set('') |
234 | 10 | Andrea Navia | client.send(ANGULO_90.encode()) |
235 | 10 | Andrea Navia | estado_label.config(text="Estado: Inclinado angulo 90°") |
236 | 10 | Andrea Navia | else: |
237 | 10 | Andrea Navia | estado_label.config(text="Estado: Angulo no encontrado") |
238 | 10 | Andrea Navia | |
239 | 10 | Andrea Navia | def golpear(): |
240 | 10 | Andrea Navia | client.send(GOLPEAR.encode()) |
241 | 10 | Andrea Navia | estado_label.config(text="Estado: Golpeando") |
242 | 10 | Andrea Navia | def desconectar(): |
243 | 10 | Andrea Navia | client.send(DESCONECTAR.encode()) |
244 | 10 | Andrea Navia | estado_label.config(text="Estado: Desconectado") |
245 | 10 | Andrea Navia | client.close() |
246 | 10 | Andrea Navia | ventana.destroy() |
247 | 10 | Andrea Navia | def mover(): |
248 | 10 | Andrea Navia | client.send(MOVER.encode()) |
249 | 10 | Andrea Navia | estado_label.config(text="Estado: Realizando Movimientos") |
250 | 10 | Andrea Navia | # Crear la ventana principal de Tkinter |
251 | 10 | Andrea Navia | ventana = tk.Tk() |
252 | 10 | Andrea Navia | ventana.title("Control de Robot con Palo de Golf") |
253 | 10 | Andrea Navia | ventana.config(bg="#ABDBD8") |
254 | 10 | Andrea Navia | a="white" |
255 | 10 | Andrea Navia | b="light goldenrod yellow" |
256 | 10 | Andrea Navia | buttonFont = font.Font(family='Helvetica', size=24, weight='bold') |
257 | 10 | Andrea Navia | buttonFontSec = font.Font(family='Helvetica', size=15, weight='bold') |
258 | 10 | Andrea Navia | boton_avanzar = tk.Button(ventana, text="^", command=avanzar, height=1, width=3, font=buttonFont,bg=a) |
259 | 10 | Andrea Navia | boton_retroceder = tk.Button(ventana, text="v", command=retroceder, height=1, width=3, font=buttonFont,bg=a) |
260 | 10 | Andrea Navia | boton_izquierda = tk.Button(ventana, text="<", command=girar_izq, height=1, width=3, font=buttonFont,bg=a) |
261 | 10 | Andrea Navia | boton_derecha = tk.Button(ventana, text=">", command=girar_der, height=1, width=3, font=buttonFont,bg=a) |
262 | 10 | Andrea Navia | |
263 | 10 | Andrea Navia | imagen_abajo=PhotoImage(file="C:/Users/nombre/Desktop/abajo.png") |
264 | 10 | Andrea Navia | boton_rotar_atras = tk.Button(ventana,image=imagen_abajo, command=rotar_brazo_Atras, height=60, width=53, font=buttonFontSec,bg=a) |
265 | 10 | Andrea Navia | |
266 | 10 | Andrea Navia | imagen_arriba=PhotoImage(file="C:/Users/nombre/Desktop/arriba.png") |
267 | 10 | Andrea Navia | boton_rotar_adelante = tk.Button(ventana,image=imagen_arriba, command=rotar_brazo_Adelante, height=60, width=53, font=buttonFontSec,bg=a) |
268 | 10 | Andrea Navia | |
269 | 10 | Andrea Navia | imagen_golpe=PhotoImage(file="C:/Users/nombre/Desktop/golpe.png") |
270 | 10 | Andrea Navia | boton_golpear = tk.Button(ventana,image=imagen_golpe, command=golpear, height=60, width=53, font=buttonFontSec) |
271 | 10 | Andrea Navia | imagen_angulo=PhotoImage(file="C:/Users/nombre/Desktop/angulo.png") |
272 | 10 | Andrea Navia | boton_angulo= tk.Button(ventana, image=imagen_angulo, command=angulo, height=45 ,width=75,fg="white" ,font=buttonFontSec,bg="#567E8B") |
273 | 10 | Andrea Navia | |
274 | 10 | Andrea Navia | imagen_apagar=PhotoImage(file="C:/Users/nombre/Desktop/apagar.png") |
275 | 10 | Andrea Navia | boton_apagar = tk.Button(ventana, image=imagen_apagar, command=desconectar, height=60, width=53, font=buttonFontSec,bg=a) |
276 | 10 | Andrea Navia | |
277 | 10 | Andrea Navia | boton_conectar = tk.Button(ventana, text="Conectar", command=ventana_ip, height=1, width=8,fg="white" ,font=buttonFontSec,bg="#567E8B") |
278 | 10 | Andrea Navia | boton_conectar.grid(column=4, row=6, padx=10, pady=8, rowspan=1) |
279 | 10 | Andrea Navia | |
280 | 10 | Andrea Navia | # Agrega la etiqueta para "Seleccionar Ángulo" y la caja desplegable |
281 | 10 | Andrea Navia | etiqueta_angulo = tk.Label(ventana, text="Seleccionar Ángulo", fg="white" , font=buttonFontSec,bg="#567E8B") |
282 | 10 | Andrea Navia | etiquetas_angulo = ttk.Combobox(ventana, values=["30°", "45°", "60°","90°"], font=buttonFontSec) |
283 | 10 | Andrea Navia | |
284 | 10 | Andrea Navia | etiqueta_angulo.grid(column=3, row=0, padx=8, pady=8, columnspan=2, rowspan=1) |
285 | 10 | Andrea Navia | etiquetas_angulo.grid(column=3, row=1, padx=8, pady=8, columnspan=2, rowspan=1) |
286 | 10 | Andrea Navia | boton_angulo.grid(column=3, row=2, padx=8, pady=8, columnspan=2, rowspan=1) |
287 | 10 | Andrea Navia | |
288 | 10 | Andrea Navia | |
289 | 10 | Andrea Navia | imagen = PhotoImage(file="C:/Users/fabian/Desktop/KIKIS.png") |
290 | 10 | Andrea Navia | logo = tk.Button(ventana, image=imagen,command=mover, height=64, width=64, font=buttonFontSec,bg=a) |
291 | 10 | Andrea Navia | boton_rotar_adelante.grid(column=0, row=0, padx=8, pady=8) |
292 | 10 | Andrea Navia | boton_avanzar.grid(column=1, row=0, padx=8, pady=8) |
293 | 10 | Andrea Navia | boton_rotar_atras.grid(column=2, row=0, padx=8, pady=8) |
294 | 10 | Andrea Navia | |
295 | 10 | Andrea Navia | boton_izquierda.grid(column=0, row=1, padx=8, pady=8) |
296 | 10 | Andrea Navia | logo.grid(column=1, row=1, padx=8, pady=8) |
297 | 10 | Andrea Navia | boton_derecha.grid(column=2, row=1, padx=8, pady=8) |
298 | 10 | Andrea Navia | |
299 | 10 | Andrea Navia | boton_golpear.grid(column=0, row=2, padx=8, pady=8) |
300 | 10 | Andrea Navia | boton_retroceder.grid(column=1, row=2, padx=8, pady=8) |
301 | 10 | Andrea Navia | |
302 | 10 | Andrea Navia | # Agrega el botón "Mover" debajo del botón de la flecha hacia la derecha |
303 | 10 | Andrea Navia | boton_apagar.grid(column=2, row=2, padx=8, pady=8) |
304 | 10 | Andrea Navia | estado_label = tk.Label(ventana, text="Estado: Desconectado", fg="white" , font=buttonFontSec,bg="#567E8B") |
305 | 10 | Andrea Navia | estado_label.grid(row=6, column=0, columnspan=4, pady=8) |
306 | 10 | Andrea Navia | ventana.mainloop() |
307 | 10 | Andrea Navia | |
308 | 10 | Andrea Navia | </code></pre> |
309 | 10 | Andrea Navia | }} |