Project

General

Profile

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

joaquin guarachi, 12/18/2022 04:45 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 1 joaquin guarachi
{{thumbnail(interfaz1.1.png, size=600, title=Interfaz de usuario v.1.1)}}
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 8 joaquin guarachi
* Subir y bajar su limpiador de arrastre
173 8 joaquin guarachi
* Conectarse y desconectarse por wi-fi a un equipo
174 2 joaquin guarachi
175 6 joaquin guarachi
{{collapse(Mostrar código)
176 2 joaquin guarachi
<pre><code class="python">
177 2 joaquin guarachi
from ev3dev2.motor import SpeedPercent, MoveTank, LargeMotor, MediumMotor, OUTPUT_A, OUTPUT_B, OUTPUT_D
178 2 joaquin guarachi
from ev3dev2.sensor import INPUT_1, INPUT_4
179 2 joaquin guarachi
from ev3dev2.sensor.lego import UltrasonicSensor, ColorSensor
180 2 joaquin guarachi
181 2 joaquin guarachi
tank = MoveTank(OUTPUT_A, OUTPUT_B)
182 2 joaquin guarachi
183 2 joaquin guarachi
mot_rot= MediumMotor(OUTPUT_D)
184 2 joaquin guarachi
185 2 joaquin guarachi
prox = UltrasonicSensor(INPUT_1)
186 2 joaquin guarachi
187 2 joaquin guarachi
188 2 joaquin guarachi
col = ColorSensor(INPUT_4)
189 2 joaquin guarachi
190 2 joaquin guarachi
def Automatic():
191 2 joaquin guarachi
    while (col.color != 6):
192 2 joaquin guarachi
        while (prox.distance_centimeters_continuous > 5):
193 2 joaquin guarachi
            tank.on(left_speed= 90, right_speed= 90)
194 2 joaquin guarachi
            if (col.color == 6):
195 2 joaquin guarachi
                tank.off(brake=True)
196 2 joaquin guarachi
                break
197 2 joaquin guarachi
        else:
198 2 joaquin guarachi
            tank.off(brake=True)
199 2 joaquin guarachi
            mot_rot.on_for_seconds(speed = 75, seconds = 1, brake=True)
200 2 joaquin guarachi
    tank.off(brake=True)
201 2 joaquin guarachi
202 2 joaquin guarachi
def Upward():
203 2 joaquin guarachi
    tank.on(left_speed= 90, right_speed= 90)
204 2 joaquin guarachi
205 2 joaquin guarachi
def Backwards():
206 2 joaquin guarachi
    tank.on(left_speed= -90, right_speed= -90)
207 2 joaquin guarachi
208 2 joaquin guarachi
def LeftTurn():
209 2 joaquin guarachi
    tank.on(left_speed= 90, right_speed= -90)
210 2 joaquin guarachi
211 2 joaquin guarachi
def RightTurn():
212 2 joaquin guarachi
    tank.on(left_speed= -90, right_speed= 90)
213 2 joaquin guarachi
214 2 joaquin guarachi
def Rotate():
215 2 joaquin guarachi
    mot_rot.on(speed = 75, brake=True)
216 2 joaquin guarachi
217 2 joaquin guarachi
def Halt():
218 2 joaquin guarachi
    tank.off(brake=True)
219 2 joaquin guarachi
    mot_rot.off(brake=True)
220 3 joaquin guarachi
</code></pre>
221 6 joaquin guarachi
}}
222 3 joaquin guarachi
223 2 joaquin guarachi
---
224 2 joaquin guarachi
225 2 joaquin guarachi
h2. Receptor de operaciones (ladrillo EV3)
226 2 joaquin guarachi
227 10 joaquin guarachi
Parte del código perteneciente a la lectura o interpretación de órdenes enviadas desde el PC en el ladrillo EV3.
228 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.
229 2 joaquin guarachi
230 6 joaquin guarachi
{{collapse(Mostrar código)
231 2 joaquin guarachi
<pre><code class="python">
232 2 joaquin guarachi
#!/usr/bin/env python3
233 2 joaquin guarachi
import socket
234 2 joaquin guarachi
from operations import *
235 2 joaquin guarachi
236 2 joaquin guarachi
conector = socket.socket()
237 2 joaquin guarachi
238 2 joaquin guarachi
puerto = 12345
239 2 joaquin guarachi
240 2 joaquin guarachi
conector.bind(('', puerto))
241 2 joaquin guarachi
242 2 joaquin guarachi
conector.listen(5)
243 2 joaquin guarachi
244 2 joaquin guarachi
conexion, addr = conector.accept()
245 2 joaquin guarachi
246 2 joaquin guarachi
while True:
247 2 joaquin guarachi
    dato_puro = conexion.recv(1)
248 2 joaquin guarachi
    key = dato_puro.decode("utf-8")
249 2 joaquin guarachi
250 2 joaquin guarachi
    if (key == 'i'):
251 2 joaquin guarachi
        Upward()
252 2 joaquin guarachi
253 2 joaquin guarachi
    if (key == 'k'):
254 2 joaquin guarachi
        Backwards()
255 2 joaquin guarachi
256 2 joaquin guarachi
    if (key == 'j'):
257 2 joaquin guarachi
        LeftTurn()
258 2 joaquin guarachi
    
259 2 joaquin guarachi
    if (key == 'l'):
260 2 joaquin guarachi
        RightTurn()
261 2 joaquin guarachi
262 2 joaquin guarachi
    if (key == 'p'):
263 2 joaquin guarachi
        Rotate()
264 2 joaquin guarachi
265 2 joaquin guarachi
    if (key == 'a'):
266 2 joaquin guarachi
        Automatic()
267 2 joaquin guarachi
268 2 joaquin guarachi
    if (key == 'q'):
269 2 joaquin guarachi
        break
270 2 joaquin guarachi
271 2 joaquin guarachi
    if (key == 'w'):
272 2 joaquin guarachi
        Halt()
273 2 joaquin guarachi
</code></pre>
274 6 joaquin guarachi
}}
275 2 joaquin guarachi
276 2 joaquin guarachi
---
277 5 joaquin guarachi
278 5 joaquin guarachi
h2. +Contenido:+
279 5 joaquin guarachi
* [[Wiki|Página principal]]
280 5 joaquin guarachi
* [[Información detallada]]
281 11 joaquin guarachi
** [[Informe de costos]]
282 5 joaquin guarachi
* [[Avances y planificación]]
283 1 joaquin guarachi
* [[Código e implementación]]
284 11 joaquin guarachi
285 11 joaquin guarachi
---