Código Utilizado » History » Version 9
Cristian Huanca, 01/05/2024 12:34 AM
1 | 1 | Melisa Laura | h2. *Índice*: |
---|---|---|---|
2 | 1 | Melisa Laura | * [[Introducción]] |
3 | 1 | Melisa Laura | * [[Organización y Planificación]] |
4 | 1 | Melisa Laura | * [[Progreso del Robot]] |
5 | 1 | Melisa Laura | * [[Análisis - Diseño]] |
6 | 1 | Melisa Laura | * [[Código Utilizado]] |
7 | 1 | Melisa Laura | * [[Resultados]] |
8 | 1 | Melisa Laura | |
9 | 1 | Melisa Laura | h1. Código Utilizado |
10 | 2 | Melisa Laura | |
11 | 7 | Melisa Laura | *+Servidor+* |
12 | 2 | Melisa Laura | |
13 | 3 | Melisa Laura | <pre> |
14 | 3 | Melisa Laura | import socket |
15 | 3 | Melisa Laura | from robot import robot |
16 | 3 | Melisa Laura | |
17 | 3 | Melisa Laura | server = socket.socket() |
18 | 3 | Melisa Laura | |
19 | 3 | Melisa Laura | PORT = 8800 |
20 | 3 | Melisa Laura | |
21 | 3 | Melisa Laura | server.bind(('', PORT)) |
22 | 3 | Melisa Laura | server.listen(1) |
23 | 3 | Melisa Laura | |
24 | 3 | Melisa Laura | |
25 | 3 | Melisa Laura | connection, adress = server.accept() |
26 | 3 | Melisa Laura | |
27 | 3 | Melisa Laura | |
28 | 3 | Melisa Laura | robot = Robot()[[]] |
29 | 3 | Melisa Laura | |
30 | 3 | Melisa Laura | actions = { |
31 | 3 | Melisa Laura | "w" : robot.move_front, |
32 | 3 | Melisa Laura | "a" : robot.move_left, |
33 | 3 | Melisa Laura | "d" : robot.move_right, |
34 | 3 | Melisa Laura | "s" : robot.move_back, |
35 | 3 | Melisa Laura | "e" : robot.move_punch, |
36 | 3 | Melisa Laura | } |
37 | 3 | Melisa Laura | |
38 | 3 | Melisa Laura | while True: |
39 | 3 | Melisa Laura | data = connection.recv(1) |
40 | 3 | Melisa Laura | keyword = data.decode("utf-8") |
41 | 3 | Melisa Laura | |
42 | 3 | Melisa Laura | if keyword in actions: |
43 | 3 | Melisa Laura | actions[keyword]() |
44 | 3 | Melisa Laura | |
45 | 3 | Melisa Laura | elif keyword == "q": |
46 | 3 | Melisa Laura | break |
47 | 3 | Melisa Laura | |
48 | 3 | Melisa Laura | |
49 | 3 | Melisa Laura | </pre> |
50 | 3 | Melisa Laura | |
51 | 4 | Melisa Laura | *+Funciones del Robot+* |
52 | 2 | Melisa Laura | |
53 | 3 | Melisa Laura | <pre> |
54 | 3 | Melisa Laura | from ev3dev2.motor import (LargeMotor, MoveTank, MediumMotor, OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D) |
55 | 3 | Melisa Laura | from time import sleep |
56 | 3 | Melisa Laura | |
57 | 3 | Melisa Laura | |
58 | 3 | Melisa Laura | class Robot: |
59 | 3 | Melisa Laura | |
60 | 3 | Melisa Laura | eje = 0 |
61 | 3 | Melisa Laura | base = -0.58 |
62 | 3 | Melisa Laura | MAX_VALUE_EJE = 140 |
63 | 3 | Melisa Laura | MIN_VALUE_EJE = -140 |
64 | 3 | Melisa Laura | MAX_VALUE_BASE = 0 |
65 | 3 | Melisa Laura | MIN_VALUE_BASE = -0.58 |
66 | 3 | Melisa Laura | SPEED = 100 |
67 | 3 | Melisa Laura | contaux1 = 0 |
68 | 3 | Melisa Laura | contaux2 = 0 |
69 | 3 | Melisa Laura | cont = 1 |
70 | 3 | Melisa Laura | |
71 | 3 | Melisa Laura | |
72 | 3 | Melisa Laura | def __init__(self): |
73 | 3 | Melisa Laura | self.out_a = LargeMotor(OUTPUT_A) # movimiento pelota |
74 | 3 | Melisa Laura | self.out_b = LargeMotor(OUTPUT_B) # ruedas de atras, movimiento |
75 | 3 | Melisa Laura | self.out_c = MediumMotor(OUTPUT_C) # ruedas de rotacion, giro izquierda/derecha |
76 | 3 | Melisa Laura | self.out_d = MediumMotor(OUTPUT_D) # lanzamiento de pelota |
77 | 3 | Melisa Laura | |
78 | 3 | Melisa Laura | # Función para desactivar completamente el robot |
79 | 3 | Melisa Laura | def off_robot(self): |
80 | 3 | Melisa Laura | self.out_a.stop() |
81 | 3 | Melisa Laura | self.out_b.stop() |
82 | 3 | Melisa Laura | self.out_c.stop() |
83 | 3 | Melisa Laura | self.out_d.stop() |
84 | 3 | Melisa Laura | |
85 | 3 | Melisa Laura | # Función para mover el robot hacia adelante |
86 | 3 | Melisa Laura | def move_front(self): |
87 | 3 | Melisa Laura | self.out_b.on_for_rotations(speed = 100, rotations= -2, brake = True, block = True) |
88 | 3 | Melisa Laura | self.out_b.stop() |
89 | 3 | Melisa Laura | |
90 | 3 | Melisa Laura | # Función para mover el robot hacia atras |
91 | 3 | Melisa Laura | def move_back(self): |
92 | 3 | Melisa Laura | self.out_b.on_for_rotations(speed = 100, rotations= 2, brake = True, block = True) |
93 | 3 | Melisa Laura | self.out_b.stop() |
94 | 3 | Melisa Laura | |
95 | 3 | Melisa Laura | # Función para mover las ruedas del robot hacia la derecha |
96 | 3 | Melisa Laura | def move_right(self): |
97 | 3 | Melisa Laura | if(self.MIN_VALUE_EJE < self.eje): # Se comprueba el eje de las ruedas para no provocar una falla en el robot / para que no se queden pegadas |
98 | 3 | Melisa Laura | self.out_c.on_for_degrees(speed = 100, degrees = -1.4, brake = True, block = True) |
99 | 3 | Melisa Laura | self.eje-=1.4 |
100 | 3 | Melisa Laura | self.contaux1-=1 |
101 | 3 | Melisa Laura | print(self.eje) |
102 | 3 | Melisa Laura | self.out_c.stop() |
103 | 3 | Melisa Laura | |
104 | 3 | Melisa Laura | # Función para mover las ruedas del robot hacia la izquierda |
105 | 3 | Melisa Laura | def move_left(self): |
106 | 3 | Melisa Laura | if(self.MAX_VALUE_EJE > self.eje): # Se comprueba el eje de las ruedas para no provocar una falla en el robot / para que no se queden pegadas |
107 | 3 | Melisa Laura | self.out_c.on_for_degrees(speed = 100, degrees = 1.4, brake = True, block = True) |
108 | 3 | Melisa Laura | self.eje+=1.4 |
109 | 3 | Melisa Laura | self.contaux2+=1 |
110 | 3 | Melisa Laura | print(self.eje) |
111 | 3 | Melisa Laura | self.out_c.stop() |
112 | 3 | Melisa Laura | |
113 | 3 | Melisa Laura | # Función para ingresar los grados para hacer el golpe |
114 | 3 | Melisa Laura | def move_punch(self, vel, grad): |
115 | 3 | Melisa Laura | self.vel = vel |
116 | 3 | Melisa Laura | self.grad = grad |
117 | 3 | Melisa Laura | self.out_d.on_for_degrees(speed = 100, degrees=grad) |
118 | 3 | Melisa Laura | self.out_d.stop() |
119 | 3 | Melisa Laura | |
120 | 3 | Melisa Laura | # Función para mover la base de la pelota, hacia adelante |
121 | 3 | Melisa Laura | def mov_adel(self): |
122 | 3 | Melisa Laura | if(self.MAX_VALUE_BASE>self.base): # Se comprueba la base de la pelota para que no falle el robot / en este caso para que no se golpee hacia atras |
123 | 3 | Melisa Laura | self.out_a.on_for_rotations(speed = 100, rotations=-0.0116, brake = True, block = True) |
124 | 3 | Melisa Laura | self.base += 0.0116 |
125 | 3 | Melisa Laura | self.out_a.stop() |
126 | 3 | Melisa Laura | |
127 | 3 | Melisa Laura | # Función para mover la base de la pelota, hacia atras |
128 | 3 | Melisa Laura | def mov_atras(self): |
129 | 3 | Melisa Laura | if(self.MIN_VALUE_BASE<self.base): # Se comprueba la base de la pelota para que no falle el robot / en este caso es para que no se pase hacia adelante |
130 | 3 | Melisa Laura | self.out_a.on_for_rotations(speed = 100, rotations=0.0116, brake = True, block = True) |
131 | 3 | Melisa Laura | self.base -= 0.0116 |
132 | 3 | Melisa Laura | self.out_a.stop() |
133 | 3 | Melisa Laura | def off(self): |
134 | 3 | Melisa Laura | if(self.base>-0.58): # Se comprueba si esta en la posicion original la base de la pelota |
135 | 3 | Melisa Laura | while(True): |
136 | 3 | Melisa Laura | if(50 == self.cont): |
137 | 3 | Melisa Laura | return False # Se rompe el ciclo solo si la base tiene 4, donde estara en posicion original |
138 | 3 | Melisa Laura | else: |
139 | 3 | Melisa Laura | self.out_a.on_for_rotations(speed = 100, rotations = 0.0116, brake = True, block = True) |
140 | 3 | Melisa Laura | self.base+=0.0116 |
141 | 3 | Melisa Laura | self.cont = self.cont + 1 |
142 | 3 | Melisa Laura | print(self.cont) |
143 | 3 | Melisa Laura | self.out_a.stop() |
144 | 3 | Melisa Laura | </pre> |
145 | 3 | Melisa Laura | |
146 | 8 | Melisa Laura | *+Interfaz Gráfica+* |
147 | 3 | Melisa Laura | |
148 | 9 | Cristian Huanca | <pre python> |
149 | 1 | Melisa Laura | |
150 | 9 | Cristian Huanca | import tkinter as tk |
151 | 9 | Cristian Huanca | from tkinter import ttk |
152 | 9 | Cristian Huanca | import socket |
153 | 1 | Melisa Laura | |
154 | 9 | Cristian Huanca | class App(tk.Tk): |
155 | 9 | Cristian Huanca | STATUS_CONNECTION = False |
156 | 9 | Cristian Huanca | PORT = 8800 |
157 | 9 | Cristian Huanca | |
158 | 9 | Cristian Huanca | def __init__(self): |
159 | 9 | Cristian Huanca | super().__init__() |
160 | 9 | Cristian Huanca | self.title("Princesita") |
161 | 9 | Cristian Huanca | self.config(padx=10, pady=10, bg="#FFC0CB") |
162 | 9 | Cristian Huanca | self.geometry("800x600") |
163 | 9 | Cristian Huanca | self.canvas = tk.Canvas(self, width=800, height=600) |
164 | 9 | Cristian Huanca | self.canvas.pack() |
165 | 9 | Cristian Huanca | self.resizable(width=False, height=False) |
166 | 9 | Cristian Huanca | self.imagen_fondo = tk.PhotoImage(file="fondo.png") |
167 | 9 | Cristian Huanca | self.imagen_fondo = self.imagen_fondo.subsample(5,5) |
168 | 9 | Cristian Huanca | self.canvas.create_image(0, 0, anchor=tk.NW, image=self.imagen_fondo) |
169 | 9 | Cristian Huanca | self.init_images() |
170 | 9 | Cristian Huanca | self.init_elements() |
171 | 9 | Cristian Huanca | |
172 | 9 | Cristian Huanca | |
173 | 9 | Cristian Huanca | def init_images(self): |
174 | 9 | Cristian Huanca | imagenes = ["encendido.png", "deslizador.png"] |
175 | 9 | Cristian Huanca | self.images = [tk.PhotoImage(file=imagen).subsample(12, 12) for imagen in imagenes] |
176 | 9 | Cristian Huanca | |
177 | 9 | Cristian Huanca | self.connect, self.deslizador= self.images |
178 | 9 | Cristian Huanca | |
179 | 9 | Cristian Huanca | |
180 | 9 | Cristian Huanca | imagenes1 = ["flechaFront.png", "flechaBack.png", "flechaLeft.png", "flechaRight.png","punteria.png"] |
181 | 9 | Cristian Huanca | self.images1 = [tk.PhotoImage(file=imagen).subsample(25, 25) for imagen in imagenes1] |
182 | 9 | Cristian Huanca | self.flechaFront, self.flechaBack, self.flechaLeft, self.flechaRight, self.punteria = self.images1 |
183 | 9 | Cristian Huanca | |
184 | 9 | Cristian Huanca | def init_elements(self): |
185 | 9 | Cristian Huanca | |
186 | 9 | Cristian Huanca | self.btn_Front = tk.Button( |
187 | 9 | Cristian Huanca | self, |
188 | 9 | Cristian Huanca | image=self.flechaFront, |
189 | 9 | Cristian Huanca | bd=0, |
190 | 9 | Cristian Huanca | bg="white", |
191 | 9 | Cristian Huanca | highlightbackground='white', |
192 | 9 | Cristian Huanca | width=80, |
193 | 9 | Cristian Huanca | height=80, |
194 | 9 | Cristian Huanca | command=lambda: self.move_front_car(None) |
195 | 9 | Cristian Huanca | ).place(x=545,y=100) |
196 | 9 | Cristian Huanca | |
197 | 9 | Cristian Huanca | |
198 | 9 | Cristian Huanca | self.btn_Back = tk.Button( |
199 | 9 | Cristian Huanca | self, |
200 | 9 | Cristian Huanca | image=self.flechaBack, |
201 | 9 | Cristian Huanca | bd=0, |
202 | 9 | Cristian Huanca | width=80, |
203 | 9 | Cristian Huanca | height=80, |
204 | 9 | Cristian Huanca | command=lambda:self.move_back_car(None) |
205 | 9 | Cristian Huanca | ).place(x=545,y=300) |
206 | 9 | Cristian Huanca | |
207 | 9 | Cristian Huanca | self.btn_Left = tk.Button( |
208 | 9 | Cristian Huanca | self, |
209 | 9 | Cristian Huanca | image = self.flechaLeft, |
210 | 9 | Cristian Huanca | bd=0, |
211 | 9 | Cristian Huanca | width=80, |
212 | 9 | Cristian Huanca | height=80, |
213 | 9 | Cristian Huanca | command=lambda:self.move_left_car(None) |
214 | 9 | Cristian Huanca | ).place(x=450,y=200) |
215 | 9 | Cristian Huanca | |
216 | 9 | Cristian Huanca | self.btn_Right = tk.Button( |
217 | 9 | Cristian Huanca | self, |
218 | 9 | Cristian Huanca | image= self.flechaRight, |
219 | 9 | Cristian Huanca | bd=0, |
220 | 9 | Cristian Huanca | width=80, |
221 | 9 | Cristian Huanca | height=80, |
222 | 9 | Cristian Huanca | command=lambda: self.move_right_car(None) |
223 | 9 | Cristian Huanca | ).place(x=640,y=200) |
224 | 9 | Cristian Huanca | |
225 | 9 | Cristian Huanca | self.btn_Punch = tk.Button( |
226 | 9 | Cristian Huanca | self, |
227 | 9 | Cristian Huanca | image=self.punteria, |
228 | 9 | Cristian Huanca | bd=0, |
229 | 9 | Cristian Huanca | width=80, |
230 | 9 | Cristian Huanca | height=80, |
231 | 9 | Cristian Huanca | command= lambda : self.move_punch(None) |
232 | 9 | Cristian Huanca | ).place(x=545,y=200) |
233 | 9 | Cristian Huanca | |
234 | 9 | Cristian Huanca | self.btn_Connect = tk.Button( |
235 | 9 | Cristian Huanca | self, |
236 | 9 | Cristian Huanca | image=self.connect, |
237 | 9 | Cristian Huanca | bd= 0, |
238 | 9 | Cristian Huanca | bg= "#fff" if self.STATUS_CONNECTION else "#000", |
239 | 9 | Cristian Huanca | width=40, |
240 | 9 | Cristian Huanca | height=40, |
241 | 9 | Cristian Huanca | command=self.ip_connect |
242 | 9 | Cristian Huanca | ).place(x=710,y=500) |
243 | 9 | Cristian Huanca | |
244 | 9 | Cristian Huanca | self.label_deslizador = tk.Label( |
245 | 9 | Cristian Huanca | self, |
246 | 9 | Cristian Huanca | image=self.deslizador, |
247 | 9 | Cristian Huanca | bd=0, |
248 | 9 | Cristian Huanca | bg= "#bcdc8c", |
249 | 9 | Cristian Huanca | highlightthickness=0, |
250 | 9 | Cristian Huanca | width=60, |
251 | 9 | Cristian Huanca | height=40, |
252 | 9 | Cristian Huanca | |
253 | 9 | Cristian Huanca | ) |
254 | 9 | Cristian Huanca | |
255 | 9 | Cristian Huanca | self.label_deslizador.place(x=90, y=500) |
256 | 9 | Cristian Huanca | self.distancia = tk.DoubleVar() |
257 | 9 | Cristian Huanca | style = ttk.Style() |
258 | 9 | Cristian Huanca | style.configure("TScale", troughcolor="#bcdc8c",background="#bcdc8c", sliderthickness=20) |
259 | 9 | Cristian Huanca | |
260 | 9 | Cristian Huanca | def update_image_position(event): |
261 | 9 | Cristian Huanca | value = self.Scala.get() |
262 | 9 | Cristian Huanca | x_position = self.Scala.winfo_x() + value * self.Scala.winfo_width() / self.Scala.cget("to") |
263 | 9 | Cristian Huanca | self.label_deslizador.place(x= 85 + self.Scala.get() * 3, y=490) |
264 | 9 | Cristian Huanca | |
265 | 9 | Cristian Huanca | self.Scala = ttk.Scale( |
266 | 9 | Cristian Huanca | self, |
267 | 9 | Cristian Huanca | orient = "horizontal", |
268 | 9 | Cristian Huanca | from_=1, to=50, |
269 | 9 | Cristian Huanca | style="TScale", |
270 | 9 | Cristian Huanca | length=200 |
271 | 9 | Cristian Huanca | |
272 | 9 | Cristian Huanca | ) |
273 | 9 | Cristian Huanca | self.Scala.place(x=90, y=540) |
274 | 9 | Cristian Huanca | |
275 | 9 | Cristian Huanca | self.Scala.bind("<Motion>", update_image_position) |
276 | 9 | Cristian Huanca | update_image_position(None) |
277 | 9 | Cristian Huanca | |
278 | 9 | Cristian Huanca | def ip_connect(self): |
279 | 9 | Cristian Huanca | if self.STATUS_CONNECTION == False: |
280 | 9 | Cristian Huanca | ventana_conexion = tk.Toplevel(self) |
281 | 9 | Cristian Huanca | ventana_conexion.title("Ventana de Conexión") |
282 | 9 | Cristian Huanca | ventana_conexion.geometry("200x150") |
283 | 9 | Cristian Huanca | ventana_conexion.resizable(width=False, height=False) |
284 | 9 | Cristian Huanca | etiqueta = tk.Label(ventana_conexion, text="Ingrese la dirección IP:") |
285 | 9 | Cristian Huanca | etiqueta.pack(pady=10) |
286 | 9 | Cristian Huanca | self.IP = tk.StringVar() |
287 | 9 | Cristian Huanca | entry_ip = tk.Entry(ventana_conexion, textvariable=self.IP) |
288 | 9 | Cristian Huanca | entry_ip.pack(pady=10) |
289 | 9 | Cristian Huanca | btn_conectar = tk.Button(ventana_conexion, text="Conectar", command= lambda: self.robot_connection(self.IP.get())) |
290 | 9 | Cristian Huanca | btn_conectar.pack(pady=10) |
291 | 9 | Cristian Huanca | else: |
292 | 9 | Cristian Huanca | self.robot_connection(self.IP.get()) |
293 | 9 | Cristian Huanca | |
294 | 9 | Cristian Huanca | def robot_connection(self,IP): |
295 | 9 | Cristian Huanca | |
296 | 9 | Cristian Huanca | if self.STATUS_CONNECTION: |
297 | 9 | Cristian Huanca | self.STATUS_CONNECTION = not self.STATUS_CONNECTION |
298 | 9 | Cristian Huanca | self.init_images() |
299 | 9 | Cristian Huanca | self.init_elements() |
300 | 9 | Cristian Huanca | |
301 | 9 | Cristian Huanca | else: |
302 | 9 | Cristian Huanca | try: |
303 | 9 | Cristian Huanca | self.client = socket.socket() |
304 | 9 | Cristian Huanca | self.client.connect(IP,self.PORT) |
305 | 9 | Cristian Huanca | self.STATUS_CONNECTION = True |
306 | 9 | Cristian Huanca | |
307 | 9 | Cristian Huanca | except socket.error: |
308 | 9 | Cristian Huanca | self.STATUS_CONNECTION = False |
309 | 9 | Cristian Huanca | |
310 | 9 | Cristian Huanca | |
311 | 9 | Cristian Huanca | |
312 | 9 | Cristian Huanca | def move_front_car(self, event): |
313 | 9 | Cristian Huanca | self.client.send(bytes([ord('w')])) |
314 | 9 | Cristian Huanca | |
315 | 9 | Cristian Huanca | def move_left_car(self, event): |
316 | 9 | Cristian Huanca | self.client.send(bytes([ord('a')])) |
317 | 9 | Cristian Huanca | |
318 | 9 | Cristian Huanca | def move_right_car(self, event): |
319 | 9 | Cristian Huanca | self.client.send(bytes([ord('d')])) |
320 | 9 | Cristian Huanca | |
321 | 9 | Cristian Huanca | def move_back_car(self, event): |
322 | 9 | Cristian Huanca | self.client.send(bytes([ord('s')])) |
323 | 9 | Cristian Huanca | |
324 | 9 | Cristian Huanca | def move_punch(self, event): |
325 | 9 | Cristian Huanca | self.client.send(bytes([ord('e')])) |
326 | 9 | Cristian Huanca | |
327 | 9 | Cristian Huanca | if __name__ == "__main__": |
328 | 9 | Cristian Huanca | app = App() |
329 | 9 | Cristian Huanca | app.mainloop() |
330 | 3 | Melisa Laura | </pre> |