Código e implementación » History » Version 17
Vranika Santiago, 12/14/2022 09:38 PM
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 | 16 | Vranika Santiago | {{collapse(Código 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 | 10 | Vranika Santiago | ventana.mainloop() |
77 | 11 | Vranika Santiago | </code></pre> |
78 | 9 | Vranika Santiago | }} |
79 | 9 | Vranika Santiago | |
80 | 17 | Vranika Santiago | {{collapse(Código server) |
81 | 9 | Vranika Santiago | <pre><code class="python"> |
82 | 9 | Vranika Santiago | import socket |
83 | 9 | Vranika Santiago | from Function import * |
84 | 9 | Vranika Santiago | s = socket.socket() |
85 | 9 | Vranika Santiago | print("Socket creado") |
86 | 9 | Vranika Santiago | port = 19999 |
87 | 9 | Vranika Santiago | s.bind( ("", port) ) |
88 | 9 | Vranika Santiago | print("El socket se creo con puerto:{}".format(port)) |
89 | 9 | Vranika Santiago | s.listen(5) |
90 | 9 | Vranika Santiago | print("EL socket is listening....") |
91 | 9 | Vranika Santiago | connect, addr = s.accept() |
92 | 9 | Vranika Santiago | print("Se conecto a {}".format(addr)) |
93 | 9 | Vranika Santiago | while True: |
94 | 9 | Vranika Santiago | rawByte = connect.recv(1) |
95 | 9 | Vranika Santiago | char = rawByte.decode('utf-8') |
96 | 9 | Vranika Santiago | if (char == 'w'): |
97 | 9 | Vranika Santiago | moveUp() |
98 | 9 | Vranika Santiago | if (char == 's'): |
99 | 9 | Vranika Santiago | moveDown() |
100 | 9 | Vranika Santiago | if (char == 'd'): |
101 | 9 | Vranika Santiago | moveRight() |
102 | 9 | Vranika Santiago | if (char == 'a'): |
103 | 9 | Vranika Santiago | moveLeft() |
104 | 9 | Vranika Santiago | if (char == 'p'): |
105 | 9 | Vranika Santiago | Disparar() |
106 | 9 | Vranika Santiago | if (char == 'f'): |
107 | 9 | Vranika Santiago | Luciano() |
108 | 9 | Vranika Santiago | if (char == 'i'): |
109 | 9 | Vranika Santiago | Inclinacion45() |
110 | 9 | Vranika Santiago | if (char == 'o'): |
111 | 9 | Vranika Santiago | Inclinacion0() |
112 | 9 | Vranika Santiago | if (char == ' '): |
113 | 9 | Vranika Santiago | Stop() |
114 | 9 | Vranika Santiago | if (char == 'm'): |
115 | 9 | Vranika Santiago | Distancia() |
116 | 9 | Vranika Santiago | if (char == 'l'): |
117 | 9 | Vranika Santiago | StopCannon() |
118 | 9 | Vranika Santiago | </code></pre> |
119 | 9 | Vranika Santiago | }} |
120 | 9 | Vranika Santiago | |
121 | 17 | Vranika Santiago | {{collapse(Código de los movimientos del robot) |
122 | 9 | Vranika Santiago | <pre><code class="python"> |
123 | 9 | Vranika Santiago | #!/usr/bin/env python3 |
124 | 9 | Vranika Santiago | from ev3dev2.motor import LargeMotor, MediumMotor, OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D, SpeedPercent, MoveTank |
125 | 9 | Vranika Santiago | from ev3dev2.sensor import INPUT_1 |
126 | 9 | Vranika Santiago | from ev3dev2.sensor .lego import UltrasonicSensor |
127 | 9 | Vranika Santiago | from ev3dev2.sound import Sound |
128 | 9 | Vranika Santiago | from ev3dev2.led import Leds |
129 | 9 | Vranika Santiago | |
130 | 9 | Vranika Santiago | motor_angle= LargeMotor(OUTPUT_A) |
131 | 9 | Vranika Santiago | cannon = MediumMotor(OUTPUT_D) |
132 | 9 | Vranika Santiago | sonido = Sound() |
133 | 9 | Vranika Santiago | sensor = UltrasonicSensor(INPUT_1) |
134 | 9 | Vranika Santiago | sensor.mode = "US-DIST-CM" |
135 | 9 | Vranika Santiago | tankmoves = MoveTank(OUTPUT_B,OUTPUT_C) |
136 | 9 | Vranika Santiago | print(sensor.value()/10) |
137 | 9 | Vranika Santiago | |
138 | 9 | Vranika Santiago | def moveUp(): |
139 | 9 | Vranika Santiago | print("Moving up...") |
140 | 9 | Vranika Santiago | tankmoves.on(SpeedPercent(100),SpeedPercent(100)) |
141 | 9 | Vranika Santiago | def moveDown(): |
142 | 9 | Vranika Santiago | print("Moving down...") |
143 | 9 | Vranika Santiago | tankmoves.on(SpeedPercent(-100),SpeedPercent(-100)) |
144 | 9 | Vranika Santiago | def moveRight(): |
145 | 9 | Vranika Santiago | print("Moving right...") |
146 | 9 | Vranika Santiago | tankmoves.on(SpeedPercent(100),SpeedPercent(-100)) |
147 | 9 | Vranika Santiago | def moveLeft(): |
148 | 9 | Vranika Santiago | print("Moving left...") |
149 | 9 | Vranika Santiago | tankmoves.on(SpeedPercent(-100),SpeedPercent(100)) |
150 | 9 | Vranika Santiago | def Inclinacion0(): |
151 | 9 | Vranika Santiago | motor_angle.on_for_degrees(7,55,brake=True) |
152 | 9 | Vranika Santiago | def Inclinacion45(): |
153 | 9 | Vranika Santiago | motor_angle.on_for_degrees(7,-55,brake=True) |
154 | 9 | Vranika Santiago | |
155 | 9 | Vranika Santiago | def Disparar(): |
156 | 9 | Vranika Santiago | cannon.on(SpeedPercent(-100)) |
157 | 9 | Vranika Santiago | def Apuntar(): |
158 | 9 | Vranika Santiago | sensor.value #valor del sensor en milimetro |
159 | 9 | Vranika Santiago | |
160 | 9 | Vranika Santiago | def Luciano(): |
161 | 9 | Vranika Santiago | sonido.speak("gorila tank mak two") |
162 | 9 | Vranika Santiago | def Distancia(): |
163 | 9 | Vranika Santiago | |
164 | 9 | Vranika Santiago | while(sensor.value()/10 <= 100 or sensor.value()/10 >= 110): |
165 | 9 | Vranika Santiago | print(sensor.value()/10) |
166 | 9 | Vranika Santiago | if(sensor.value()/10 < 100): |
167 | 9 | Vranika Santiago | tankmoves.on(SpeedPercent(-40),SpeedPercent(-40)) |
168 | 9 | Vranika Santiago | |
169 | 9 | Vranika Santiago | else: |
170 | 9 | Vranika Santiago | tankmoves.on(SpeedPercent(40),SpeedPercent(40)) |
171 | 9 | Vranika Santiago | Stop() |
172 | 9 | Vranika Santiago | |
173 | 9 | Vranika Santiago | def Stop(): |
174 | 9 | Vranika Santiago | tankmoves.stop() |
175 | 9 | Vranika Santiago | |
176 | 9 | Vranika Santiago | def StopCannon(): |
177 | 9 | Vranika Santiago | cannon.stop() |
178 | 9 | Vranika Santiago | sonido.play_file("misioncumplida.wav",100,1) |
179 | 9 | Vranika Santiago | |
180 | 9 | Vranika Santiago | </code></pre> |
181 | 9 | Vranika Santiago | }} |