Programación e Implementación del sistema » History » Version 2
catalina ramirez, 11/11/2024 08:38 AM
1 | 1 | catalina ramirez | h1. Programación e Implementación del sistema |
---|---|---|---|
2 | 1 | catalina ramirez | |
3 | 1 | catalina ramirez | ___ |
4 | 1 | catalina ramirez | |
5 | 1 | catalina ramirez | |
6 | 1 | catalina ramirez | h2. +Funciones+ |
7 | 1 | catalina ramirez | |
8 | 1 | catalina ramirez | |
9 | 2 | catalina ramirez | <pre> |
10 | 2 | catalina ramirez | from time import sleep |
11 | 2 | catalina ramirez | |
12 | 2 | catalina ramirez | from ev3dev2.motor import LargeMotor, OUTPUT_A, OUTPUT_B, SpeedPercent, MoveTank |
13 | 2 | catalina ramirez | from ev3dev2.sensor import INPUT_1 |
14 | 2 | catalina ramirez | from ev3dev2.sensor.lego import TouchSensor |
15 | 2 | catalina ramirez | from ev3dev2.led import Leds |
16 | 2 | catalina ramirez | from time import sleep |
17 | 2 | catalina ramirez | |
18 | 2 | catalina ramirez | from ev3dev2.motor import LargeMotor, OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D, SpeedPercent, MoveTank, MediumMotor |
19 | 2 | catalina ramirez | from ev3dev2.sensor import INPUT_1 |
20 | 2 | catalina ramirez | from ev3dev2.sensor.lego import TouchSensor |
21 | 2 | catalina ramirez | from ev3dev2.led import Leds |
22 | 2 | catalina ramirez | from ev3dev2.sound import Sound |
23 | 2 | catalina ramirez | |
24 | 2 | catalina ramirez | TANK_DRIVE = MoveTank(OUTPUT_A, OUTPUT_B) |
25 | 2 | catalina ramirez | |
26 | 2 | catalina ramirez | MOTOR_IZQUIERDO = LargeMotor(OUTPUT_A) |
27 | 2 | catalina ramirez | MOTOR_DERECHO = LargeMotor(OUTPUT_B) |
28 | 2 | catalina ramirez | MOTOR_APRETAR_GARRA = MediumMotor(OUTPUT_C) |
29 | 2 | catalina ramirez | MOTOR_SUBIR_BAJAR = LargeMotor(OUTPUT_D) |
30 | 2 | catalina ramirez | |
31 | 2 | catalina ramirez | # GARRA = LargeMotor(OUTPUT_C) |
32 | 2 | catalina ramirez | RAPIDO = SpeedPercent(100) |
33 | 2 | catalina ramirez | MEDIO = SpeedPercent(50) |
34 | 2 | catalina ramirez | LENTO = SpeedPercent(10) |
35 | 2 | catalina ramirez | |
36 | 2 | catalina ramirez | def avanzar(): |
37 | 2 | catalina ramirez | TANK_DRIVE.on_for_seconds(-50, -50, 1) |
38 | 2 | catalina ramirez | |
39 | 2 | catalina ramirez | def retroceder(): |
40 | 2 | catalina ramirez | TANK_DRIVE.on_for_seconds(50, 50, 1) |
41 | 2 | catalina ramirez | |
42 | 2 | catalina ramirez | def girar_derecha(): |
43 | 2 | catalina ramirez | TANK_DRIVE.on_for_seconds(-50, 50, 1) |
44 | 2 | catalina ramirez | |
45 | 2 | catalina ramirez | def girar_izquierda(): |
46 | 2 | catalina ramirez | TANK_DRIVE.on_for_seconds(50, -50, 1) |
47 | 2 | catalina ramirez | |
48 | 2 | catalina ramirez | |
49 | 2 | catalina ramirez | def hablar(): |
50 | 2 | catalina ramirez | sound = Sound() |
51 | 2 | catalina ramirez | sound.speak("Hello") |
52 | 2 | catalina ramirez | </pre> |
53 | 2 | catalina ramirez | |
54 | 2 | catalina ramirez | |
55 | 1 | catalina ramirez | ____ |
56 | 1 | catalina ramirez | |
57 | 1 | catalina ramirez | ____ |
58 | 1 | catalina ramirez | |
59 | 1 | catalina ramirez | |
60 | 1 | catalina ramirez | h2. +Servidor+ |
61 | 1 | catalina ramirez | |
62 | 1 | catalina ramirez | |
63 | 2 | catalina ramirez | <pre> |
64 | 2 | catalina ramirez | import socket |
65 | 2 | catalina ramirez | import library as lib |
66 | 2 | catalina ramirez | |
67 | 2 | catalina ramirez | HOST = "" |
68 | 2 | catalina ramirez | PORT = 8080 |
69 | 2 | catalina ramirez | |
70 | 2 | catalina ramirez | # Creación y setup de socket |
71 | 2 | catalina ramirez | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
72 | 2 | catalina ramirez | print("Socket creado") |
73 | 2 | catalina ramirez | s.bind((HOST, PORT)) |
74 | 2 | catalina ramirez | print("El socket se creo con puerto: " + str(PORT)) |
75 | 2 | catalina ramirez | s.listen(4) |
76 | 2 | catalina ramirez | print("El socket esta escuchando...") |
77 | 2 | catalina ramirez | |
78 | 2 | catalina ramirez | # Se espera la conexión con el cliente |
79 | 2 | catalina ramirez | client, addr = s.accept() |
80 | 2 | catalina ramirez | print("Se conecto a " + str(addr)) |
81 | 2 | catalina ramirez | |
82 | 2 | catalina ramirez | # Main loop |
83 | 2 | catalina ramirez | while True: |
84 | 2 | catalina ramirez | # Se recibe un byte y se decodifica |
85 | 2 | catalina ramirez | data = client.recv(16) |
86 | 2 | catalina ramirez | key = data.decode() |
87 | 2 | catalina ramirez | |
88 | 2 | catalina ramirez | #Teclado |
89 | 2 | catalina ramirez | if key == 'w': |
90 | 2 | catalina ramirez | lib.avanzar() |
91 | 2 | catalina ramirez | elif key == 'a': |
92 | 2 | catalina ramirez | lib.girar_izquierda() |
93 | 2 | catalina ramirez | elif key == 's': |
94 | 2 | catalina ramirez | lib.retroceder() |
95 | 2 | catalina ramirez | elif key == 'd': |
96 | 2 | catalina ramirez | lib.girar_derecha() |
97 | 2 | catalina ramirez | elif key == 'space': |
98 | 2 | catalina ramirez | lib.hablar() |
99 | 2 | catalina ramirez | #Mando |
100 | 2 | catalina ramirez | elif key == "ARRIBA": |
101 | 2 | catalina ramirez | lib.avanzar() |
102 | 2 | catalina ramirez | elif key == "IZQUIERDA": |
103 | 2 | catalina ramirez | lib.girar_izquierda() |
104 | 2 | catalina ramirez | elif key == "DERECHA": |
105 | 2 | catalina ramirez | lib.girar_derecha() |
106 | 2 | catalina ramirez | elif key == "ABAJO": |
107 | 2 | catalina ramirez | lib.retroceder() |
108 | 2 | catalina ramirez | |
109 | 2 | catalina ramirez | elif key == "EQUIS": |
110 | 2 | catalina ramirez | print(key) |
111 | 2 | catalina ramirez | elif key == "O": |
112 | 2 | catalina ramirez | print(key) |
113 | 2 | catalina ramirez | elif key == "CUADRADO": |
114 | 2 | catalina ramirez | print(key) |
115 | 2 | catalina ramirez | elif key == "TRIANGULO": |
116 | 2 | catalina ramirez | print(key) |
117 | 2 | catalina ramirez | #Salir |
118 | 2 | catalina ramirez | elif key == 'q': |
119 | 2 | catalina ramirez | print('q') |
120 | 2 | catalina ramirez | break |
121 | 2 | catalina ramirez | |
122 | 2 | catalina ramirez | print("Cerrando client") |
123 | 2 | catalina ramirez | client.close() |
124 | 2 | catalina ramirez | print("Cerrando socket") |
125 | 2 | catalina ramirez | s.close() |
126 | 2 | catalina ramirez | </pre> |
127 | 2 | catalina ramirez | |
128 | 2 | catalina ramirez | |
129 | 2 | catalina ramirez | |
130 | 2 | catalina ramirez | |
131 | 1 | catalina ramirez | ___ |
132 | 1 | catalina ramirez | |
133 | 1 | catalina ramirez | ___ |
134 | 1 | catalina ramirez | |
135 | 1 | catalina ramirez | |
136 | 1 | catalina ramirez | |
137 | 1 | catalina ramirez | h2. +Interfaz Gráfica+ |
138 | 2 | catalina ramirez | |
139 | 2 | catalina ramirez | |
140 | 2 | catalina ramirez | <pre> |
141 | 2 | catalina ramirez | import socket |
142 | 2 | catalina ramirez | import client |
143 | 2 | catalina ramirez | from tkinter import * |
144 | 2 | catalina ramirez | from tkinter import ttk |
145 | 2 | catalina ramirez | import multiprocessing |
146 | 2 | catalina ramirez | |
147 | 2 | catalina ramirez | def mandar_input(socket_p, mensaje): |
148 | 2 | catalina ramirez | |
149 | 2 | catalina ramirez | socket_p.sendall(mensaje) |
150 | 2 | catalina ramirez | |
151 | 2 | catalina ramirez | return |
152 | 2 | catalina ramirez | |
153 | 2 | catalina ramirez | def gui_process(queue): |
154 | 2 | catalina ramirez | def check_queue(): |
155 | 2 | catalina ramirez | try: |
156 | 2 | catalina ramirez | |
157 | 2 | catalina ramirez | # Verifica si hay algún mensaje en la cola y se ejecuta la funcion para mandarlo al servidor |
158 | 2 | catalina ramirez | message = queue.get_nowait() |
159 | 2 | catalina ramirez | mandar_input(socket, message.encode()) |
160 | 2 | catalina ramirez | |
161 | 2 | catalina ramirez | except multiprocessing.queues.Empty: |
162 | 2 | catalina ramirez | pass |
163 | 2 | catalina ramirez | |
164 | 2 | catalina ramirez | ventana_principal.after(100, check_queue) |
165 | 2 | catalina ramirez | |
166 | 2 | catalina ramirez | #Se crea el socket para conectarlo con el servidor y se guarda. |
167 | 2 | catalina ramirez | socket = client.retornar_socket() |
168 | 2 | catalina ramirez | print("Socket creado") |
169 | 2 | catalina ramirez | |
170 | 2 | catalina ramirez | #Ventana Principal |
171 | 2 | catalina ramirez | ventana_principal = Tk() |
172 | 2 | catalina ramirez | ventana_principal.title("EV3.") |
173 | 2 | catalina ramirez | ventana_principal.config(bg="#FF5757") |
174 | 2 | catalina ramirez | ventana_principal.geometry("800x700") |
175 | 2 | catalina ramirez | ventana_principal.resizable(0, 0) |
176 | 2 | catalina ramirez | |
177 | 2 | catalina ramirez | #Label |
178 | 2 | catalina ramirez | label_movimiento_ev3 = Label(ventana_principal, text="Movimiento EV3.") |
179 | 2 | catalina ramirez | label_movimiento_ev3.place(x=10, y= 10) |
180 | 2 | catalina ramirez | |
181 | 2 | catalina ramirez | #Se manda las teclas del teclado si es que son presionadas |
182 | 2 | catalina ramirez | ventana_principal.bind("<Key>", lambda event: mandar_input(socket, event.keysym.encode())) |
183 | 2 | catalina ramirez | |
184 | 2 | catalina ramirez | # Verificar la cola cada 100 ms |
185 | 2 | catalina ramirez | ventana_principal.after(100, check_queue) |
186 | 2 | catalina ramirez | |
187 | 2 | catalina ramirez | # Ejecutar la ventana |
188 | 2 | catalina ramirez | ventana_principal.mainloop() |
189 | 2 | catalina ramirez | |
190 | 2 | catalina ramirez | if __name__ == "__main__": |
191 | 2 | catalina ramirez | |
192 | 2 | catalina ramirez | # Cola para la comunicación entre procesos |
193 | 2 | catalina ramirez | queue = multiprocessing.Queue() |
194 | 2 | catalina ramirez | |
195 | 2 | catalina ramirez | # Crear el proceso de tkinter |
196 | 2 | catalina ramirez | gui_process(queue) |
197 | 2 | catalina ramirez | </pre> |