• Código y Implementación » History » Version 3
Nelson Ramirez, 12/12/2023 09:06 AM
1 | 1 | Nelson Ramirez | h1. • Código y Implementación |
---|---|---|---|
2 | 1 | Nelson Ramirez | |
3 | 1 | Nelson Ramirez | |
4 | 1 | Nelson Ramirez | |
5 | 1 | Nelson Ramirez | Código de la Interfaz. |
6 | 1 | Nelson Ramirez | <pre><code class="Python"> |
7 | 1 | Nelson Ramirez | import os |
8 | 1 | Nelson Ramirez | import socket |
9 | 1 | Nelson Ramirez | |
10 | 1 | Nelson Ramirez | class ControlRemotoGUI: |
11 | 1 | Nelson Ramirez | def __init__(self, master): |
12 | 1 | Nelson Ramirez | self.master = master |
13 | 1 | Nelson Ramirez | master.title("Control Remoto LEGO EV3") |
14 | 1 | Nelson Ramirez | |
15 | 1 | Nelson Ramirez | server= socket.socket() |
16 | 1 | Nelson Ramirez | ip= "192.168.1.84" |
17 | 1 | Nelson Ramirez | port= 8080 |
18 | 1 | Nelson Ramirez | |
19 | 1 | Nelson Ramirez | def avanzar(): |
20 | 1 | Nelson Ramirez | server.send(bytes([ord['w']])) |
21 | 1 | Nelson Ramirez | |
22 | 1 | Nelson Ramirez | def retroceder(): |
23 | 1 | Nelson Ramirez | server.send(bytes([ord['s']])) |
24 | 1 | Nelson Ramirez | |
25 | 1 | Nelson Ramirez | def izquierda(): |
26 | 1 | Nelson Ramirez | server.send(bytes([ord['a']])) |
27 | 1 | Nelson Ramirez | |
28 | 1 | Nelson Ramirez | def derecha(): |
29 | 1 | Nelson Ramirez | server.send(bytes([ord['d']])) |
30 | 1 | Nelson Ramirez | |
31 | 1 | Nelson Ramirez | def golpear(): |
32 | 1 | Nelson Ramirez | server.send(bytes([ord['e']])) |
33 | 1 | Nelson Ramirez | |
34 | 1 | Nelson Ramirez | def detener(): |
35 | 1 | Nelson Ramirez | server.send(bytes([ord[' ']])) |
36 | 1 | Nelson Ramirez | |
37 | 1 | Nelson Ramirez | def conectar(addres): |
38 | 1 | Nelson Ramirez | try: |
39 | 1 | Nelson Ramirez | server.connect((addres, port)) |
40 | 1 | Nelson Ramirez | messagebox.showinfo("Conectado, se establecio la conexion correctamente") |
41 | 1 | Nelson Ramirez | except socket.error: |
42 | 1 | Nelson Ramirez | messagebox.showwarning("Conexion erronea, verifique la direccion ip") |
43 | 1 | Nelson Ramirez | |
44 | 1 | Nelson Ramirez | # Obtener la ruta actual |
45 | 1 | Nelson Ramirez | script_dir = os.path.dirname(os.path.abspath(__file__)) |
46 | 1 | Nelson Ramirez | |
47 | 1 | Nelson Ramirez | # Cargar imagenes desde la carpeta |
48 | 1 | Nelson Ramirez | self.img_avanzar = self.load_image(os.path.join(script_dir, "Arriba.gif")) |
49 | 1 | Nelson Ramirez | self.img_retroceder = self.load_image(os.path.join(script_dir, "Abajo.gif")) |
50 | 1 | Nelson Ramirez | self.img_izquierda = self.load_image(os.path.join(script_dir, "Izquierda.gif")) |
51 | 1 | Nelson Ramirez | self.img_derecha = self.load_image(os.path.join(script_dir, "Derecha.gif")) |
52 | 1 | Nelson Ramirez | self.img_golpear = self.load_image(os.path.join(script_dir, "Stop.gif")) |
53 | 1 | Nelson Ramirez | self.img_conectar = self.load_image(os.path.join(script_dir, "Conectar.gif")) |
54 | 1 | Nelson Ramirez | |
55 | 1 | Nelson Ramirez | # Configuracion de la ventana |
56 | 1 | Nelson Ramirez | master.geometry("770x500") |
57 | 1 | Nelson Ramirez | master.resizable(width=False, height=False) |
58 | 1 | Nelson Ramirez | |
59 | 1 | Nelson Ramirez | # Cargar la imagen de fondo y almacenarla como un atributo de instancia |
60 | 1 | Nelson Ramirez | self.bg_image = tk.PhotoImage(file=os.path.join(script_dir, "Background.gif")) |
61 | 1 | Nelson Ramirez | |
62 | 1 | Nelson Ramirez | # Mostrar la imagen de fondo en un Label |
63 | 1 | Nelson Ramirez | label = tk.Label(master, image=self.bg_image) |
64 | 1 | Nelson Ramirez | label.place(x=0, y=0, relwidth=1, relheight=1) |
65 | 1 | Nelson Ramirez | |
66 | 1 | Nelson Ramirez | self.btn_avanzar = tk.Button(master, image=self.img_avanzar, command=self.avanzar) |
67 | 1 | Nelson Ramirez | self.btn_avanzar.place(relx=0.5, rely=0.25, anchor=tk.CENTER) |
68 | 1 | Nelson Ramirez | |
69 | 1 | Nelson Ramirez | self.btn_retroceder = tk.Button(master, image=self.img_retroceder, command=self.retroceder) |
70 | 1 | Nelson Ramirez | self.btn_retroceder.place(relx=0.5, rely=0.75, anchor=tk.CENTER) |
71 | 1 | Nelson Ramirez | |
72 | 1 | Nelson Ramirez | self.btn_izquierda = tk.Button(master, image=self.img_izquierda, command=self.izquierda) |
73 | 1 | Nelson Ramirez | self.btn_izquierda.place(relx=0.25, rely=0.5, anchor=tk.CENTER) |
74 | 1 | Nelson Ramirez | |
75 | 1 | Nelson Ramirez | self.btn_derecha = tk.Button(master, image=self.img_derecha, command=self.derecha) |
76 | 1 | Nelson Ramirez | self.btn_derecha.place(relx=0.75, rely=0.5, anchor=tk.CENTER) |
77 | 1 | Nelson Ramirez | |
78 | 1 | Nelson Ramirez | # Boton de golpear |
79 | 1 | Nelson Ramirez | self.btn_golpear = tk.Button(master, image=self.img_golpear, command=self.golpear) |
80 | 1 | Nelson Ramirez | self.btn_golpear.place(relx=0.5, rely=0.5, anchor=tk.CENTER) |
81 | 1 | Nelson Ramirez | |
82 | 1 | Nelson Ramirez | # Boton de conectar con el servidor |
83 | 1 | Nelson Ramirez | self.btn_conectar = tk.Button(master, image=self.img_conectar, command=self.conectar(ip)) |
84 | 1 | Nelson Ramirez | self.btn_conectar.place(relx=0.15, rely=0.1, anchor=tk.CENTER) |
85 | 1 | Nelson Ramirez | |
86 | 1 | Nelson Ramirez | def load_image(self, path): |
87 | 1 | Nelson Ramirez | img = tk.PhotoImage(file=path) |
88 | 1 | Nelson Ramirez | return img |
89 | 1 | Nelson Ramirez | |
90 | 1 | Nelson Ramirez | if __name__ == "__main__": |
91 | 1 | Nelson Ramirez | root = tk.Tk() |
92 | 1 | Nelson Ramirez | app = ControlRemotoGUI(root) |
93 | 1 | Nelson Ramirez | root.mainloop() |
94 | 1 | Nelson Ramirez | </code></pre> |
95 | 1 | Nelson Ramirez | |
96 | 2 | Nelson Ramirez | h3. Codigo del servidor |
97 | 1 | Nelson Ramirez | |
98 | 1 | Nelson Ramirez | <pre><code class="Python"> |
99 | 1 | Nelson Ramirez | import socket |
100 | 1 | Nelson Ramirez | |
101 | 1 | Nelson Ramirez | s= socket.socket() |
102 | 1 | Nelson Ramirez | print("Socket creado") |
103 | 1 | Nelson Ramirez | port = 8080 |
104 | 1 | Nelson Ramirez | s.bind(('', port)) |
105 | 1 | Nelson Ramirez | print("El socket se creo con puerto: {}".format(port)) |
106 | 1 | Nelson Ramirez | s.listen(5) |
107 | 1 | Nelson Ramirez | print("EL socket is listening....") |
108 | 1 | Nelson Ramirez | connect, addr = s.accept() |
109 | 1 | Nelson Ramirez | print("Se conecto a {}".format(addr)) |
110 | 1 | Nelson Ramirez | while True: |
111 | 1 | Nelson Ramirez | rawByte = connect.recv(1) |
112 | 1 | Nelson Ramirez | char = rawByte.decode('utf-8') |
113 | 1 | Nelson Ramirez | if (char == 'w'): |
114 | 1 | Nelson Ramirez | Mov_Avanzar() |
115 | 1 | Nelson Ramirez | if (char == 's'): |
116 | 1 | Nelson Ramirez | Mov_Retroceder() |
117 | 1 | Nelson Ramirez | if (char == 'd'): |
118 | 1 | Nelson Ramirez | Mov_Izquierda() |
119 | 1 | Nelson Ramirez | if (char == 'a'): |
120 | 1 | Nelson Ramirez | Mov_Derecha() |
121 | 1 | Nelson Ramirez | if (char == 'e'): |
122 | 1 | Nelson Ramirez | Mov_Golpear() |
123 | 1 | Nelson Ramirez | if (char == ' '): |
124 | 1 | Nelson Ramirez | Mov_Detener() |
125 | 1 | Nelson Ramirez | |
126 | 1 | Nelson Ramirez | </code></pre> |
127 | 1 | Nelson Ramirez | |
128 | 2 | Nelson Ramirez | h3. Código de las funciones |
129 | 1 | Nelson Ramirez | |
130 | 1 | Nelson Ramirez | <pre><code class="Python"> |
131 | 1 | Nelson Ramirez | |
132 | 1 | Nelson Ramirez | from ev3dev2.motor import LargeMotor, MoveTank,OUTPUT_A, OUTPUT_B, OUTPUT_C |
133 | 1 | Nelson Ramirez | from time import sleep |
134 | 1 | Nelson Ramirez | |
135 | 1 | Nelson Ramirez | m= MoveTank(OUTPUT_A,OUTPUT_B) |
136 | 1 | Nelson Ramirez | n= LargeMotor(OUTPUT_C) |
137 | 1 | Nelson Ramirez | l= LargeMotor(OUTPUT_A) |
138 | 1 | Nelson Ramirez | r= LargeMotor(OUTPUT_B) |
139 | 1 | Nelson Ramirez | |
140 | 1 | Nelson Ramirez | def Mov_Avanzar(): |
141 | 1 | Nelson Ramirez | m.run_forever(speed_sp=-400) |
142 | 1 | Nelson Ramirez | |
143 | 1 | Nelson Ramirez | def Mov_Retrocer(): |
144 | 1 | Nelson Ramirez | m.run_forever(speed_sp=400) |
145 | 1 | Nelson Ramirez | |
146 | 1 | Nelson Ramirez | def Mov_Izquierda(): |
147 | 1 | Nelson Ramirez | l.run_forever(speed_sp=-60) |
148 | 1 | Nelson Ramirez | r.run_forever(speed_sp=60) |
149 | 1 | Nelson Ramirez | |
150 | 1 | Nelson Ramirez | def Mov_Derecha(): |
151 | 1 | Nelson Ramirez | l.run_forever(speed_sp=60) |
152 | 1 | Nelson Ramirez | r.run_forever(speed_sp=-60) |
153 | 1 | Nelson Ramirez | |
154 | 1 | Nelson Ramirez | def Mov_Golpear(): |
155 | 1 | Nelson Ramirez | n.on_for_deggres(speed=10, deggres=45) |
156 | 1 | Nelson Ramirez | n.on_for_deggres(speed=100, deggres=-105) |
157 | 1 | Nelson Ramirez | n.off() |
158 | 1 | Nelson Ramirez | |
159 | 1 | Nelson Ramirez | def Detener(): |
160 | 1 | Nelson Ramirez | m.stop() |
161 | 1 | Nelson Ramirez | n.stop() |
162 | 1 | Nelson Ramirez | </code></pre> |
163 | 3 | Nelson Ramirez | |
164 | 3 | Nelson Ramirez | h2. +*Ficheros*+ |