Programación e Implementación del sistema¶
Funciones¶
from time import sleep
from ev3dev2.motor import LargeMotor, OUTPUT_A, OUTPUT_B, SpeedPercent, MoveTank
from ev3dev2.sensor import INPUT_1
from ev3dev2.sensor.lego import TouchSensor
from ev3dev2.led import Leds
from time import sleep
from ev3dev2.motor import LargeMotor, OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D, SpeedPercent, MoveTank, MediumMotor
from ev3dev2.sensor import INPUT_1
from ev3dev2.sensor.lego import TouchSensor
from ev3dev2.led import Leds
from ev3dev2.sound import Sound
TANK_DRIVE = MoveTank(OUTPUT_A, OUTPUT_B)
MOTOR_IZQUIERDO = LargeMotor(OUTPUT_A)
MOTOR_DERECHO = LargeMotor(OUTPUT_B)
MOTOR_APRETAR_GARRA = MediumMotor(OUTPUT_C)
MOTOR_SUBIR_BAJAR = LargeMotor(OUTPUT_D)
RAPIDO = SpeedPercent(100)
MEDIO = SpeedPercent(50)
LENTO = SpeedPercent(10)
def avanzar():
TANK_DRIVE.on_for_seconds(-50, -50, 1)
def retroceder():
TANK_DRIVE.on_for_seconds(50, 50, 1)
def girar_derecha():
TANK_DRIVE.on_for_seconds(-50, 50, 1)
def girar_izquierda():
TANK_DRIVE.on_for_seconds(50, -50, 1)
def hablar():
sound = Sound()
sound.speak("Hello")
Servidor¶
import socket
import library as lib
HOST = ""
PORT = 8080
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
print("Socket creado")
s.bind((HOST, PORT))
print("El socket se creo con puerto: " + str(PORT))
s.listen(4)
print("El socket esta escuchando...")
client, addr = s.accept()
print("Se conecto a " + str(addr))
while True:
data = client.recv(16)
key = data.decode()
if key == 'w':
lib.avanzar()
elif key == 'a':
lib.girar_izquierda()
elif key == 's':
lib.retroceder()
elif key == 'd':
lib.girar_derecha()
elif key == 'space':
lib.hablar()
elif key == "ARRIBA":
lib.avanzar()
elif key == "IZQUIERDA":
lib.girar_izquierda()
elif key == "DERECHA":
lib.girar_derecha()
elif key == "ABAJO":
lib.retroceder()
elif key == "EQUIS":
print(key)
elif key == "O":
print(key)
elif key == "CUADRADO":
print(key)
elif key == "TRIANGULO":
print(key)
elif key == 'q':
print('q')
break
print("Cerrando client")
client.close()
print("Cerrando socket")
s.close()
Interfaz Gráfica¶
import socket
import client
from tkinter import *
from tkinter import ttk
import multiprocessing
def mandar_input(socket_p, mensaje):
socket_p.sendall(mensaje)
return
def gui_process(queue):
def check_queue():
try:
message = queue.get_nowait()
mandar_input(socket, message.encode())
except multiprocessing.queues.Empty:
pass
ventana_principal.after(100, check_queue)
socket = client.retornar_socket()
print("Socket creado")
ventana_principal = Tk()
ventana_principal.title("EV3.")
ventana_principal.config(bg="#FF5757")
ventana_principal.geometry("800x700")
ventana_principal.resizable(0, 0)
label_movimiento_ev3 = Label(ventana_principal, text="Movimiento EV3.")
label_movimiento_ev3.place(x=10, y= 10)
ventana_principal.bind("<Key>", lambda event: mandar_input(socket, event.keysym.encode()))
ventana_principal.after(100, check_queue)
ventana_principal.mainloop()
if __name__ == "__main__":
queue = multiprocessing.Queue()
gui_process(queue)