Project

General

Profile

Codigo Utilizado » History » Version 18

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