Project

General

Profile

Codigo Utilizado » History » Version 14

fernando diaz, 11/18/2024 04:47 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
    
64 13 fernando diaz
def move_front():
65 1 fernando diaz
        robot.straight(150)
66 14 fernando diaz
                
67 13 fernando diaz
def move_back():
68 1 fernando diaz
        robot.straight(-150)
69 14 fernando diaz
                
70 13 fernando diaz
def move_right():
71 13 fernando diaz
        robot.turn(300)
72 14 fernando diaz
              
73 13 fernando diaz
def move_left():
74 13 fernando diaz
        robot.turn(-300)
75 14 fernando diaz
              
76 13 fernando diaz
def move_grab():
77 13 fernando diaz
    robot.run_until_stalled(-300, Stop.HOLD, 40)
78 14 fernando diaz
    motor_garra.run_until_stalled(300, Stop.COAST, 40)
79 14 fernando diaz
    
80 13 fernando diaz
81 13 fernando diaz
# Detener el robot
82 13 fernando diaz
robot.stop()
83 13 fernando diaz
84 13 fernando diaz
ev3.speaker.beep()
85 13 fernando diaz
</code></pre>
86 5 fernando diaz
*Interfaz Gráfica*
87 2 fernando diaz
88 2 fernando diaz
<pre><code class="python">
89 1 fernando diaz
import tkinter as tk
90 6 fernando diaz
from tkinter import ttk
91 1 fernando diaz
from tkinter import messagebox
92 1 fernando diaz
import socket
93 1 fernando diaz
94 1 fernando diaz
class App(tk.Tk):
95 1 fernando diaz
    STATUS_CONNECTION = False
96 10 fernando diaz
    PORT = 8080
97 8 fernando diaz
98 1 fernando diaz
    def __init__(self):
99 1 fernando diaz
        super().__init__()
100 1 fernando diaz
        self.title("Ball-e")
101 1 fernando diaz
        self.config(padx=10, pady=10, bg="#FFC0CB")
102 1 fernando diaz
        self.geometry("800x600")
103 1 fernando diaz
        self.canvas = tk.Canvas(self, width=800, height=600)
104 1 fernando diaz
        self.canvas.pack()
105 1 fernando diaz
        self.resizable(width=False, height=False)
106 6 fernando diaz
        self.imagen_fondo = tk.PhotoImage(file="fondo.png")
107 6 fernando diaz
        self.imagen_fondo = self.imagen_fondo.subsample(5,5)
108 6 fernando diaz
        self.canvas.create_image(0, 0, anchor=tk.NW, image=self.imagen_fondo)
109 6 fernando diaz
        self.init_images()
110 1 fernando diaz
        self.init_elements()
111 7 fernando diaz
        self.grab_open = True  # Inicialmente, la garra está abierta
112 8 fernando diaz
113 6 fernando diaz
    def init_images(self):
114 6 fernando diaz
        imagenes = ["encendido.png"]
115 6 fernando diaz
        self.images = [tk.PhotoImage(file=imagen).subsample(12, 12) for imagen in imagenes]
116 6 fernando diaz
        
117 7 fernando diaz
        self.garra_abierta_img = tk.PhotoImage(file="garra_abierta.png").subsample(10, 10)  
118 7 fernando diaz
        self.garra_cerrada_img = tk.PhotoImage(file="garra_cerrada.png").subsample(10, 10)
119 7 fernando diaz
120 1 fernando diaz
        imagenes1 = ["flechaFront.png", "flechaBack.png", "flechaLeft.png", "flechaRight.png"]
121 1 fernando diaz
        self.images1 = [tk.PhotoImage(file=imagen).subsample(25, 25) for imagen in imagenes1]
122 8 fernando diaz
        self.flechaFront, self.flechaBack, self.flechaLeft, self.flechaRight= self.images1
123 8 fernando diaz
        
124 1 fernando diaz
    def init_elements(self):
125 8 fernando diaz
        btn_color = "#fff" if self.STATUS_CONNECTION else "#000" 
