Project

General

Profile

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

joaquin guarachi, 11/08/2022 08:48 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 1 joaquin guarachi
<pre><code class="python">
10 2 joaquin guarachi
#//Proyecto Robot Robo-Cleaner
11 1 joaquin guarachi
12 2 joaquin guarachi
# //Imports
13 2 joaquin guarachi
from tkinter import *
14 2 joaquin guarachi
from tkinter import messagebox
15 2 joaquin guarachi
from tkinter import ttk
16 2 joaquin guarachi
import socket
17 2 joaquin guarachi
18 2 joaquin guarachi
from setuptools import Command
19 2 joaquin guarachi
20 2 joaquin guarachi
root = Tk()
21 2 joaquin guarachi
root.title("Robo-Cleaner")
22 2 joaquin guarachi
root.resizable(False, False)
23 2 joaquin guarachi
root.geometry("900x400")
24 2 joaquin guarachi
25 2 joaquin guarachi
# //Conexión Socket
26 2 joaquin guarachi
client = socket.socket()
27 2 joaquin guarachi
port = 12345
28 2 joaquin guarachi
29 2 joaquin guarachi
def get_IP():
30 2 joaquin guarachi
    vtn_ip = Tk()
31 2 joaquin guarachi
    vtn_ip.geometry("275x100")
32 2 joaquin guarachi
    vtn_ip.resizable(False,False)
33 2 joaquin guarachi
    vtn_ip.title("Ajustes de la Conexión IP")
34 2 joaquin guarachi
35 2 joaquin guarachi
    inform = Label(vtn_ip,text="Dirección IP: ").place(x=10, y=10)
36 2 joaquin guarachi
    addr = StringVar(vtn_ip)
37 2 joaquin guarachi
    ip = ttk.Entry(vtn_ip, textvariable = addr).place(x=100, y=10)
38 2 joaquin guarachi
    
39 2 joaquin guarachi
    boton = Button(vtn_ip, text = "Establecer", command = lambda:[conection(addr.get()+""), vtn_ip.destroy()]).place(x=98, y=50)
40 2 joaquin guarachi
    print(addr.get())
41 2 joaquin guarachi
42 2 joaquin guarachi
def conection(addr):
43 2 joaquin guarachi
    port = 12345
44 2 joaquin guarachi
    try:
45 2 joaquin guarachi
        client.connect((addr, port))
46 2 joaquin guarachi
        messagebox.showinfo("Mensaje Recibido", "El host se conectó al robot: \nDirección IP: {0}\nPuerto: {1}".format(addr,port))
47 2 joaquin guarachi
    except socket.error:
48 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)
49 2 joaquin guarachi
        get_IP()
50 2 joaquin guarachi
        client.close()
51 2 joaquin guarachi
52 2 joaquin guarachi
53 2 joaquin guarachi
# //Funciones 
54 2 joaquin guarachi
def Upward(event):
55 2 joaquin guarachi
    client.send(bytes([ord('i')]))
56 2 joaquin guarachi
57 2 joaquin guarachi
def Backwards(event):
58 2 joaquin guarachi
    client.send(bytes([ord('k')]))
59 2 joaquin guarachi
60 2 joaquin guarachi
def LeftTurn(event):
61 2 joaquin guarachi
    client.send(bytes([ord('j')]))
62 2 joaquin guarachi
63 2 joaquin guarachi
def RightTurn(event):
64 2 joaquin guarachi
    client.send(bytes([ord('l')]))
65 2 joaquin guarachi
66 2 joaquin guarachi
def Rotate(event):
67 2 joaquin guarachi
    client.send(bytes([ord('p')]))
68 2 joaquin guarachi
69 2 joaquin guarachi
def Automatic():
70 2 joaquin guarachi
    client.send(bytes([ord('a')]))
71 2 joaquin guarachi
72 2 joaquin guarachi
def disconection():
73 2 joaquin guarachi
    client.send(bytes([ord('q')]))
74 2 joaquin guarachi
75 2 joaquin guarachi
def Halt(event):
76 2 joaquin guarachi
    client.send(bytes([ord('w')]))
