Código e implementación » History » Version 9
Vranika Santiago, 12/14/2022 03:22 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 | 9 | Vranika Santiago | }} |
15 | 9 | Vranika Santiago | |
16 | 9 | Vranika Santiago | {{collapse(Servidor) |
17 | 9 | Vranika Santiago | <pre><code class="python"> |
18 | 9 | Vranika Santiago | import socket |
19 | 9 | Vranika Santiago | from Function import * |
20 | 9 | Vranika Santiago | s = socket.socket() |
21 | 9 | Vranika Santiago | print("Socket creado") |
22 | 9 | Vranika Santiago | port = 19999 |
23 | 9 | Vranika Santiago | s.bind( ("", port) ) |
24 | 9 | Vranika Santiago | print("El socket se creo con puerto:{}".format(port)) |
25 | 9 | Vranika Santiago | s.listen(5) |
26 | 9 | Vranika Santiago | print("EL socket is listening....") |
27 | 9 | Vranika Santiago | connect, addr = s.accept() |
28 | 9 | Vranika Santiago | print("Se conecto a {}".format(addr)) |
29 | 9 | Vranika Santiago | while True: |
30 | 9 | Vranika Santiago | rawByte = connect.recv(1) |
31 | 9 | Vranika Santiago | char = rawByte.decode('utf-8') |
32 | 9 | Vranika Santiago | if (char == 'w'): |
33 | 9 | Vranika Santiago | moveUp() |
34 | 9 | Vranika Santiago | if (char == 's'): |
35 | 9 | Vranika Santiago | moveDown() |
36 | 9 | Vranika Santiago | if (char == 'd'): |
37 | 9 | Vranika Santiago | moveRight() |
38 | 9 | Vranika Santiago | if (char == 'a'): |
39 | 9 | Vranika Santiago | moveLeft() |
40 | 9 | Vranika Santiago | if (char == 'p'): |
41 | 9 | Vranika Santiago | Disparar() |
42 | 9 | Vranika Santiago | if (char == 'f'): |
43 | 9 | Vranika Santiago | Luciano() |
44 | 9 | Vranika Santiago | if (char == 'i'): |
45 | 9 | Vranika Santiago | Inclinacion45() |
46 | 9 | Vranika Santiago | if (char == 'o'): |
47 | 9 | Vranika Santiago | Inclinacion0() |
48 | 9 | Vranika Santiago | if (char == ' '): |
49 | 9 | Vranika Santiago | Stop() |
50 | 9 | Vranika Santiago | if (char == 'm'): |
51 | 9 | Vranika Santiago | Distancia() |
52 | 9 | Vranika Santiago | if (char == 'l'): |
53 | 9 | Vranika Santiago | StopCannon() |
54 | 9 | Vranika Santiago | </code></pre> |
55 | 9 | Vranika Santiago | }} |
56 | 9 | Vranika Santiago | |
57 | 9 | Vranika Santiago | {{collapse(Movimientos del robot) |
58 | 9 | Vranika Santiago | <pre><code class="python"> |
59 | 9 | Vranika Santiago | #!/usr/bin/env python3 |
60 | 9 | Vranika Santiago | from ev3dev2.motor import LargeMotor, MediumMotor, OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D, SpeedPercent, MoveTank |
61 | 9 | Vranika Santiago | from ev3dev2.sensor import INPUT_1 |
62 | 9 | Vranika Santiago | from ev3dev2.sensor .lego import UltrasonicSensor |
63 | 9 | Vranika Santiago | from ev3dev2.sound import Sound |
64 | 9 | Vranika Santiago | from ev3dev2.led import Leds |
65 | 9 | Vranika Santiago | |
66 | 9 | Vranika Santiago | motor_angle= LargeMotor(OUTPUT_A) |
67 | 9 | Vranika Santiago | cannon = MediumMotor(OUTPUT_D) |
68 | 9 | Vranika Santiago | sonido = Sound() |
69 | 9 | Vranika Santiago | sensor = UltrasonicSensor(INPUT_1) |
70 | 9 | Vranika Santiago | sensor.mode = "US-DIST-CM" |
71 | 9 | Vranika Santiago | tankmoves = MoveTank(OUTPUT_B,OUTPUT_C) |
72 | 9 | Vranika Santiago | print(sensor.value()/10) |
73 | 9 | Vranika Santiago | |
74 | 9 | Vranika Santiago | def moveUp(): |
75 | 9 | Vranika Santiago | print("Moving up...") |
76 | 9 | Vranika Santiago | tankmoves.on(SpeedPercent(100),SpeedPercent(100)) |
77 | 9 | Vranika Santiago | def moveDown(): |
78 | 9 | Vranika Santiago | print("Moving down...") |
79 | 9 | Vranika Santiago | tankmoves.on(SpeedPercent(-100),SpeedPercent(-100)) |
80 | 9 | Vranika Santiago | def moveRight(): |
81 | 9 | Vranika Santiago | print("Moving right...") |
82 | 9 | Vranika Santiago | tankmoves.on(SpeedPercent(100),SpeedPercent(-100)) |
83 | 9 | Vranika Santiago | def moveLeft(): |
84 | 9 | Vranika Santiago | print("Moving left...") |
85 | 9 | Vranika Santiago | tankmoves.on(SpeedPercent(-100),SpeedPercent(100)) |
86 | 9 | Vranika Santiago | def Inclinacion0(): |
87 | 9 | Vranika Santiago | motor_angle.on_for_degrees(7,55,brake=True) |
88 | 9 | Vranika Santiago | def Inclinacion45(): |
89 | 9 | Vranika Santiago | motor_angle.on_for_degrees(7,-55,brake=True) |
90 | 9 | Vranika Santiago | |
91 | 9 | Vranika Santiago | def Disparar(): |
92 | 9 | Vranika Santiago | cannon.on(SpeedPercent(-100)) |
93 | 9 | Vranika Santiago | def Apuntar(): |
94 | 9 | Vranika Santiago | sensor.value #valor del sensor en milimetro |
95 | 9 | Vranika Santiago | |
96 | 9 | Vranika Santiago | def Luciano(): |
97 | 9 | Vranika Santiago | sonido.speak("gorila tank mak two") |
98 | 9 | Vranika Santiago | def Distancia(): |
99 | 9 | Vranika Santiago | |
100 | 9 | Vranika Santiago | while(sensor.value()/10 <= 100 or sensor.value()/10 >= 110): |
101 | 9 | Vranika Santiago | print(sensor.value()/10) |
102 | 9 | Vranika Santiago | if(sensor.value()/10 < 100): |
103 | 9 | Vranika Santiago | tankmoves.on(SpeedPercent(-40),SpeedPercent(-40)) |
104 | 9 | Vranika Santiago | |
105 | 9 | Vranika Santiago | else: |
106 | 9 | Vranika Santiago | tankmoves.on(SpeedPercent(40),SpeedPercent(40)) |
107 | 9 | Vranika Santiago | Stop() |
108 | 9 | Vranika Santiago | |
109 | 9 | Vranika Santiago | def Stop(): |
110 | 9 | Vranika Santiago | tankmoves.stop() |
111 | 9 | Vranika Santiago | |
112 | 9 | Vranika Santiago | def StopCannon(): |
113 | 9 | Vranika Santiago | cannon.stop() |
114 | 9 | Vranika Santiago | sonido.play_file("misioncumplida.wav",100,1) |
115 | 9 | Vranika Santiago | |
116 | 9 | Vranika Santiago | </code></pre> |
117 | 9 | Vranika Santiago | }} |