Codigo Utilizado » History » Version 9
« Previous -
Version 9/21
(diff) -
Next » -
Current version
fernando diaz, 11/07/2024 09:23 AM
Código Utilizado¶
Servidor
import socket
from robot import robot
server = socket.socket()
PORT = 8080
server.bind(('', PORT))
server.listen(1)
connection, adress = server.accept()
robot = Robot()[[]]
actions = {
"w" : robot.move_front,
"a" : robot.move_left,
"d" : robot.move_right,
"s" : robot.move_back,
"e" : robot.move_grab,
}
while True:
data = connection.recv(1)
keyword = data.decode("utf-8")
if keyword in actions:
actions[keyword]()
elif keyword == "q":
break
Interfaz Gráfica
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import socket
class App(tk.Tk):
STATUS_CONNECTION = False
PORT = 0
def __init__(self):
super().__init__()
self.title("Ball-e")
self.config(padx=10, pady=10, bg="#FFC0CB")
self.geometry("800x600")
self.canvas = tk.Canvas(self, width=800, height=600)
self.canvas.pack()
self.resizable(width=False, height=False)
self.imagen_fondo = tk.PhotoImage(file="fondo.png")
self.imagen_fondo = self.imagen_fondo.subsample(5,5)
self.canvas.create_image(0, 0, anchor=tk.NW, image=self.imagen_fondo)
self.init_images()
self.init_elements()
self.grab_open = True # Inicialmente, la garra está abierta
def init_images(self):
imagenes = ["encendido.png"]
self.images = [tk.PhotoImage(file=imagen).subsample(12, 12) for imagen in imagenes]
self.garra_abierta_img = tk.PhotoImage(file="garra_abierta.png").subsample(10, 10)
self.garra_cerrada_img = tk.PhotoImage(file="garra_cerrada.png").subsample(10, 10)
imagenes1 = ["flechaFront.png", "flechaBack.png", "flechaLeft.png", "flechaRight.png"]
self.images1 = [tk.PhotoImage(file=imagen).subsample(25, 25) for imagen in imagenes1]
self.flechaFront, self.flechaBack, self.flechaLeft, self.flechaRight= self.images1
def init_elements(self):
btn_color = "#fff" if self.STATUS_CONNECTION else "#000"
self.btn_Connect = tk.Button(self, bd=0, bg=btn_color, width=40, height=40, command=self.ip_connect).place(x=710, y=500)
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)
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)
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)
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)
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)
def ip_connect(self):
if not self.STATUS_CONNECTION:
ventana_conexion = tk.Toplevel(self)
ventana_conexion.title("Ventana de Conexión")
ventana_conexion.geometry("200x150")
ventana_conexion.resizable(width=False, height=False)
etiqueta = tk.Label(ventana_conexion, text="Ingrese la dirección IP:")
etiqueta.pack(pady=10)
self.IP = tk.StringVar()
entry_ip = tk.Entry(ventana_conexion, textvariable=self.IP)
entry_ip.pack(pady=10)
btn_conectar = tk.Button(ventana_conexion, text="Conectar", command= lambda: self.robot_connection(self.IP.get()))
btn_conectar.pack(pady=10)
else:
self.robot_connection(self.IP.get())
def robot_connection(self, IP):
if self.STATUS_CONNECTION:
self.STATUS_CONNECTION = not self.STATUS_CONNECTION
self.init_elements()
else:
try:
self.client = socket.socket()
self.client.connect((IP, self.PORT))
self.STATUS_CONNECTION = True
self.init_elements()
except socket.error:
messagebox.showerror("Error", "No se pudo conectar al robot.")
self.STATUS_CONNECTION = False
def move_front_car(self, event):
self.client.send(bytes([ord('w')]))
def move_left_car(self, event):
self.client.send(bytes([ord('a')]))
def move_right_car(self, event):
self.client.send(bytes([ord('d')]))
def move_back_car(self, event):
self.client.send(bytes([ord('s')]))
def move_grab_car(self, event):
self.client.send(bytes([ord('e')]))
if self.grab_open:
self.grab_open = False # Ahora la garra está cerrada
self.btn_Grab.config(image=self.garra_cerrada_img)
else:
self.grab_open = True # Ahora la garra está abierta
self.btn_Grab.config(image=self.garra_abierta_img)
if __name__ == "__main__":
app = App()
app.mainloop()