Project

General

Profile

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

Version 8 (Vranika Santiago, 12/04/2022 06:59 PM) → Version 9/18 (Vranika Santiago, 12/14/2022 03:22 AM)

*Índice:*
* [[Introducción]]
* [[Organización y planificación]]
* [[Análisis y diseño]]
* [[Código e implementación]]

---

h1. Código e implementación

---

{{collapse(Interfaz gráfica (GUI))

}}

{{collapse(Servidor)
<pre><code class="python">
import socket
from Function import *
s = socket.socket()
print("Socket creado")
port = 19999
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'):
moveUp()
if (char == 's'):
moveDown()
if (char == 'd'):
moveRight()
if (char == 'a'):
moveLeft()
if (char == 'p'):
Disparar()
if (char == 'f'):
Luciano()
if (char == 'i'):
Inclinacion45()
if (char == 'o'):
Inclinacion0()
if (char == ' '):
Stop()
if (char == 'm'):
Distancia()
if (char == 'l'):
StopCannon()
</code></pre>
}}

{{collapse(Movimientos del robot)
<pre><code class="python">
#!/usr/bin/env python3
from ev3dev2.motor import LargeMotor, MediumMotor, OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D, SpeedPercent, MoveTank
from ev3dev2.sensor import INPUT_1
from ev3dev2.sensor .lego import UltrasonicSensor
from ev3dev2.sound import Sound
from ev3dev2.led import Leds

motor_angle= LargeMotor(OUTPUT_A)
cannon = MediumMotor(OUTPUT_D)
sonido = Sound()
sensor = UltrasonicSensor(INPUT_1)
sensor.mode = "US-DIST-CM"
tankmoves = MoveTank(OUTPUT_B,OUTPUT_C)
print(sensor.value()/10)

def moveUp():
print("Moving up...")
tankmoves.on(SpeedPercent(100),SpeedPercent(100))
def moveDown():
print("Moving down...")
tankmoves.on(SpeedPercent(-100),SpeedPercent(-100))
def moveRight():
print("Moving right...")
tankmoves.on(SpeedPercent(100),SpeedPercent(-100))
def moveLeft():
print("Moving left...")
tankmoves.on(SpeedPercent(-100),SpeedPercent(100))
def Inclinacion0():
motor_angle.on_for_degrees(7,55,brake=True)
def Inclinacion45():
motor_angle.on_for_degrees(7,-55,brake=True)

def Disparar():
cannon.on(SpeedPercent(-100))
def Apuntar():
sensor.value #valor del sensor en milimetro

def Luciano():
sonido.speak("gorila tank mak two")
def Distancia():

while(sensor.value()/10 <= 100 or sensor.value()/10 >= 110):
print(sensor.value()/10)
if(sensor.value()/10 < 100):
tankmoves.on(SpeedPercent(-40),SpeedPercent(-40))

else:
tankmoves.on(SpeedPercent(40),SpeedPercent(40))
Stop()

def Stop():
tankmoves.stop()

def StopCannon():
cannon.stop()
sonido.play_file("misioncumplida.wav",100,1)

</code></pre>
}}