Project

General

Profile

Código e Implementación » History » Version 39

« Previous - Version 39/41 (diff) - Next » - Current version
Cesar Jimenez, 12/12/2023 10:17 PM


ㅤㅤㅤㅤㅤㅤㅤㅤ⠀⠀⠀Wikipedia


Grupo A:

Integrantes: Cristian Sánchez, Jean Pier Duran, Sergio Rabanal, Valentina Álvarez, César Jiménez
Nombre del Producto: Machine EV3 G
Modulo: Proyecto I
Docente: Humberto Urrutia
Fecha de Inicio: 2023-08-14
Fecha de Termino: 2023-11-30


ㅤㅤㅤㅤㅤㅤㅤ⠀⠀⠀⠀⠀⠀⠀⠀Índice:

ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤIntefaz Gráfica


import tkinter as tk
from tkinter import ttk
from tkinter import *
from tkinter import messagebox
from tkinter import PhotoImage
import socket
conection = False

def stop(Event):
    try:
        server.send(bytes([ord(' ')]))
    except BrokenPipeError:
        messagebox.showwarning("Lo siento","Intentelo Nuevamente")

def Up(Event):
    try:
        server.send(bytes([ord('w')]))
    except BrokenPipeError:
        messagebox.showwarning("Lo siento","Intentelo Nuevamente")

def Down(Event):
    try:
        server.send(bytes([ord('s')]))
    except BrokenPipeError:
        messagebox.showwarning("Lo siento","Intentelo Nuevamente")

def Left(Event):
    try:
        server.send(bytes([ord('a')]))
    except BrokenPipeError:
        messagebox.showwarning("Lo siento","Intentelo Nuevamente")

def Right(Event):
    try:
        server.send(bytes([ord('d')]))
    except BrokenPipeError:
        messagebox.showwarning("Lo siento","Intentelo Nuevamente")

def Shoot():
    try: 
        if (angle.get() == 45):
            server.send(bytes([ord('1')]))
        elif (angle.get() == 60):
            server.send(bytes([ord('2')]))
        elif (angle.get() == 90):
            server.send(bytes([ord('3')]))

    except BrokenPipeError:
        messagebox.showwarning("Lo siento","Intentelo Nuevamente")

def Disconnect():
    if conection:
        server.send(bytes([ord('z')]))
        ventana.destroy()
    else:
        ventana.destroy()

def Power(value):
    if (value == 45):
        angle.set(value)
    elif (value == 60):
        angle.set(value)
    elif (value == 90):
        angle.set(value)
    else:
        messagebox.showwarning("¡Error!","Ángulo no asignado")

def Validar():
    pts = 0
    numero = True
    for k in ip.get():
        if (k == '.'):
            pts += 1
        elif (k.isdigit()):
            continue
        else:
            numero = False
    if (pts == 3 and numero):
        conectar()
    else:
        messagebox.showwarning("Error","Ingrese una dirección valida")
    ip.set("")

def conectar():
    puerto = 4999
    direccion = ip.get()
    try: 
        server.connect((direccion,puerto))
        messagebox.showinfo("Conectado","Se ha establecido la conexion exitosamente")
        conection = True
        entrada_IP.config(state=DISABLED)
    except BrokenPipeError:
        messagebox.showwarning("Desconectado","Ingrese la dirección correcta")
        conection = False
    ip.set("")

# Ventana principal
ventana = tk.Tk()
ventana.geometry("750x500") 
ventana.title("Machine EV3")
ventana.configure(bg="light sea green")  
ventana.resizable(0,0)

ip = StringVar()
angle = IntVar()
angle.set(0)

# Ingresar Imagenes
up = PhotoImage(file="up.png")
down = PhotoImage(file="down.png")
left = PhotoImage(file="left.png")
right = PhotoImage(file="right.png")
cross = PhotoImage(file="cross.png")

# Dimensiones de las Imagenes
up = up.subsample(6, 6)
down = down.subsample(6, 6)
left = left.subsample(6, 6)
right = right.subsample(6, 6)
cross = cross.subsample(6, 6)

# Iniciar Servidor
server = socket.socket()

# Boton Arriba
boton_arriba = Button(None,text="UP", command= lambda:[Up(None)], background="cadetblue2", image=up)
boton_arriba.place(x=150, y=105)

ventana.bind('<KeyPress-w>',Up)
ventana.bind('<KeyRelease-w>',stop)

# Boton Derecha
boton_derech = Button(None, repeatdelay=20,repeatinterval=20 ,text="RIGHT", command= lambda:[Right(None)], background="cadetblue2", image=right)
boton_derech.place(x=260, y=215)

ventana.bind('<KeyPress-d>',Right)
ventana.bind('<KeyRelease-d>',stop)

# Boton Izquierda
boton_izq = Button(None, repeatdelay=20,repeatinterval=20, text="LEFT", command= lambda:[Left(None)], background="cadetblue2", image=left)
boton_izq.place(x=40, y=215)

ventana.bind('<KeyPress-a>',Left)
ventana.bind('<KeyRelease-a>',stop)

