-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalibration_panel.py
More file actions
1069 lines (883 loc) · 40.6 KB
/
Copy pathcalibration_panel.py
File metadata and controls
1069 lines (883 loc) · 40.6 KB
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Robot Calibration Panel
Consolidates all calibration settings:
- Motor direction verification
- Gripper PWM calibration
- DH parameters editing
"""
from PyQt5 import QtCore, QtGui, QtWidgets
import json
import logging
import paths
from forward_kinematics import get_dh_params, reload_dh_parameters
from config_g_manager import JOINT_TO_DRIVES, get_joint_directions, set_joint_direction
import config
logger = logging.getLogger(__name__)
class CollapsibleSection(QtWidgets.QWidget):
"""A section with a clickable header that toggles content visibility."""
expanded = QtCore.pyqtSignal(object) # Emitted with self when section expands
def __init__(self, title, is_expanded=True, parent=None):
super().__init__(parent)
self._title = title
self._expanded = is_expanded
layout = QtWidgets.QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(2)
self.toggle_btn = QtWidgets.QPushButton()
self.toggle_btn.setStyleSheet("""
QPushButton {
text-align: left;
font-weight: bold;
font-size: 13px;
padding: 6px 10px;
background-color: #e0e0e0;
border: 1px solid #ccc;
border-radius: 3px;
}
QPushButton:hover { background-color: #d0d0d0; }
""")
self.toggle_btn.clicked.connect(self._toggle)
layout.addWidget(self.toggle_btn)
self.content = QtWidgets.QWidget()
self.content_layout = QtWidgets.QVBoxLayout(self.content)
self.content_layout.setContentsMargins(0, 4, 0, 0)
layout.addWidget(self.content)
self._update_header()
if not is_expanded:
self.content.hide()
def _toggle(self):
self._expanded = not self._expanded
self.content.setVisible(self._expanded)
self._update_header()
if self._expanded:
self.expanded.emit(self)
def collapse(self):
"""Collapse this section programmatically."""
if self._expanded:
self._expanded = False
self.content.hide()
self._update_header()
def _update_header(self):
arrow = "\u25BC" if self._expanded else "\u25B6"
self.toggle_btn.setText(f"{arrow} {self._title}")
def add_widget(self, widget):
self.content_layout.addWidget(widget)
def add_layout(self, layout):
self.content_layout.addLayout(layout)
# DH parameters file path
DH_PARAMS_FILE = paths.get_data_dir() / 'dh_parameters.json'
GRIPPER_CALIBRATION_FILE = paths.get_data_dir() / 'gripper_calibration.json'
HOME_POSITION_FILE = paths.get_data_dir() / 'home_position.json'
PARK_POSITION_FILE = paths.get_data_dir() / 'park_position.json'
def load_gripper_calibration_on_startup():
"""
Load gripper calibration from file and apply to config module.
Call this at application startup to ensure settings are loaded
before the calibration panel is opened.
"""
try:
if GRIPPER_CALIBRATION_FILE.exists():
with open(GRIPPER_CALIBRATION_FILE, 'r') as f:
data = json.load(f)
config.GRIPPER_PWM_OPEN = data.get('pwm_open', 255)
config.GRIPPER_PWM_CLOSED = data.get('pwm_closed', 0)
logger.info(f"Loaded gripper calibration: open={config.GRIPPER_PWM_OPEN}, closed={config.GRIPPER_PWM_CLOSED}")
else:
logger.debug("No gripper calibration file found, using defaults")
except Exception as e:
logger.error(f"Error loading gripper calibration on startup: {e}")
def load_home_position_on_startup():
"""
Load home position from file and apply to config module.
Call this at application startup.
"""
try:
if HOME_POSITION_FILE.exists():
with open(HOME_POSITION_FILE, 'r') as f:
data = json.load(f)
for joint in ('Art1', 'Art2', 'Art3', 'Art4', 'Art5', 'Art6'):
if joint in data:
config.HOME_POSITION[joint] = data[joint]
logger.info(f"Loaded home position: {config.HOME_POSITION}")
else:
logger.debug("No home position file found, using defaults")
except Exception as e:
logger.error(f"Error loading home position on startup: {e}")
def load_park_position_on_startup():
"""
Load park position from file and apply to config module.
Call this at application startup.
"""
try:
if PARK_POSITION_FILE.exists():
with open(PARK_POSITION_FILE, 'r') as f:
data = json.load(f)
for joint in ('Art1', 'Art2', 'Art3', 'Art4', 'Art5', 'Art6'):
if joint in data:
config.PARK_POSITION[joint] = data[joint]
logger.info(f"Loaded park position: {config.PARK_POSITION}")
else:
logger.debug("No park position file found, using defaults")
except Exception as e:
logger.error(f"Error loading park position on startup: {e}")
class JointCalibrationWidget(QtWidgets.QWidget):
"""Compact single-row widget for verifying direction of a single joint"""
test_movement = QtCore.pyqtSignal(str, float) # joint_name, delta_angle
direction_changed = QtCore.pyqtSignal(str, int) # joint_name, direction (+1 or -1)
def __init__(self, joint_name, joint_description, parent=None):
super().__init__(parent)
self.joint_name = joint_name
self.joint_description = joint_description
self.current_direction = 1
self.setup_ui()
def setup_ui(self):
layout = QtWidgets.QHBoxLayout(self)
layout.setContentsMargins(4, 2, 4, 2)
name_label = QtWidgets.QLabel(f"<b>{self.joint_name}</b>")
name_label.setMinimumWidth(35)
layout.addWidget(name_label)
desc_label = QtWidgets.QLabel(self.joint_description)
desc_label.setStyleSheet("color: #666;")
desc_label.setMinimumWidth(140)
layout.addWidget(desc_label)
layout.addStretch()
# Test buttons
self.test_minus = QtWidgets.QPushButton("\u221210\u00b0")
self.test_minus.setFixedWidth(45)
self.test_minus.setStyleSheet("background-color: #ffcccc;")
self.test_minus.clicked.connect(lambda: self.test_movement.emit(self.joint_name, -10))
layout.addWidget(self.test_minus)
self.test_plus = QtWidgets.QPushButton("+10\u00b0")
self.test_plus.setFixedWidth(45)
self.test_plus.setStyleSheet("background-color: #ccffcc;")
self.test_plus.clicked.connect(lambda: self.test_movement.emit(self.joint_name, 10))
layout.addWidget(self.test_plus)
layout.addSpacing(10)
# Direction toggle
self.direction_button_group = QtWidgets.QButtonGroup(self)
self.forward_radio = QtWidgets.QRadioButton("Fwd")
self.forward_radio.setChecked(True)
self.direction_button_group.addButton(self.forward_radio, 1)
layout.addWidget(self.forward_radio)
self.reverse_radio = QtWidgets.QRadioButton("Rev")
self.direction_button_group.addButton(self.reverse_radio, -1)
layout.addWidget(self.reverse_radio)
self.forward_radio.clicked.connect(lambda: self._on_user_direction_click(1))
self.reverse_radio.clicked.connect(lambda: self._on_user_direction_click(-1))
def _on_user_direction_click(self, direction):
"""Called when user clicks a direction radio button"""
self.current_direction = direction
self.direction_changed.emit(self.joint_name, direction)
def set_direction(self, direction):
"""Set direction programmatically"""
self.current_direction = direction
if direction == 1:
self.forward_radio.setChecked(True)
else:
self.reverse_radio.setChecked(True)
def get_direction(self):
"""Get current direction value"""
return self.current_direction
class GripperCalibrationWidget(QtWidgets.QWidget):
"""Widget for calibrating gripper open/closed positions via slide-observe-capture"""
test_gripper = QtCore.pyqtSignal(int) # PWM value to send to servo
limits_changed = QtCore.pyqtSignal() # Emitted when open/closed limits are captured
def __init__(self, parent=None):
super().__init__(parent)
self._open_pwm = 255
self._closed_pwm = 0
self.setup_ui()
self.load_calibration()
def setup_ui(self):
main_layout = QtWidgets.QVBoxLayout(self)
desc = QtWidgets.QLabel(
"Drag the slider to move the gripper, then capture the open and closed positions."
)
desc.setWordWrap(True)
desc.setStyleSheet("color: #666; margin-bottom: 4px;")
main_layout.addWidget(desc)
frame = QtWidgets.QFrame()
frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
frame_layout = QtWidgets.QVBoxLayout(frame)
# Slider row
slider_row = QtWidgets.QHBoxLayout()
closed_label = QtWidgets.QLabel("Closed")
closed_label.setStyleSheet("font-weight: bold; color: #cc4444;")
slider_row.addWidget(closed_label)
self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
self.slider.setRange(0, 255)
self.slider.setValue(128)
self.slider.setMinimumHeight(28)
slider_row.addWidget(self.slider, 1)
open_label = QtWidgets.QLabel("Open")
open_label.setStyleSheet("font-weight: bold; color: #44aa44;")
slider_row.addWidget(open_label)
self.pwm_label = QtWidgets.QLabel("PWM: 128")
self.pwm_label.setMinimumWidth(60)
self.pwm_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
self.pwm_label.setStyleSheet("color: #666;")
slider_row.addWidget(self.pwm_label)
frame_layout.addLayout(slider_row)
# Update label live; send command on release
self.slider.valueChanged.connect(lambda v: self.pwm_label.setText(f"PWM: {v}"))
self.slider.sliderReleased.connect(lambda: self.test_gripper.emit(self.slider.value()))
# Capture buttons
btn_row = QtWidgets.QHBoxLayout()
self.set_closed_btn = QtWidgets.QPushButton("Set as Closed Limit")
self.set_closed_btn.setStyleSheet("background-color: #ffcccc;")
self.set_closed_btn.clicked.connect(self._capture_closed)
btn_row.addWidget(self.set_closed_btn)
self.set_open_btn = QtWidgets.QPushButton("Set as Open Limit")
self.set_open_btn.setStyleSheet("background-color: #ccffcc;")
self.set_open_btn.clicked.connect(self._capture_open)
btn_row.addWidget(self.set_open_btn)
frame_layout.addLayout(btn_row)
# Saved limits display
limits_row = QtWidgets.QHBoxLayout()
self.closed_limit_label = QtWidgets.QLabel("Closed: 0")
self.closed_limit_label.setStyleSheet("font-weight: bold; color: #cc4444;")
limits_row.addWidget(self.closed_limit_label)
limits_row.addStretch()
self.open_limit_label = QtWidgets.QLabel("Open: 255")
self.open_limit_label.setStyleSheet("font-weight: bold; color: #44aa44;")
limits_row.addWidget(self.open_limit_label)
frame_layout.addLayout(limits_row)
main_layout.addWidget(frame)
def _capture_open(self):
self._open_pwm = self.slider.value()
self._update_limit_labels()
self.limits_changed.emit()
def _capture_closed(self):
self._closed_pwm = self.slider.value()
self._update_limit_labels()
self.limits_changed.emit()
def _update_limit_labels(self):
self.closed_limit_label.setText(f"Closed: {self._closed_pwm}")
self.open_limit_label.setText(f"Open: {self._open_pwm}")
def get_open_pwm(self):
return self._open_pwm
def get_closed_pwm(self):
return self._closed_pwm
def load_calibration(self):
try:
if GRIPPER_CALIBRATION_FILE.exists():
with open(GRIPPER_CALIBRATION_FILE, 'r') as f:
data = json.load(f)
self._open_pwm = data.get('pwm_open', 255)
self._closed_pwm = data.get('pwm_closed', 0)
logger.info("Loaded gripper calibration from file")
else:
self._open_pwm = config.GRIPPER_PWM_OPEN
self._closed_pwm = config.GRIPPER_PWM_CLOSED
logger.info("Using default gripper calibration from config")
self._update_limit_labels()
self.apply_to_config()
except Exception as e:
logger.error(f"Error loading gripper calibration: {e}")
def apply_to_config(self):
config.GRIPPER_PWM_OPEN = self._open_pwm
config.GRIPPER_PWM_CLOSED = self._closed_pwm
logger.debug(f"Applied gripper PWM: open={self._open_pwm}, closed={self._closed_pwm}")
class DHParametersWidget(QtWidgets.QWidget):
"""Widget for editing DH parameters table"""
parameters_changed = QtCore.pyqtSignal() # Emitted when parameters are saved
preview_changed = QtCore.pyqtSignal() # Emitted when any value changes (for live preview)
def __init__(self, parent=None):
super().__init__(parent)
self.spinboxes = {}
self._loading = False
self.setup_ui()
self.load_parameters()
def setup_ui(self):
"""Create UI elements for DH parameters"""
main_layout = QtWidgets.QVBoxLayout(self)
main_layout.setContentsMargins(0, 0, 0, 0)
# Create table
self.table = QtWidgets.QTableWidget()
self.table.setColumnCount(5)
self.table.setHorizontalHeaderLabels(["Link", "θ offset (°)", "d (mm)", "a (mm)", "α (°)"])
self.table.setRowCount(6)
# Set column widths
header = self.table.horizontalHeader()
header.setSectionResizeMode(0, QtWidgets.QHeaderView.Fixed)
header.setSectionResizeMode(1, QtWidgets.QHeaderView.Stretch)
header.setSectionResizeMode(2, QtWidgets.QHeaderView.Stretch)
header.setSectionResizeMode(3, QtWidgets.QHeaderView.Stretch)
header.setSectionResizeMode(4, QtWidgets.QHeaderView.Stretch)
self.table.setColumnWidth(0, 40)
self.table.setAlternatingRowColors(True)
self.table.setMinimumHeight(220)
self.table.verticalHeader().setDefaultSectionSize(30)
self.table.setStyleSheet("""
QTableWidget {
background-color: #ffffff;
alternate-background-color: #f9f9f9;
gridline-color: #ddd;
border: 1px solid #ccc;
}
QHeaderView::section {
background-color: #e8e8e8;
padding: 4px;
border: 1px solid #ccc;
font-weight: bold;
}
""")
# Create widgets for each cell
for row in range(6):
# Link number (read-only)
link_item = QtWidgets.QTableWidgetItem(str(row + 1))
link_item.setFlags(QtCore.Qt.ItemIsEnabled)
link_item.setTextAlignment(QtCore.Qt.AlignCenter)
self.table.setItem(row, 0, link_item)
# theta_offset spinbox
spinbox = QtWidgets.QDoubleSpinBox()
spinbox.setRange(-360, 360)
spinbox.setDecimals(2)
spinbox.setSingleStep(1.0)
spinbox.setAlignment(QtCore.Qt.AlignCenter)
self.table.setCellWidget(row, 1, spinbox)
self.spinboxes[(row, 'theta_offset')] = spinbox
# d, a, alpha spinboxes
for col, param in enumerate(['d', 'a', 'alpha'], start=2):
spinbox = QtWidgets.QDoubleSpinBox()
spinbox.setRange(-1000 if param in ['d', 'a'] else -360, 1000 if param in ['d', 'a'] else 360)
spinbox.setDecimals(2)
spinbox.setSingleStep(1.0)
spinbox.setAlignment(QtCore.Qt.AlignCenter)
self.table.setCellWidget(row, col, spinbox)
self.spinboxes[(row, param)] = spinbox
main_layout.addWidget(self.table)
# Connect all spinboxes to emit preview_changed
for spinbox in self.spinboxes.values():
spinbox.valueChanged.connect(self._on_value_changed)
# Buttons (Load/Reset only — Save is handled by the panel-level Save All)
button_layout = QtWidgets.QHBoxLayout()
self.load_button = QtWidgets.QPushButton("Load")
self.load_button.clicked.connect(self.load_parameters)
button_layout.addWidget(self.load_button)
self.reset_button = QtWidgets.QPushButton("Reset to Default")
self.reset_button.clicked.connect(self.reset_to_default)
button_layout.addWidget(self.reset_button)
button_layout.addStretch()
main_layout.addLayout(button_layout)
def load_parameters(self):
"""Load DH parameters from file"""
self._loading = True
try:
if DH_PARAMS_FILE.exists():
with open(DH_PARAMS_FILE, 'r') as f:
dh_params = json.load(f)
for link_data in dh_params['links']:
row = link_data['link'] - 1
self.spinboxes[(row, 'theta_offset')].setValue(link_data['theta_offset'])
self.spinboxes[(row, 'd')].setValue(link_data['d'])
self.spinboxes[(row, 'a')].setValue(link_data['a'])
self.spinboxes[(row, 'alpha')].setValue(link_data['alpha'])
logger.info("Loaded DH parameters from file")
else:
self.reset_to_default()
except Exception as e:
logger.error(f"Error loading DH parameters: {e}")
finally:
self._loading = False
def _on_value_changed(self):
"""Handle spinbox/combo value change - emit preview signal"""
if not self._loading:
self.preview_changed.emit()
def get_parameters(self):
"""Get current DH parameters from the table as a list of dicts"""
params = []
descriptions = ["Base rotation", "Shoulder", "Elbow", "Wrist roll", "Wrist pitch", "Wrist yaw / TCP"]
for row in range(6):
params.append({
"link": row + 1,
"theta_offset": self.spinboxes[(row, 'theta_offset')].value(),
"d": self.spinboxes[(row, 'd')].value(),
"a": self.spinboxes[(row, 'a')].value(),
"alpha": self.spinboxes[(row, 'alpha')].value(),
"description": descriptions[row],
})
return params
def save_parameters(self):
"""Save DH parameters to file"""
try:
descriptions = ["Base rotation", "Shoulder", "Elbow", "Wrist roll", "Wrist pitch", "Wrist yaw / TCP"]
dh_data = {
"version": "1.1",
"description": "ThorRR Robot DH Parameters",
"date_modified": QtCore.QDateTime.currentDateTime().toString("yyyy-MM-dd"),
"links": []
}
for row in range(6):
link_data = {
"link": row + 1,
"theta_offset": self.spinboxes[(row, 'theta_offset')].value(),
"d": self.spinboxes[(row, 'd')].value(),
"a": self.spinboxes[(row, 'a')].value(),
"alpha": self.spinboxes[(row, 'alpha')].value(),
"description": descriptions[row]
}
dh_data['links'].append(link_data)
with open(DH_PARAMS_FILE, 'w') as f:
json.dump(dh_data, f, indent=4)
reload_dh_parameters()
self.parameters_changed.emit()
logger.info("Saved DH parameters to file")
except Exception as e:
logger.error(f"Error saving DH parameters: {e}")
raise
def reset_to_default(self):
"""Reset to default ThorRR DH parameters"""
self._loading = True
try:
default_params = [
{"theta_offset": 0, "d": 202, "a": 0, "alpha": 90},
{"theta_offset": 90, "d": 0, "a": 160, "alpha": 0},
{"theta_offset": 90, "d": 0, "a": 0, "alpha": 90},
{"theta_offset": 0, "d": 195, "a": 0, "alpha": -90},
{"theta_offset": 0, "d": 0, "a": 0, "alpha": 90},
{"theta_offset": 0, "d": 67.15, "a": 0, "alpha": 0},
]
for row, params in enumerate(default_params):
self.spinboxes[(row, 'theta_offset')].setValue(params['theta_offset'])
self.spinboxes[(row, 'd')].setValue(params['d'])
self.spinboxes[(row, 'a')].setValue(params['a'])
self.spinboxes[(row, 'alpha')].setValue(params['alpha'])
logger.info("Reset DH parameters to defaults")
finally:
self._loading = False
class HomePositionWidget(QtWidgets.QWidget):
"""Widget for setting the software home position (joint angles)"""
position_changed = QtCore.pyqtSignal()
# Joint limits matching the main GUI spinboxes
JOINT_LIMITS = {
'Art1': (-97, 97),
'Art2': (-90, 90),
'Art3': (-90, 90),
'Art4': (-180, 180),
'Art5': (-90, 90),
'Art6': (-180, 180),
}
def __init__(self, parent=None):
super().__init__(parent)
self.spinboxes = {}
self._loading = False
self.setup_ui()
self.load_from_config()
def setup_ui(self):
layout = QtWidgets.QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
desc = QtWidgets.QLabel(
"Joint angles the robot moves to when the Home button is pressed."
)
desc.setWordWrap(True)
desc.setStyleSheet("color: #666; margin-bottom: 4px;")
layout.addWidget(desc)
# Grid of joint spinboxes
grid = QtWidgets.QGridLayout()
for i, (joint, (lo, hi)) in enumerate(self.JOINT_LIMITS.items()):
label = QtWidgets.QLabel(f"{joint}:")
label.setFixedWidth(40)
grid.addWidget(label, i // 3, (i % 3) * 2)
spinbox = QtWidgets.QDoubleSpinBox()
spinbox.setRange(lo, hi)
spinbox.setDecimals(1)
spinbox.setSuffix("°")
spinbox.setValue(config.HOME_POSITION.get(joint, 0.0))
spinbox.valueChanged.connect(self._on_value_changed)
grid.addWidget(spinbox, i // 3, (i % 3) * 2 + 1)
self.spinboxes[joint] = spinbox
layout.addLayout(grid)
# Buttons
btn_layout = QtWidgets.QHBoxLayout()
self.capture_button = QtWidgets.QPushButton("Capture Current")
self.capture_button.setToolTip("Set home position to current joint angles")
self.capture_button.clicked.connect(self._capture_current)
btn_layout.addWidget(self.capture_button)
self.reset_button = QtWidgets.QPushButton("Reset to Zero")
self.reset_button.clicked.connect(self._reset_to_zero)
btn_layout.addWidget(self.reset_button)
btn_layout.addStretch()
layout.addLayout(btn_layout)
def load_from_config(self):
"""Load values from config.HOME_POSITION into spinboxes"""
self._loading = True
for joint, spinbox in self.spinboxes.items():
spinbox.setValue(config.HOME_POSITION.get(joint, 0.0))
self._loading = False
def _on_value_changed(self):
if not self._loading:
self._save()
self.position_changed.emit()
def _save(self):
"""Save current spinbox values to config and file"""
data = {}
for joint, spinbox in self.spinboxes.items():
val = spinbox.value()
config.HOME_POSITION[joint] = val
data[joint] = val
try:
with open(HOME_POSITION_FILE, 'w') as f:
json.dump(data, f, indent=4)
logger.debug(f"Saved home position: {data}")
except Exception as e:
logger.error(f"Error saving home position: {e}")
def _capture_current(self):
"""Capture current joint angles from the main GUI spinboxes"""
gui = self._find_gui_instance()
if not gui:
logger.warning("Cannot capture: no GUI instance found")
return
self._loading = True
for joint in self.spinboxes:
spinbox_name = f'SpinBox{joint}'
if hasattr(gui, spinbox_name):
val = getattr(gui, spinbox_name).value()
self.spinboxes[joint].setValue(val)
self._loading = False
self._save()
self.position_changed.emit()
def _reset_to_zero(self):
self._loading = True
for spinbox in self.spinboxes.values():
spinbox.setValue(0.0)
self._loading = False
self._save()
self.position_changed.emit()
def _find_gui_instance(self):
"""Walk up the parent chain to find the CalibrationPanel's gui_instance"""
parent = self.parent()
while parent:
if hasattr(parent, 'gui_instance'):
return parent.gui_instance
parent = parent.parent()
return None
class ParkPositionWidget(QtWidgets.QWidget):
"""Widget for setting the park position (joint angles before shutdown)"""
position_changed = QtCore.pyqtSignal()
JOINT_LIMITS = {
'Art1': (-97, 97),
'Art2': (-90, 90),
'Art3': (-90, 90),
'Art4': (-180, 180),
'Art5': (-90, 90),
'Art6': (-180, 180),
}
def __init__(self, parent=None):
super().__init__(parent)
self.spinboxes = {}
self._loading = False
self.setup_ui()
self.load_from_config()
def setup_ui(self):
layout = QtWidgets.QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
desc = QtWidgets.QLabel(
"Position the robot moves to before shutdown. "
"Gripper closes and motors disable after arrival."
)
desc.setWordWrap(True)
desc.setStyleSheet("color: #666; margin-bottom: 4px;")
layout.addWidget(desc)
grid = QtWidgets.QGridLayout()
for i, (joint, (lo, hi)) in enumerate(self.JOINT_LIMITS.items()):
label = QtWidgets.QLabel(f"{joint}:")
label.setFixedWidth(40)
grid.addWidget(label, i // 3, (i % 3) * 2)
spinbox = QtWidgets.QDoubleSpinBox()
spinbox.setRange(lo, hi)
spinbox.setDecimals(1)
spinbox.setSuffix("\u00b0")
spinbox.setValue(config.PARK_POSITION.get(joint, 0.0))
spinbox.valueChanged.connect(self._on_value_changed)
grid.addWidget(spinbox, i // 3, (i % 3) * 2 + 1)
self.spinboxes[joint] = spinbox
layout.addLayout(grid)
btn_layout = QtWidgets.QHBoxLayout()
self.capture_button = QtWidgets.QPushButton("Capture Current")
self.capture_button.setToolTip("Set park position to current joint angles")
self.capture_button.clicked.connect(self._capture_current)
btn_layout.addWidget(self.capture_button)
self.reset_button = QtWidgets.QPushButton("Reset to Default")
self.reset_button.clicked.connect(self._reset_to_default)
btn_layout.addWidget(self.reset_button)
btn_layout.addStretch()
layout.addLayout(btn_layout)
def load_from_config(self):
self._loading = True
for joint, spinbox in self.spinboxes.items():
spinbox.setValue(config.PARK_POSITION.get(joint, 0.0))
self._loading = False
def _on_value_changed(self):
if not self._loading:
self._save()
self.position_changed.emit()
def _save(self):
data = {}
for joint, spinbox in self.spinboxes.items():
val = spinbox.value()
config.PARK_POSITION[joint] = val
data[joint] = val
try:
with open(PARK_POSITION_FILE, 'w') as f:
json.dump(data, f, indent=4)
logger.debug(f"Saved park position: {data}")
except Exception as e:
logger.error(f"Error saving park position: {e}")
def _capture_current(self):
gui = self._find_gui_instance()
if not gui:
logger.warning("Cannot capture: no GUI instance found")
return
self._loading = True
for joint in self.spinboxes:
spinbox_name = f'SpinBox{joint}'
if hasattr(gui, spinbox_name):
val = getattr(gui, spinbox_name).value()
self.spinboxes[joint].setValue(val)
self._loading = False
self._save()
self.position_changed.emit()
def _reset_to_default(self):
self._loading = True
defaults = {'Art1': 0.0, 'Art2': 90.0, 'Art3': -90.0,
'Art4': 0.0, 'Art5': 0.0, 'Art6': 0.0}
for joint, spinbox in self.spinboxes.items():
spinbox.setValue(defaults.get(joint, 0.0))
self._loading = False
self._save()
self.position_changed.emit()
def _find_gui_instance(self):
parent = self.parent()
while parent:
if hasattr(parent, 'gui_instance'):
return parent.gui_instance
parent = parent.parent()
return None
class CalibrationPanel(QtWidgets.QWidget):
"""Main calibration panel with direction verification, gripper calibration, and DH parameters"""
def __init__(self, gui_instance, parent=None):
super().__init__(parent)
self.gui_instance = gui_instance
self.joint_widgets = {}
self.setup_ui()
self.load_current_calibration()
logger.info("Calibration panel initialised")
def setup_ui(self):
"""Create the calibration panel UI"""
main_layout = QtWidgets.QVBoxLayout(self)
# Header
header = QtWidgets.QLabel("<h2>Robot Calibration</h2>")
main_layout.addWidget(header)
# Scroll area
scroll = QtWidgets.QScrollArea()
scroll.setWidgetResizable(True)
scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
scroll_content = QtWidgets.QWidget()
scroll_layout = QtWidgets.QVBoxLayout(scroll_content)
# --- Joint Calibration ---
joint_section = CollapsibleSection("Joint Calibration", is_expanded=True)
instructions = QtWidgets.QLabel(
"Click +10\u00b0 to test each joint. If the robot moves opposite "
"to the visualization, toggle to Rev."
)
instructions.setWordWrap(True)
instructions.setStyleSheet("color: #666; margin-bottom: 4px;")
joint_section.add_widget(instructions)
joint_info = [
('Art1', 'Base rotation'),
('Art2', 'Shoulder pitch'),
('Art3', 'Elbow pitch'),
('Art4', 'Wrist roll'),
('Art5', 'Wrist pitch'),
('Art6', 'Wrist yaw')
]
for joint_name, description in joint_info:
widget = JointCalibrationWidget(joint_name, description)
widget.test_movement.connect(self.on_test_movement)
widget.direction_changed.connect(self.on_joint_direction_changed)
self.joint_widgets[joint_name] = widget
joint_section.add_widget(widget)
scroll_layout.addWidget(joint_section)
# --- Gripper Calibration ---
gripper_section = CollapsibleSection("Gripper Calibration", is_expanded=False)
self.gripper_calibration = GripperCalibrationWidget()
self.gripper_calibration.test_gripper.connect(self.on_test_gripper)
self.gripper_calibration.limits_changed.connect(self._auto_save_gripper)
gripper_section.add_widget(self.gripper_calibration)
scroll_layout.addWidget(gripper_section)
# --- Home Position ---
home_section = CollapsibleSection("Home Position", is_expanded=False)
self.home_position = HomePositionWidget()
home_section.add_widget(self.home_position)
scroll_layout.addWidget(home_section)
# --- Park Position ---
park_section = CollapsibleSection("Park Position", is_expanded=False)
self.park_position = ParkPositionWidget()
park_section.add_widget(self.park_position)
scroll_layout.addWidget(park_section)
# --- DH Parameters ---
dh_section = CollapsibleSection("DH Parameters", is_expanded=False)
self.dh_parameters = DHParametersWidget()
dh_section.add_widget(self.dh_parameters)
scroll_layout.addWidget(dh_section)
# Accordion: expanding one section collapses the others
self._sections = [joint_section, gripper_section, home_section,
park_section, dh_section]
for section in self._sections:
section.expanded.connect(self._on_section_expanded)
scroll_layout.addStretch()
scroll.setWidget(scroll_content)
main_layout.addWidget(scroll)
# Save All button at the bottom
self.save_all_button = QtWidgets.QPushButton("Save All Calibration Settings")
self.save_all_button.setStyleSheet(
"background-color: #4CAF50; color: white; font-weight: bold; "
"padding: 8px; font-size: 13px;"
)
self.save_all_button.clicked.connect(self.save_all)
main_layout.addWidget(self.save_all_button)
# Status bar
self.status_label = QtWidgets.QLabel("Status: Ready to calibrate")
self.status_label.setStyleSheet("background-color: #e0e0e0; padding: 5px;")
main_layout.addWidget(self.status_label)
def _on_section_expanded(self, opened_section):
"""Accordion: collapse every section except the one just opened."""
for section in self._sections:
if section is not opened_section:
section.collapse()
def on_test_movement(self, joint_name, delta_angle):
"""Handle test movement button clicks"""
logger.info(f"Test movement: {joint_name} {delta_angle:+.1f}°")
# Get the corresponding spinbox from main GUI
joint_spinbox_map = {
'Art1': 'SpinBoxArt1',
'Art2': 'SpinBoxArt2',
'Art3': 'SpinBoxArt3',
'Art4': 'SpinBoxArt4',
'Art5': 'SpinBoxArt5',
'Art6': 'SpinBoxArt6'
}
spinbox_name = joint_spinbox_map.get(joint_name)
if spinbox_name and hasattr(self.gui_instance, spinbox_name):
spinbox = getattr(self.gui_instance, spinbox_name)
new_value = spinbox.value() + delta_angle
spinbox.setValue(new_value)
# Execute movement
self.gui_instance.FKMoveJoint(joint_name)
self.status_label.setText(f"Status: Moved {joint_name} {delta_angle:+.1f}° → {new_value:.1f}°")
self.status_label.setStyleSheet("background-color: #ccffcc; padding: 5px;")
def on_joint_direction_changed(self, joint_name, direction):
"""Send M569 command to firmware and update config.g for persistence."""
drives = JOINT_TO_DRIVES.get(joint_name)
if drives is None:
return
try:
s_value = 0 if direction == 1 else 1
dir_label = 'Forward' if direction == 1 else 'Reverse'
# Send M569 commands to firmware for each drive
commands_sent = 0
if self.gui_instance and hasattr(self.gui_instance, 'command_sender'):
for drive in drives:
command = f"M569 P{drive} S{s_value}"
if self.gui_instance.command_sender.send_if_connected(command):
commands_sent += 1
else:
logger.warning(f"Not connected - M569 P{drive} not sent")
# Always persist to config.g (even when not connected)
config_g_path = paths.get_exe_dir() / 'Firmware Configs' / 'config.g'
set_joint_direction(config_g_path, joint_name, direction)
if commands_sent > 0:
logger.info(f"{joint_name} direction set to {dir_label} (sent {commands_sent} M569 command(s))")
self.status_label.setText(f"Status: {joint_name} = {dir_label} (M569 sent)")
else:
logger.info(f"{joint_name} direction set to {dir_label} (saved to config.g, not connected)")
self.status_label.setText(f"Status: {joint_name} = {dir_label} (saved, not connected)")
self.status_label.setStyleSheet("background-color: #ccffcc; padding: 5px;")
except Exception as e:
logger.error(f"Error updating {joint_name} direction: {e}")
self.status_label.setText(f"Status: Error - {e}")
self.status_label.setStyleSheet("background-color: #ffcccc; padding: 5px;")
def on_test_gripper(self, pwm_value):
"""Handle gripper test button clicks - send direct PWM command"""
logger.info(f"Test gripper PWM: {pwm_value}")
if not self.gui_instance:
self.status_label.setText("Status: No GUI instance - cannot send command")
self.status_label.setStyleSheet("background-color: #ffcccc; padding: 5px;")
return
# Convert PWM to servo angle (0-255 PWM -> 0-180 servo angle)
servo_angle = int((pwm_value / 255.0) * 180.0)
command = f"M280 P0 S{servo_angle}"
# Send via command_sender if connected
if hasattr(self.gui_instance, 'command_sender') and self.gui_instance.command_sender:
sent = self.gui_instance.command_sender.send_if_connected(command)
if sent:
self.status_label.setText(f"Status: Sent gripper test PWM={pwm_value} (angle={servo_angle}°)")
self.status_label.setStyleSheet("background-color: #ccffcc; padding: 5px;")
else:
self.status_label.setText("Status: Not connected - cannot send gripper command")
self.status_label.setStyleSheet("background-color: #ffcccc; padding: 5px;")
else:
self.status_label.setText("Status: Command sender not available")
self.status_label.setStyleSheet("background-color: #ffcccc; padding: 5px;")
def load_current_calibration(self):
"""Load direction settings from config.g M569 commands"""
try: