-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui_classe.py
519 lines (412 loc) · 17.5 KB
/
gui_classe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
import math
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import numpy as np
from math import *
import random
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import threading
import matplotlib.pyplot as plt
from servoing_function import *
mutex = threading.Lock()
store_pos_robot_x = np.zeros((max_count, 1))
store_pos_robot_y = np.zeros((max_count, 1))
store_ref_x_pixel = np.ones((max_count, 1))
store_ref_y_pixel = np.ones((max_count, 1))
store_image_x_pixel = np.zeros((max_count, 1))
store_image_y_pixel = np.zeros((max_count, 1))
store_temps_s = np.zeros((max_count, 1))
####################
# Classe : Parametre
#
# Hérite de la classe QWidget
#
# Cette classe crée un Widget qu'on peut insérer dans la fenêtre principale
#
# On peut accéder à la valeur de ce paramètre grâce à la méthode accesseur value()
#
#####################
class Parametre(QWidget):
def __init__(self, val_label, val_min=0.0, val_max=100.0, val_step=1.0, default=0.0):
QWidget.__init__(self)
# ---------------------------------------------------------------------
# ATTRIBUTS
# ---------------------------------------------------------------------
self.val = QDoubleSpinBox()
# ---------------------------------------------------------------------
# CREATION DU WIDGET
# ---------------------------------------------------------------------
label = QLabel(val_label)
hspacer = QSpacerItem(0, 0, QSizePolicy.Expanding, QSizePolicy.Expanding)
self.val.setRange(val_min, val_max)
self.val.setSingleStep(val_step)
self.val.setValue(default)
layout = QHBoxLayout()
layout.addWidget(label)
layout.addSpacerItem(hspacer)
layout.addWidget(self.val)
self.setLayout(layout)
# -------------------------------------------------------------------------
# ACCESSEUR
# -------------------------------------------------------------------------
def value(self):
return self.val.value()
# ------------------------------------------------------------------------------
#
# Classe : Figure_Image
#
# Hérite de la classe QWidget
#
# Cette classe crée un Widget qu'on peut insérer dans la fenêtre principale
#
# ------------------------------------------------------------------------------
class Figure_Image(QWidget):
def __init__(self):
QWidget.__init__(self)
# ---------------------------------------------------------------------
# ATTRIBUTS
# ---------------------------------------------------------------------
self.figure = Figure(figsize=(10, 5))
self.canvas = FigureCanvas(self.figure)
self.ax = self.figure.gca()
# ---------------------------------------------------------------------
# AXES, TITRES, ETC.
# ---------------------------------------------------------------------
self.ax.set_title("Coordonnées des points dans l'image")
self.ax.grid(True)
self.ax.set_xlabel('x [pixel]')
self.ax.set_ylabel('y [pixel]')
self.ax.set_xlim(-400.0, 400.0)
self.ax.set_ylim(-400.0, 400.0)
# ---------------------------------------------------------------------
# CREATION DES GRAPHES
# ---------------------------------------------------------------------
self.point_consigne, = self.ax.plot([], [], color='red', marker='*', markersize=10)
self.point_courant, = self.ax.plot([], [], color='green', marker='*', markersize=10)
# ---------------------------------------------------------------------
# MISE EN FORME
# ---------------------------------------------------------------------
layout = QVBoxLayout()
layout.addWidget(self.canvas)
self.setLayout(layout)
# -------------------------------------------------------------------------
# MISE A JOUR DE LA FIGURE
# -------------------------------------------------------------------------
def refresh_image(self, point_consigne, point_courant):
# point consigne
self.point_consigne.set_xdata(point_consigne[0])
self.point_consigne.set_ydata(point_consigne[1])
# point courant
self.point_courant.set_xdata(point_courant[0])
self.point_courant.set_ydata(point_courant[1])
self.canvas.draw()
# ------------------------------------------------------------------------------
#
# Classe : Figure_Robot
#
# Hérite de la classe QWidget
#
# Cette classe crée un Widget qu'on peut insérer dans la fenêtre principale
#
# ------------------------------------------------------------------------------
class Figure_Robot(QWidget):
def __init__(self):
QWidget.__init__(self)
# ---------------------------------------------------------------------
# ATTRIBUTS
# ---------------------------------------------------------------------
self.figure = Figure(figsize=(10, 5))
self.canvas = FigureCanvas(self.figure)
self.ax = self.figure.gca()
# ---------------------------------------------------------------------
# AXES, TITRES, ETC.
# ---------------------------------------------------------------------
self.ax.set_title("Position de l'organe terminal du robot dans le repère monde")
self.ax.grid(True)
self.ax.set_xlabel('x [m]')
self.ax.set_ylabel('y [m]')
self.ax.set_xlim(-0.3, 0.3)
self.ax.set_ylim(-0.3, 0.3)
self.position_initiale, = self.ax.plot([], [], color='red', marker='o', markersize=10)
self.position_courante, = self.ax.plot([], [], color='green', marker='o', markersize=10)
# ---------------------------------------------------------------------
# CREATION DES GRAPHES
# ---------------------------------------------------------------------
self.plot_image = self.ax.plot(animated=True)
self.plot_robot = self.ax.plot(animated=True)
# ---------------------------------------------------------------------
# MISE EN FORME
# ---------------------------------------------------------------------
layout = QVBoxLayout()
layout.addWidget(self.canvas)
self.setLayout(layout)
# -------------------------------------------------------------------------
# MISE A JOUR DE LA FIGURE
# -------------------------------------------------------------------------
def refresh_robot(self, position_initiale, position_courante):
# point consigne
self.position_initiale.set_xdata(position_initiale[0])
self.position_initiale.set_ydata(position_initiale[1])
# point courant
self.position_courante.set_xdata(position_courante[0])
self.position_courante.set_ydata(position_courante[1])
self.canvas.draw()
# ------------------------------------------------------------------------------
#
# Classe :
#
# Hérite de la classe QMainWindow
#
# ------------------------------------------------------------------------------
class Fenetre(QMainWindow):
# -----------------------------------------------------------------------
# Initialisation de la fenetre
# -----------------------------------------------------------------------
def __init__(self, parent=None):
super(Fenetre, self).__init__(parent)
# ------------------------------------------------------------------
# Variables
# ------------------------------------------------------------------
# Declenchement du controleur et de l'affichage des donnees
self.period_control = 50 # ms
self.period_gui = 50 # ms
self.timer_gui = QTimer()
self.timer_gui.timeout.connect(self.refresh_gui)
self.timer_control = QTimer()
self.timer_control.timeout.connect(self.visual_servoing_class)
self.setGeometry(30, 30, 1500, 500)
# Paramètres réels
self.focale = 20e-3
self.dim_x = 10e-6
self.dim_y = 10e-6
self.distance = 0.4
# Parametres estimés
self.focale_e = Parametre(
val_label="Focale estimée [mm] :",
val_min=0.0,
val_max=50.0,
val_step=1.0,
default=20.0)
self.distance_e = Parametre(
val_label="Distance estimée [m] :",
val_min=0.0,
val_max=1.0,
val_step=0.1,
default=0.4)
self.angle_e = Parametre(
val_label="Angle estimé [deg] :",
val_min=0.0,
val_max=90.0,
val_step=2.0,
default=0.0)
self.dim_x_e = Parametre(
val_label="Taille estimée du pixel /x [μm] :",
val_min=0.0,
val_max=50.0,
val_step=0.5,
default=10.0)
self.dim_y_e = Parametre(
val_label="Taille estimée du pixel /y [μm] :",
val_min=0.0,
val_max=50.0,
val_step=0.5,
default=10.0)
self.gain = Parametre(
val_label="Gain :",
val_min=0,
val_max=20,
val_step=0.5,
default=5)
self.consigne_x = Parametre(
val_label="Consigne /x [pixel] :",
val_min=-400.0,
val_max=400.0,
val_step=10.0,
default=0.0)
self.consigne_y = Parametre(
val_label="Consigne /y [pixel] :",
val_min=-400.0,
val_max=400.0,
val_step=10,
default=0.0)
# Figures
self.fig_image = Figure_Image()
self.fig_robot = Figure_Robot()
# Boutons
self.bouton_run = QPushButton("Démarrer")
self.bouton_stop = QPushButton("Arrêter")
self.bouton_stop.setEnabled(False)
# Asservissement visuel
self.count = 0
self.image_point_consigne = np.array([[0.0], [0.0]])
self.image_point_courant = np.array([[0.0], [0.0]])
self.robot_position_initiale = np.array([[0.0], [0.0]])
self.robot_position_courante = np.array([[0.0], [0.0]])
# -------------------------------------------------------------------
# Creation de l'interface graphique
# -------------------------------------------------------------------
# Parametres
# -----------
param_box = QGroupBox("Paramètres")
layout_param = QVBoxLayout()
layout_param.addWidget(self.focale_e)
layout_param.addWidget(self.distance_e)
layout_param.addWidget(self.angle_e)
layout_param.addWidget(self.dim_x_e)
layout_param.addWidget(self.dim_y_e)
layout_param.addWidget(self.gain)
param_box.setLayout(layout_param)
# Consigne
# -----------
consigne_box = QGroupBox("Consigne")
layout_consigne = QVBoxLayout()
layout_consigne.addWidget(self.consigne_x)
layout_consigne.addWidget(self.consigne_y)
consigne_box.setLayout(layout_consigne)
# Boutons
# -----------
layout_boutons = QHBoxLayout()
layout_boutons.addWidget(self.bouton_run)
layout_boutons.addWidget(self.bouton_stop)
# Control
# -----------
layout_control = QVBoxLayout()
layout_control.addWidget(param_box)
layout_control.addWidget(consigne_box)
layout_control.addLayout(layout_boutons)
# Figures
# -----------
layout_fig = QHBoxLayout()
layout_fig.addWidget(self.fig_image)
layout_fig.addWidget(self.fig_robot)
# Main Layout
# -----------
main_layout = QHBoxLayout()
main_layout.addLayout(layout_control)
main_layout.addLayout(layout_fig)
# Widget Central
# -----------
central_widget = QWidget()
central_widget.setLayout(main_layout)
self.setCentralWidget(central_widget)
# Titre/Icone/etc. de la fenêtre
# -----------
self.setWindowTitle("Asservissement visuel")
self.setWindowIcon(QIcon("./logo_polytech.png"))
# -------------------------------------------------------------------
# Comportement des boutons
# -------------------------------------------------------------------
self.bouton_run.clicked.connect(self.start_timers)
self.bouton_stop.clicked.connect(self.stop_timers)
# -----------------------------------------------------------------------
# Asservissement visuel
# -----------------------------------------------------------------------
# Cette méthode est appelée lors d'un appui sur le bouton "Démarrer"
# Elle met en route les deux timers.
def start_timers(self):
# lancement des timers
self.timer_gui.start(self.period_gui)
self.timer_control.start(self.period_control)
# etat des boutons
self.bouton_run.setEnabled(False)
self.bouton_stop.setEnabled(False)
self.angle_e.setEnabled(False)
self.gain.setEnabled(False)
self.dim_x_e.setEnabled(False)
self.dim_y_e.setEnabled(False)
self.gain.setEnabled(False)
self.consigne_x.setEnabled(False)
self.consigne_y.setEnabled(False)
self.distance_e.setEnabled(False)
self.focale_e.setEnabled(False)
self.count = 0
# Cette méthode est appelée lors d'un appui sur le bouton "Arrêter"
# Elle arrête les deux timers.
def stop_timers(self):
global store_pos_robot_x
global store_pos_robot_y
global store_ref_x_pixel
global store_ref_y_pixel
global store_image_x_pixel
global store_image_y_pixel
global store_temps_s
store_ref_x_pixel = self.image_point_consigne[0] * np.ones((max_count, 1))
store_ref_y_pixel = self.image_point_consigne[1] * np.ones((max_count, 1))
# Arret des timers
self.timer_control.stop()
# etat des boutons
self.bouton_run.setEnabled(True)
self.bouton_stop.setEnabled(False)
self.angle_e.setEnabled(True)
self.gain.setEnabled(True)
self.dim_x_e.setEnabled(True)
self.dim_y_e.setEnabled(True)
self.gain.setEnabled(True)
self.consigne_x.setEnabled(True)
self.consigne_y.setEnabled(True)
self.distance_e.setEnabled(True)
self.focale_e.setEnabled(True)
self.image_point_consigne = np.array([[0.0], [0.0]])
self.image_point_courant = np.array([[0.0], [0.0]])
self.robot_position_intiale = np.array([[0.0], [0.0]])
self.robot_position_courante = np.array([[0.0], [0.0]])
for i in range(max_count):
store_temps_s[i] = i * self.period_control * 1e-3
fig1, ax1 = plt.subplots()
fig1.canvas.draw()
ax1.plot(store_temps_s[:max_count - 1, 0], store_pos_robot_x[:max_count - 1, 0], 'r-', linewidth=2,
label='End effector x position')
ax1.plot(store_temps_s[:max_count - 1, 0], store_pos_robot_y[:max_count - 1, 0], 'b-', linewidth=2,
label='End effector y position')
ax1.set_facecolor('w')
plt.legend(facecolor='white', framealpha=0)
plt.xlabel('time(seconds)')
plt.ylabel('m')
plt.grid()
fig1.suptitle('Robot end effector position', fontsize=13)
plt.show()
fig2, ax2 = plt.subplots()
fig2.canvas.draw()
ax2.plot(store_temps_s[:max_count - 2, 0], store_ref_x_pixel[:max_count - 2, 0], 'r--', linewidth=2,
label='Image x reference position')
ax2.plot(store_temps_s[:max_count - 2, 0], store_ref_y_pixel[:max_count - 2, 0], 'b--', linewidth=2,
label='Image y reference position')
ax2.plot(store_temps_s[:max_count - 2, 0], store_image_x_pixel[:max_count - 2, 0], 'r-', linewidth=2,
label='Image x current position')
ax2.plot(store_temps_s[:max_count - 2, 0], store_image_y_pixel[:max_count - 2, 0], 'b-', linewidth=2,
label='Image y current position')
ax2.set_facecolor('w')
plt.legend(facecolor='white', framealpha=0)
plt.xlabel('time(seconds)')
plt.ylabel('pixels')
plt.grid()
fig2.suptitle('Image point position', fontsize=13)
plt.show()
fig3, ax3 = plt.subplots()
fig3.canvas.draw()
ax3.plot(store_image_x_pixel[:max_count - 2, 0], store_image_y_pixel[:max_count - 2, 0], 'k-', linewidth=2)
ax3.set_facecolor('w')
plt.xlabel('pixels')
plt.ylabel('pixels')
plt.grid()
fig3.suptitle('Image point trajectory in the image plane', fontsize=13)
plt.show()
# print(store_image_x_pixel[0])
# Cette méthode est appelée à chaque coup d'horloge de timer_control
#
# def visual_servoing_class(self):
# visual_servoing(object)
# -----------------------------------------------------------------------
# Affichage des donnees
# -----------------------------------------------------------------------
def refresh_gui(self):
global mutex
self.image_point_consigne[0] = self.consigne_x.value()
self.image_point_consigne[1] = self.consigne_y.value()
mutex.acquire()
self.fig_image.refresh_image(self.image_point_consigne, self.image_point_courant)
self.fig_robot.refresh_robot(self.robot_position_initiale, self.robot_position_courante)
mutex.release()