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