Project

General

Profile

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

Bruno Amestica, 12/12/2023 09:08 AM

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