Project

General

Profile

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

Vranika Santiago, 12/14/2022 03:38 AM

1 1 Vranika Santiago
*Índice:* 
2 1 Vranika Santiago
* [[Introducción]]
3 1 Vranika Santiago
* [[Organización y planificación]]
4 1 Vranika Santiago
* [[Análisis y diseño]]
5 1 Vranika Santiago
* [[Código e implementación]]
6 1 Vranika Santiago
7 1 Vranika Santiago
---
8 1 Vranika Santiago
9 1 Vranika Santiago
h1. Código e implementación
10 2 Vranika Santiago
11 2 Vranika Santiago
---
12 9 Vranika Santiago
13 15 Vranika Santiago
h2. Interfaz gráfica (GUI)
14 15 Vranika Santiago
15 14 Vranika Santiago
{{collapse(Ver código)
16 11 Vranika Santiago
<pre><code class="python">
17 10 Vranika Santiago
import tkinter as tk
18 10 Vranika Santiago
from tkinter import ttk
19 10 Vranika Santiago
from tkinter import *
20 10 Vranika Santiago
import socket 
21 10 Vranika Santiago
22 10 Vranika Santiago
ventana = tk.Tk()
23 10 Vranika Santiago
ventana.config(width=900, height=700)
24 10 Vranika Santiago
ventana.title("Interfaz -> Gorilla-Tank")
25 10 Vranika Santiago
ventana.configure(bg="gray25")
26 10 Vranika Santiago
27 10 Vranika Santiago
def Arriba(event= None):
28 10 Vranika Santiago
    clientSocket.send(bytes([ord('w')]))
29 10 Vranika Santiago
    
30 10 Vranika Santiago
def Abajo(event=None):
31 10 Vranika Santiago
    clientSocket.send(bytes([ord('s')]))
32 10 Vranika Santiago
    
33 10 Vranika Santiago
def Izquierda(event=None):
34 10 Vranika Santiago
    clientSocket.send(bytes([ord('a')]))
35 10 Vranika Santiago
36 10 Vranika Santiago
def Derecha(event=None):
37 10 Vranika Santiago
    clientSocket.send(bytes([ord('d')]))
38 10 Vranika Santiago
39 10 Vranika Santiago
def Disparar(event=None):
40 10 Vranika Santiago
    clientSocket.send(bytes([ord('p')]))
41 10 Vranika Santiago
42 10 Vranika Santiago
def Angulo():
43 10 Vranika Santiago
    if(opcion.get()=="45"):
44 10 Vranika Santiago
        clientSocket.send(bytes([ord('i')]))
45 10 Vranika Santiago
    elif(opcion.get() == "0"):
46 10 Vranika Santiago
        clientSocket.send(bytes([ord('o')]))
47 10 Vranika Santiago
48 10 Vranika Santiago
def Luciano():
49 10 Vranika Santiago
    clientSocket.send(bytes([ord('f')]))
50 10 Vranika Santiago
    
51 10 Vranika Santiago
def Distancia():
52 10 Vranika Santiago
    clientSocket.send(bytes([ord('m')]))
53 10 Vranika Santiago
def on_realese(event):
54 10 Vranika Santiago
    print("click realese")
55 10 Vranika Santiago
    clientSocket.send(bytes([ord(' ')]))
56 1 Vranika Santiago
def Cannon(event):
57 1 Vranika Santiago
    clientSocket.send(bytes([ord('l')]))
58 10 Vranika Santiago
59 1 Vranika Santiago
def getAddres():
60 11 Vranika Santiago
    w_ip = Tk()
61 11 Vranika Santiago
    w_ip.geometry("300x100")
62 10 Vranika Santiago
63 11 Vranika Santiago
    dato = StringVar(w_ip)
64 11 Vranika Santiago
    w_ip.title("Configurar Ip")
65 11 Vranika Santiago
    ip = ttk.Entry(w_ip,textvariable=dato).place(x=10,y=10)
66 11 Vranika Santiago
    button = Button(w_ip,text =" Aplicar",command=lambda:[conectar(dato.get()),w_ip.destroy()]).place(x=170,y=9)
67 11 Vranika Santiago
    print(dato.get())
68 1 Vranika Santiago
69 1 Vranika Santiago
def conectar(adress):
70 11 Vranika Santiago
    port = 19999
71 11 Vranika Santiago
    try:
72 11 Vranika Santiago
        clientSocket.connect((adress,port))
73 11 Vranika Santiago
        messagebox.showinfo("Mensaje Servido","Cliente conectado al robot: {0} : {1}".format(adress,port))
74 11 Vranika Santiago
    except socket.error:
75 11 Vranika Santiago
        messagebox.showwarning("Conexión erronea","No se ha logrado al conexíon, verifique la Ip {0}".format(adress))
76 11 Vranika Santiago
        getAddres()
77 11 Vranika Santiago
        clientSocket.close()
78 10 Vranika Santiago
ventana.mainloop()
79 11 Vranika Santiago
</code></pre>
80 9 Vranika Santiago
}}
81 9 Vranika Santiago
82 14 Vranika Santiago
{{collapse(Ver código)
83 9 Vranika Santiago
<pre><code class="python">
84 9 Vranika Santiago
import socket
85 9 Vranika Santiago
from Function import *
86 9 Vranika Santiago
s = socket.socket()
87 9 Vranika Santiago
print("Socket creado")
88 9 Vranika Santiago
port = 19999
89 9 Vranika Santiago
s.bind( ("", port) )
90 9 Vranika Santiago
print("El socket se creo con puerto:{}".format(port))
91 9 Vranika Santiago
s.listen(5)
92 9 Vranika Santiago
print("EL socket is listening....")
93 9 Vranika Santiago
connect, addr = s.accept()
94 9 Vranika Santiago
print("Se conecto a {}".format(addr))
95 9 Vranika Santiago
while True:
96 9 Vranika Santiago
    rawByte = connect.recv(1)
97 9 Vranika Santiago
    char = rawByte.decode('utf-8')
98 9 Vranika Santiago
    if (char == 'w'):
99 9 Vranika Santiago
        moveUp()
100 9 Vranika Santiago
    if (char == 's'):
101 9 Vranika Santiago
        moveDown()
102 9 Vranika Santiago
    if (char == 'd'):
103 9 Vranika Santiago
        moveRight()
104 9 Vranika Santiago
    if (char == 'a'):
105 9 Vranika Santiago
        moveLeft()
106 9 Vranika Santiago
    if (char == 'p'):
107 9 Vranika Santiago
        Disparar()
108 9 Vranika Santiago
    if (char == 'f'):
109 9 Vranika Santiago
        Luciano()  
110 9 Vranika Santiago
    if (char == 'i'):
111 9 Vranika Santiago
        Inclinacion45()
112 9 Vranika Santiago
    if (char == 'o'):
113 9 Vranika Santiago
        Inclinacion0()
114 9 Vranika Santiago
    if (char == ' '):
115 9 Vranika Santiago
        Stop()
116 9 Vranika Santiago
    if (char == 'm'):
117 9 Vranika Santiago
        Distancia()
118 9 Vranika Santiago
    if (char == 'l'):
119 9 Vranika Santiago
        StopCannon()
120 9 Vranika Santiago
</code></pre>
121 9 Vranika Santiago
}}
122 9 Vranika Santiago
123 14 Vranika Santiago
{{collapse(Ver código)
124 9 Vranika Santiago
<pre><code class="python">
125 9 Vranika Santiago
#!/usr/bin/env python3
126 9 Vranika Santiago
from ev3dev2.motor import LargeMotor, MediumMotor, OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D, SpeedPercent, MoveTank
127 9 Vranika Santiago
from ev3dev2.sensor import INPUT_1
128 9 Vranika Santiago
from ev3dev2.sensor .lego import UltrasonicSensor
129 9 Vranika Santiago
from ev3dev2.sound import Sound
130 9 Vranika Santiago
from ev3dev2.led import Leds
131 9 Vranika Santiago
132 9 Vranika Santiago
motor_angle= LargeMotor(OUTPUT_A)
133 9 Vranika Santiago
cannon = MediumMotor(OUTPUT_D)
134 9 Vranika Santiago
sonido = Sound()
135 9 Vranika Santiago
sensor = UltrasonicSensor(INPUT_1)
136 9 Vranika Santiago
sensor.mode = "US-DIST-CM"
137 9 Vranika Santiago
tankmoves = MoveTank(OUTPUT_B,OUTPUT_C)
138 9 Vranika Santiago
print(sensor.value()/10)
139 9 Vranika Santiago
140 9 Vranika Santiago
def moveUp():
141 9 Vranika Santiago
    print("Moving up...")
142 9 Vranika Santiago
    tankmoves.on(SpeedPercent(100),SpeedPercent(100))
143 9 Vranika Santiago
def moveDown():
144 9 Vranika Santiago
    print("Moving down...")
145 9 Vranika Santiago
    tankmoves.on(SpeedPercent(-100),SpeedPercent(-100))
146 9 Vranika Santiago
def moveRight():
147 9 Vranika Santiago
    print("Moving right...")
148 9 Vranika Santiago
    tankmoves.on(SpeedPercent(100),SpeedPercent(-100))
149 9 Vranika Santiago
def moveLeft():
150 9 Vranika Santiago
    print("Moving left...")
151 9 Vranika Santiago
    tankmoves.on(SpeedPercent(-100),SpeedPercent(100))
152 9 Vranika Santiago
def Inclinacion0():
153 9 Vranika Santiago
    motor_angle.on_for_degrees(7,55,brake=True)
154 9 Vranika Santiago
def Inclinacion45():
155 9 Vranika Santiago
    motor_angle.on_for_degrees(7,-55,brake=True)
156 9 Vranika Santiago
    
157 9 Vranika Santiago
def Disparar():    
158 9 Vranika Santiago
    cannon.on(SpeedPercent(-100))
159 9 Vranika Santiago
def Apuntar():
160 9 Vranika Santiago
    sensor.value #valor del sensor en milimetro
161 9 Vranika Santiago
    
162 9 Vranika Santiago
def Luciano():
163 9 Vranika Santiago
    sonido.speak("gorila tank mak two")
164 9 Vranika Santiago
def Distancia():
165 9 Vranika Santiago
    
166 9 Vranika Santiago
    while(sensor.value()/10 <= 100 or sensor.value()/10 >= 110):
167 9 Vranika Santiago
        print(sensor.value()/10)
168 9 Vranika Santiago
        if(sensor.value()/10 < 100):
169 9 Vranika Santiago
            tankmoves.on(SpeedPercent(-40),SpeedPercent(-40))
170 9 Vranika Santiago
            
171 9 Vranika Santiago
        else:
172 9 Vranika Santiago
            tankmoves.on(SpeedPercent(40),SpeedPercent(40))
173 9 Vranika Santiago
    Stop()
174 9 Vranika Santiago
    
175 9 Vranika Santiago
def Stop():
176 9 Vranika Santiago
    tankmoves.stop()
177 9 Vranika Santiago
    
178 9 Vranika Santiago
def StopCannon():
179 9 Vranika Santiago
    cannon.stop()
180 9 Vranika Santiago
    sonido.play_file("misioncumplida.wav",100,1)
181 9 Vranika Santiago
   
182 9 Vranika Santiago
</code></pre>
183 9 Vranika Santiago
}}