Project

General

Profile

Código e implementación » History » Version 4

« Previous - Version 4/17 (diff) - Next » - Current version
Bruno Amestica, 12/12/2023 08:59 AM


Código e implementación

{{collapse(Código de la interfaz gráfica)

import tkinter as tk
from tkinter import messagebox
import socket

def adelante():
    clientSocket.send(bytes([ord('w')]))

def atras():
    clientSocket.send(bytes([ord('s')]))

def derecha():
    clientSocket.send(bytes([ord('d')]))

def izquierda():
    clientSocket.send(bytes([ord('a')]))

def golpear():
    clientSocket.send(bytes([ord('r')]))

def on_release(event):
    clientSocket.send(bytes([ord(' ')]))

def abrirventana():
    ip = entry.get()
    try:
        clientSocket.connect((ip,port))
        messagebox.showinfo("Conexión exitosa","Cliente conectado al robot: {0} : {1}".format(ip,port))

        ventana.destroy()
        nueva_ventana = tk.Tk()
        nueva_ventana.title("Lego® Mindstorms EV3")
        nueva_ventana.geometry("720x405")
        nueva_ventana.resizable(width=False, height=False)
        #nueva_ventana.iconbitmap(icono)

        canvas = tk.Canvas(nueva_ventana, width=720, height=405, highlightthickness=0)
        canvas.pack()

        img = tk.PhotoImage(file="/home/proyecto1/Escritorio/interfaz/fondo2.png")
        canvas.create_image(360, 202, anchor="center", image=img)

        boton_adelante = tk.Button(nueva_ventana, text="Adelante",repeatdelay=50,repeatinterval=50, command=adelante, bg="#2e6c89", fg="white", font=("Helvetica", 10, "bold"))
        boton_adelante.bind('<ButtonRelease-1>',on_release)
        boton_adelante.place(x=361, y=120, anchor="center")

        boton_atras = tk.Button(nueva_ventana, text="Atrás",repeatdelay=50,repeatinterval=50, command=atras, bg="#2e6c89", fg="white", font=("Helvetica", 10, "bold"))
        boton_atras.bind('<ButtonRelease-1>',on_release)
        boton_atras.place(x=361, y=300, anchor="center")

        boton_izquierda = tk.Button(nueva_ventana, text="Izquierda",repeatdelay=50,repeatinterval=50, command=izquierda, bg="#2e6c89", fg="white", font=("Helvetica", 10, "bold"))
        boton_izquierda.bind('<ButtonRelease-1>',on_release)
        boton_izquierda.place(x=220, y=202, anchor="center")

        boton_derecha = tk.Button(nueva_ventana, text="Derecha",repeatdelay=50,repeatinterval=50, command=derecha, bg="#2e6c89", fg="white", font=("Helvetica", 10, "bold"))
        boton_derecha.bind('<ButtonRelease-1>',on_release)
        boton_derecha.place(x=500, y=202, anchor="center")

        boton_golpear = tk.Button(nueva_ventana, text="Golpear", command=golpear, bg="#2e6c89", fg="white", font=("Helvetica", 10, "bold"))
        boton_golpear.place(x=360, y=202, anchor="center")

        boton_salir = tk.Button(nueva_ventana, text="Salir", command=nueva_ventana.destroy, bg="#040c0c", fg="white", font=("Helvetica", 10, "bold"))
        boton_salir.place(x=650, y=360)

        nueva_ventana.mainloop()
    except socket.error:
        messagebox.showwarning("Conexión erronea","No se ha logrado al conexión, verifique la ip {0}".format(ip))
        clientSocket.close()

ventana = tk.Tk()
ventana.title("Lego® Mindstorms EV3")
ventana.geometry("720x405")
ventana.resizable(width=False, height=False)
#icono = "home/proyecto1/Escritorio/interfaz/lego.ico" 
#ventana.iconbitmap(icono)

canvas = tk.Canvas(ventana, width=720, height=405, highlightthickness=0)
canvas.pack()

img = tk.PhotoImage(file="/home/proyecto1/Escritorio/interfaz/fondo.png")
canvas.create_image(360, 202, anchor="center", image=img)

rect_width = 370
rect_height = 220
rect1 = canvas.create_rectangle(0, 0, rect_width, rect_height, fill="#0c1d25", outline="")
canvas.place(relx=0.5, rely=0.5, anchor="center")

canvas.coords(rect1, (canvas.winfo_reqwidth() - rect_width) / 2,
                        (canvas.winfo_reqheight() - rect_height) / 2,
                        (canvas.winfo_reqwidth() + rect_width) / 2,
                        (canvas.winfo_reqheight() + rect_height) / 2)

rect_width = 360
rect_height = 210
rect2 = canvas.create_rectangle(0, 0, rect_width, rect_height, fill="#01070a", outline="")
canvas.place(relx=0.5, rely=0.5, anchor="center")

canvas.coords(rect2, (canvas.winfo_reqwidth() - rect_width) / 2,
                        (canvas.winfo_reqheight() - rect_height) / 2,
                        (canvas.winfo_reqwidth() + rect_width) / 2,
                        (canvas.winfo_reqheight() + rect_height) / 2)

texto = canvas.create_text(360, 140, text="GOLFFENHEIMER®", font="Helvetica 16 bold", fill="white", anchor="center")

entry = tk.Entry(ventana, font=("Helvetica", 12), bg="#0c141b", fg="white", bd=2, relief="flat", highlightbackground="#15222e", highlightthickness=2)
entry.place(x=360, y=202.5, anchor="center")

boton = tk.Button(ventana, text="Conectar", command=abrirventana, bg="#1a3c4c", fg="white", font=("Helvetica", 10, "bold"), relief=tk.FLAT)
boton.place(x=360, y=260, anchor="center")

boton_salir = tk.Button(ventana, text="Salir", command=ventana.destroy, bg="#040c0c", fg="white", font=("Helvetica", 10, "bold"))
boton_salir.place(x=650, y=360)

clientSocket = socket.socket()
port = 4040

ventana.mainloop()

Código del server

import socket
from funciones import *

s = socket.socket()
print("Socket creado")
port = 4040
s.bind(('', port))
print("El socket se creo con puerto: {}".format(port))
s.listen(5)
print("EL socket is listening....")
connect, addr = s.accept()
print("Se conecto a  {}".format(addr))

while True:
    rawByte = connect.recv(1)
    char = rawByte.decode('utf-8')
    if (char == 'w'):
        adelante()
    if (char == 'a'):
        izquierda()
    if (char == 's'):
        atras()
    if (char == 'd'):
        derecha()
    if (char == 'r'):
        golpear()
    if (char == ' '):
        stop()
}}

Código de movilidad del robot

from ev3dev2.motor import MediumMotor, OUTPUT_A, OUTPUT_C, OUTPUT_D, MoveTank,SpeedPercent

tank_drive = MoveTank(OUTPUT_A, OUTPUT_D)
stick = MediumMotor(OUTPUT_C)

def adelante():
    print("Moving up...")
    tank_drive.on(SpeedPercent(100),SpeedPercent(100))

def atras():
    print("Moving down...")
    tank_drive.on(SpeedPercent(-100),SpeedPercent(-100))

def derecha():
    print("Moving right...")
    tank_drive.on(SpeedPercent(-100),SpeedPercent(100))

def izquierda():
    print("Moving left...")
    tank_drive.on(SpeedPercent(100),SpeedPercent(-100))

def golpear():
    stick.on_for_degrees(SpeedPercent(-100),27000)

def stop():
    tank_drive.stop()

Logo.png (715 KB) Katalina Oviedo, 12/12/2023 09:14 AM