Project

General

Profile

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

brayan garcia, 12/15/2022 09:19 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 18 brayan garcia
Para que el robot funcione, se llevo a cabo la codificación de tres algoritmos, los cuales cada uno tienen distintos propósitos.
14 18 brayan garcia
En este apartado se encuentran:
15 18 brayan garcia
16 16 Vranika Santiago
{{collapse(Código interfaz gráfica (GUI): )
17 11 Vranika Santiago
<pre><code class="python">
18 10 Vranika Santiago
import tkinter as tk
19 10 Vranika Santiago
from tkinter import ttk
20 10 Vranika Santiago
from tkinter import *
21 10 Vranika Santiago
import socket 
22 10 Vranika Santiago
23 10 Vranika Santiago
ventana = tk.Tk()
24 10 Vranika Santiago
ventana.config(width=900, height=700)
25 10 Vranika Santiago
ventana.title("Interfaz -> Gorilla-Tank")
26 10 Vranika Santiago
ventana.configure(bg="gray25")
27 10 Vranika Santiago
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 1 Vranika Santiago
def Cannon(event):
58 1 Vranika Santiago
    clientSocket.send(bytes([ord('l')]))
59 10 Vranika Santiago
60 1 Vranika Santiago
def getAddres():
61 11 Vranika Santiago
    w_ip = Tk()
62 11 Vranika Santiago
    w_ip.geometry("300x100")
63 10 Vranika Santiago
64 11 Vranika Santiago
    dato = StringVar(w_ip)
65 11 Vranika Santiago
    w_ip.title("Configurar Ip")
66 11 Vranika Santiago
    ip = ttk.Entry(w_ip,textvariable=dato).place(x=10,y=10)
67 11 Vranika Santiago
    button = Button(w_ip,text =" Aplicar",command=lambda:[conectar(dato.get()),w_ip.destroy()]).place(x=170,y=9)
68 11 Vranika Santiago
    print(dato.get())
69 1 Vranika Santiago
70 1 Vranika Santiago
def conectar(adress):
71 11 Vranika Santiago
    port = 19999
72 11 Vranika Santiago
    try:
73 11 Vranika Santiago
        clientSocket.connect((adress,port))
74 11 Vranika Santiago
        messagebox.showinfo("Mensaje Servido","Cliente conectado al robot: {0} : {1}".format(adress,port))
75 11 Vranika Santiago
    except socket.error:
76 11 Vranika Santiago
        messagebox.showwarning("Conexión erronea","No se ha logrado al conexíon, verifique la Ip {0}".format(adress))
77 11 Vranika Santiago
        getAddres()
78 11 Vranika Santiago
        clientSocket.close()
79 10 Vranika Santiago
ventana.mainloop()
80 11 Vranika Santiago
</code></pre>
81 9 Vranika Santiago
}}
82 9 Vranika Santiago
83 17 Vranika Santiago
{{collapse(Código server)
84 9 Vranika Santiago
<pre><code class="python">
85 9 Vranika Santiago
import socket
86 9 Vranika Santiago
from Function import *
87 9 Vranika Santiago
s = socket.socket()
88 9 Vranika Santiago
print("Socket creado")
89 9 Vranika Santiago
port = 19999
90 9 Vranika Santiago
s.bind( ("", port) )
91 9 Vranika Santiago
print("El socket se creo con puerto:{}".format(port))
92 9 Vranika Santiago
s.listen(5)
93 9 Vranika Santiago
print("EL socket is listening....")
94 9 Vranika Santiago
connect, addr = s.accept()
95 9 Vranika Santiago
print("Se conecto a {}".format(addr))
96 9 Vranika Santiago
while True:
97 9 Vranika Santiago
    rawByte = connect.recv(1)
98 9 Vranika Santiago
    char = rawByte.decode('utf-8')
99 9 Vranika Santiago
    if (char == 'w'):
100 9 Vranika Santiago
        moveUp()
101 9 Vranika Santiago
    if (char == 's'):
102 9 Vranika Santiago
        moveDown()
103 9 Vranika Santiago
    if (char == 'd'):
104 9 Vranika Santiago
        moveRight()
105 9 Vranika Santiago
    if (char == 'a'):
106 9 Vranika Santiago
        moveLeft()
107 9 Vranika Santiago
    if (char == 'p'):
108 9 Vranika Santiago
        Disparar()
109 9 Vranika Santiago
    if (char == 'f'):
110 9 Vranika Santiago
        Luciano()  
111 9 Vranika Santiago
    if (char == 'i'):
112 9 Vranika Santiago
        Inclinacion45()
113 9 Vranika Santiago
    if (char == 'o'):
114 9 Vranika Santiago
        Inclinacion0()
115 9 Vranika Santiago
    if (char == ' '):
116 9 Vranika Santiago
        Stop()
117 9 Vranika Santiago
    if (char == 'm'):
118 9 Vranika Santiago
        Distancia()
119 9 Vranika Santiago
    if (char == 'l'):
120 9 Vranika Santiago
        StopCannon()
121 9 Vranika Santiago
</code></pre>
122 9 Vranika Santiago
}}
123 9 Vranika Santiago
124 17 Vranika Santiago
{{collapse(Código de los movimientos del robot)
125 9 Vranika Santiago
<pre><code class="python">
126 9 Vranika Santiago
#!/usr/bin/env python3
127 9 Vranika Santiago
from ev3dev2.motor import LargeMotor, MediumMotor, OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D, SpeedPercent, MoveTank
128 9 Vranika Santiago
from ev3dev2.sensor import INPUT_1
129 9 Vranika Santiago
from ev3dev2.sensor .lego import UltrasonicSensor
130 9 Vranika Santiago
from ev3dev2.sound import Sound
131 9 Vranika Santiago
from ev3dev2.led import Leds
132 9 Vranika Santiago
133 9 Vranika Santiago
motor_angle= LargeMotor(OUTPUT_A)
134 9 Vranika Santiago
cannon = MediumMotor(OUTPUT_D)
135 9 Vranika Santiago
sonido = Sound()
136 9 Vranika Santiago
sensor = UltrasonicSensor(INPUT_1)
137 9 Vranika Santiago
sensor.mode = "US-DIST-CM"
138 9 Vranika Santiago
tankmoves = MoveTank(OUTPUT_B,OUTPUT_C)
139 9 Vranika Santiago
print(sensor.value()/10)
140 9 Vranika Santiago
141 9 Vranika Santiago
def moveUp():
142 9 Vranika Santiago
    print("Moving up...")
143 9 Vranika Santiago
    tankmoves.on(SpeedPercent(100),SpeedPercent(100))
144 9 Vranika Santiago
def moveDown():
145 9 Vranika Santiago
    print("Moving down...")
146 9 Vranika Santiago
    tankmoves.on(SpeedPercent(-100),SpeedPercent(-100))
147 9 Vranika Santiago
def moveRight():
148 9 Vranika Santiago
    print("Moving right...")
149 9 Vranika Santiago
    tankmoves.on(SpeedPercent(100),SpeedPercent(-100))
150 9 Vranika Santiago
def moveLeft():
151 9 Vranika Santiago
    print("Moving left...")
152 9 Vranika Santiago
    tankmoves.on(SpeedPercent(-100),SpeedPercent(100))
153 9 Vranika Santiago
def Inclinacion0():
154 9 Vranika Santiago
    motor_angle.on_for_degrees(7,55,brake=True)
155 9 Vranika Santiago
def Inclinacion45():
156 9 Vranika Santiago
    motor_angle.on_for_degrees(7,-55,brake=True)
157 9 Vranika Santiago
    
158 9 Vranika Santiago
def Disparar():    
159 9 Vranika Santiago
    cannon.on(SpeedPercent(-100))
160 9 Vranika Santiago
def Apuntar():
161 9 Vranika Santiago
    sensor.value #valor del sensor en milimetro
162 9 Vranika Santiago
    
163 9 Vranika Santiago
def Luciano():
164 9 Vranika Santiago
    sonido.speak("gorila tank mak two")
165 9 Vranika Santiago
def Distancia():
166 9 Vranika Santiago
    
167 9 Vranika Santiago
    while(sensor.value()/10 <= 100 or sensor.value()/10 >= 110):
168 9 Vranika Santiago
        print(sensor.value()/10)
169 9 Vranika Santiago
        if(sensor.value()/10 < 100):
170 9 Vranika Santiago
            tankmoves.on(SpeedPercent(-40),SpeedPercent(-40))
171 9 Vranika Santiago
            
172 9 Vranika Santiago
        else:
173 9 Vranika Santiago
            tankmoves.on(SpeedPercent(40),SpeedPercent(40))
174 9 Vranika Santiago
    Stop()
175 9 Vranika Santiago
    
176 9 Vranika Santiago
def Stop():
177 9 Vranika Santiago
    tankmoves.stop()
178 9 Vranika Santiago
    
179 9 Vranika Santiago
def StopCannon():
180 9 Vranika Santiago
    cannon.stop()
181 9 Vranika Santiago
    sonido.play_file("misioncumplida.wav",100,1)
182 9 Vranika Santiago
   
183 9 Vranika Santiago
</code></pre>
184 9 Vranika Santiago
}}