# Boton Abajo
boton_abajo = Button(None, repeatdelay=20, repeatinterval=20,text="DOWN", command= lambda:[Down(None)], background="cadetblue2", image=down)
boton_abajo.place(x=150, y=325)

ventana.bind('<KeyPress-s>',Down)
ventana.bind('<KeyRelease-s>',stop)

# Boton Disparar
boton_disparar = Button(text="SHOT", command = Shoot, background="cadetblue2", image=cross)
boton_disparar.place(x=515, y=270)

# Boton Power de 45°
boton_p45 = Button(text="Angle 45%", command=lambda:Power(45), background="cadetblue2",fg = "DodgerBlue4")
boton_p45.config(width=7, height=4)
boton_p45.place(x=410, y=220)

# Boton Power de 60°
boton_p60 = Button(text="Angle 60%", command=lambda:Power(60), background="cadetblue2",fg = "DodgerBlue4")
boton_p60.config(width=7, height=4)
boton_p60.place(x=520, y=160)

# Boton potenca de 90°
boton_p90 = Button(text="Angle 90%", command=lambda:Power(90), background="cadetblue2",fg = "DodgerBlue4")
boton_p90.config(width=7, height=4)
boton_p90.place(x=630, y=220)

# Desconectar Brick
boton_exit = Button(text="Shutdown",command = Disconnect,background="cadetblue2",fg = "DodgerBlue4")
boton_exit.config(width=9, height=1)
boton_exit.place(x=620, y=20)

#Ingreso ip
entrada_IP = Entry(ventana,textvariable = ip, width = 18)
entrada_IP.focus_set()
entrada_IP.place(x = 320, y=25)

# Conexión
boton_conectar = Button(text="Conect",background="cadetblue2",fg = "DodgerBlue4",command = Validar)
boton_conectar.config(width=9, height=1)
boton_conectar.place(x=490, y=20)

# Detener Motores
boton_arriba.bind('<ButtonRelease-1>',stop)
boton_abajo.bind('<ButtonRelease-1>',stop)
boton_izq.bind('<ButtonRelease-1>',stop)
boton_derech.bind('<ButtonRelease-1>',stop)

ventana.mainloop()

⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ㅤㅤㅤㅤㅤ⠀⠀Servidor


import socket
from Funciones import *

# Crear un socket del server receptor
server = socket.socket()

# Vincular el socket a una dirección IP y un puerto
server.bind(('',4999))
print("CONECTADO")

# Poner el socket en modo de escucha
server.listen(5)  # Permite hasta 5 conexiones pendientes
print("Esperando conexiones entrantes...")

# Aceptar una conexión entrante
socket_client, address_client = server.accept()
print("Conexion entrante desde ",address_client)

while True:
    recept = socket_client.recv(1).decode("utf-8")
    if (recept == 'w'): # arriba
        Mov_Up()
    if (recept == 's'): # abajo
        Mov_Down()
    if (recept == 'a'): # izquierda
        Mov_Left()
    if (recept == 'd'): # derecha
        Mov_Right()
    if (recept.isdigit()): # disparo por angulo 
        if (recept == '1'):
            Angle45()
        elif (recept == '2'):
            Angle60()
        elif (recept == '3'):
            Angle90()
    if (recept == 'z'): # Cierre Ventana
        break
    if (recept == ' '): # Detener Movimiento
        Stop_Motor()

⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ㅤ⠀⠀⠀ㅤ⠀Funciones Principales del Robot


from ev3dev2.motor import LargeMotor,OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D

#Agregar Funciones del ROBOT
rueda_left = LargeMotor(OUTPUT_C)
rueda_right = LargeMotor(OUTPUT_B)
motor_A = LargeMotor(OUTPUT_A)
motor_D = LargeMotor(OUTPUT_D)

def Mov_Right():
    motor_A.run_forever(speed_sp=-80)

def Mov_Left():
    motor_A.run_forever(speed_sp=80)

def Mov_Down():
    rueda_left.run_forever(speed_sp=500)
    rueda_right.run_forever(speed_sp=500)

def Mov_Up():
    rueda_left.run_forever(speed_sp=-500)
    rueda_right.run_forever(speed_sp=-500)

def Stop_Motor():
    rueda_left.stop()
    rueda_right.stop()
    motor_A.stop()

def Angle45():
    motor_D.on_for_degrees(speed=10, degrees = 45)
    motor_D.on_for_degrees(speed=100, degrees = -105)
    motor_D.on_for_degrees(speed=10, degrees = 60)
    motor_D.off()

def Angle60():
    motor_D.on_for_degrees(speed=10, degrees = 60)
    motor_D.on_for_degrees(speed=100, degrees = -120)
    motor_D.on_for_degrees(speed=10, degrees = 60)
    motor_D.off()

def Angle90():
    motor_D.on_for_degrees(speed=10, degrees = 90)
    motor_D.on_for_degrees(speed=100, degrees = -150)
    motor_D.on_for_degrees(speed=10, degrees = 60)
    motor_D.off()

logoPRO.png (91.7 KB) Cesar Jimenez, 09/17/2023 12:15 AM