77 2 joaquin guarachi
78 2 joaquin guarachi
# //Diseño de botones
79 2 joaquin guarachi
check_photo = PhotoImage(file="checked.png")
80 2 joaquin guarachi
exit_photo = PhotoImage(file="exit.png")
81 2 joaquin guarachi
automatic_photo = PhotoImage(file="letter-a.png")
82 2 joaquin guarachi
root_canvas = Canvas(root, width=900, height=400)
83 2 joaquin guarachi
root_canvas.pack(fil="both", expand=True)
84 2 joaquin guarachi
root_canvas.create_image(260, 100, image=check_photo, anchor="nw")
85 2 joaquin guarachi
root_canvas.create_image(260, 140, image=exit_photo, anchor="nw")
86 2 joaquin guarachi
root_canvas.create_image(260, 180, image=automatic_photo, anchor="nw")
87 2 joaquin guarachi
88 2 joaquin guarachi
# //Botones y texto de la interfaz tkinter
89 2 joaquin guarachi
title_label = Label(root, text="[ Robo-Cleaner Interface ]", font=("Helvetica", 14), width=20, foreground="#757575").place(x=330, y=30)
90 2 joaquin guarachi
version_label = Label(root, text="Version 1.1", font=("Helvetica", 12), width=12, foreground="#757575").place(x=120, y=340)
91 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)
92 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)
93 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)
94 2 joaquin guarachi
botton_forward = Button(root, text="Adelante", font=("Helvetica",12), width=12, bg="RoyalBlue1", foreground="white")
95 2 joaquin guarachi
botton_forward.place(x=550, y=100)
96 2 joaquin guarachi
botton_forward.bind('<ButtonPress>',Upward)
97 2 joaquin guarachi
botton_forward.bind('<ButtonRelease>',Halt)
98 2 joaquin guarachi
botton_rotate = Button(root, text="Rotar Helice", font=("Helvetica", 12), width=12, bg="RoyalBlue1", foreground="white")
99 2 joaquin guarachi
botton_rotate.place(x=550, y=140)
100 2 joaquin guarachi
botton_rotate.bind('<ButtonPress>',Rotate)
101 2 joaquin guarachi
botton_rotate.bind('<ButtonRelease>',Halt)
102 2 joaquin guarachi
botton_rotate_left = Button(root, text="Rotar Izq", font=("Helvetica", 12), width=12, bg="RoyalBlue1", foreground="white")
103 2 joaquin guarachi
botton_rotate_left.place(x=420, y=140)
104 2 joaquin guarachi
botton_rotate_left.bind('<ButtonPress>',LeftTurn)
105 2 joaquin guarachi
botton_rotate_left.bind('<ButtonRelease>',Halt)
106 2 joaquin guarachi
botton_rotate_right = Button(root, text="Rotar Dere", font=("Helvetica", 12), width=12, bg="RoyalBlue1", foreground="white")
107 2 joaquin guarachi
botton_rotate_right.place(x=680, y=140)
108 2 joaquin guarachi
botton_rotate_right.bind('<ButtonPress>',RightTurn)
109 2 joaquin guarachi
botton_rotate_right.bind('<ButtonRelease>',Halt)
110 2 joaquin guarachi
botton_backward = Button(root, text="Atras", font=("Helvetica", 12), width= 12, bg="RoyalBlue1", foreground="white")
111 2 joaquin guarachi
botton_backward.place(x=550, y=180)
112 2 joaquin guarachi
botton_backward.bind('<ButtonPress>',Backwards)
113 2 joaquin guarachi
botton_backward.bind('<ButtonRelease>',Halt)
114 2 joaquin guarachi
botton_close = Button(root, text=" Salir ", command=root.destroy, font=("Helvetica", 14), width=12).place(x=700, y=340)
115 2 joaquin guarachi
116 2 joaquin guarachi
#Protocolos y mainloop
117 2 joaquin guarachi
root.protocol("WM_DELETE_WINDOWS")
118 2 joaquin guarachi
root.mainloop()
119 1 joaquin guarachi
</code></pre>
120 2 joaquin guarachi
121 2 joaquin guarachi
---
122 2 joaquin guarachi
123 2 joaquin guarachi
h2. Parte operacional (código órdenes del robot)
124 2 joaquin guarachi
125 2 joaquin guarachi
Parte del código perteneciente a los movimientos y acciones que puede realizar el robot.
126 2 joaquin guarachi
127 2 joaquin guarachi
<pre><code class="python">
128 2 joaquin guarachi
from ev3dev2.motor import SpeedPercent, MoveTank, LargeMotor, MediumMotor, OUTPUT_A, OUTPUT_B, OUTPUT_D
129 2 joaquin guarachi
from ev3dev2.sensor import INPUT_1, INPUT_4
130 2 joaquin guarachi
from ev3dev2.sensor.lego import UltrasonicSensor, ColorSensor
131 2 joaquin guarachi
132 2 joaquin guarachi
tank = MoveTank(OUTPUT_A, OUTPUT_B)
133 2 joaquin guarachi
134 2 joaquin guarachi
mot_rot= MediumMotor(OUTPUT_D)
135 2 joaquin guarachi
136 2 joaquin guarachi
prox = UltrasonicSensor(INPUT_1)
137 2 joaquin guarachi
138 2 joaquin guarachi
139 2 joaquin guarachi
col = ColorSensor(INPUT_4)
140 2 joaquin guarachi
141 2 joaquin guarachi
def Automatic():
142 2 joaquin guarachi
    while (col.color != 6):
