WALLE.py
1 |
#!/usr/bin/env python3
|
---|---|
2 |
import evdev |
3 |
import ev3dev.auto as ev3 |
4 |
import time |
5 |
import socket |
6 |
from ev3dev.ev3 import * |
7 |
from ev3dev2.motor import LargeMotor, OUTPUT_A, OUTPUT_B, SpeedPercent, MoveTank |
8 |
from ev3dev2.motor import LargeMotor |
9 |
motorA=LargeMotor("outA")
|
10 |
motorD=LargeMotor("outD")
|
11 |
motorB=LargeMotor("outB")
|
12 |
|
13 |
## Funciones
|
14 |
def avanzar(): |
15 |
motorA.on(-50)
|
16 |
motorD.on(-50)
|
17 |
time.sleep(1)
|
18 |
motorA.stop() |
19 |
motorD.stop() |
20 |
def golpear_1(): |
21 |
motorB.on(-5)
|
22 |
time.sleep(0.5)
|
23 |
motorB.stop() |
24 |
def golpear_2(): |
25 |
mototB.on(70)
|
26 |
motorB.stop() |
27 |
time.sleep(0.5)
|
28 |
def golpear_3(): |
29 |
motorB.on(5)
|
30 |
time.sleep(0.5)
|
31 |
motorB.stop() |
32 |
def retroceder(): |
33 |
motorD.on(50,False) |
34 |
motorA.on(50,False) |
35 |
time.sleep(1)
|
36 |
motorA.stop() |
37 |
motorD.stop() |
38 |
def derecha(): |
39 |
motorA.on(-20)
|
40 |
motorD.on(20)
|
41 |
time.sleep(0.5)
|
42 |
motorA.stop() |
43 |
motorD.stop() |
44 |
def izquierda(): |
45 |
motorD.on(-20)
|
46 |
motorA.on(20)
|
47 |
time.sleep(0.5)
|
48 |
motorD.stop() |
49 |
motorA.stop() |
50 |
#funciones de inclinacion del angulo
|
51 |
def angulo_30(): |
52 |
motorB.on_for_degrees(30,10) |
53 |
def angulo_45(): |
54 |
motorB.on_for_degrees(45.10)
|
55 |
def angulo_60(): |
56 |
motorB.on_for_degrees(60,10) |
57 |
def angulo_90(): |
58 |
motorB.on_for_degrees(90,10) |
59 |
#obtener ip de la conexion
|
60 |
def obtener_ip(): |
61 |
try:
|
62 |
# Conectarse a un servicio que devuelva la dirección IP pública.
|
63 |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
64 |
s.connect(("8.8.8.8", 80)) # Conectar a un servidor DNS (Google DNS en este caso). |
65 |
ip = s.getsockname()[0]
|
66 |
s.close() |
67 |
return ip
|
68 |
except Exception as e: |
69 |
print(f"Error al obtener la dirección IP: {e}")
|
70 |
return None |
71 |
host = obtener_ip() |
72 |
port = 12345
|
73 |
|
74 |
server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) |
75 |
server_socket.bind((host, port)) |
76 |
|
77 |
server_socket.listen(1)
|
78 |
print("Listening at port 12345")
|
79 |
print(host) |
80 |
|
81 |
client_socket, addr = server_socket.accept() |
82 |
print("Connection from: " + str(addr)) |
83 |
client_socket.send("Bienvenido".encode())
|
84 |
while True: |
85 |
data = client_socket.recv(1024).decode()
|
86 |
if data=="avanzar": |
87 |
avanzar() |
88 |
elif data == "golpear": |
89 |
golpear_2() |
90 |
elif data=="atras": |
91 |
golpear_3() |
92 |
elif data=="adelante": |
93 |
golpear_1() |
94 |
elif data=="retroceder": |
95 |
retroceder() |
96 |
elif data=="derecha": |
97 |
derecha() |
98 |
elif data=="izquierda": |
99 |
izquierda() |
100 |
elif data=="desconectar": |
101 |
server_socket.close() |
102 |
print("Disconect")
|
103 |
elif data=="30°": |
104 |
angulo_30() |
105 |
elif data=="45°": |
106 |
angulo_45() |
107 |
elif data=="60°": |
108 |
angulo_60() |
109 |
elif data=="90°": |
110 |
angulo_90() |