Código e implementación » History » Version 10
joaquin guarachi, 11/10/2022 10:45 AM
1 | 1 | joaquin guarachi | h1=. Código e implementación |
---|---|---|---|
2 | 1 | joaquin guarachi | |
3 | 2 | joaquin guarachi | --- |
4 | 2 | joaquin guarachi | |
5 | 2 | joaquin guarachi | h2. Interfaz de usuario (GUI) |
6 | 2 | joaquin guarachi | |
7 | 2 | joaquin guarachi | Código de la interfaz de usuario del proyecto. Permite controlar al robot mediante una conexión wi-fi usando teclas o botones. Posee diversas funciones que incluyen movimiento, utilidad y manejo de conexión con el robot. |
8 | 2 | joaquin guarachi | |
9 | 6 | joaquin guarachi | {{collapse(Mostrar código) |
10 | 1 | joaquin guarachi | <pre><code class="python"> |
11 | 2 | joaquin guarachi | #//Proyecto Robot Robo-Cleaner |
12 | 1 | joaquin guarachi | |
13 | 2 | joaquin guarachi | # //Imports |
14 | 2 | joaquin guarachi | from tkinter import * |
15 | 2 | joaquin guarachi | from tkinter import messagebox |
16 | 2 | joaquin guarachi | from tkinter import ttk |
17 | 2 | joaquin guarachi | import socket |
18 | 2 | joaquin guarachi | |
19 | 2 | joaquin guarachi | from setuptools import Command |
20 | 2 | joaquin guarachi | |
21 | 2 | joaquin guarachi | root = Tk() |
22 | 2 | joaquin guarachi | root.title("Robo-Cleaner") |
23 | 2 | joaquin guarachi | root.resizable(False, False) |
24 | 2 | joaquin guarachi | root.geometry("900x400") |
25 | 2 | joaquin guarachi | |
26 | 2 | joaquin guarachi | # //Conexión Socket |
27 | 2 | joaquin guarachi | client = socket.socket() |
28 | 2 | joaquin guarachi | port = 12345 |
29 | 2 | joaquin guarachi | |
30 | 2 | joaquin guarachi | def get_IP(): |
31 | 2 | joaquin guarachi | vtn_ip = Tk() |
32 | 2 | joaquin guarachi | vtn_ip.geometry("275x100") |
33 | 2 | joaquin guarachi | vtn_ip.resizable(False,False) |
34 | 2 | joaquin guarachi | vtn_ip.title("Ajustes de la Conexión IP") |
35 | 2 | joaquin guarachi | |
36 | 2 | joaquin guarachi | inform = Label(vtn_ip,text="Dirección IP: ").place(x=10, y=10) |
37 | 2 | joaquin guarachi | addr = StringVar(vtn_ip) |
38 | 2 | joaquin guarachi | ip = ttk.Entry(vtn_ip, textvariable = addr).place(x=100, y=10) |
39 | 2 | joaquin guarachi | |
40 | 2 | joaquin guarachi | boton = Button(vtn_ip, text = "Establecer", command = lambda:[conection(addr.get()+""), vtn_ip.destroy()]).place(x=98, y=50) |
41 | 2 | joaquin guarachi | print(addr.get()) |
42 | 2 | joaquin guarachi | |
43 | 2 | joaquin guarachi | def conection(addr): |
44 | 2 | joaquin guarachi | port = 12345 |
45 | 2 | joaquin guarachi | try: |
46 | 2 | joaquin guarachi | client.connect((addr, port)) |
47 | 2 | joaquin guarachi | messagebox.showinfo("Mensaje Recibido", "El host se conectó al robot: \nDirección IP: {0}\nPuerto: {1}".format(addr,port)) |
48 | 2 | joaquin guarachi | except socket.error: |
49 | 2 | joaquin guarachi | messagebox.showwarning("Error de conexión", "No se logró realizar la conexión. \nRevise la dirección IP e intentelo nuevamente. \nDirección IP: " + addr) |
50 | 2 | joaquin guarachi | get_IP() |
51 | 2 | joaquin guarachi | client.close() |
52 | 2 | joaquin guarachi | |
53 | 2 | joaquin guarachi | |
54 | 2 | joaquin guarachi | # //Funciones |
55 | 2 | joaquin guarachi | def Upward(event): |
56 | 2 | joaquin guarachi | client.send(bytes([ord('i')])) |
57 | 2 | joaquin guarachi | |
58 | 2 | joaquin guarachi | def Backwards(event): |
59 | 2 | joaquin guarachi | client.send(bytes([ord('k')])) |
60 | 2 | joaquin guarachi | |
61 | 2 | joaquin guarachi | def LeftTurn(event): |
62 | 2 | joaquin guarachi | client.send(bytes([ord('j')])) |
63 | 2 | joaquin guarachi | |
64 | 2 | joaquin guarachi | def RightTurn(event): |
65 | 2 | joaquin guarachi | client.send(bytes([ord('l')])) |
66 | 2 | joaquin guarachi | |
67 | 2 | joaquin guarachi | def Rotate(event): |
68 | 2 | joaquin guarachi | client.send(bytes([ord('p')])) |
69 | 2 | joaquin guarachi | |
70 | 2 | joaquin guarachi | def Automatic(): |
71 | 2 | joaquin guarachi | client.send(bytes([ord('a')])) |
72 | 2 | joaquin guarachi | |
73 | 2 | joaquin guarachi | def disconection(): |
74 | 2 | joaquin guarachi | client.send(bytes([ord('q')])) |
75 | 2 | joaquin guarachi | |
76 | 2 | joaquin guarachi | def Halt(event): |
77 | 2 | joaquin guarachi | client.send(bytes([ord('w')])) |
78 | 2 | joaquin guarachi | |
79 | 2 | joaquin guarachi | # //Diseño de botones |
80 | 2 | joaquin guarachi | check_photo = PhotoImage(file="checked.png") |
81 | 2 | joaquin guarachi | exit_photo = PhotoImage(file="exit.png") |
82 | 2 | joaquin guarachi | automatic_photo = PhotoImage(file="letter-a.png") |
83 | 2 | joaquin guarachi | root_canvas = Canvas(root, width=900, height=400) |
84 | 2 | joaquin guarachi | root_canvas.pack(fil="both", expand=True) |
85 | 2 | joaquin guarachi | root_canvas.create_image(260, 100, image=check_photo, anchor="nw") |
86 | 2 | joaquin guarachi | root_canvas.create_image(260, 140, image=exit_photo, anchor="nw") |
87 | 2 | joaquin guarachi | root_canvas.create_image(260, 180, image=automatic_photo, anchor="nw") |
88 | 2 | joaquin guarachi | |
89 | 2 | joaquin guarachi | # //Botones y texto de la interfaz tkinter |
90 | 2 | joaquin guarachi | title_label = Label(root, text="[ Robo-Cleaner Interface ]", font=("Helvetica", 14), width=20, foreground="#757575").place(x=330, y=30) |
91 | 2 | joaquin guarachi | version_label = Label(root, text="Version 1.1", font=("Helvetica", 12), width=12, foreground="#757575").place(x=120, y=340) |
92 | 2 | joaquin guarachi | botton_connect = Button(root, text="Conectar", font=("Helvetica", 12), width=12, bg="RoyalBlue1", foreground="white", command=get_IP).place(x=120, y=100) |
93 | 2 | joaquin guarachi | botton_disconect = Button(root, text="Desconectar", font=("Helvetica", 12), width=12, bg="RoyalBlue1", foreground="white", command=disconection).place(x=120, y=140) |
94 | 2 | joaquin guarachi | botton_automatic = Button(root, text="Automatico", font=("Helvetica", 12), width=12, bg="RoyalBlue1", foreground="white",command=Automatic).place(x=120, y=180) |
95 | 2 | joaquin guarachi | botton_forward = Button(root, text="Adelante", font=("Helvetica",12), width=12, bg="RoyalBlue1", foreground="white") |
96 | 2 | joaquin guarachi | botton_forward.place(x=550, y=100) |
97 | 2 | joaquin guarachi | botton_forward.bind('<ButtonPress>',Upward) |
98 | 2 | joaquin guarachi | botton_forward.bind('<ButtonRelease>',Halt) |
99 | 2 | joaquin guarachi | botton_rotate = Button(root, text="Rotar Helice", font=("Helvetica", 12), width=12, bg="RoyalBlue1", foreground="white") |
100 | 2 | joaquin guarachi | botton_rotate.place(x=550, y=140) |
101 | 2 | joaquin guarachi | botton_rotate.bind('<ButtonPress>',Rotate) |
102 | 2 | joaquin guarachi | botton_rotate.bind('<ButtonRelease>',Halt) |
103 | 2 | joaquin guarachi | botton_rotate_left = Button(root, text="Rotar Izq", font=("Helvetica", 12), width=12, bg="RoyalBlue1", foreground="white") |
104 | 2 | joaquin guarachi | botton_rotate_left.place(x=420, y=140) |
105 | 2 | joaquin guarachi | botton_rotate_left.bind('<ButtonPress>',LeftTurn) |
106 | 2 | joaquin guarachi | botton_rotate_left.bind('<ButtonRelease>',Halt) |
107 | 2 | joaquin guarachi | botton_rotate_right = Button(root, text="Rotar Dere", font=("Helvetica", 12), width=12, bg="RoyalBlue1", foreground="white") |
108 | 2 | joaquin guarachi | botton_rotate_right.place(x=680, y=140) |
109 | 2 | joaquin guarachi | botton_rotate_right.bind('<ButtonPress>',RightTurn) |
110 | 2 | joaquin guarachi | botton_rotate_right.bind('<ButtonRelease>',Halt) |
111 | 2 | joaquin guarachi | botton_backward = Button(root, text="Atras", font=("Helvetica", 12), width= 12, bg="RoyalBlue1", foreground="white") |
112 | 2 | joaquin guarachi | botton_backward.place(x=550, y=180) |
113 | 2 | joaquin guarachi | botton_backward.bind('<ButtonPress>',Backwards) |
114 | 2 | joaquin guarachi | botton_backward.bind('<ButtonRelease>',Halt) |
115 | 2 | joaquin guarachi | botton_close = Button(root, text=" Salir ", command=root.destroy, font=("Helvetica", 14), width=12).place(x=700, y=340) |
116 | 2 | joaquin guarachi | |
117 | 2 | joaquin guarachi | #Protocolos y mainloop |
118 | 2 | joaquin guarachi | root.protocol("WM_DELETE_WINDOWS") |
119 | 2 | joaquin guarachi | root.mainloop() |
120 | 1 | joaquin guarachi | </code></pre> |
121 | 2 | joaquin guarachi | |
122 | 1 | joaquin guarachi | {{thumbnail(interfaz1.1.png, size=600, title=Interfaz de usuario v.1.1)}} |
123 | 7 | joaquin guarachi | |
124 | 7 | joaquin guarachi | }} |
125 | 4 | joaquin guarachi | |
126 | 2 | joaquin guarachi | --- |
127 | 2 | joaquin guarachi | |
128 | 2 | joaquin guarachi | h2. Parte operacional (código órdenes del robot) |
129 | 2 | joaquin guarachi | |
130 | 2 | joaquin guarachi | Parte del código perteneciente a los movimientos y acciones que puede realizar el robot. |
131 | 8 | joaquin guarachi | El robot mediante sus funciones puede: |
132 | 8 | joaquin guarachi | * Moverse hacia adelante y hacia atrás |
133 | 8 | joaquin guarachi | * Girar a la izquierda y derecha |
134 | 8 | joaquin guarachi | * Accionar el arco giratorio limpiador |
135 | 8 | joaquin guarachi | * Subir y bajar su limpiador de arrastre |
136 | 8 | joaquin guarachi | * Conectarse y desconectarse por wi-fi a un equipo |
137 | 2 | joaquin guarachi | |
138 | 6 | joaquin guarachi | {{collapse(Mostrar código) |
139 | 2 | joaquin guarachi | <pre><code class="python"> |
140 | 2 | joaquin guarachi | from ev3dev2.motor import SpeedPercent, MoveTank, LargeMotor, MediumMotor, OUTPUT_A, OUTPUT_B, OUTPUT_D |
141 | 2 | joaquin guarachi | from ev3dev2.sensor import INPUT_1, INPUT_4 |
142 | 2 | joaquin guarachi | from ev3dev2.sensor.lego import UltrasonicSensor, ColorSensor |
143 | 2 | joaquin guarachi | |
144 | 2 | joaquin guarachi | tank = MoveTank(OUTPUT_A, OUTPUT_B) |
145 | 2 | joaquin guarachi | |
146 | 2 | joaquin guarachi | mot_rot= MediumMotor(OUTPUT_D) |
147 | 2 | joaquin guarachi | |
148 | 2 | joaquin guarachi | prox = UltrasonicSensor(INPUT_1) |
149 | 2 | joaquin guarachi | |
150 | 2 | joaquin guarachi | |
151 | 2 | joaquin guarachi | col = ColorSensor(INPUT_4) |
152 | 2 | joaquin guarachi | |
153 | 2 | joaquin guarachi | def Automatic(): |
154 | 2 | joaquin guarachi | while (col.color != 6): |
155 | 2 | joaquin guarachi | while (prox.distance_centimeters_continuous > 5): |
156 | 2 | joaquin guarachi | tank.on(left_speed= 90, right_speed= 90) |
157 | 2 | joaquin guarachi | if (col.color == 6): |
158 | 2 | joaquin guarachi | tank.off(brake=True) |
159 | 2 | joaquin guarachi | break |
160 | 2 | joaquin guarachi | else: |
161 | 2 | joaquin guarachi | tank.off(brake=True) |
162 | 2 | joaquin guarachi | mot_rot.on_for_seconds(speed = 75, seconds = 1, brake=True) |
163 | 2 | joaquin guarachi | tank.off(brake=True) |
164 | 2 | joaquin guarachi | |
165 | 2 | joaquin guarachi | def Upward(): |
166 | 2 | joaquin guarachi | tank.on(left_speed= 90, right_speed= 90) |
167 | 2 | joaquin guarachi | |
168 | 2 | joaquin guarachi | def Backwards(): |
169 | 2 | joaquin guarachi | tank.on(left_speed= -90, right_speed= -90) |
170 | 2 | joaquin guarachi | |
171 | 2 | joaquin guarachi | def LeftTurn(): |
172 | 2 | joaquin guarachi | tank.on(left_speed= 90, right_speed= -90) |
173 | 2 | joaquin guarachi | |
174 | 2 | joaquin guarachi | def RightTurn(): |
175 | 2 | joaquin guarachi | tank.on(left_speed= -90, right_speed= 90) |
176 | 2 | joaquin guarachi | |
177 | 2 | joaquin guarachi | def Rotate(): |
178 | 2 | joaquin guarachi | mot_rot.on(speed = 75, brake=True) |
179 | 2 | joaquin guarachi | |
180 | 2 | joaquin guarachi | def Halt(): |
181 | 2 | joaquin guarachi | tank.off(brake=True) |
182 | 2 | joaquin guarachi | mot_rot.off(brake=True) |
183 | 3 | joaquin guarachi | </code></pre> |
184 | 6 | joaquin guarachi | }} |
185 | 3 | joaquin guarachi | |
186 | 2 | joaquin guarachi | --- |
187 | 2 | joaquin guarachi | |
188 | 2 | joaquin guarachi | h2. Receptor de operaciones (ladrillo EV3) |
189 | 2 | joaquin guarachi | |
190 | 10 | joaquin guarachi | Parte del código perteneciente a la lectura o interpretación de órdenes enviadas desde el PC en el ladrillo EV3. |
191 | 9 | joaquin guarachi | El Robot-Cleaner es capaz de recibir e interpretar órdenes gracias al uso de la librería socket, que permite el envío de información mediante una conexión establecida. |
192 | 2 | joaquin guarachi | |
193 | 6 | joaquin guarachi | {{collapse(Mostrar código) |
194 | 2 | joaquin guarachi | <pre><code class="python"> |
195 | 2 | joaquin guarachi | #!/usr/bin/env python3 |
196 | 2 | joaquin guarachi | import socket |
197 | 2 | joaquin guarachi | from operations import * |
198 | 2 | joaquin guarachi | |
199 | 2 | joaquin guarachi | conector = socket.socket() |
200 | 2 | joaquin guarachi | |
201 | 2 | joaquin guarachi | puerto = 12345 |
202 | 2 | joaquin guarachi | |
203 | 2 | joaquin guarachi | conector.bind(('', puerto)) |
204 | 2 | joaquin guarachi | |
205 | 2 | joaquin guarachi | conector.listen(5) |
206 | 2 | joaquin guarachi | |
207 | 2 | joaquin guarachi | conexion, addr = conector.accept() |
208 | 2 | joaquin guarachi | |
209 | 2 | joaquin guarachi | while True: |
210 | 2 | joaquin guarachi | dato_puro = conexion.recv(1) |
211 | 2 | joaquin guarachi | key = dato_puro.decode("utf-8") |
212 | 2 | joaquin guarachi | |
213 | 2 | joaquin guarachi | if (key == 'i'): |
214 | 2 | joaquin guarachi | Upward() |
215 | 2 | joaquin guarachi | |
216 | 2 | joaquin guarachi | if (key == 'k'): |
217 | 2 | joaquin guarachi | Backwards() |
218 | 2 | joaquin guarachi | |
219 | 2 | joaquin guarachi | if (key == 'j'): |
220 | 2 | joaquin guarachi | LeftTurn() |
221 | 2 | joaquin guarachi | |
222 | 2 | joaquin guarachi | if (key == 'l'): |
223 | 2 | joaquin guarachi | RightTurn() |
224 | 2 | joaquin guarachi | |
225 | 2 | joaquin guarachi | if (key == 'p'): |
226 | 2 | joaquin guarachi | Rotate() |
227 | 2 | joaquin guarachi | |
228 | 2 | joaquin guarachi | if (key == 'a'): |
229 | 2 | joaquin guarachi | Automatic() |
230 | 2 | joaquin guarachi | |
231 | 2 | joaquin guarachi | if (key == 'q'): |
232 | 2 | joaquin guarachi | break |
233 | 2 | joaquin guarachi | |
234 | 2 | joaquin guarachi | if (key == 'w'): |
235 | 2 | joaquin guarachi | Halt() |
236 | 2 | joaquin guarachi | </code></pre> |
237 | 6 | joaquin guarachi | }} |
238 | 2 | joaquin guarachi | |
239 | 2 | joaquin guarachi | --- |
240 | 5 | joaquin guarachi | |
241 | 5 | joaquin guarachi | h2. +Contenido:+ |
242 | 5 | joaquin guarachi | * [[Wiki|Página principal]] |
243 | 5 | joaquin guarachi | * [[Información detallada]] |
244 | 5 | joaquin guarachi | * [[Avances y planificación]] |
245 | 5 | joaquin guarachi | * [[Código e implementación]] |
246 | 5 | joaquin guarachi | * [[Informe de costos]] |