126 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)
127 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)
128 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)
129 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)
130 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)
131 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)
132 8 fernando diaz
        
133 2 fernando diaz
    def ip_connect(self):
134 1 fernando diaz
        if not self.STATUS_CONNECTION:
135 1 fernando diaz
            ventana_conexion = tk.Toplevel(self)
136 1 fernando diaz
            ventana_conexion.title("Ventana de Conexión")
137 1 fernando diaz
            ventana_conexion.geometry("200x150")
138 1 fernando diaz
            ventana_conexion.resizable(width=False, height=False)
139 1 fernando diaz
            etiqueta = tk.Label(ventana_conexion, text="Ingrese la dirección IP:")
140 1 fernando diaz
            etiqueta.pack(pady=10)
141 1 fernando diaz
            self.IP = tk.StringVar()
142 1 fernando diaz
            entry_ip = tk.Entry(ventana_conexion, textvariable=self.IP)
143 1 fernando diaz
            entry_ip.pack(pady=10)
144 1 fernando diaz
            btn_conectar = tk.Button(ventana_conexion, text="Conectar", command= lambda: self.robot_connection(self.IP.get()))
145 1 fernando diaz
            btn_conectar.pack(pady=10)
146 1 fernando diaz
        else:
147 1 fernando diaz
            self.robot_connection(self.IP.get())
148 1 fernando diaz
149 1 fernando diaz
    def robot_connection(self, IP):
150 1 fernando diaz
        if self.STATUS_CONNECTION:
151 3 fernando diaz
            self.STATUS_CONNECTION = not self.STATUS_CONNECTION
152 1 fernando diaz
            self.init_elements()  
153 1 fernando diaz
        else:
154 1 fernando diaz
            try:
155 1 fernando diaz
                self.client = socket.socket()
156 1 fernando diaz
                self.client.connect((IP, self.PORT))  
157 1 fernando diaz
                self.STATUS_CONNECTION = True
158 12 fernando diaz
                self.init_elements()
159 12 fernando diaz
                messagebox.showinfo("Conexión exitosa", "Se ha conectado al robot correctamente.")  
160 12 fernando diaz
                self.window_ip.destroy()
161 1 fernando diaz
            except socket.error:
162 1 fernando diaz
                messagebox.showerror("Error", "No se pudo conectar al robot.")
163 1 fernando diaz
                self.STATUS_CONNECTION = False
164 8 fernando diaz
165 1 fernando diaz
    def move_front_car(self, event):
166 1 fernando diaz
        self.client.send(bytes([ord('w')]))
167 8 fernando diaz
168 1 fernando diaz
    def move_left_car(self, event):
169 1 fernando diaz
        self.client.send(bytes([ord('a')]))
170 8 fernando diaz
171 1 fernando diaz
    def move_right_car(self, event):
172 1 fernando diaz
        self.client.send(bytes([ord('d')]))
173 8 fernando diaz
174 1 fernando diaz
    def move_back_car(self, event):
175 1 fernando diaz
        self.client.send(bytes([ord('s')]))
176 8 fernando diaz
        
177 7 fernando diaz
    def move_grab_car(self, event):
178 7 fernando diaz
        self.client.send(bytes([ord('e')])) 
179 7 fernando diaz
        if self.grab_open:
180 7 fernando diaz
            self.grab_open = False  # Ahora la garra está cerrada
181 7 fernando diaz
            self.btn_Grab.config(image=self.garra_cerrada_img)
182 7 fernando diaz
        else:
183 7 fernando diaz
            self.grab_open = True  # Ahora la garra está abierta
184 8 fernando diaz
            self.btn_Grab.config(image=self.garra_abierta_img)
185 7 fernando diaz
186 1 fernando diaz
if __name__ == "__main__":
187 1 fernando diaz
    app = App()
188 1 fernando diaz
    app.mainloop()
189 8 fernando diaz
190 6 fernando diaz
191 6 fernando diaz
192 6 fernando diaz
193 1 fernando diaz
</code></pre>