Project

General

Profile

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

Katalina Oviedo, 12/12/2023 09:13 AM

1 17 Katalina Oviedo
{{thumbnail(Logo.png, size=300, title=Logo)}}
2 17 Katalina Oviedo
3 17 Katalina Oviedo
h2. Índice
4 17 Katalina Oviedo
5 17 Katalina Oviedo
* [[Introducción]]
6 17 Katalina Oviedo
* [[Objetivos]]
7 17 Katalina Oviedo
* [[Organización y planificación]]
8 17 Katalina Oviedo
* [[Desarrollo del proyecto]]
9 17 Katalina Oviedo
* [[Código e implementación]]
10 17 Katalina Oviedo
11 17 Katalina Oviedo
---
12 17 Katalina Oviedo
13 1 Bruno Amestica
h1. Código e implementación
14 2 Bruno Amestica
15 16 Katalina Oviedo
{{collapse(Código interfaz gráfica)
16 13 Bruno Amestica
<pre><code class="ruby">
17 12 Bruno Amestica
import tkinter as tk
18 12 Bruno Amestica
from tkinter import messagebox
19 12 Bruno Amestica
import socket
20 1 Bruno Amestica
21 12 Bruno Amestica
def adelante():
22 12 Bruno Amestica
    clientSocket.send(bytes([ord('w')]))
23 12 Bruno Amestica
24 12 Bruno Amestica
def atras():
25 12 Bruno Amestica
    clientSocket.send(bytes([ord('s')]))
26 12 Bruno Amestica
27 12 Bruno Amestica
def derecha():
28 12 Bruno Amestica
    clientSocket.send(bytes([ord('d')]))
29 12 Bruno Amestica
30 12 Bruno Amestica
def izquierda():
31 12 Bruno Amestica
    clientSocket.send(bytes([ord('a')]))
32 12 Bruno Amestica
33 12 Bruno Amestica
def golpear():
34 12 Bruno Amestica
    clientSocket.send(bytes([ord('r')]))
35 12 Bruno Amestica
    
36 12 Bruno Amestica
def on_release(event):
37 12 Bruno Amestica
    clientSocket.send(bytes([ord(' ')]))
38 12 Bruno Amestica
39 12 Bruno Amestica
def abrirventana():
40 12 Bruno Amestica
    ip = entry.get()
41 12 Bruno Amestica
    try:
42 12 Bruno Amestica
        clientSocket.connect((ip,port))
43 12 Bruno Amestica
        messagebox.showinfo("Conexión exitosa","Cliente conectado al robot: {0} : {1}".format(ip,port))
44 12 Bruno Amestica
45 12 Bruno Amestica
        ventana.destroy()
46 12 Bruno Amestica
        nueva_ventana = tk.Tk()
47 12 Bruno Amestica
        nueva_ventana.title("Lego® Mindstorms EV3")
48 12 Bruno Amestica
        nueva_ventana.geometry("720x405")
49 12 Bruno Amestica
        nueva_ventana.resizable(width=False, height=False)
50 12 Bruno Amestica
        #nueva_ventana.iconbitmap(icono)
51 12 Bruno Amestica
52 12 Bruno Amestica
        canvas = tk.Canvas(nueva_ventana, width=720, height=405, highlightthickness=0)
53 12 Bruno Amestica
        canvas.pack()
54 12 Bruno Amestica
55 12 Bruno Amestica
        img = tk.PhotoImage(file="/home/proyecto1/Escritorio/interfaz/fondo2.png")
56 12 Bruno Amestica
        canvas.create_image(360, 202, anchor="center", image=img)
57 12 Bruno Amestica
58 12 Bruno Amestica
        boton_adelante = tk.Button(nueva_ventana, text="Adelante",repeatdelay=50,repeatinterval=50, command=adelante, bg="#2e6c89", fg="white", font=("Helvetica", 10, "bold"))
59 12 Bruno Amestica
        boton_adelante.bind('<ButtonRelease-1>',on_release)
60 12 Bruno Amestica
        boton_adelante.place(x=361, y=120, anchor="center")
61 12 Bruno Amestica
62 12 Bruno Amestica
        boton_atras = tk.Button(nueva_ventana, text="Atrás",repeatdelay=50,repeatinterval=50, command=atras, bg="#2e6c89", fg="white", font=("Helvetica", 10, "bold"))
63 12 Bruno Amestica
        boton_atras.bind('<ButtonRelease-1>',on_release)
64 12 Bruno Amestica
        boton_atras.place(x=361, y=300, anchor="center")
65 12 Bruno Amestica
66 12 Bruno Amestica
        boton_izquierda = tk.Button(nueva_ventana, text="Izquierda",repeatdelay=50,repeatinterval=50, command=izquierda, bg="#2e6c89", fg="white", font=("Helvetica", 10, "bold"))
67 12 Bruno Amestica
        boton_izquierda.bind('<ButtonRelease-1>',on_release)
68 12 Bruno Amestica
        boton_izquierda.place(x=220, y=202, anchor="center")
69 12 Bruno Amestica
70 12 Bruno Amestica
        boton_derecha = tk.Button(nueva_ventana, text="Derecha",repeatdelay=50,repeatinterval=50, command=derecha, bg="#2e6c89", fg="white", font=("Helvetica", 10, "bold"))
71 12 Bruno Amestica
        boton_derecha.bind('<ButtonRelease-1>',on_release)
72 12 Bruno Amestica
        boton_derecha.place(x=500, y=202, anchor="center")
73 12 Bruno Amestica
74 12 Bruno Amestica
        boton_golpear = tk.Button(nueva_ventana, text="Golpear", command=golpear, bg="#2e6c89", fg="white", font=("Helvetica", 10, "bold"))
75 12 Bruno Amestica
        boton_golpear.place(x=360, y=202, anchor="center")
76 12 Bruno Amestica
77 12 Bruno Amestica
        boton_salir = tk.Button(nueva_ventana, text="Salir", command=nueva_ventana.destroy, bg="#040c0c", fg="white", font=("Helvetica", 10, "bold"))
78 12 Bruno Amestica
        boton_salir.place(x=650, y=360)
79 12 Bruno Amestica
80 12 Bruno Amestica
        nueva_ventana.mainloop()
81 12 Bruno Amestica
    except socket.error:
82 12 Bruno Amestica
        messagebox.showwarning("Conexión erronea","No se ha logrado al conexión, verifique la ip {0}".format(ip))
83 12 Bruno Amestica
        clientSocket.close()
84 12 Bruno Amestica
85 12 Bruno Amestica
ventana = tk.Tk()
86 12 Bruno Amestica
ventana.title("Lego® Mindstorms EV3")
87 12 Bruno Amestica
ventana.geometry("720x405")
88 12 Bruno Amestica
ventana.resizable(width=False, height=False)
89 12 Bruno Amestica
#icono = "home/proyecto1/Escritorio/interfaz/lego.ico"
90 12 Bruno Amestica
#ventana.iconbitmap(icono)
91 12 Bruno Amestica
92 12 Bruno Amestica
canvas = tk.Canvas(ventana, width=720, height=405, highlightthickness=0)
93 12 Bruno Amestica
canvas.pack()
94 12 Bruno Amestica
95 12 Bruno Amestica
img = tk.PhotoImage(file="/home/proyecto1/Escritorio/interfaz/fondo.png")
96 12 Bruno Amestica
canvas.create_image(360, 202, anchor="center", image=img)
97 12 Bruno Amestica
98 12 Bruno Amestica
rect_width = 370
99 12 Bruno Amestica
rect_height = 220
100 12 Bruno Amestica
rect1 = canvas.create_rectangle(0, 0, rect_width, rect_height, fill="#0c1d25", outline="")
101 12 Bruno Amestica
canvas.place(relx=0.5, rely=0.5, anchor="center")
102 12 Bruno Amestica
103 12 Bruno Amestica
canvas.coords(rect1, (canvas.winfo_reqwidth() - rect_width) / 2,
104 12 Bruno Amestica
                        (canvas.winfo_reqheight() - rect_height) / 2,
105 12 Bruno Amestica
                        (canvas.winfo_reqwidth() + rect_width) / 2,
106 12 Bruno Amestica
                        (canvas.winfo_reqheight() + rect_height) / 2)
107 12 Bruno Amestica
108 12 Bruno Amestica
rect_width = 360
109 12 Bruno Amestica
rect_height = 210
110 12 Bruno Amestica
rect2 = canvas.create_rectangle(0, 0, rect_width, rect_height, fill="#01070a", outline="")
111 12 Bruno Amestica
canvas.place(relx=0.5, rely=0.5, anchor="center")
112 12 Bruno Amestica
113 12 Bruno Amestica
canvas.coords(rect2, (canvas.winfo_reqwidth() - rect_width) / 2,
114 12 Bruno Amestica
                        (canvas.winfo_reqheight() - rect_height) / 2,
115 12 Bruno Amestica
                        (canvas.winfo_reqwidth() + rect_width) / 2,
116 12 Bruno Amestica
                        (canvas.winfo_reqheight() + rect_height) / 2)
117 12 Bruno Amestica
118 12 Bruno Amestica
texto = canvas.create_text(360, 140, text="GOLFFENHEIMER®", font="Helvetica 16 bold", fill="white", anchor="center")
119 12 Bruno Amestica
120 12 Bruno Amestica
entry = tk.Entry(ventana, font=("Helvetica", 12), bg="#0c141b", fg="white", bd=2, relief="flat", highlightbackground="#15222e", highlightthickness=2)
121 12 Bruno Amestica
entry.place(x=360, y=202.5, anchor="center")
122 12 Bruno Amestica
123 12 Bruno Amestica
boton = tk.Button(ventana, text="Conectar", command=abrirventana, bg="#1a3c4c", fg="white", font=("Helvetica", 10, "bold"), relief=tk.FLAT)
124 12 Bruno Amestica
boton.place(x=360, y=260, anchor="center")
125 12 Bruno Amestica
126 12 Bruno Amestica
boton_salir = tk.Button(ventana, text="Salir", command=ventana.destroy, bg="#040c0c", fg="white", font=("Helvetica", 10, "bold"))
127 12 Bruno Amestica
boton_salir.place(x=650, y=360)
128 12 Bruno Amestica
129 12 Bruno Amestica
clientSocket = socket.socket()
130 12 Bruno Amestica
port = 4040
131 12 Bruno Amestica
132 12 Bruno Amestica
ventana.mainloop()
133 13 Bruno Amestica
</code></pre>
134 1 Bruno Amestica
}}
135 14 Bruno Amestica
136 16 Katalina Oviedo
{{collapse(Código servidor)
137 14 Bruno Amestica
<pre><code class="ruby">
138 15 Bruno Amestica
#!/usr/bin/env python3
139 14 Bruno Amestica
from ev3dev2.motor import MediumMotor, OUTPUT_A, OUTPUT_C, OUTPUT_D, MoveTank,SpeedPercent
140 14 Bruno Amestica
141 14 Bruno Amestica
tank_drive = MoveTank(OUTPUT_A, OUTPUT_D)
142 14 Bruno Amestica
stick = MediumMotor(OUTPUT_C)
143 14 Bruno Amestica
144 14 Bruno Amestica
def adelante():
145 14 Bruno Amestica
    print("Moving up...")
146 14 Bruno Amestica
    tank_drive.on(SpeedPercent(100),SpeedPercent(100))
147 14 Bruno Amestica
148 14 Bruno Amestica
def atras():
149 14 Bruno Amestica
    print("Moving down...")
150 14 Bruno Amestica
    tank_drive.on(SpeedPercent(-100),SpeedPercent(-100))
151 14 Bruno Amestica
152 14 Bruno Amestica
def derecha():
153 14 Bruno Amestica
    print("Moving right...")
154 14 Bruno Amestica
    tank_drive.on(SpeedPercent(-100),SpeedPercent(100))
155 14 Bruno Amestica
156 14 Bruno Amestica
def izquierda():
157 14 Bruno Amestica
    print("Moving left...")
158 14 Bruno Amestica
    tank_drive.on(SpeedPercent(100),SpeedPercent(-100))
159 14 Bruno Amestica
    
160 14 Bruno Amestica
def golpear():
161 14 Bruno Amestica
    stick.on_for_degrees(SpeedPercent(-100),27000)
162 14 Bruno Amestica
163 14 Bruno Amestica
def stop():
164 14 Bruno Amestica
    tank_drive.stop()
165 14 Bruno Amestica
</code></pre>
166 14 Bruno Amestica
}}
167 14 Bruno Amestica
168 16 Katalina Oviedo
{{collapse(Código movimientos del robot)
169 14 Bruno Amestica
<pre><code class="ruby">
170 15 Bruno Amestica
import socket
171 14 Bruno Amestica
from funciones import *
172 14 Bruno Amestica
173 14 Bruno Amestica
s = socket.socket()
174 14 Bruno Amestica
print("Socket creado")
175 14 Bruno Amestica
port = 4040
176 14 Bruno Amestica
s.bind(('', port))
177 14 Bruno Amestica
print("El socket se creo con puerto: {}".format(port))
178 14 Bruno Amestica
s.listen(5)
179 14 Bruno Amestica
print("EL socket is listening....")
180 14 Bruno Amestica
connect, addr = s.accept()
181 14 Bruno Amestica
print("Se conecto a  {}".format(addr))
182 14 Bruno Amestica
183 14 Bruno Amestica
while True:
184 14 Bruno Amestica
    rawByte = connect.recv(1)
185 14 Bruno Amestica
    char = rawByte.decode('utf-8')
186 14 Bruno Amestica
    if (char == 'w'):
187 14 Bruno Amestica
        adelante()
188 14 Bruno Amestica
    if (char == 'a'):
189 14 Bruno Amestica
        izquierda()
190 14 Bruno Amestica
    if (char == 's'):
191 14 Bruno Amestica
        atras()
192 14 Bruno Amestica
    if (char == 'd'):
193 14 Bruno Amestica
        derecha()
194 14 Bruno Amestica
    if (char == 'r'):
195 14 Bruno Amestica
        golpear()
196 14 Bruno Amestica
    if (char == ' '):
197 14 Bruno Amestica
        stop()
198 14 Bruno Amestica
</code></pre>
199 14 Bruno Amestica
}}