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