Project

General

Profile

Codigo Utilizado » History » Version 9

fernando diaz, 11/07/2024 09:23 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 9 fernando diaz
from robot import robot
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 9 fernando diaz
robot = Robot()[[]]
22 9 fernando diaz
23 9 fernando diaz
actions = {
24 9 fernando diaz
    "w" : robot.move_front,
25 9 fernando diaz
    "a" : robot.move_left,
26 9 fernando diaz
    "d" : robot.move_right,
27 9 fernando diaz
    "s" : robot.move_back,
28 9 fernando diaz
    "e" : robot.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 5 fernando diaz
*Interfaz Gráfica*
43 2 fernando diaz
44 2 fernando diaz
<pre><code class="python">
45 1 fernando diaz
import tkinter as tk
46 6 fernando diaz
from tkinter import ttk
47 1 fernando diaz
from tkinter import messagebox
48 1 fernando diaz
import socket
49 1 fernando diaz
50 1 fernando diaz
class App(tk.Tk):
51 1 fernando diaz
    STATUS_CONNECTION = False
52 1 fernando diaz
    PORT = 0
53 8 fernando diaz
54 1 fernando diaz
    def __init__(self):
55 1 fernando diaz
        super().__init__()
56 1 fernando diaz
        self.title("Ball-e")
57 1 fernando diaz
        self.config(padx=10, pady=10, bg="#FFC0CB")
58 1 fernando diaz
        self.geometry("800x600")
59 1 fernando diaz
        self.canvas = tk.Canvas(self, width=800, height=600)
60 1 fernando diaz
        self.canvas.pack()
61 1 fernando diaz
        self.resizable(width=False, height=False)
62 6 fernando diaz
        self.imagen_fondo = tk.PhotoImage(file="fondo.png")
63 6 fernando diaz
        self.imagen_fondo = self.imagen_fondo.subsample(5,5)
64 6 fernando diaz
        self.canvas.create_image(0, 0, anchor=tk.NW, image=self.imagen_fondo)
65 6 fernando diaz
        self.init_images()
66 1 fernando diaz
        self.init_elements()
67 7 fernando diaz
        self.grab_open = True  # Inicialmente, la garra está abierta
68 8 fernando diaz
69 6 fernando diaz
    def init_images(self):
70 6 fernando diaz
        imagenes = ["encendido.png"]
71 6 fernando diaz
        self.images = [tk.PhotoImage(file=imagen).subsample(12, 12) for imagen in imagenes]
72 6 fernando diaz
        
73 7 fernando diaz
        self.garra_abierta_img = tk.PhotoImage(file="garra_abierta.png").subsample(10, 10)  
74 7 fernando diaz
        self.garra_cerrada_img = tk.PhotoImage(file="garra_cerrada.png").subsample(10, 10)
75 7 fernando diaz
76 1 fernando diaz
        imagenes1 = ["flechaFront.png", "flechaBack.png", "flechaLeft.png", "flechaRight.png"]
77 1 fernando diaz
        self.images1 = [tk.PhotoImage(file=imagen).subsample(25, 25) for imagen in imagenes1]
78 8 fernando diaz
        self.flechaFront, self.flechaBack, self.flechaLeft, self.flechaRight= self.images1
79 8 fernando diaz
        
80 1 fernando diaz
    def init_elements(self):
81 8 fernando diaz
        btn_color = "#fff" if self.STATUS_CONNECTION else "#000" 
82 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)
83 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)
84 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)
85 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)
86 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)
87 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)
88 8 fernando diaz
        
89 2 fernando diaz
    def ip_connect(self):
90 1 fernando diaz
        if not self.STATUS_CONNECTION:
91 1 fernando diaz
            ventana_conexion = tk.Toplevel(self)
92 1 fernando diaz
            ventana_conexion.title("Ventana de Conexión")
93 1 fernando diaz
            ventana_conexion.geometry("200x150")
94 1 fernando diaz
            ventana_conexion.resizable(width=False, height=False)
95 1 fernando diaz
            etiqueta = tk.Label(ventana_conexion, text="Ingrese la dirección IP:")
96 1 fernando diaz
            etiqueta.pack(pady=10)
97 1 fernando diaz
            self.IP = tk.StringVar()
98 1 fernando diaz
            entry_ip = tk.Entry(ventana_conexion, textvariable=self.IP)
99 1 fernando diaz
            entry_ip.pack(pady=10)
100 1 fernando diaz
            btn_conectar = tk.Button(ventana_conexion, text="Conectar", command= lambda: self.robot_connection(self.IP.get()))
101 1 fernando diaz
            btn_conectar.pack(pady=10)
102 1 fernando diaz
        else:
103 1 fernando diaz
            self.robot_connection(self.IP.get())
104 1 fernando diaz
105 1 fernando diaz
    def robot_connection(self, IP):
106 1 fernando diaz
        if self.STATUS_CONNECTION:
107 3 fernando diaz
            self.STATUS_CONNECTION = not self.STATUS_CONNECTION
108 1 fernando diaz
            self.init_elements()  
109 1 fernando diaz
        else:
110 1 fernando diaz
            try:
111 1 fernando diaz
                self.client = socket.socket()
112 1 fernando diaz
                self.client.connect((IP, self.PORT))  
113 1 fernando diaz
                self.STATUS_CONNECTION = True
114 1 fernando diaz
                self.init_elements()  
115 1 fernando diaz
            except socket.error:
116 1 fernando diaz
                messagebox.showerror("Error", "No se pudo conectar al robot.")
117 1 fernando diaz
                self.STATUS_CONNECTION = False
118 8 fernando diaz
119 1 fernando diaz
    def move_front_car(self, event):
120 1 fernando diaz
        self.client.send(bytes([ord('w')]))
121 8 fernando diaz
122 1 fernando diaz
    def move_left_car(self, event):
123 1 fernando diaz
        self.client.send(bytes([ord('a')]))
124 8 fernando diaz
125 1 fernando diaz
    def move_right_car(self, event):
126 1 fernando diaz
        self.client.send(bytes([ord('d')]))
127 8 fernando diaz
128 1 fernando diaz
    def move_back_car(self, event):
129 1 fernando diaz
        self.client.send(bytes([ord('s')]))
130 8 fernando diaz
        
131 7 fernando diaz
    def move_grab_car(self, event):
132 7 fernando diaz
        self.client.send(bytes([ord('e')])) 
133 7 fernando diaz
        if self.grab_open:
134 7 fernando diaz
            self.grab_open = False  # Ahora la garra está cerrada
135 7 fernando diaz
            self.btn_Grab.config(image=self.garra_cerrada_img)
136 7 fernando diaz
        else:
137 7 fernando diaz
            self.grab_open = True  # Ahora la garra está abierta
138 8 fernando diaz
            self.btn_Grab.config(image=self.garra_abierta_img)
139 7 fernando diaz
140 1 fernando diaz
if __name__ == "__main__":
141 1 fernando diaz
    app = App()
142 1 fernando diaz
    app.mainloop()
143 8 fernando diaz
144 6 fernando diaz
145 6 fernando diaz
146 6 fernando diaz
147 1 fernando diaz
</code></pre>