Project

General

Profile

Código e implementación » History » Version 13

joaquin guarachi, 12/18/2022 05:07 PM

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 12 joaquin guarachi
#//Project Robot Robo-Cleaner
12 2 joaquin guarachi
from tkinter import *
13 2 joaquin guarachi
from tkinter import messagebox
14 2 joaquin guarachi
from tkinter import ttk
15 2 joaquin guarachi
import socket
16 2 joaquin guarachi
17 1 joaquin guarachi
root = Tk()
18 2 joaquin guarachi
root.title("Robo-Cleaner")
19 2 joaquin guarachi
root.resizable(False, False)
20 2 joaquin guarachi
root.geometry("900x400")
21 2 joaquin guarachi
22 12 joaquin guarachi
# //Socket Connection & Socket Connection Windows
23 2 joaquin guarachi
client = socket.socket()
24 2 joaquin guarachi
port = 12345
25 2 joaquin guarachi
26 2 joaquin guarachi
def get_IP():
27 2 joaquin guarachi
    vtn_ip = Tk()
28 2 joaquin guarachi
    vtn_ip.geometry("275x100")
29 2 joaquin guarachi
    vtn_ip.resizable(False,False)
30 2 joaquin guarachi
    vtn_ip.title("Ajustes de la Conexión IP")
31 2 joaquin guarachi
32 2 joaquin guarachi
    inform = Label(vtn_ip,text="Dirección IP: ").place(x=10, y=10)
33 2 joaquin guarachi
    addr = StringVar(vtn_ip)
34 2 joaquin guarachi
    ip = ttk.Entry(vtn_ip, textvariable = addr).place(x=100, y=10)
35 2 joaquin guarachi
    
36 2 joaquin guarachi
    boton = Button(vtn_ip, text = "Establecer", command = lambda:[conection(addr.get()+""), vtn_ip.destroy()]).place(x=98, y=50)
37 2 joaquin guarachi
    print(addr.get())
38 2 joaquin guarachi
39 2 joaquin guarachi
def conection(addr):
40 2 joaquin guarachi
    port = 12345
41 2 joaquin guarachi
    try:
42 2 joaquin guarachi
        client.connect((addr, port))
43 1 joaquin guarachi
        messagebox.showinfo("Mensaje Recibido", "El host se conectó al robot: \nDirección IP: {0}\nPuerto: {1}".format(addr,port))
44 1 joaquin guarachi
    except socket.error:
45 1 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)
46 1 joaquin guarachi
        get_IP()
47 1 joaquin guarachi
48 2 joaquin guarachi
49 12 joaquin guarachi
50 12 joaquin guarachi
# //Functions
51 1 joaquin guarachi
def Upward(event):
52 12 joaquin guarachi
    try:
53 12 joaquin guarachi
        client.send(bytes([ord('i')]))
54 12 joaquin guarachi
    except BrokenPipeError:
55 12 joaquin guarachi
        print()
56 1 joaquin guarachi
57 2 joaquin guarachi
def Backwards(event):
58 12 joaquin guarachi
    try:
59 12 joaquin guarachi
        client.send(bytes([ord('k')]))
60 12 joaquin guarachi
    except BrokenPipeError:
61 12 joaquin guarachi
        print()
62 2 joaquin guarachi
63 1 joaquin guarachi
def LeftTurn(event):
64 12 joaquin guarachi
    try:
65 12 joaquin guarachi
        client.send(bytes([ord('j')]))
66 12 joaquin guarachi
    except BrokenPipeError:
67 12 joaquin guarachi
        print()
68 1 joaquin guarachi
69 1 joaquin guarachi
def RightTurn(event):
70 12 joaquin guarachi
    try:
71 12 joaquin guarachi
        client.send(bytes([ord('l')]))
72 12 joaquin guarachi
    except BrokenPipeError:
73 12 joaquin guarachi
        print()
