Project

General

Profile

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

« Previous - Version 24/41 (diff) - Next » - Current version
Cesar Jimenez, 12/12/2023 03:00 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():
    try:
        server.send(bytes([ord('w')]))
    except BrokenPipeError:
        messagebox.showwarning("Lo siento","Intentelo Nuevamente")

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

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

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

def Power(Pow):
    if (Pow == 45):
        server.send(bytes([ord('1')]))
    elif (Pow == 60):
        server.send(bytes([ord('2')]))
    else:
        server.send(bytes([ord('3')]))

def Validar():
    pts = 0
    numero = True
    for k in ip.get():
        if not k.isdigit():
            numero = False
        elif k == '.':
            pts += 1
    if pts == 3 and numero:
        conectar()
    else:
        messagebox.showwarning("Error", "Ingrese una dirección válida")
    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
    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()

# 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, repeatdelay=20,repeatinterval=20,text="UP", command=Up, background="cadetblue2", image=up)
boton_arriba.place(x=150, y=105)

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

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

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

# Boton Disparar
boton_disparar = Button(text="SHOT", command=lambda:[Power(90)], background="cadetblue2", image=cross)
boton_disparar.place(x=515, y=270)

# Boton Power de 30°
boton_p30 = Button(text="Angle 45%", command=lambda:[Power(45)], background="cadetblue2",fg = "DodgerBlue4")
boton_p30.config(width=7, height=4)
boton_p30.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_p100 = Button(text="Angle 90%", command=lambda:[Power(90)], background="cadetblue2",fg = "DodgerBlue4")
boton_p100.config(width=7, height=4)
boton_p100.place(x=630, y=220)

# Desconectar Brick
boton_exit = Button(text="Disconnect",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
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