143 2 joaquin guarachi
        while (prox.distance_centimeters_continuous > 5):
144 2 joaquin guarachi
            tank.on(left_speed= 90, right_speed= 90)
145 2 joaquin guarachi
            if (col.color == 6):
146 2 joaquin guarachi
                tank.off(brake=True)
147 2 joaquin guarachi
                break
148 2 joaquin guarachi
        else:
149 2 joaquin guarachi
            tank.off(brake=True)
150 2 joaquin guarachi
            mot_rot.on_for_seconds(speed = 75, seconds = 1, brake=True)
151 2 joaquin guarachi
    tank.off(brake=True)
152 2 joaquin guarachi
153 2 joaquin guarachi
def Upward():
154 2 joaquin guarachi
    tank.on(left_speed= 90, right_speed= 90)
155 2 joaquin guarachi
156 2 joaquin guarachi
def Backwards():
157 2 joaquin guarachi
    tank.on(left_speed= -90, right_speed= -90)
158 2 joaquin guarachi
159 2 joaquin guarachi
def LeftTurn():
160 2 joaquin guarachi
    tank.on(left_speed= 90, right_speed= -90)
161 2 joaquin guarachi
162 2 joaquin guarachi
def RightTurn():
163 2 joaquin guarachi
    tank.on(left_speed= -90, right_speed= 90)
164 2 joaquin guarachi
165 2 joaquin guarachi
def Rotate():
166 2 joaquin guarachi
    mot_rot.on(speed = 75, brake=True)
167 2 joaquin guarachi
168 2 joaquin guarachi
def Halt():
169 2 joaquin guarachi
    tank.off(brake=True)
170 2 joaquin guarachi
    mot_rot.off(brake=True)
171 2 joaquin guarachi
</code></pre>
172 2 joaquin guarachi
173 2 joaquin guarachi
---
174 2 joaquin guarachi
175 2 joaquin guarachi
h2. Receptor de operaciones (ladrillo EV3)
176 2 joaquin guarachi
177 2 joaquin guarachi
Parte del código perteneciente a la lectura o interpretación de órdenes enviadas desde el PC en el ladrillo EV3
178 2 joaquin guarachi
179 2 joaquin guarachi
<pre><code class="python">
180 2 joaquin guarachi
#!/usr/bin/env python3
181 2 joaquin guarachi
import socket
182 2 joaquin guarachi
from operations import *
183 2 joaquin guarachi
184 2 joaquin guarachi
conector = socket.socket()
185 2 joaquin guarachi
186 2 joaquin guarachi
puerto = 12345
187 2 joaquin guarachi
188 2 joaquin guarachi
conector.bind(('', puerto))
189 2 joaquin guarachi
190 2 joaquin guarachi
conector.listen(5)
191 2 joaquin guarachi
192 2 joaquin guarachi
conexion, addr = conector.accept()
193 2 joaquin guarachi
194 2 joaquin guarachi
while True:
195 2 joaquin guarachi
    dato_puro = conexion.recv(1)
196 2 joaquin guarachi
    key = dato_puro.decode("utf-8")
197 2 joaquin guarachi
198 2 joaquin guarachi
    if (key == 'i'):
199 2 joaquin guarachi
        Upward()
200 2 joaquin guarachi
201 2 joaquin guarachi
    if (key == 'k'):
202 2 joaquin guarachi
        Backwards()
203 2 joaquin guarachi
204 2 joaquin guarachi
    if (key == 'j'):
205 2 joaquin guarachi
        LeftTurn()
206 2 joaquin guarachi
    
207 2 joaquin guarachi
    if (key == 'l'):
208 2 joaquin guarachi
        RightTurn()
209 2 joaquin guarachi
210 2 joaquin guarachi
    if (key == 'p'):
211 2 joaquin guarachi
        Rotate()
212 2 joaquin guarachi
213 2 joaquin guarachi
    if (key == 'a'):
214 2 joaquin guarachi
        Automatic()
215 2 joaquin guarachi
216 2 joaquin guarachi
    if (key == 'q'):
217 2 joaquin guarachi
        break
218 2 joaquin guarachi
219 2 joaquin guarachi
    if (key == 'w'):
220 2 joaquin guarachi
        Halt()
221 2 joaquin guarachi
</code></pre>
222 2 joaquin guarachi
223 2 joaquin guarachi
---