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