-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui_modern.py
More file actions
2386 lines (1992 loc) · 94 KB
/
gui_modern.py
File metadata and controls
2386 lines (1992 loc) · 94 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
# -*- coding: utf-8 -*-
"""
Bifrost Modern UI - Mode-Based Interface
Industry-standard robot control interface with mode switching
Modes:
- CONTROL: Unified IK/FK target control with sequence recording
- TEACH: Dedicated sequence programming
- TERMINAL: Console and debugging
- CALIBRATE: DH parameters and calibration
- FRAMES: Coordinate frame management
"""
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtWidgets import (QWidget, QMainWindow, QVBoxLayout, QHBoxLayout,
QGridLayout, QLabel, QPushButton, QRadioButton,
QDoubleSpinBox, QCheckBox, QComboBox, QSpinBox,
QFrame, QButtonGroup, QStackedWidget, QTableWidget,
QTableWidgetItem, QHeaderView, QPlainTextEdit,
QLineEdit, QListWidget, QGroupBox, QSizePolicy)
from robot_3d_visualizer import Robot3DCanvas
class TableItemLabelWrapper:
"""
Wrapper class to make QTableWidgetItem behave like QLabel for compatibility
with existing bifrost.py code that calls .setText() on labels
"""
def __init__(self, table_item):
self.table_item = table_item
def setText(self, text):
"""Delegate setText to QTableWidgetItem"""
self.table_item.setText(text)
def text(self):
"""Delegate text() to QTableWidgetItem"""
return self.table_item.text()
def setStyleSheet(self, stylesheet):
"""Handle stylesheet by setting background colour for table items"""
# Extract background-color from stylesheet
if "background-color" in stylesheet:
# Simple parsing for common patterns
if "rgb(200, 255, 200)" in stylesheet:
self.table_item.setBackground(QtGui.QColor(200, 255, 200))
elif "rgb(255, 200, 200)" in stylesheet:
self.table_item.setBackground(QtGui.QColor(255, 200, 200))
elif "rgb(255, 255, 200)" in stylesheet:
self.table_item.setBackground(QtGui.QColor(255, 255, 200))
elif "rgb(200, 200, 200)" in stylesheet:
self.table_item.setBackground(QtGui.QColor(200, 200, 200))
# Ignore other stylesheet properties for table items
class ModernMainWindow(QMainWindow):
"""Modern mode-based GUI for Bifrost robot control"""
def __init__(self):
super().__init__()
self.setWindowTitle("Bifrost - ThorRR Robot Control")
self.setMinimumSize(1024, 600)
# Create central widget
self.centralwidget = QWidget()
self.setCentralWidget(self.centralwidget)
# Main layout
main_layout = QVBoxLayout(self.centralwidget)
main_layout.setContentsMargins(5, 5, 5, 5)
main_layout.setSpacing(5)
# Top bar (connection status)
self.top_bar = ConnectionBar()
main_layout.addWidget(self.top_bar)
# Mode selector bar
self.mode_selector = ModeSelectorBar()
main_layout.addWidget(self.mode_selector)
# Main content area (split left/right)
content_splitter = QtWidgets.QSplitter(Qt.Horizontal)
# Left panel: Mode-specific controls (40%)
self.mode_stack = QStackedWidget()
content_splitter.addWidget(self.mode_stack)
# Right panel: Unified robot state (60%)
self.robot_state_panel = RobotStatePanel()
content_splitter.addWidget(self.robot_state_panel)
# Set splitter proportions and initial sizes
content_splitter.setStretchFactor(0, 40) # Left: 40%
content_splitter.setStretchFactor(1, 60) # Right: 60%
# Force initial sizes (40% / 60% of 1200px window = 480px / 720px)
content_splitter.setSizes([480, 720])
# Set minimum width for left panel so it can't be collapsed
self.mode_stack.setMinimumWidth(350)
# Prevent splitter panels from collapsing
content_splitter.setCollapsible(0, False) # Left panel
content_splitter.setCollapsible(1, False) # Right panel
main_layout.addWidget(content_splitter)
# Create mode panels
self.setup_mode_panels()
# Connect mode switching
self.mode_selector.mode_changed.connect(self.switch_mode)
# Default to JOG mode
self.switch_mode(0)
def setup_mode_panels(self):
"""Create all mode-specific panels (JOG removed - controls in sidebar)"""
# Mode 0: CONTROL (unified IK/FK + sequence recording)
self.control_panel = ControlModePanel()
self.mode_stack.addWidget(self.control_panel)
# Mode 1: TEACH
self.teach_panel = TeachModePanel()
self.mode_stack.addWidget(self.teach_panel)
# Mode 2: TERMINAL
self.terminal_panel = TerminalModePanel()
self.mode_stack.addWidget(self.terminal_panel)
# Mode 3: CALIBRATE (includes DH parameters)
from calibration_panel import CalibrationPanel
# gui_instance will be set later by BifrostGUI
self.calibration_panel = CalibrationPanel(gui_instance=None)
self.mode_stack.addWidget(self.calibration_panel)
# Mode 4: FRAMES
from frame_panel import FrameManagementPanel
self.frames_panel = FrameManagementPanel()
self.mode_stack.addWidget(self.frames_panel)
def switch_mode(self, mode_index):
"""Switch to specified mode"""
self.mode_stack.setCurrentIndex(mode_index)
class ConnectionBar(QFrame):
"""Top bar showing connection status and settings"""
def __init__(self):
super().__init__()
self.setFrameShape(QFrame.StyledPanel)
self.setMinimumHeight(50)
self.setMaximumHeight(50)
self.setStyleSheet("ConnectionBar { background-color: #e8e8e8; border-bottom: 2px solid #ccc; }")
layout = QHBoxLayout(self)
layout.setContentsMargins(10, 5, 10, 5)
layout.setSpacing(10)
# App title
title = QLabel("Bifrost")
title_font = QtGui.QFont()
title_font.setPointSize(12)
title_font.setBold(True)
title.setFont(title_font)
layout.addWidget(title)
layout.addWidget(QLabel("|"))
# Serial port
self.SerialPortLabel = QLabel("COM Port:")
layout.addWidget(self.SerialPortLabel)
self.SerialPortComboBox = QComboBox()
self.SerialPortComboBox.setMinimumWidth(80)
layout.addWidget(self.SerialPortComboBox)
self.SerialPortRefreshButton = QPushButton("⟳")
self.SerialPortRefreshButton.setMaximumWidth(30)
self.SerialPortRefreshButton.setToolTip("Refresh serial ports")
layout.addWidget(self.SerialPortRefreshButton)
# Baud rate
layout.addWidget(QLabel("@"))
self.BaudRateComboBox = QComboBox()
self.BaudRateComboBox.setMinimumWidth(80)
self.BaudRateComboBox.addItems([
"9600", "14400", "19200", "28800", "38400",
"57600", "115200", "230400", "250000", "500000", "1000000", "2000000"
])
self.BaudRateComboBox.setCurrentText("115200")
layout.addWidget(self.BaudRateComboBox)
layout.addWidget(QLabel("|"))
# Connection status
layout.addWidget(QLabel("Status:"))
self.RobotStateDisplay = QLabel("Disconnected")
self.RobotStateDisplay.setFrameShape(QFrame.Box)
self.RobotStateDisplay.setMinimumWidth(100)
self.RobotStateDisplay.setAlignment(Qt.AlignCenter)
self.RobotStateDisplay.setStyleSheet("background-color: rgb(255, 0, 0); font-weight: bold; padding: 3px;")
layout.addWidget(self.RobotStateDisplay)
layout.addWidget(QLabel("|"))
# Simulation mode checkbox
self.SimulationModeCheckBox = QCheckBox("Simulation Mode")
self.SimulationModeCheckBox.setToolTip("Enable to use simulated robot (no hardware required)")
layout.addWidget(self.SimulationModeCheckBox)
layout.addStretch()
# Connect/Disconnect button
self.ConnectButton = QPushButton("Connect")
self.ConnectButton.setMinimumWidth(100)
self.ConnectButton.setMinimumHeight(35)
connect_font = QtGui.QFont()
connect_font.setBold(True)
self.ConnectButton.setFont(connect_font)
layout.addWidget(self.ConnectButton)
layout.addWidget(QLabel("|"))
# Emergency Stop button - freezes motors and stops sequences
self.EmergencyStopButton = QPushButton("🛑 E-STOP")
self.EmergencyStopButton.setMinimumWidth(100)
self.EmergencyStopButton.setMinimumHeight(35)
estop_font = QtGui.QFont()
estop_font.setBold(True)
estop_font.setPointSize(10)
self.EmergencyStopButton.setFont(estop_font)
self.EmergencyStopButton.setStyleSheet("""
QPushButton {
background-color: #D32F2F;
color: white;
border: 3px solid #B71C1C;
border-radius: 4px;
}
QPushButton:hover {
background-color: #C62828;
}
QPushButton:pressed {
background-color: #B71C1C;
}
""")
self.EmergencyStopButton.setToolTip("Emergency Stop - Freeze motors and abort sequences")
layout.addWidget(self.EmergencyStopButton)
# About button
self.SettingsButton = QPushButton("ℹ")
self.SettingsButton.setMaximumWidth(40)
self.SettingsButton.setMinimumHeight(35)
self.SettingsButton.setToolTip("About")
layout.addWidget(self.SettingsButton)
class ModeSelectorBar(QFrame):
"""Mode selection buttons"""
mode_changed = pyqtSignal(int)
def __init__(self):
super().__init__()
self.setFrameShape(QFrame.StyledPanel)
self.setMinimumHeight(50)
self.setMaximumHeight(50)
self.setStyleSheet("ModeSelectorBar { background-color: #d8d8d8; border-bottom: 2px solid #bbb; }")
layout = QHBoxLayout(self)
layout.setContentsMargins(10, 5, 10, 5)
layout.setSpacing(10)
# Mode buttons (JOG removed - axis controls now in sidebar)
self.mode_group = QButtonGroup()
self.btn_inverse = QPushButton("🎮 CONTROL")
self.btn_inverse.setCheckable(True)
self.btn_inverse.setMinimumHeight(35)
self.btn_inverse.setMinimumWidth(130)
self.mode_group.addButton(self.btn_inverse, 0)
layout.addWidget(self.btn_inverse)
self.btn_teach = QPushButton("💾 TEACH")
self.btn_teach.setCheckable(True)
self.btn_teach.setMinimumHeight(35)
self.btn_teach.setMinimumWidth(120)
self.mode_group.addButton(self.btn_teach, 1)
layout.addWidget(self.btn_teach)
# Index where addon buttons will be inserted (after TEACH)
self._addon_insert_index = layout.count()
# Stretch to push remaining buttons to the right
layout.addStretch()
self.btn_terminal = QPushButton("🖥 TERMINAL")
self.btn_terminal.setCheckable(True)
self.btn_terminal.setMinimumHeight(35)
self.btn_terminal.setMinimumWidth(140)
self.mode_group.addButton(self.btn_terminal, 2)
layout.addWidget(self.btn_terminal)
self.btn_calibrate = QPushButton("🔧 CALIBRATE")
self.btn_calibrate.setCheckable(True)
self.btn_calibrate.setMinimumHeight(35)
self.btn_calibrate.setMinimumWidth(150)
self.mode_group.addButton(self.btn_calibrate, 3)
layout.addWidget(self.btn_calibrate)
self.btn_frames = QPushButton("📐 FRAMES")
self.btn_frames.setCheckable(True)
self.btn_frames.setMinimumHeight(35)
self.btn_frames.setMinimumWidth(120)
self.mode_group.addButton(self.btn_frames, 4)
layout.addWidget(self.btn_frames)
# Set default to INVERSE (was JOG, now removed)
self.btn_inverse.setChecked(True)
# Connect signals
self.mode_group.buttonClicked.connect(self.on_mode_clicked)
# Style selected button
self.update_button_styles()
def on_mode_clicked(self, button):
"""Handle mode button click"""
mode_id = self.mode_group.id(button)
self.mode_changed.emit(mode_id)
self.update_button_styles()
def add_mode(self, label, icon=""):
"""Dynamically add a mode button (used by addon system).
Args:
label: Text for the button.
icon: Optional emoji prefix.
Returns:
int: The assigned mode ID.
"""
text = f"{icon} {label}" if icon else label
btn = QPushButton(text)
btn.setCheckable(True)
btn.setMinimumHeight(35)
btn.setMinimumWidth(120)
mode_id = len(self.mode_group.buttons())
self.mode_group.addButton(btn, mode_id)
# Insert after TEACH (before the stretch), not at the end
self.layout().insertWidget(self._addon_insert_index, btn)
self._addon_insert_index += 1
self.update_button_styles()
return mode_id
def update_button_styles(self):
"""Update visual style of mode buttons"""
for button in self.mode_group.buttons():
if button.isChecked():
button.setStyleSheet("""
QPushButton {
background-color: #4CAF50;
color: white;
font-weight: bold;
border: 2px solid #45a049;
border-radius: 3px;
}
""")
else:
button.setStyleSheet("""
QPushButton {
background-color: #f0f0f0;
color: #333;
border: 1px solid #ccc;
border-radius: 3px;
}
QPushButton:hover {
background-color: #e0e0e0;
}
""")
class AxisRow(QFrame):
"""Single axis control row with +/- buttons and value display"""
def __init__(self, joint_name, axis_label, is_gripper=False, is_cartesian=False):
super().__init__()
self.joint_name = joint_name
self.is_gripper = is_gripper
self.is_cartesian = is_cartesian
self.axis_label = axis_label
layout = QVBoxLayout(self)
layout.setContentsMargins(2, 2, 2, 2)
layout.setSpacing(1)
# Top row: Joint/axis label + indicators
top_row = QHBoxLayout()
top_row.setSpacing(2)
# Label formatting depends on mode
if is_gripper:
label_text = "Grip"
elif is_cartesian:
# Cartesian mode: "X (mm)" or "Roll (°)"
label_text = f"{joint_name} ({axis_label})"
else:
# Joint mode: "A1", "A2", etc.
label_text = joint_name
self.label = QLabel(label_text)
self.label.setStyleSheet("font-size: 8pt; font-weight: bold;")
top_row.addWidget(self.label)
top_row.addStretch()
# Endstop indicator (small colored dot)
self.endstop_indicator = QLabel("●")
self.endstop_indicator.setStyleSheet("font-size: 7pt; color: #4CAF50;") # Green = OK
self.endstop_indicator.setToolTip("Endstop status")
top_row.addWidget(self.endstop_indicator)
# Position match indicator
self.position_indicator = QLabel("○")
self.position_indicator.setStyleSheet("font-size: 7pt; color: #888;")
self.position_indicator.setToolTip("Position match")
top_row.addWidget(self.position_indicator)
layout.addLayout(top_row)
# Bottom row: [-] value [+]
bottom_row = QHBoxLayout()
bottom_row.setSpacing(2)
# Minus button
self.minus_btn = QPushButton("-")
self.minus_btn.setFixedSize(24, 24)
self.minus_btn.setStyleSheet("font-size: 10pt; font-weight: bold;")
self.minus_btn.setProperty("joint", joint_name)
self.minus_btn.setProperty("direction", -1)
bottom_row.addWidget(self.minus_btn)
# Value label (read-only)
self.value_label = QLabel("0.0" if not is_gripper else "0%")
self.value_label.setAlignment(Qt.AlignCenter)
self.value_label.setStyleSheet("font-size: 8pt; background: #f0f0f0; border-radius: 2px; padding: 2px;")
self.value_label.setMinimumWidth(40)
bottom_row.addWidget(self.value_label, 1)
# Plus button
self.plus_btn = QPushButton("+")
self.plus_btn.setFixedSize(24, 24)
self.plus_btn.setStyleSheet("font-size: 10pt; font-weight: bold;")
self.plus_btn.setProperty("joint", joint_name)
self.plus_btn.setProperty("direction", 1)
bottom_row.addWidget(self.plus_btn)
layout.addLayout(bottom_row)
# Gripper preset buttons row (Open / Closed)
if is_gripper:
preset_row = QHBoxLayout()
preset_row.setSpacing(2)
self.close_btn = QPushButton("Close")
self.close_btn.setFixedHeight(22)
self.close_btn.setStyleSheet("font-size: 7pt; background: #ffcccc;")
preset_row.addWidget(self.close_btn)
self.open_btn = QPushButton("Open")
self.open_btn.setFixedHeight(22)
self.open_btn.setStyleSheet("font-size: 7pt; background: #ccffcc;")
preset_row.addWidget(self.open_btn)
layout.addLayout(preset_row)
# Separator line at bottom
self.setStyleSheet("AxisRow { border-bottom: 1px solid #ddd; }")
def set_value(self, value):
"""Update the displayed value"""
if self.is_gripper:
self.value_label.setText(f"{int(value)}%")
elif self.is_cartesian:
# Cartesian mode: mm for position, ° for orientation
if self.joint_name in ['X', 'Y', 'Z']:
self.value_label.setText(f"{value:.1f}")
else:
# Roll, Pitch, Yaw - degrees
self.value_label.setText(f"{value:.1f}")
else:
# Joint mode - degrees
self.value_label.setText(f"{value:.1f}")
def set_endstop_status(self, triggered):
"""Update endstop indicator colour"""
if triggered:
self.endstop_indicator.setStyleSheet("font-size: 7pt; color: #f44336;") # Red
else:
self.endstop_indicator.setStyleSheet("font-size: 7pt; color: #4CAF50;") # Green
def set_position_match(self, matched):
"""Update position match indicator"""
if matched:
self.position_indicator.setStyleSheet("font-size: 7pt; color: #4CAF50;") # Green
else:
self.position_indicator.setStyleSheet("font-size: 7pt; color: #888;") # Grey
class AxisControlColumn(QFrame):
"""Narrow vertical column with axis controls and step toggle"""
# Signal emitted when step value changes
step_changed = pyqtSignal(float)
# Signal emitted when control mode changes (joint vs cartesian)
mode_changed = pyqtSignal(str) # "joint", "cartesian"
# Signal emitted when coordinate frame changes
frame_changed = pyqtSignal(str) # frame name: "base", "tool", "world"
# Signal emitted when movement type changes (g0=rapid, g1=feed)
movement_type_changed = pyqtSignal(str) # "G0", "G1"
# Signal emitted when feedrate changes
feedrate_changed = pyqtSignal(float)
# Signal emitted when tool selection changes
tool_changed = pyqtSignal(str) # tool name
# Control mode definitions
JOINT_MODE = "joint"
CARTESIAN_MODE = "cartesian"
def __init__(self):
super().__init__()
self.setFrameShape(QFrame.StyledPanel)
self.setFixedWidth(105)
self.current_step = 1.0 # Default step size
self.current_mode = self.JOINT_MODE # Default to joint control
self.current_frame = "base" # Default coordinate frame
layout = QVBoxLayout(self)
layout.setContentsMargins(3, 3, 3, 3)
layout.setSpacing(0)
# Mode selector (Joint / XYZ toggle)
mode_frame = QFrame()
mode_frame.setStyleSheet("background: #d0d0d0; border-radius: 3px;")
mode_layout = QHBoxLayout(mode_frame)
mode_layout.setContentsMargins(2, 2, 2, 2)
mode_layout.setSpacing(2)
self.joint_mode_btn = QPushButton("Joint")
self.joint_mode_btn.setCheckable(True)
self.joint_mode_btn.setChecked(True)
self.joint_mode_btn.setFixedHeight(22)
self.joint_mode_btn.setStyleSheet("""
QPushButton {
font-size: 8pt;
border: 1px solid #888;
border-radius: 2px;
background: #fff;
}
QPushButton:checked {
background: #2196F3;
color: white;
font-weight: bold;
}
""")
self.joint_mode_btn.clicked.connect(lambda: self._set_mode(self.JOINT_MODE))
mode_layout.addWidget(self.joint_mode_btn)
self.cartesian_mode_btn = QPushButton("XYZ")
self.cartesian_mode_btn.setCheckable(True)
self.cartesian_mode_btn.setFixedHeight(22)
self.cartesian_mode_btn.setStyleSheet("""
QPushButton {
font-size: 8pt;
border: 1px solid #888;
border-radius: 2px;
background: #fff;
}
QPushButton:checked {
background: #2196F3;
color: white;
font-weight: bold;
}
""")
self.cartesian_mode_btn.clicked.connect(lambda: self._set_mode(self.CARTESIAN_MODE))
mode_layout.addWidget(self.cartesian_mode_btn)
layout.addWidget(mode_frame)
# Coordinate frame selector (only visible in Cartesian mode)
self.frame_selector_container = QFrame()
self.frame_selector_container.setStyleSheet("background: #e0e0e0; border-radius: 2px;")
frame_sel_layout = QHBoxLayout(self.frame_selector_container)
frame_sel_layout.setContentsMargins(2, 2, 2, 2)
frame_sel_layout.setSpacing(2)
frame_label = QLabel("Frame:")
frame_label.setStyleSheet("font-size: 7pt;")
frame_sel_layout.addWidget(frame_label)
self.frame_selector = QComboBox()
self.frame_selector.setFixedHeight(20)
self.frame_selector.setStyleSheet("font-size: 7pt;")
self.frame_selector.addItems(["base", "tcp"]) # Populated from frame controller
self.frame_selector.currentTextChanged.connect(self._on_frame_changed)
frame_sel_layout.addWidget(self.frame_selector, 1)
layout.addWidget(self.frame_selector_container)
self.frame_selector_container.setVisible(False) # Hidden in joint mode
# Small spacer
layout.addSpacing(2)
# Axis rows container (will be repopulated on mode change)
self.rows_container = QVBoxLayout()
self.rows_container.setSpacing(0)
layout.addLayout(self.rows_container)
# Create rows for both modes
self.rows = {}
self._create_joint_rows()
# Gripper row (always visible)
gripper_row = AxisRow("Gripper", "", is_gripper=True)
self.rows["Gripper"] = gripper_row
layout.addWidget(gripper_row)
# Movement type section
move_frame = QFrame()
move_frame.setStyleSheet("background: #e0e8f0; border-radius: 3px;")
move_layout = QVBoxLayout(move_frame)
move_layout.setContentsMargins(3, 3, 3, 3)
move_layout.setSpacing(2)
# Movement Type heading
move_heading = QLabel("Move Type")
move_heading.setStyleSheet("font-size: 7pt; font-weight: bold; color: #555;")
move_heading.setAlignment(Qt.AlignCenter)
move_layout.addWidget(move_heading)
# G0/G1 toggle row
move_btn_row = QHBoxLayout()
move_btn_row.setSpacing(2)
self.g0_btn = QPushButton("G0")
self.g0_btn.setCheckable(True)
self.g0_btn.setChecked(True)
self.g0_btn.setFixedHeight(20)
self.g0_btn.setToolTip("Rapid movement")
self.g0_btn.setStyleSheet("""
QPushButton {
font-size: 7pt;
border: 1px solid #888;
border-radius: 2px;
background: #fff;
}
QPushButton:checked {
background: #FF9800;
color: white;
font-weight: bold;
}
""")
self.g0_btn.clicked.connect(lambda: self._set_movement_type("G0"))
move_btn_row.addWidget(self.g0_btn)
self.g1_btn = QPushButton("G1")
self.g1_btn.setCheckable(True)
self.g1_btn.setFixedHeight(20)
self.g1_btn.setToolTip("Feed movement")
self.g1_btn.setStyleSheet("""
QPushButton {
font-size: 7pt;
border: 1px solid #888;
border-radius: 2px;
background: #fff;
}
QPushButton:checked {
background: #FF9800;
color: white;
font-weight: bold;
}
""")
self.g1_btn.clicked.connect(lambda: self._set_movement_type("G1"))
move_btn_row.addWidget(self.g1_btn)
move_layout.addLayout(move_btn_row)
# Feedrate row (only enabled when G1)
feed_row = QHBoxLayout()
feed_row.setSpacing(2)
feed_label = QLabel("F:")
feed_label.setStyleSheet("font-size: 7pt;")
feed_label.setFixedWidth(12)
feed_row.addWidget(feed_label)
self.feedrate_spin = QSpinBox()
self.feedrate_spin.setRange(1, 10000)
self.feedrate_spin.setValue(1000)
self.feedrate_spin.setSuffix("")
self.feedrate_spin.setFixedHeight(20)
self.feedrate_spin.setStyleSheet("font-size: 7pt;")
self.feedrate_spin.setEnabled(False) # Disabled by default (G0 mode)
self.feedrate_spin.valueChanged.connect(lambda v: self.feedrate_changed.emit(float(v)))
feed_row.addWidget(self.feedrate_spin, 1)
move_layout.addLayout(feed_row)
layout.addWidget(move_frame)
self.current_movement_type = "G0"
# Tool frame selector
tool_frame = QFrame()
tool_frame.setStyleSheet("background: #e8e0f0; border-radius: 3px;")
tool_layout = QVBoxLayout(tool_frame)
tool_layout.setContentsMargins(3, 3, 3, 3)
tool_layout.setSpacing(2)
tool_heading = QLabel("Tool")
tool_heading.setStyleSheet("font-size: 7pt; font-weight: bold; color: #555;")
tool_heading.setAlignment(Qt.AlignCenter)
tool_layout.addWidget(tool_heading)
self.tool_combo = QComboBox()
self.tool_combo.setFixedHeight(20)
self.tool_combo.setStyleSheet("font-size: 7pt;")
self.tool_combo.addItem("None")
self.tool_combo.currentTextChanged.connect(self._on_tool_changed)
tool_layout.addWidget(self.tool_combo)
layout.addWidget(tool_frame)
layout.addStretch()
# Step toggle section
step_frame = QFrame()
step_frame.setStyleSheet("background: #e8e8e8; border-radius: 3px;")
step_layout = QVBoxLayout(step_frame)
step_layout.setContentsMargins(3, 3, 3, 3)
step_layout.setSpacing(2)
# Step buttons row
btn_row = QHBoxLayout()
btn_row.setSpacing(2)
self.step_buttons = {}
for step_val in [0.1, 1.0, 10.0]:
btn = QPushButton(f"{step_val:g}")
btn.setCheckable(True)
btn.setFixedHeight(22)
btn.setStyleSheet("""
QPushButton {
font-size: 8pt;
border: 1px solid #999;
border-radius: 2px;
background: #fff;
}
QPushButton:checked {
background: #4CAF50;
color: white;
font-weight: bold;
}
""")
btn.clicked.connect(lambda checked, s=step_val: self._on_step_clicked(s))
self.step_buttons[step_val] = btn
btn_row.addWidget(btn)
# Set default step
self.step_buttons[1.0].setChecked(True)
step_layout.addLayout(btn_row)
layout.addWidget(step_frame)
# Quick commands section
quick_frame = QFrame()
quick_frame.setStyleSheet("background: #f0e8e8; border-radius: 3px;")
quick_layout = QVBoxLayout(quick_frame)
quick_layout.setContentsMargins(3, 3, 3, 3)
quick_layout.setSpacing(2)
# Quick Commands heading
quick_heading = QLabel("Quick")
quick_heading.setStyleSheet("font-size: 7pt; font-weight: bold; color: #555;")
quick_heading.setAlignment(Qt.AlignCenter)
quick_layout.addWidget(quick_heading)
# Home button
self.HomeButton = QPushButton("Master")
self.HomeButton.setFixedHeight(24)
self.HomeButton.setStyleSheet("""
QPushButton {
font-size: 7pt;
border: 1px solid #888;
border-radius: 2px;
background: #fff;
}
QPushButton:hover {
background: #e8f4e8;
}
""")
self.HomeButton.setToolTip("Master all axes (G28)")
quick_layout.addWidget(self.HomeButton)
# Home position button
self.ZeroPositionButton = QPushButton("Home")
self.ZeroPositionButton.setFixedHeight(24)
self.ZeroPositionButton.setStyleSheet("""
QPushButton {
font-size: 7pt;
border: 1px solid #888;
border-radius: 2px;
background: #fff;
}
QPushButton:hover {
background: #e8e8f4;
}
""")
self.ZeroPositionButton.setToolTip("Go to home position")
quick_layout.addWidget(self.ZeroPositionButton)
# Park button
self.ParkButton = QPushButton("Park")
self.ParkButton.setFixedHeight(24)
self.ParkButton.setStyleSheet("""
QPushButton {
font-size: 7pt;
border: 1px solid #888;
border-radius: 2px;
background: #fff;
}
QPushButton:hover {
background: #f4e8e8;
}
""")
self.ParkButton.setToolTip("Park robot, close gripper, disable motors")
quick_layout.addWidget(self.ParkButton)
layout.addWidget(quick_frame)
def _create_joint_rows(self):
"""Create axis rows for joint control mode (J1-J6)"""
self._clear_rows()
joint_data = [
("A1", "X"), ("A2", "Y"), ("A3", "Z"),
("A4", "U"), ("A5", "V"), ("A6", "W")
]
for joint_name, axis_label in joint_data:
row = AxisRow(joint_name, axis_label, is_gripper=False)
self.rows[joint_name] = row
self.rows_container.addWidget(row)
def _create_cartesian_rows(self):
"""Create axis rows for Cartesian control mode (X, Y, Z, Roll, Pitch, Yaw)"""
self._clear_rows()
cartesian_data = [
("X", "mm"), ("Y", "mm"), ("Z", "mm"),
("Roll", "°"), ("Pitch", "°"), ("Yaw", "°")
]
for axis_name, unit in cartesian_data:
row = AxisRow(axis_name, unit, is_gripper=False, is_cartesian=True)
self.rows[axis_name] = row
self.rows_container.addWidget(row)
def _clear_rows(self):
"""Remove all axis rows (except gripper)"""
# Keep gripper row reference
gripper = self.rows.get("Gripper")
# Clear container
while self.rows_container.count():
item = self.rows_container.takeAt(0)
if item.widget():
item.widget().deleteLater()
# Reset rows dict but preserve gripper
self.rows = {}
if gripper:
self.rows["Gripper"] = gripper
def _set_mode(self, mode):
"""Switch between joint and cartesian control modes"""
if mode == self.current_mode:
return
self.current_mode = mode
# Update button states
self.joint_mode_btn.setChecked(mode == self.JOINT_MODE)
self.cartesian_mode_btn.setChecked(mode == self.CARTESIAN_MODE)
# Show/hide frame selector based on mode
self.frame_selector_container.setVisible(mode == self.CARTESIAN_MODE)
# Recreate rows for new mode
if mode == self.JOINT_MODE:
self._create_joint_rows()
else:
self._create_cartesian_rows()
# Emit signal for external handling
self.mode_changed.emit(mode)
def get_mode(self):
"""Get current control mode"""
return self.current_mode
def _on_frame_changed(self, frame_text):
"""Handle coordinate frame selection change"""
frame_name = frame_text if frame_text else "base"
if frame_name != self.current_frame:
self.current_frame = frame_name
self.frame_changed.emit(frame_name)
def get_frame(self):
"""Get current coordinate frame"""
return self.current_frame
def set_available_frames(self, frames):
"""Update the frame selector with available frames"""
current = self.current_frame
self.frame_selector.blockSignals(True)
self.frame_selector.clear()
for frame in frames:
self.frame_selector.addItem(frame)
# Restore previous selection if still present
idx = self.frame_selector.findText(current)
if idx >= 0:
self.frame_selector.setCurrentIndex(idx)
self.frame_selector.blockSignals(False)
def _on_step_clicked(self, step_value):
"""Handle step button click"""
self.current_step = step_value
# Uncheck all buttons except clicked one
for val, btn in self.step_buttons.items():
btn.setChecked(val == step_value)
self.step_changed.emit(step_value)
def get_step(self):
"""Get current step value"""
return self.current_step
def _set_movement_type(self, move_type):
"""Switch between G0 (rapid) and G1 (feed) movement types"""
if move_type == self.current_movement_type:
return
self.current_movement_type = move_type
# Update button states
self.g0_btn.setChecked(move_type == "G0")
self.g1_btn.setChecked(move_type == "G1")
# Enable/disable feedrate based on mode
self.feedrate_spin.setEnabled(move_type == "G1")
# Emit signal
self.movement_type_changed.emit(move_type)
def get_movement_type(self):
"""Get current movement type (G0 or G1)"""
return self.current_movement_type
def _on_tool_changed(self, text):
"""Handle tool combo box selection change"""
if text:
self.tool_changed.emit(text)
def update_tools(self, tools):
"""Update tool combo box items, preserving current selection."""
current = self.tool_combo.currentText()
self.tool_combo.blockSignals(True)
self.tool_combo.clear()
self.tool_combo.addItem("None")
self.tool_combo.addItems(tools)
if current in tools:
self.tool_combo.setCurrentText(current)
self.tool_combo.blockSignals(False)
def get_feedrate(self):
"""Get current feedrate value"""
return float(self.feedrate_spin.value())
def set_feedrate(self, value):
"""Set feedrate value"""
self.feedrate_spin.setValue(int(value))
class RobotStatePanel(QFrame):
"""Right panel - always visible robot state with 3D visualisation and axis controls"""
def __init__(self):
super().__init__()
self.setFrameShape(QFrame.StyledPanel)
layout = QVBoxLayout(self)
layout.setContentsMargins(5, 5, 5, 5)