Código e Implementación » History » Version 23
Version 22 (Cesar Jimenez, 12/04/2023 07:19 PM) → Version 23/41 (Cesar Jimenez, 12/04/2023 07:48 PM)
h1. ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤWikipedia
!logoPRO.png!
----
+*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:*
* [[Introducción]]
* [[Descripción y Objetivos]]
* [[Organización y Planificación]]
* [[Análisis y Diseño]]
* [[Código e Implementación]]
* [[Bibliografía]]
h1. ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤIntefaz Gráfica
<pre><code class="ruby">
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')]))
elif (Pow == 90):
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 (k == '.': '.'):
pts += 1
elif (k.isdigit()):
continue
else:
numero = False
if pts (pts == 3 and numero: numero):
conectar()
else:
messagebox.showwarning("Error", "Ingrese messagebox.showwarning("Error","Ingrese una dirección válida") 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
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)], command=lambda:[Power(50)], 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()
</code></pre>
h1. Servidor
<pre><code class="ruby">
import socket
from Funciones import *
# Crear un socket del server servreceptor
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) 192.168.47.147")
while True:
recept = socket_client.recv(1).decode("utf-8") socket_client.recv(1).recept.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()): (Id.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()
</code></pre>
h1. Funciones Principales del Robot
<pre><code class="ruby">
from ev3dev.ev3 import Sound
from ev3dev2.motor import LargeMotor,OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D OUTPUT_D, MoveTank
from time import sleep
import time
#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) motor_A.run_forever(speed_sp=80)
def Mov_Left():
motor_A.run_forever(speed_sp=80) 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 = LargeMotor(OUTPUT_D)
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 = LargeMotor(OUTPUT_D)
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 = LargeMotor(OUTPUT_D)
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()
def Sonido():
sound = Sound()
sound.beep()
sound.speak("Vini o' rey do futebol")
</code></pre>
!logoPRO.png!
----
+*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:*
* [[Introducción]]
* [[Descripción y Objetivos]]
* [[Organización y Planificación]]
* [[Análisis y Diseño]]
* [[Código e Implementación]]
* [[Bibliografía]]
h1. ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤIntefaz Gráfica
<pre><code class="ruby">
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')]))
elif (Pow == 90):
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 (k == '.': '.'):
pts += 1
elif (k.isdigit()):
continue
else:
numero = False
if pts (pts == 3 and numero: numero):
conectar()
else:
messagebox.showwarning("Error", "Ingrese messagebox.showwarning("Error","Ingrese una dirección válida") 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
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)], command=lambda:[Power(50)], 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()
</code></pre>
h1. Servidor
<pre><code class="ruby">
import socket
from Funciones import *
# Crear un socket del server servreceptor
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) 192.168.47.147")
while True:
recept = socket_client.recv(1).decode("utf-8") socket_client.recv(1).recept.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()): (Id.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()
</code></pre>
h1. Funciones Principales del Robot
<pre><code class="ruby">
from ev3dev.ev3 import Sound
from ev3dev2.motor import LargeMotor,OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D OUTPUT_D, MoveTank
from time import sleep
import time
#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) motor_A.run_forever(speed_sp=80)
def Mov_Left():
motor_A.run_forever(speed_sp=80) 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 = LargeMotor(OUTPUT_D)
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 = LargeMotor(OUTPUT_D)
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 = LargeMotor(OUTPUT_D)
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()
def Sonido():
sound = Sound()
sound.beep()
sound.speak("Vini o' rey do futebol")
</code></pre>