74 1 joaquin guarachi
75 1 joaquin guarachi
def Rotate(event):
76 12 joaquin guarachi
    try:
77 12 joaquin guarachi
        client.send(bytes([ord('p')]))
78 12 joaquin guarachi
    except BrokenPipeError:
79 12 joaquin guarachi
        print()
80 1 joaquin guarachi
81 12 joaquin guarachi
def LeverUp():
82 12 joaquin guarachi
    try:
83 12 joaquin guarachi
        client.send(bytes([ord('y')]))
84 12 joaquin guarachi
    except BrokenPipeError:
85 12 joaquin guarachi
        messagebox.showerror("Error", "El robot no está conectado")
86 12 joaquin guarachi
87 12 joaquin guarachi
def LeverDown():
88 12 joaquin guarachi
    try:
89 12 joaquin guarachi
        client.send(bytes([ord('h')]))
90 12 joaquin guarachi
    except BrokenPipeError:
91 12 joaquin guarachi
        messagebox.showerror("Error", "El robot no está conectado")
92 12 joaquin guarachi
93 1 joaquin guarachi
def Automatic():
94 12 joaquin guarachi
    try:
95 12 joaquin guarachi
        client.send(bytes([ord('a')]))
96 12 joaquin guarachi
    except BrokenPipeError:
97 12 joaquin guarachi
        messagebox.showerror("Error", "El robot no está conectado")
98 2 joaquin guarachi
99 2 joaquin guarachi
def disconection():
100 12 joaquin guarachi
    try:
101 12 joaquin guarachi
        client.send(bytes([ord('q')]))
102 12 joaquin guarachi
        messagebox.showinfo("Información", "El robot se desconectó correctamente")
103 12 joaquin guarachi
    except BrokenPipeError:
104 12 joaquin guarachi
        messagebox.showwarning("Advertencia", "No hay un robot conectado a la máquina")
105 1 joaquin guarachi
106 2 joaquin guarachi
def Halt(event):
107 12 joaquin guarachi
    try:
108 12 joaquin guarachi
        client.send(bytes([ord('w')]))
109 12 joaquin guarachi
    except BrokenPipeError:
110 12 joaquin guarachi
        messagebox.showerror("Error", "El robot no está conectado")
