Project

General

Profile

Codigo Utilizado » History » Version 21

andre guerra, 12/13/2024 12:37 AM

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