1
|
import tkinter as tk
|
2
|
from tkinter import ttk
|
3
|
import socket
|
4
|
|
5
|
|
6
|
key_states = {
|
7
|
'w': False,
|
8
|
'a': False,
|
9
|
's': False,
|
10
|
'd': False,
|
11
|
'space': False,
|
12
|
'i': False,
|
13
|
'o': False,
|
14
|
'p': False,
|
15
|
'u': False,
|
16
|
'j': False,
|
17
|
'q': False,
|
18
|
}
|
19
|
|
20
|
|
21
|
STATUS_CONNECTION = False
|
22
|
PORT = 8080
|
23
|
client = None
|
24
|
|
25
|
|
26
|
def send_command(command):
|
27
|
if STATUS_CONNECTION and client:
|
28
|
try:
|
29
|
client.send(bytes([ord(command)]))
|
30
|
print(f"Comando '{command}' enviado al robot.")
|
31
|
except socket.error as e:
|
32
|
print(f"Error al enviar el comando '{command}': {e}")
|
33
|
close_connection()
|
34
|
|
35
|
else:
|
36
|
print(f"No hay conexión. Comando '{command}' no enviado.")
|
37
|
|
38
|
|
39
|
def key_press(event):
|
40
|
key = event.keysym.lower()
|
41
|
if key in key_states:
|
42
|
if not key_states[key]:
|
43
|
key_states[key] = True
|
44
|
send_command(key)
|
45
|
|
46
|
|
47
|
if key == 'escape':
|
48
|
window.quit()
|
49
|
|
50
|
|
51
|
def key_release(event):
|
52
|
key = event.keysym.lower()
|
53
|
if key in key_states:
|
54
|
key_states[key] = False
|
55
|
|
56
|
|
57
|
def toggle_connection():
|
58
|
global STATUS_CONNECTION, client
|
59
|
if STATUS_CONNECTION:
|
60
|
close_connection()
|
61
|
else:
|
62
|
IP = "172.20.10.13"
|
63
|
try:
|
64
|
client = socket.socket()
|
65
|
client.connect(('172.20.10.13', PORT))
|
66
|
STATUS_CONNECTION = True
|
67
|
print("Conexión establecida con el robot")
|
68
|
connection_button.config(image=disconnect_icon)
|
69
|
except socket.error as e:
|
70
|
print(f"Error de conexión: {e}")
|
71
|
STATUS_CONNECTION = False
|
72
|
connection_button.config(image=connect_icon)
|
73
|
|
74
|
|
75
|
def close_connection():
|
76
|
global STATUS_CONNECTION, client
|
77
|
if client:
|
78
|
client.close()
|
79
|
STATUS_CONNECTION = False
|
80
|
print("Conexión cerrada.")
|
81
|
connection_button.config(image=connect_icon)
|
82
|
|
83
|
|
84
|
window = tk.Tk()
|
85
|
window.title("Control del Robot")
|
86
|
window.geometry("900x500")
|
87
|
|
88
|
|
89
|
window.eval('tk::PlaceWindow . center')
|
90
|
|
91
|
|
92
|
icons = {
|
93
|
'w': tk.PhotoImage(file="images/W.png").subsample(3, 3),
|
94
|
'a': tk.PhotoImage(file="images/A.png").subsample(3, 3),
|
95
|
's': tk.PhotoImage(file="images/S.png").subsample(3, 3),
|
96
|
'd': tk.PhotoImage(file="images/D.png").subsample(3, 3),
|
97
|
'space': tk.PhotoImage(file="images/W.png").subsample(3, 3),
|
98
|
'i': tk.PhotoImage(file="images/UP.png").subsample(3, 3),
|
99
|
'o': tk.PhotoImage(file="images/W.png").subsample(3, 3),
|
100
|
'p': tk.PhotoImage(file="images/DOWN.png").subsample(3, 3),
|
101
|
'u': tk.PhotoImage(file="images/U.png").subsample(3, 3),
|
102
|
'j': tk.PhotoImage(file="images/J.png").subsample(3, 3),
|
103
|
'q': tk.PhotoImage(file="images/W.png").subsample(3, 3),
|
104
|
}
|
105
|
|
106
|
|
107
|
connect_icon = tk.PhotoImage(file="images/connect.png").subsample(3, 3)
|
108
|
disconnect_icon = tk.PhotoImage(file="images/connect.png").subsample(3, 3)
|
109
|
|
110
|
|
111
|
bg_image = tk.PhotoImage(file="images/Fondo.png")
|
112
|
bg_label = tk.Label(window, image=bg_image)
|
113
|
bg_label.place(x=0, y=0)
|
114
|
|
115
|
|
116
|
btn_w = tk.Button(window, image=icons['w'], command=lambda: send_command('w'))
|
117
|
btn_w.place(x=150, y=260)
|
118
|
|
119
|
btn_s = tk.Button(window, image=icons['s'], command=lambda: send_command('s'))
|
120
|
btn_s.place(x=150, y=340)
|
121
|
|
122
|
btn_a = tk.Button(window, image=icons['a'], command=lambda: send_command('a'))
|
123
|
btn_a.place(x=70, y=340)
|
124
|
|
125
|
btn_d = tk.Button(window, image=icons['d'], command=lambda: send_command('d'))
|
126
|
btn_d.place(x=230, y=340)
|
127
|
|
128
|
|
129
|
|
130
|
btn_u = tk.Button(window, image=icons['u'], command=lambda: send_command('u'))
|
131
|
btn_u.place(x=600, y=360)
|
132
|
|
133
|
btn_i = tk.Button(window, image=icons['i'], command=lambda: send_command('i'))
|
134
|
btn_i.place(x=800, y=360)
|
135
|
|
136
|
|
137
|
|
138
|
btn_p = tk.Button(window, image=icons['p'], command=lambda: send_command('p'))
|
139
|
btn_p.place(x=800, y=400)
|
140
|
|
141
|
btn_j = tk.Button(window, image=icons['j'], command=lambda: send_command('j'))
|
142
|
btn_j.place(x=600, y=400)
|
143
|
|
144
|
|
145
|
|
146
|
|
147
|
connection_button = tk.Button(window, image=connect_icon, command=toggle_connection)
|
148
|
connection_button.place(x=700, y=360)
|
149
|
|
150
|
|
151
|
window.bind("<KeyPress>", key_press)
|
152
|
window.bind("<KeyRelease>", key_release)
|
153
|
|
154
|
|
155
|
window.mainloop()
|