Codigo Utilizado » History » Version 13
fernando diaz, 11/18/2024 04:41 PM
1 | 2 | fernando diaz | h1. Código Utilizado |
---|---|---|---|
2 | 1 | fernando diaz | |
3 | 9 | fernando diaz | |
4 | 9 | fernando diaz | |
5 | 9 | fernando diaz | *Servidor* |
6 | 9 | fernando diaz | |
7 | 9 | fernando diaz | <pre><code class="python"> |
8 | 9 | fernando diaz | |
9 | 9 | fernando diaz | import socket |
10 | 11 | fernando diaz | from main import * |
11 | 9 | fernando diaz | |
12 | 9 | fernando diaz | server = socket.socket() |
13 | 9 | fernando diaz | |
14 | 9 | fernando diaz | PORT = 8080 |
15 | 9 | fernando diaz | |
16 | 9 | fernando diaz | server.bind(('', PORT)) |
17 | 9 | fernando diaz | server.listen(1) |
18 | 9 | fernando diaz | |
19 | 9 | fernando diaz | connection, adress = server.accept() |
20 | 9 | fernando diaz | |
21 | 1 | fernando diaz | |
22 | 10 | fernando diaz | |
23 | 9 | fernando diaz | actions = { |
24 | 10 | fernando diaz | "w" : move_front, |
25 | 10 | fernando diaz | "a" : move_left, |
26 | 10 | fernando diaz | "d" : move_right, |
27 | 10 | fernando diaz | "s" : move_back, |
28 | 10 | fernando diaz | "e" : move_grab, |
29 | 9 | fernando diaz | } |
30 | 9 | fernando diaz | |
31 | 9 | fernando diaz | while True: |
32 | 9 | fernando diaz | data = connection.recv(1) |
33 | 9 | fernando diaz | keyword = data.decode("utf-8") |
34 | 9 | fernando diaz | |
35 | 9 | fernando diaz | if keyword in actions: |
36 | 9 | fernando diaz | actions[keyword]() |
37 | 9 | fernando diaz | |
38 | 9 | fernando diaz | elif keyword == "q": |
39 | 9 | fernando diaz | break |
40 | 9 | fernando diaz | |
41 | 9 | fernando diaz | </code></pre> |
42 | 13 | fernando diaz | |
43 | 13 | fernando diaz | *Funciones* |
44 | 13 | fernando diaz | <pre><code class="python"> |
45 | 13 | fernando diaz | #!/usr/bin/env pybricks-micropython |
46 | 13 | fernando diaz | from pybricks.hubs import EV3Brick |
47 | 13 | fernando diaz | from pybricks.ev3devices import Motor |
48 | 13 | fernando diaz | from pybricks.parameters import Port, Stop, Direction, Button, Color |
49 | 13 | fernando diaz | from pybricks.tools import wait, StopWatch, DataLog |
50 | 13 | fernando diaz | from pybricks.robotics import DriveBase |
51 | 13 | fernando diaz | from pybricks.media.ev3dev import SoundFile, ImageFile |
52 | 13 | fernando diaz | |
53 | 13 | fernando diaz | #Initialize the EV3 brick. |
54 | 13 | fernando diaz | ev3 = EV3Brick() |
55 | 13 | fernando diaz | |
56 | 13 | fernando diaz | motor_right = Motor(Port.A, Direction.COUNTERCLOCKWISE) |
57 | 13 | fernando diaz | motor_left = Motor(Port.D, Direction.COUNTERCLOCKWISE) |
58 | 13 | fernando diaz | motor_garra= Motor(Port.B) |
59 | 13 | fernando diaz | |
60 | 13 | fernando diaz | robot = DriveBase(motor_left,motor_right, wheel_diameter=55.5, axle_track=84) |
61 | 13 | fernando diaz | robot.settings(600, 500, 1000, 500) |
62 | 13 | fernando diaz | |
63 | 13 | fernando diaz | def off_robot(): |
64 | 13 | fernando diaz | robot.out_a.stop() |
65 | 13 | fernando diaz | robot.out_b.stop() |
66 | 13 | fernando diaz | robot.out_d.stop() |
67 | 13 | fernando diaz | |
68 | 13 | fernando diaz | def move_front(): |
69 | 13 | fernando diaz | robot.straight(150) |
70 | 13 | fernando diaz | #robot.out_a.stop() |
71 | 13 | fernando diaz | #robot.out_d.stop() |
72 | 13 | fernando diaz | |
73 | 13 | fernando diaz | def move_back(): |
74 | 13 | fernando diaz | robot.straight(-150) |
75 | 13 | fernando diaz | #robot.out_a.stop() |
76 | 13 | fernando diaz | #robot.out_d.stop() |
77 | 13 | fernando diaz | |
78 | 13 | fernando diaz | def move_right(): |
79 | 13 | fernando diaz | robot.turn(300) |
80 | 13 | fernando diaz | #robot.out_d.stop() |
81 | 13 | fernando diaz | #robot.out_a.stop() |
82 | 13 | fernando diaz | |
83 | 13 | fernando diaz | def move_left(): |
84 | 13 | fernando diaz | robot.turn(-300) |
85 | 13 | fernando diaz | #robot.out_d.stop() |
86 | 13 | fernando diaz | #robot.out_a.stop() |
87 | 13 | fernando diaz | |
88 | 13 | fernando diaz | def move_grab(): |
89 | 13 | fernando diaz | robot.run_until_stalled(-300, Stop.HOLD, 40) |
90 | 13 | fernando diaz | #motor_garra.run_until_stalled(300, Stop.COAST, 40) |
91 | 13 | fernando diaz | #robot.out_b.stop() |
92 | 13 | fernando diaz | |
93 | 13 | fernando diaz | #Intentamos cargar los motores de manera distinta. |
94 | 13 | fernando diaz | |
95 | 13 | fernando diaz | #def runBC(int runB = 0, int runC = 0): |
96 | 13 | fernando diaz | # setMotorSpeed(motorB, runB) |
97 | 13 | fernando diaz | # setMotorSpeed(motorC, runC) |
98 | 13 | fernando diaz | |
99 | 13 | fernando diaz | # Detener el robot |
100 | 13 | fernando diaz | robot.stop() |
101 | 13 | fernando diaz | |
102 | 13 | fernando diaz | ev3.speaker.beep() |
103 | 13 | fernando diaz | </code></pre> |
104 | 5 | fernando diaz | *Interfaz Gráfica* |
105 | 2 | fernando diaz | |
106 | 2 | fernando diaz | <pre><code class="python"> |
107 | 1 | fernando diaz | import tkinter as tk |
108 | 6 | fernando diaz | from tkinter import ttk |
109 | 1 | fernando diaz | from tkinter import messagebox |
110 | 1 | fernando diaz | import socket |
111 | 1 | fernando diaz | |
112 | 1 | fernando diaz | class App(tk.Tk): |
113 | 1 | fernando diaz | STATUS_CONNECTION = False |
114 | 10 | fernando diaz | PORT = 8080 |
115 | 8 | fernando diaz | |
116 | 1 | fernando diaz | def __init__(self): |
117 | 1 | fernando diaz | super().__init__() |
118 | 1 | fernando diaz | self.title("Ball-e") |
119 | 1 | fernando diaz | self.config(padx=10, pady=10, bg="#FFC0CB") |
120 | 1 | fernando diaz | self.geometry("800x600") |
121 | 1 | fernando diaz | self.canvas = tk.Canvas(self, width=800, height=600) |
122 | 1 | fernando diaz | self.canvas.pack() |
123 | 1 | fernando diaz | self.resizable(width=False, height=False) |
124 | 6 | fernando diaz | self.imagen_fondo = tk.PhotoImage(file="fondo.png") |
125 | 6 | fernando diaz | self.imagen_fondo = self.imagen_fondo.subsample(5,5) |
126 | 6 | fernando diaz | self.canvas.create_image(0, 0, anchor=tk.NW, image=self.imagen_fondo) |
127 | 6 | fernando diaz | self.init_images() |
128 | 1 | fernando diaz | self.init_elements() |
129 | 7 | fernando diaz | self.grab_open = True # Inicialmente, la garra está abierta |
130 | 8 | fernando diaz | |
131 | 6 | fernando diaz | def init_images(self): |
132 | 6 | fernando diaz | imagenes = ["encendido.png"] |
133 | 6 | fernando diaz | self.images = [tk.PhotoImage(file=imagen).subsample(12, 12) for imagen in imagenes] |
134 | 6 | fernando diaz | |
135 | 7 | fernando diaz | self.garra_abierta_img = tk.PhotoImage(file="garra_abierta.png").subsample(10, 10) |
136 | 7 | fernando diaz | self.garra_cerrada_img = tk.PhotoImage(file="garra_cerrada.png").subsample(10, 10) |
137 | 7 | fernando diaz | |
138 | 1 | fernando diaz | imagenes1 = ["flechaFront.png", "flechaBack.png", "flechaLeft.png", "flechaRight.png"] |
139 | 1 | fernando diaz | self.images1 = [tk.PhotoImage(file=imagen).subsample(25, 25) for imagen in imagenes1] |
140 | 8 | fernando diaz | self.flechaFront, self.flechaBack, self.flechaLeft, self.flechaRight= self.images1 |
141 | 8 | fernando diaz | |
142 | 1 | fernando diaz | def init_elements(self): |
143 | 8 | fernando diaz | btn_color = "#fff" if self.STATUS_CONNECTION else "#000" |
144 | 6 | fernando diaz | self.btn_Connect = tk.Button(self, bd=0, bg=btn_color, width=40, height=40, command=self.ip_connect).place(x=710, y=500) |
145 | 8 | fernando diaz | self.btn_Front = tk.Button(self, image= self.flechaFront, bd=0, bg="white", highlightbackground='white', width=80, height=80, command=lambda: self.move_front_car(None)).place(x=545,y=100) |
146 | 8 | fernando diaz | self.btn_Back = tk.Button(self, image= self.flechaBack, bd=0, width=80, height=80, command=lambda:self.move_back_car(None)).place(x=545,y=300) |
147 | 8 | fernando diaz | self.btn_Left = tk.Button(self, image= self.flechaLeft, bd=0, width=80, height=80, command=lambda:self.move_left_car(None)).place(x=450,y=200) |
148 | 6 | fernando diaz | self.btn_Right = tk.Button(self, image= self.flechaRight, bd=0, width=80, height=80, command=lambda: self.move_right_car(None)).place(x=640,y=200) |
149 | 8 | fernando diaz | self.btn_Grab = tk.Button(self, image=self.garra_abierta_img, bd=0, command=lambda: self.move_grab_car(None)).place(x=545, y=200) |
150 | 8 | fernando diaz | |
151 | 2 | fernando diaz | def ip_connect(self): |
152 | 1 | fernando diaz | if not self.STATUS_CONNECTION: |
153 | 1 | fernando diaz | ventana_conexion = tk.Toplevel(self) |
154 | 1 | fernando diaz | ventana_conexion.title("Ventana de Conexión") |
155 | 1 | fernando diaz | ventana_conexion.geometry("200x150") |
156 | 1 | fernando diaz | ventana_conexion.resizable(width=False, height=False) |
157 | 1 | fernando diaz | etiqueta = tk.Label(ventana_conexion, text="Ingrese la dirección IP:") |
158 | 1 | fernando diaz | etiqueta.pack(pady=10) |
159 | 1 | fernando diaz | self.IP = tk.StringVar() |
160 | 1 | fernando diaz | entry_ip = tk.Entry(ventana_conexion, textvariable=self.IP) |
161 | 1 | fernando diaz | entry_ip.pack(pady=10) |
162 | 1 | fernando diaz | btn_conectar = tk.Button(ventana_conexion, text="Conectar", command= lambda: self.robot_connection(self.IP.get())) |
163 | 1 | fernando diaz | btn_conectar.pack(pady=10) |
164 | 1 | fernando diaz | else: |
165 | 1 | fernando diaz | self.robot_connection(self.IP.get()) |
166 | 1 | fernando diaz | |
167 | 1 | fernando diaz | def robot_connection(self, IP): |
168 | 1 | fernando diaz | if self.STATUS_CONNECTION: |
169 | 3 | fernando diaz | self.STATUS_CONNECTION = not self.STATUS_CONNECTION |
170 | 1 | fernando diaz | self.init_elements() |
171 | 1 | fernando diaz | else: |
172 | 1 | fernando diaz | try: |
173 | 1 | fernando diaz | self.client = socket.socket() |
174 | 1 | fernando diaz | self.client.connect((IP, self.PORT)) |
175 | 1 | fernando diaz | self.STATUS_CONNECTION = True |
176 | 12 | fernando diaz | self.init_elements() |
177 | 12 | fernando diaz | messagebox.showinfo("Conexión exitosa", "Se ha conectado al robot correctamente.") |
178 | 12 | fernando diaz | self.window_ip.destroy() |
179 | 1 | fernando diaz | except socket.error: |
180 | 1 | fernando diaz | messagebox.showerror("Error", "No se pudo conectar al robot.") |
181 | 1 | fernando diaz | self.STATUS_CONNECTION = False |
182 | 8 | fernando diaz | |
183 | 1 | fernando diaz | def move_front_car(self, event): |
184 | 1 | fernando diaz | self.client.send(bytes([ord('w')])) |
185 | 8 | fernando diaz | |
186 | 1 | fernando diaz | def move_left_car(self, event): |
187 | 1 | fernando diaz | self.client.send(bytes([ord('a')])) |
188 | 8 | fernando diaz | |
189 | 1 | fernando diaz | def move_right_car(self, event): |
190 | 1 | fernando diaz | self.client.send(bytes([ord('d')])) |
191 | 8 | fernando diaz | |
192 | 1 | fernando diaz | def move_back_car(self, event): |
193 | 1 | fernando diaz | self.client.send(bytes([ord('s')])) |
194 | 8 | fernando diaz | |
195 | 7 | fernando diaz | def move_grab_car(self, event): |
196 | 7 | fernando diaz | self.client.send(bytes([ord('e')])) |
197 | 7 | fernando diaz | if self.grab_open: |
198 | 7 | fernando diaz | self.grab_open = False # Ahora la garra está cerrada |
199 | 7 | fernando diaz | self.btn_Grab.config(image=self.garra_cerrada_img) |
200 | 7 | fernando diaz | else: |
201 | 7 | fernando diaz | self.grab_open = True # Ahora la garra está abierta |
202 | 8 | fernando diaz | self.btn_Grab.config(image=self.garra_abierta_img) |
203 | 7 | fernando diaz | |
204 | 1 | fernando diaz | if __name__ == "__main__": |
205 | 1 | fernando diaz | app = App() |
206 | 1 | fernando diaz | app.mainloop() |
207 | 8 | fernando diaz | |
208 | 6 | fernando diaz | |
209 | 6 | fernando diaz | |
210 | 6 | fernando diaz | |
211 | 1 | fernando diaz | </code></pre> |