Código Utilizado » History » Version 7
Melisa Laura, 12/21/2023 04:16 PM
1 | 1 | Melisa Laura | h2. *Índice*: |
---|---|---|---|
2 | 1 | Melisa Laura | * [[Introducción]] |
3 | 1 | Melisa Laura | * [[Organización y Planificación]] |
4 | 1 | Melisa Laura | * [[Progreso del Robot]] |
5 | 1 | Melisa Laura | * [[Análisis - Diseño]] |
6 | 1 | Melisa Laura | * [[Código Utilizado]] |
7 | 1 | Melisa Laura | * [[Resultados]] |
8 | 1 | Melisa Laura | |
9 | 1 | Melisa Laura | h1. Código Utilizado |
10 | 2 | Melisa Laura | |
11 | 7 | Melisa Laura | *+Servidor+* |
12 | 2 | Melisa Laura | |
13 | 3 | Melisa Laura | <pre> |
14 | 3 | Melisa Laura | import socket |
15 | 3 | Melisa Laura | from robot import robot |
16 | 3 | Melisa Laura | |
17 | 3 | Melisa Laura | server = socket.socket() |
18 | 3 | Melisa Laura | |
19 | 3 | Melisa Laura | PORT = 8800 |
20 | 3 | Melisa Laura | |
21 | 3 | Melisa Laura | server.bind(('', PORT)) |
22 | 3 | Melisa Laura | server.listen(1) |
23 | 3 | Melisa Laura | |
24 | 3 | Melisa Laura | |
25 | 3 | Melisa Laura | connection, adress = server.accept() |
26 | 3 | Melisa Laura | |
27 | 3 | Melisa Laura | |
28 | 3 | Melisa Laura | robot = Robot()[[]] |
29 | 3 | Melisa Laura | |
30 | 3 | Melisa Laura | actions = { |
31 | 3 | Melisa Laura | "w" : robot.move_front, |
32 | 3 | Melisa Laura | "a" : robot.move_left, |
33 | 3 | Melisa Laura | "d" : robot.move_right, |
34 | 3 | Melisa Laura | "s" : robot.move_back, |
35 | 3 | Melisa Laura | "e" : robot.move_punch, |
36 | 3 | Melisa Laura | } |
37 | 3 | Melisa Laura | |
38 | 3 | Melisa Laura | while True: |
39 | 3 | Melisa Laura | data = connection.recv(1) |
40 | 3 | Melisa Laura | keyword = data.decode("utf-8") |
41 | 3 | Melisa Laura | |
42 | 3 | Melisa Laura | if keyword in actions: |
43 | 3 | Melisa Laura | actions[keyword]() |
44 | 3 | Melisa Laura | |
45 | 3 | Melisa Laura | elif keyword == "q": |
46 | 3 | Melisa Laura | break |
47 | 3 | Melisa Laura | |
48 | 3 | Melisa Laura | |
49 | 3 | Melisa Laura | </pre> |
50 | 3 | Melisa Laura | |
51 | 4 | Melisa Laura | *+Funciones del Robot+* |
52 | 2 | Melisa Laura | |
53 | 3 | Melisa Laura | <pre> |
54 | 3 | Melisa Laura | from ev3dev2.motor import (LargeMotor, MoveTank, MediumMotor, OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D) |
55 | 3 | Melisa Laura | from time import sleep |
56 | 3 | Melisa Laura | |
57 | 3 | Melisa Laura | |
58 | 3 | Melisa Laura | class Robot: |
59 | 3 | Melisa Laura | |
60 | 3 | Melisa Laura | eje = 0 |
61 | 3 | Melisa Laura | base = -0.58 |
62 | 3 | Melisa Laura | MAX_VALUE_EJE = 140 |
63 | 3 | Melisa Laura | MIN_VALUE_EJE = -140 |
64 | 3 | Melisa Laura | MAX_VALUE_BASE = 0 |
65 | 3 | Melisa Laura | MIN_VALUE_BASE = -0.58 |
66 | 3 | Melisa Laura | SPEED = 100 |
67 | 3 | Melisa Laura | contaux1 = 0 |
68 | 3 | Melisa Laura | contaux2 = 0 |
69 | 3 | Melisa Laura | cont = 1 |
70 | 3 | Melisa Laura | |
71 | 3 | Melisa Laura | |
72 | 3 | Melisa Laura | def __init__(self): |
73 | 3 | Melisa Laura | self.out_a = LargeMotor(OUTPUT_A) # movimiento pelota |
74 | 3 | Melisa Laura | self.out_b = LargeMotor(OUTPUT_B) # ruedas de atras, movimiento |
75 | 3 | Melisa Laura | self.out_c = MediumMotor(OUTPUT_C) # ruedas de rotacion, giro izquierda/derecha |
76 | 3 | Melisa Laura | self.out_d = MediumMotor(OUTPUT_D) # lanzamiento de pelota |
77 | 3 | Melisa Laura | |
78 | 3 | Melisa Laura | # Función para desactivar completamente el robot |
79 | 3 | Melisa Laura | def off_robot(self): |
80 | 3 | Melisa Laura | self.out_a.stop() |
81 | 3 | Melisa Laura | self.out_b.stop() |
82 | 3 | Melisa Laura | self.out_c.stop() |
83 | 3 | Melisa Laura | self.out_d.stop() |
84 | 3 | Melisa Laura | |
85 | 3 | Melisa Laura | # Función para mover el robot hacia adelante |
86 | 3 | Melisa Laura | def move_front(self): |
87 | 3 | Melisa Laura | self.out_b.on_for_rotations(speed = 100, rotations= -2, brake = True, block = True) |
88 | 3 | Melisa Laura | self.out_b.stop() |
89 | 3 | Melisa Laura | |
90 | 3 | Melisa Laura | # Función para mover el robot hacia atras |
91 | 3 | Melisa Laura | def move_back(self): |
92 | 3 | Melisa Laura | self.out_b.on_for_rotations(speed = 100, rotations= 2, brake = True, block = True) |
93 | 3 | Melisa Laura | self.out_b.stop() |
94 | 3 | Melisa Laura | |
95 | 3 | Melisa Laura | # Función para mover las ruedas del robot hacia la derecha |
96 | 3 | Melisa Laura | def move_right(self): |
97 | 3 | Melisa Laura | if(self.MIN_VALUE_EJE < self.eje): # Se comprueba el eje de las ruedas para no provocar una falla en el robot / para que no se queden pegadas |
98 | 3 | Melisa Laura | self.out_c.on_for_degrees(speed = 100, degrees = -1.4, brake = True, block = True) |
99 | 3 | Melisa Laura | self.eje-=1.4 |
100 | 3 | Melisa Laura | self.contaux1-=1 |
101 | 3 | Melisa Laura | print(self.eje) |
102 | 3 | Melisa Laura | self.out_c.stop() |
103 | 3 | Melisa Laura | |
104 | 3 | Melisa Laura | # Función para mover las ruedas del robot hacia la izquierda |
105 | 3 | Melisa Laura | def move_left(self): |
106 | 3 | Melisa Laura | if(self.MAX_VALUE_EJE > self.eje): # Se comprueba el eje de las ruedas para no provocar una falla en el robot / para que no se queden pegadas |
107 | 3 | Melisa Laura | self.out_c.on_for_degrees(speed = 100, degrees = 1.4, brake = True, block = True) |
108 | 3 | Melisa Laura | self.eje+=1.4 |
109 | 3 | Melisa Laura | self.contaux2+=1 |
110 | 3 | Melisa Laura | print(self.eje) |
111 | 3 | Melisa Laura | self.out_c.stop() |
112 | 3 | Melisa Laura | |
113 | 3 | Melisa Laura | # Función para ingresar los grados para hacer el golpe |
114 | 3 | Melisa Laura | def move_punch(self, vel, grad): |
115 | 3 | Melisa Laura | self.vel = vel |
116 | 3 | Melisa Laura | self.grad = grad |
117 | 3 | Melisa Laura | self.out_d.on_for_degrees(speed = 100, degrees=grad) |
118 | 3 | Melisa Laura | self.out_d.stop() |
119 | 3 | Melisa Laura | |
120 | 3 | Melisa Laura | # Función para mover la base de la pelota, hacia adelante |
121 | 3 | Melisa Laura | def mov_adel(self): |
122 | 3 | Melisa Laura | if(self.MAX_VALUE_BASE>self.base): # Se comprueba la base de la pelota para que no falle el robot / en este caso para que no se golpee hacia atras |
123 | 3 | Melisa Laura | self.out_a.on_for_rotations(speed = 100, rotations=-0.0116, brake = True, block = True) |
124 | 3 | Melisa Laura | self.base += 0.0116 |
125 | 3 | Melisa Laura | self.out_a.stop() |
126 | 3 | Melisa Laura | |
127 | 3 | Melisa Laura | # Función para mover la base de la pelota, hacia atras |
128 | 3 | Melisa Laura | def mov_atras(self): |
129 | 3 | Melisa Laura | if(self.MIN_VALUE_BASE<self.base): # Se comprueba la base de la pelota para que no falle el robot / en este caso es para que no se pase hacia adelante |
130 | 3 | Melisa Laura | self.out_a.on_for_rotations(speed = 100, rotations=0.0116, brake = True, block = True) |
131 | 3 | Melisa Laura | self.base -= 0.0116 |
132 | 3 | Melisa Laura | self.out_a.stop() |
133 | 3 | Melisa Laura | def off(self): |
134 | 3 | Melisa Laura | if(self.base>-0.58): # Se comprueba si esta en la posicion original la base de la pelota |
135 | 3 | Melisa Laura | while(True): |
136 | 3 | Melisa Laura | if(50 == self.cont): |
137 | 3 | Melisa Laura | return False # Se rompe el ciclo solo si la base tiene 4, donde estara en posicion original |
138 | 3 | Melisa Laura | else: |
139 | 3 | Melisa Laura | self.out_a.on_for_rotations(speed = 100, rotations = 0.0116, brake = True, block = True) |
140 | 3 | Melisa Laura | self.base+=0.0116 |
141 | 3 | Melisa Laura | self.cont = self.cont + 1 |
142 | 3 | Melisa Laura | print(self.cont) |
143 | 3 | Melisa Laura | self.out_a.stop() |
144 | 3 | Melisa Laura | </pre> |
145 | 3 | Melisa Laura | |
146 | 2 | Melisa Laura | h1. *Interfaz Gráfica* |
147 | 3 | Melisa Laura | |
148 | 3 | Melisa Laura | <pre> |
149 | 3 | Melisa Laura | |
150 | 3 | Melisa Laura | |
151 | 3 | Melisa Laura | </pre> |