111 1 joaquin guarachi
112 12 joaquin guarachi
# //Set own directory for background image
113 1 joaquin guarachi
check_photo = PhotoImage(file="checked.png")
114 2 joaquin guarachi
exit_photo = PhotoImage(file="exit.png")
115 1 joaquin guarachi
automatic_photo = PhotoImage(file="letter-a.png")
116 2 joaquin guarachi
root_canvas = Canvas(root, width=900, height=400)
117 2 joaquin guarachi
root_canvas.pack(fil="both", expand=True)
118 1 joaquin guarachi
root_canvas.create_image(260, 100, image=check_photo, anchor="nw")
119 1 joaquin guarachi
root_canvas.create_image(260, 140, image=exit_photo, anchor="nw")
120 1 joaquin guarachi
root_canvas.create_image(260, 180, image=automatic_photo, anchor="nw")
121 1 joaquin guarachi
122 12 joaquin guarachi
# //Buttons of interface tkinter & labels
123 12 joaquin guarachi
title_label = Label(root, text="[ Robo-Cleaner Interface ]", font=("Helvetica", 14), width=21, foreground="#757575").place(x=330, y=30)
124 12 joaquin guarachi
version_label = Label(root, text="Version 2.0", font=("Helvetica", 12), width=16, foreground="#757575").place(x=120, y=340)
125 12 joaquin guarachi
botton_connect = Button(root, text="Conectar", font=("Helvetica", 12), width=12, bg="ForestGreen", foreground="white", command=get_IP).place(x=120, y=100)
126 12 joaquin guarachi
botton_disconect = Button(root, text="Desconectar", font=("Helvetica", 12), width=12, bg="FireBrick2", foreground="white", command=disconection).place(x=120, y=140)
127 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)
128 12 joaquin guarachi
botton_forward = Button(root, text="Avanzar", font=("Helvetica",12), width=12, bg="SpringGreen3", foreground="DarkGreen")
129 2 joaquin guarachi
botton_forward.place(x=550, y=100)
130 2 joaquin guarachi
botton_forward.bind('<ButtonPress>',Upward)
131 2 joaquin guarachi
botton_forward.bind('<ButtonRelease>',Halt)
132 12 joaquin guarachi
botton_rotate = Button(root, text="Rotar Helice", font=("Helvetica", 12), width=12, bg="Chartreuse2", foreground="DarkGreen")
133 2 joaquin guarachi
botton_rotate.place(x=550, y=140)
134 2 joaquin guarachi
botton_rotate.bind('<ButtonPress>',Rotate)
135 2 joaquin guarachi
botton_rotate.bind('<ButtonRelease>',Halt)
136 12 joaquin guarachi
botton_rotate_left = Button(root, text="Girar Izq.", font=("Helvetica", 12), width=12, bg="PaleGreen3", foreground="DarkGreen")
137 12 joaquin guarachi
botton_rotate_left.place(x=410, y=140)
138 2 joaquin guarachi
botton_rotate_left.bind('<ButtonPress>',LeftTurn)
139 2 joaquin guarachi
botton_rotate_left.bind('<ButtonRelease>',Halt)
140 12 joaquin guarachi
botton_rotate_right = Button(root, text="Girar Der.", font=("Helvetica", 12), width=12, bg="PaleGreen3", foreground="DarkGreen")
141 12 joaquin guarachi
botton_rotate_right.place(x=690, y=140)
142 2 joaquin guarachi
botton_rotate_right.bind('<ButtonPress>',RightTurn)
143 2 joaquin guarachi
botton_rotate_right.bind('<ButtonRelease>',Halt)
144 12 joaquin guarachi
botton_backward = Button(root, text="Retroceder", font=("Helvetica", 12), width= 12, bg="SpringGreen3", foreground="DarkGreen")
145 2 joaquin guarachi
botton_backward.place(x=550, y=180)
146 2 joaquin guarachi
botton_backward.bind('<ButtonPress>',Backwards)
147 2 joaquin guarachi
botton_backward.bind('<ButtonRelease>',Halt)
148 12 joaquin guarachi
botton_lever_up = Button(root, text="Levantar Pala", font=("Helvetica", 12), width=12, bg="OliveDrab4",foreground="white", command=LeverUp)
149 12 joaquin guarachi
botton_lever_up.place(x=480, y=220)
150 12 joaquin guarachi
botton_lever_down = Button(root, text="Bajar Pala", font=("Helvetica", 12), width=12, bg="GoldenRod4",foreground="white", command=LeverDown)
151 12 joaquin guarachi
botton_lever_down.place(x=620, y=220)
152 12 joaquin guarachi
botton_close = Button(root, text=" Salir ", command=root.destroy, font=("Helvetica", 14), width=12, bg="Red3", foreground="white").place(x=700, y=340)
153 2 joaquin guarachi
154 12 joaquin guarachi
#Protocols & mainloop for root
155 2 joaquin guarachi
root.protocol("WM_DELETE_WINDOWS")
156 2 joaquin guarachi
root.mainloop()
157 1 joaquin guarachi
</code></pre>
158 2 joaquin guarachi
159 13 joaquin guarachi
{{thumbnail(interfazfinal.png, size=600, title=Interfaz de usuario final)}}
160 7 joaquin guarachi
161 7 joaquin guarachi
}}
162 4 joaquin guarachi
163 2 joaquin guarachi
---
164 2 joaquin guarachi
165 2 joaquin guarachi
h2. Parte operacional (código órdenes del robot)
166 2 joaquin guarachi
167 2 joaquin guarachi
Parte del código perteneciente a los movimientos y acciones que puede realizar el robot.
168 8 joaquin guarachi
El robot mediante sus funciones puede:
169 8 joaquin guarachi
* Moverse hacia adelante y hacia atrás
170 8 joaquin guarachi
* Girar a la izquierda y derecha
171 8 joaquin guarachi
* Accionar el arco giratorio limpiador
172 13 joaquin guarachi
* Subir y bajar su pala limpiadora
173 8 joaquin guarachi
* Conectarse y desconectarse por wi-fi a un equipo
174 2 joaquin guarachi
175 1 joaquin guarachi
{{collapse(Mostrar código)
176 6 joaquin guarachi
<pre><code class="python">
177 13 joaquin guarachi
#!/usr/bin/env python3
178 13 joaquin guarachi
from ev3dev2.motor import SpeedPercent, MoveTank, LargeMotor, MediumMotor, OUTPUT_A, OUTPUT_B, OUTPUT_C ,OUTPUT_D
179 1 joaquin guarachi
from ev3dev2.sensor import INPUT_1, INPUT_4
180 2 joaquin guarachi
from ev3dev2.sensor.lego import UltrasonicSensor, ColorSensor
181 1 joaquin guarachi
182 13 joaquin guarachi
tank = MoveTank(OUTPUT_A, OUTPUT_D)
183 2 joaquin guarachi
184 13 joaquin guarachi
mot_rot= MediumMotor(OUTPUT_B)
185 2 joaquin guarachi
186 13 joaquin guarachi
mot_lev = MediumMotor(OUTPUT_C)
187 13 joaquin guarachi
188 2 joaquin guarachi
prox = UltrasonicSensor(INPUT_1)
189 2 joaquin guarachi
190 2 joaquin guarachi
191 2 joaquin guarachi
col = ColorSensor(INPUT_4)
192 2 joaquin guarachi
193 2 joaquin guarachi
def Automatic():
194 2 joaquin guarachi
    while (col.color != 6):
195 2 joaquin guarachi
        while (prox.distance_centimeters_continuous > 5):
196 2 joaquin guarachi
            tank.on(left_speed= 90, right_speed= 90)
197 2 joaquin guarachi
            if (col.color == 6):
198 2 joaquin guarachi
                tank.off(brake=True)
199 2 joaquin guarachi
                break
200 2 joaquin guarachi
        else:
201 2 joaquin guarachi
            tank.off(brake=True)
202 2 joaquin guarachi
            mot_rot.on_for_seconds(speed = 75, seconds = 1, brake=True)
203 2 joaquin guarachi
    tank.off(brake=True)
204 2 joaquin guarachi
205 2 joaquin guarachi
def Upward():
206 1 joaquin guarachi
    tank.on(left_speed= 90, right_speed= 90)
207 2 joaquin guarachi
208 1 joaquin guarachi
def Backwards():
209 2 joaquin guarachi
    tank.on(left_speed= -90, right_speed= -90)
210 2 joaquin guarachi
211 2 joaquin guarachi
def LeftTurn():
212 13 joaquin guarachi
    tank.on(left_speed= 90, right_speed= 0)
213 1 joaquin guarachi
214 1 joaquin guarachi
def RightTurn():
215 13 joaquin guarachi
    tank.on(left_speed= 0, right_speed= 90)
216 1 joaquin guarachi
217 1 joaquin guarachi
def Rotate():
218 1 joaquin guarachi
    mot_rot.on(speed = 75, brake=True)
219 1 joaquin guarachi
220 13 joaquin guarachi
def LeverUp():
221 13 joaquin guarachi
    mot_lev.on_for_degrees(speed= 18, degrees= 90, brake= True)
222 13 joaquin guarachi
223 13 joaquin guarachi
def LeverDown():
224 13 joaquin guarachi
    mot_lev.on_for_degrees(speed= 18, degrees= -90, brake= True)
225 13 joaquin guarachi
226 13 joaquin guarachi
227 2 joaquin guarachi
def Halt():
228 2 joaquin guarachi
    tank.off(brake=True)
229 2 joaquin guarachi
    mot_rot.off(brake=True)
230 3 joaquin guarachi
</code></pre>
231 6 joaquin guarachi
}}
232 3 joaquin guarachi
233 2 joaquin guarachi
---
234 2 joaquin guarachi
235 2 joaquin guarachi
h2. Receptor de operaciones (ladrillo EV3)
236 2 joaquin guarachi
237 10 joaquin guarachi
Parte del código perteneciente a la lectura o interpretación de órdenes enviadas desde el PC en el ladrillo EV3.
238 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.
239 2 joaquin guarachi
240 6 joaquin guarachi
{{collapse(Mostrar código)
241 2 joaquin guarachi
<pre><code class="python">
242 2 joaquin guarachi
#!/usr/bin/env python3
243 2 joaquin guarachi
import socket
244 2 joaquin guarachi
from operations import *
245 2 joaquin guarachi
246 2 joaquin guarachi
conector = socket.socket()
247 2 joaquin guarachi
248 2 joaquin guarachi
puerto = 12345
249 2 joaquin guarachi
250 2 joaquin guarachi
conector.bind(('', puerto))
251 2 joaquin guarachi
252 2 joaquin guarachi
conector.listen(5)
253 2 joaquin guarachi
254 2 joaquin guarachi
conexion, addr = conector.accept()
255 2 joaquin guarachi
256 2 joaquin guarachi
while True:
257 2 joaquin guarachi
    dato_puro = conexion.recv(1)
258 2 joaquin guarachi
    key = dato_puro.decode("utf-8")
259 2 joaquin guarachi
260 2 joaquin guarachi
    if (key == 'i'):
261 2 joaquin guarachi
        Upward()
262 2 joaquin guarachi
263 2 joaquin guarachi
    if (key == 'k'):
264 2 joaquin guarachi
        Backwards()
265 2 joaquin guarachi
266 2 joaquin guarachi
    if (key == 'j'):
267 1 joaquin guarachi
        LeftTurn()
268 1 joaquin guarachi
    
269 1 joaquin guarachi
    if (key == 'l'):
270 1 joaquin guarachi
        RightTurn()
271 1 joaquin guarachi
272 1 joaquin guarachi
    if (key == 'p'):
273 2 joaquin guarachi
        Rotate()
274 13 joaquin guarachi
275 13 joaquin guarachi
    if (key == 'y'):
276 13 joaquin guarachi
        LeverUp()
277 13 joaquin guarachi
278 13 joaquin guarachi
    if (key == 'h'):
279 13 joaquin guarachi
        LeverDown()
280 2 joaquin guarachi
281 2 joaquin guarachi
    if (key == 'a'):
282 2 joaquin guarachi
        Automatic()
283 2 joaquin guarachi
284 2 joaquin guarachi
    if (key == 'q'):
285 2 joaquin guarachi
        break
286 2 joaquin guarachi
287 2 joaquin guarachi
    if (key == 'w'):
288 2 joaquin guarachi
        Halt()
289 2 joaquin guarachi
</code></pre>
290 6 joaquin guarachi
}}
291 2 joaquin guarachi
292 2 joaquin guarachi
---
293 5 joaquin guarachi
294 5 joaquin guarachi
h2. +Contenido:+
295 5 joaquin guarachi
* [[Wiki|Página principal]]
296 5 joaquin guarachi
* [[Información detallada]]
297 11 joaquin guarachi
** [[Informe de costos]]
298 5 joaquin guarachi
* [[Avances y planificación]]
299 1 joaquin guarachi
* [[Código e implementación]]
300 11 joaquin guarachi
301 11 joaquin guarachi
---