Project

General

Profile

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

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