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