-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-robot.py
More file actions
429 lines (365 loc) · 11.7 KB
/
Copy pathtest-robot.py
File metadata and controls
429 lines (365 loc) · 11.7 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
import math
import sys
from loguru import logger
if sys.stderr:
logger.remove()
logger.add(sys.stderr, level="DEBUG")
from OCC.Core.AIS import AIS_Shape
from OCC.Core.Aspect import Aspect_GradientFillMethod
from OCC.Core.gp import gp_EulerSequence, gp_Quaternion, gp_Trsf, gp_Vec
# PySide6
from PySide6.QtGui import QGuiApplication
from PySide6.QtWidgets import QApplication, QMainWindow
# local
from qtViewer3d import qtViewer3dWidget
def getColor(r: int | tuple[int, int, int], g=0, b=0):
"""设置 OCC 颜色值
Parameters
----------
`r` : int | tuple[int, int, int]
_description_
`g` : int, optional
_description_, by default 0
`b` : int, optional
_description_, by default 0
Returns
-------
_type_
_description_
"""
if isinstance(r, tuple):
r, g, b = r
return Quantity_Color(r / float(255), g / float(255), b / float(255), Quantity_TOC_RGB)
# end def
def rotation_around_z(rotation_radius=None, rotation_degree=None):
"""
绕自身 Z 轴旋转
"""
def _rot(r_radius):
a_trsf = gp_Trsf()
a_quat = gp_Quaternion()
a_quat.SetEulerAngles(gp_EulerSequence.gp_Intrinsic_XYZ, 0, 0, r_radius)
a_trsf.SetRotation(a_quat)
return a_trsf
# end def
if rotation_radius:
return _rot(rotation_radius)
elif rotation_degree:
return _rot(math.radians(rotation_degree))
else:
raise ValueError("请输入旋转值")
# end if
# end def
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowTitle("机器人通用平台")
primary_screen = QGuiApplication.primaryScreen().size()
self.setGeometry(0, 0, primary_screen.width() * 0.8, primary_screen.height() * 0.8)
# 去掉窗口标题
# self.setWindowFlags(Qt.FramelessWindowHint)
self.canvas = qtViewer3dWidget(self)
self.display = self.canvas.viewer3d.Context.Display
self.context = self.canvas.viewer3d.Context
self.view = self.canvas.viewer3d.View
self.v3d = self.canvas.viewer3d
self.setCentralWidget(self.canvas)
# self.view.SetBgGradientColors(getColor(40, 40, 40), getColor(170, 170, 170), Aspect_GradientFillMethod.Aspect_GradientFillMethod_Vertical, True)
self.view.SetBgGradientColors(
getColor(37, 55, 113), getColor(36, 151, 132), Aspect_GradientFillMethod.Aspect_GradientFillMethod_Vertical, True
)
# 开启抗锯齿
self.canvas.viewer3d.EnableAntiAliasing()
self.centerOnScreen()
# # 设置视图为隐藏线模式 (HLR)
# gui.v3d.SetModeHLR()
# end alternate constructor
def centerOnScreen(self):
# 获取屏幕的尺寸
primary_screen = QGuiApplication.primaryScreen().size()
size = self.geometry()
x = (primary_screen.width() - size.width()) // 2
y = 0.7 * (primary_screen.height() - size.height()) // 2
# 移动主窗口到中心位置
self.move(x, y)
# end def
# end class
class MyRobot:
def __init__(self, install_hight: int, DH=None, other_input=None):
"""
Purpose: value
"""
self.gui: MainWindow = None # 不建议在创建机器人时设置显示窗口
if DH:
self.from_DH(DH)
else:
"""
========================================================================================
初始旋转角
========================================================================================
"""
# todo 需要根据 DH 定义初始角度
self.theta_1 = 0.0
self.theta_2 = 0.0
self.theta_3 = 0.0
self.theta_4 = 0.0
self.theta_5 = 0.0
self.theta_6 = 0.0
"""
========================================================================================
初始变换
========================================================================================
"""
# todo 需要根据 DH 定义初始变换 (theta=0 时的变换)
self.base = gp_Trsf() # 安装位置
self._T01 = gp_Trsf() # 从安装位置到 J1 的变换
self._T01.SetTranslation(gp_Vec(0, 0, install_hight)) # J1 轴安装时距离底座的高度,无法反应在 DH 中
self._T12 = gp_Trsf() # 从 J1 到 J2 的变换
self._T23 = gp_Trsf() # 从 J2 到 J3 的变换
self._T34 = gp_Trsf() # 从 J3 到 J4 的变换
self._T45 = gp_Trsf() # 从 J4 到 J5 的变换
self._T56 = gp_Trsf() # 从 J5 到 J6 的变换
self._T6t = gp_Trsf() # 从 J6 到手抓的变换
# end if
"""
========================================================================================
显示模型
========================================================================================
"""
# todo 需要完成用于显示模型的构建
self.model_J1: AIS_Shape = None
self._model_J2: AIS_Shape = None
self._model_J3: AIS_Shape = None
self._model_J4: AIS_Shape = None
self._model_J5: AIS_Shape = None
self._model_J6: AIS_Shape = None
self._model_tool: AIS_Shape = None
# end alternate constructor
"""
========================================================================================
求正解
========================================================================================
"""
@property
def T01(self):
"""
从安装位置到 J1 的变换
"""
return self._T01
# end property
@property
def T12(self):
"""
从 J1 到 J2 的变换
"""
return self._T01.Multiplied(rotation_around_z(self.theta_1)).Multiplied(self._T12)
# end property
@property
def T23(self):
"""
从 J2 到 J3 的变换
"""
return self.T02.Multiplied(rotation_around_z(self.theta_2)).Multiplied(self._T23)
# end property
@property
def T34(self):
"""
从 J3 到 J4 的变换
"""
return self.T03.Multiplied(rotation_around_z(self.theta_3)).Multiplied(self._T34)
# end property
@property
def T45(self):
"""
从 J4 到 J5 的变换
"""
return self.T04.Multiplied(rotation_around_z(self.theta_4)).Multiplied(self._T45)
# end property
@property
def T56(self):
"""
从 J5 到 J6 的变换
"""
return self.T05.Multiplied(rotation_around_z(self.theta_5)).Multiplied(self._T56)
# end property
@property
def T6t(self):
"""
从 J6 到 手抓 的变换
"""
return self.T06.Multiplied(rotation_around_z(self.theta_6)).Multiplied(self._T6t)
# end property
@property
def T02(self):
"""
从安装位置到 J2 的变换
"""
return self.base.Multiplied(self.T12)
# end property
@property
def T03(self):
"""
从安装位置到 J3 的变换
"""
return self.T02.Multiplied(self.T23)
# end property
@property
def T04(self):
"""
从安装位置到 J4 的变换
"""
return self.T03.Multiplied(self.T34)
# end property
@property
def T05(self):
"""
从安装位置到 J5 的变换
"""
return self.T04.Multiplied(self.T45)
# end property
@property
def T06(self):
"""
从安装位置到 J6 的变换
"""
return self.T05.Multiplied(self.T56)
# end property
@property
def T0t(self):
"""
从安装位置到手抓的变换
"""
return self.T06.Multiplied(self.T6t)
# end property
"""
========================================================================================
模型实际显示的位置
========================================================================================
"""
def model_J2(self):
"""
实际显示的模型位置
"""
self._model_J2.SetLocalTransformation(self.T01)
return self._model_J2
# end property
def model_J3(self):
"""
实际显示的模型位置
"""
self._model_J3.SetLocalTransformation(self.T02)
return self._model_J3
# end property
def model_J4(self):
"""
实际显示的模型位置
"""
self._model_J4.SetLocalTransformation(self.T03)
return self._model_J4
# end property
def model_J5(self):
"""
实际显示的模型位置
"""
self._model_J5.SetLocalTransformation(self.T04)
return self._model_J5
# end property
def model_J6(self):
"""
实际显示的模型位置
"""
self._model_J6.SetLocalTransformation(self.T05)
return self._model_J6
# end property
def model_tool(self):
"""
实际显示的模型位置
"""
self._model_tool.SetLocalTransformation(self.T06)
return self._model_tool
# end property
def from_DH(self, DH):
"""从 DH 坐标中构建机器人
Parameters
----------
`DH` : _type_
_description_
"""
# todo 需要完成构建函数
pass
# end def
def display(self, UPDATE_FLAG=False, gui: MainWindow = None):
"""
Purpose:在 OCC 窗口展示机器人
"""
if not self.gui:
self.gui = gui
gui.display(self.model_J1, False)
gui.display(self.model_J2, False)
gui.display(self.model_J3, False)
gui.display(self.model_J4, False)
gui.display(self.model_J5, False)
gui.display(self.model_J6, False)
gui.display(self.model_tool, False)
logger.info(f"当前角度:\n{self.theta_1},{self.theta_2},{self.theta_3},{self.theta_4},{self.theta_5},{self.theta_6}")
if UPDATE_FLAG:
gui.context.UpdateCurrentViewer()
# end def
def update(self, new_angles: list[float], UPDATE_FLAG=False):
"""更新机器人
Parameters
----------
`new_angles` : list[float]
六个新的角度
`UPDATE_FLAG` : bool, optional
是否刷新视图,by default False
"""
self.theta_1, self.theta_2, self.theta_3, self.theta_4, self.theta_5, self.theta_6 = new_angles
self.display(UPDATE_FLAG=UPDATE_FLAG)
# end def
# end class
"""
========================================================================================
测试代码
========================================================================================
"""
def test(gui: MainWindow):
fanuc_DH = None # todo 输入实际的 DH,并完成 MyRobot 的 DH 解析函数
fanuc_robot = MyRobot(install_hight=500, DH=fanuc_DH)
# 显示
fanuc_robot.display(True, gui=gui)
# 变换角度后显示
new_angles = [
3.14,
3.14,
3.14,
3.14,
3.14,
3.14,
]
fanuc_robot.update(new_angles, True)
gui.v3d.FitAll()
# end def
"""
========================================================================================
创建窗口用于显示
========================================================================================
"""
def mainGUI():
app = QApplication().instance()
if not app:
app = QApplication(sys.argv)
gui = MainWindow()
gui.show()
gui.canvas.InitDriver()
gui.canvas.qApp = app
#!test
test(gui)
#!test
# 窗口置顶
gui.raise_()
app.exec()
# end def
if __name__ == "__main__":
mainGUI()
# end main