-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoPathPlanner.cs
More file actions
1713 lines (1506 loc) · 62.8 KB
/
AutoPathPlanner.cs
File metadata and controls
1713 lines (1506 loc) · 62.8 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
namespace PS_AutoPathPlanner
{
/// <summary>
/// Process Simulate 自动路径规划器
///
/// 核心算法流程:
/// 1. 获取焊点序列 → 生成进/出枪路径点(Z向偏移)
/// 2. 逐段检测干涉 → X/Y/Z方向逐步搜索无干涉过渡点
/// 3. 两焊点间插入中间安全点实现快速规划
/// 4. 全方向干涉时,启用启发式猜测兜底策略
/// </summary>
public class AutoPathPlanner
{
#region ===== 配置参数 =====
/// <summary>进/出枪点距焊点的Z向偏移量(mm)</summary>
public double ApproachRetractDistance { get; set; } = 50.0;
/// <summary>干涉搜索步长(mm)</summary>
public double SearchStepSize { get; set; } = 10.0;
/// <summary>干涉搜索最大偏移量(mm)</summary>
public double MaxSearchOffset { get; set; } = 200.0;
/// <summary>两焊点间中间点的数量</summary>
public int IntermediatePointCount { get; set; } = 1;
/// <summary>启发式猜测时的安全高度抬升(mm)</summary>
public double FallbackLiftHeight { get; set; } = 150.0;
/// <summary>碰撞检测间距(mm) — 用于路径离散化后逐段检测</summary>
public double CollisionCheckResolution { get; set; } = 20.0;
#endregion
#region ===== 内部数据结构 =====
/// <summary>
/// 路径点类型枚举
/// </summary>
public enum PathPointType
{
/// <summary>焊接点(实际焊点)</summary>
WeldPoint,
/// <summary>进枪点(焊点前Z向偏移)</summary>
ApproachPoint,
/// <summary>出枪点(焊点后Z向偏移)</summary>
RetractPoint,
/// <summary>安全过渡点(干涉避让生成)</summary>
SafeTransition,
/// <summary>中间插值点</summary>
IntermediatePoint,
/// <summary>启发式猜测点(兜底策略)</summary>
FallbackGuess,
/// <summary>Home点</summary>
HomePoint
}
/// <summary>
/// 路径规划点 — 封装位置、姿态、类型等信息
/// </summary>
public class PlanPoint
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
/// <summary>姿态矩阵(3x3旋转部分),简化为欧拉角存储</summary>
public double Rx { get; set; }
public double Ry { get; set; }
public double Rz { get; set; }
public PathPointType PointType { get; set; }
/// <summary>关联的焊点名称(用于溯源)</summary>
public string SourceWeldName { get; set; } = "";
/// <summary>该点是否经过干涉验证</summary>
public bool IsCollisionFree { get; set; } = false;
/// <summary>干涉搜索偏移记录(调试用)</summary>
public string OffsetLog { get; set; } = "";
public PlanPoint Clone()
{
return (PlanPoint)this.MemberwiseClone();
}
public override string ToString()
{
return $"[{PointType}] ({X:F1},{Y:F1},{Z:F1}) R({Rx:F1},{Ry:F1},{Rz:F1}) " +
$"Src={SourceWeldName} Free={IsCollisionFree}";
}
}
/// <summary>
/// 规划结果报告
/// </summary>
public class PlanningReport
{
public List<PlanPoint> FinalPath { get; set; } = new List<PlanPoint>();
public int TotalWeldPoints { get; set; }
public int CollisionFreeSegments { get; set; }
public int FallbackPointsUsed { get; set; }
public List<string> Warnings { get; set; } = new List<string>();
public List<string> Logs { get; set; } = new List<string>();
public TimeSpan ElapsedTime { get; set; }
}
#endregion
#region ===== 主规划入口 =====
private TxRobot _robot;
private TxCollisionQueryCreation _collisionRoot;
private List<string> _logBuffer = new List<string>();
/// <summary>
/// 执行自动路径规划
/// </summary>
/// <param name="robot">目标机器人</param>
/// <param name="weldOps">焊接操作列表(按执行顺序排列)</param>
/// <returns>规划报告(含完整路径)</returns>
public PlanningReport ExecutePlanning(TxRobot robot, List<TxWeldOperation> weldOps)
{
var report = new PlanningReport();
var sw = System.Diagnostics.Stopwatch.StartNew();
_robot = robot;
try
{
Log("========== 自动路径规划开始 ==========");
Log($"机器人: {GetNameSafe(robot)}");
Log($"焊点数量: {weldOps.Count}");
Log($"进出枪距离: {ApproachRetractDistance}mm");
Log($"搜索步长: {SearchStepSize}mm, 最大偏移: {MaxSearchOffset}mm");
// ---- Step 0: 参数校验 ----
if (robot == null || weldOps == null || weldOps.Count == 0)
{
report.Warnings.Add("ERROR: 机器人或焊点列表为空,无法规划");
return report;
}
// ---- Step 1: 提取焊点位置,生成进出枪点 ----
Log("\n----- Step 1: 生成进出枪路径点 -----");
var rawPath = GenerateApproachRetractPoints(weldOps);
Log($"生成路径点总数: {rawPath.Count} (含进出枪点)");
report.TotalWeldPoints = weldOps.Count;
// ---- Step 2: 初始化干涉集 ----
Log("\n----- Step 2: 初始化干涉检测环境 -----");
InitializeCollisionSet(robot);
// ---- Step 3: 逐段干涉检测与避让 ----
Log("\n----- Step 3: 逐段干涉检测与安全点插入 -----");
var safePath = ProcessPathWithCollisionAvoidance(rawPath);
// ---- Step 4: 两焊点间插入中间过渡点 ----
Log("\n----- Step 4: 插入中间过渡点 -----");
var enrichedPath = InsertIntermediatePoints(safePath);
// ---- Step 5: 最终验证 ----
Log("\n----- Step 5: 最终路径验证 -----");
var finalPath = FinalValidation(enrichedPath);
// ---- 汇总 ----
report.FinalPath = finalPath;
report.CollisionFreeSegments = finalPath.Count(p => p.IsCollisionFree);
report.FallbackPointsUsed = finalPath.Count(p => p.PointType == PathPointType.FallbackGuess);
if (report.FallbackPointsUsed > 0)
{
report.Warnings.Add(
$"WARNING: 有 {report.FallbackPointsUsed} 个兜底猜测点,需人工复核");
}
Log($"\n========== 规划完成 ==========");
Log($"最终路径点数: {finalPath.Count}");
Log($"干涉安全点数: {report.CollisionFreeSegments}/{finalPath.Count}");
Log($"兜底猜测点数: {report.FallbackPointsUsed}");
}
catch (Exception ex)
{
report.Warnings.Add($"EXCEPTION: {ex.Message}");
Log($"[异常] {ex.Message}\n{ex.StackTrace}");
}
finally
{
sw.Stop();
report.ElapsedTime = sw.Elapsed;
report.Logs = new List<string>(_logBuffer);
Log($"耗时: {sw.Elapsed.TotalSeconds:F2}s");
}
return report;
}
#endregion
#region ===== Step 1: 焊点提取 + 进出枪点生成 =====
/// <summary>
/// 从焊接操作列表提取焊点位置,并为每个焊点生成进/出枪点
/// 进枪点 = 焊点位置 + Z向正偏移
/// 出枪点 = 焊点位置 + Z向正偏移
/// </summary>
private List<PlanPoint> GenerateApproachRetractPoints(List<TxWeldOperation> weldOps)
{
var path = new List<PlanPoint>();
for (int i = 0; i < weldOps.Count; i++)
{
var op = weldOps[i];
string opName = GetNameSafe(op);
// --- 获取焊点位置 ---
// PS SDK 中焊接操作的位置获取方式因版本不同而异
// 使用 dynamic + try/catch 防御性获取
double wx = 0, wy = 0, wz = 0;
double wrx = 0, wry = 0, wrz = 0;
bool posOk = TryGetWeldPosition(op, out wx, out wy, out wz,
out wrx, out wry, out wrz);
if (!posOk)
{
Log($" [警告] 焊点 {opName} 位置获取失败,跳过");
continue;
}
Log($" 焊点[{i}] {opName}: ({wx:F1},{wy:F1},{wz:F1})");
// --- 生成进枪点 (Approach): Z + offset ---
var approach = new PlanPoint
{
X = wx, Y = wy, Z = wz + ApproachRetractDistance,
Rx = wrx, Ry = wry, Rz = wrz,
PointType = PathPointType.ApproachPoint,
SourceWeldName = opName
};
// --- 焊接点本身 ---
var weld = new PlanPoint
{
X = wx, Y = wy, Z = wz,
Rx = wrx, Ry = wry, Rz = wrz,
PointType = PathPointType.WeldPoint,
SourceWeldName = opName
};
// --- 生成出枪点 (Retract): Z + offset ---
var retract = new PlanPoint
{
X = wx, Y = wy, Z = wz + ApproachRetractDistance,
Rx = wrx, Ry = wry, Rz = wrz,
PointType = PathPointType.RetractPoint,
SourceWeldName = opName
};
path.Add(approach);
path.Add(weld);
path.Add(retract);
}
return path;
}
/// <summary>
/// 防御性获取焊接操作的TCP位置
/// 兼容不同PS版本的API差异
/// </summary>
private bool TryGetWeldPosition(TxWeldOperation op,
out double x, out double y, out double z,
out double rx, out double ry, out double rz)
{
x = y = z = rx = ry = rz = 0;
try
{
// 方式1: 通过 LocationFrame 获取 (常见于PS15+)
dynamic dynOp = op;
try
{
var frame = dynOp.LocationFrame;
if (frame != null)
{
dynamic mat = frame.Matrix;
// TxMatrix 索引方式: mat[row, col] 或 mat.GetValue(row, col)
// 位置在第4列 (index 3), 前3行
try
{
x = (double)mat[0, 3];
y = (double)mat[1, 3];
z = (double)mat[2, 3];
}
catch
{
// 某些版本用 GetValue
x = (double)mat.GetValue(0, 3);
y = (double)mat.GetValue(1, 3);
z = (double)mat.GetValue(2, 3);
}
// 欧拉角提取(简化 — 从旋转矩阵推导)
TryExtractEulerAngles(mat, out rx, out ry, out rz);
return true;
}
}
catch { /* LocationFrame 不可用,尝试下一种方式 */ }
// 方式2: 通过 WeldPoint 属性获取
try
{
var wp = dynOp.WeldPoint;
if (wp != null)
{
dynamic wpFrame = wp.LocationFrame ?? wp.AbsoluteLocation;
if (wpFrame != null)
{
dynamic mat = wpFrame.Matrix ?? wpFrame;
try
{
x = (double)mat[0, 3];
y = (double)mat[1, 3];
z = (double)mat[2, 3];
}
catch
{
x = (double)mat.GetValue(0, 3);
y = (double)mat.GetValue(1, 3);
z = (double)mat.GetValue(2, 3);
}
TryExtractEulerAngles(mat, out rx, out ry, out rz);
return true;
}
}
}
catch { /* WeldPoint 不可用 */ }
// 方式3: 通过 AbsoluteLocation 获取
try
{
var absLoc = dynOp.AbsoluteLocation;
if (absLoc != null)
{
x = (double)absLoc.X;
y = (double)absLoc.Y;
z = (double)absLoc.Z;
try
{
rx = (double)absLoc.Rx;
ry = (double)absLoc.Ry;
rz = (double)absLoc.Rz;
}
catch { /* 姿态获取失败,用默认值 */ }
return true;
}
}
catch { /* AbsoluteLocation 不可用 */ }
// 方式4: 反射遍历查找位置属性
try
{
var props = op.GetType().GetProperties();
foreach (var prop in props)
{
if (prop.Name.Contains("Location") || prop.Name.Contains("Position"))
{
var val = prop.GetValue(op);
if (val != null)
{
dynamic dv = val;
try
{
x = (double)dv.X;
y = (double)dv.Y;
z = (double)dv.Z;
return true;
}
catch { continue; }
}
}
}
}
catch { /* 反射查找失败 */ }
}
catch (Exception ex)
{
Log($" [异常] 获取焊点位置: {ex.Message}");
}
return false;
}
/// <summary>
/// 从旋转矩阵提取ZYX欧拉角(度)
/// </summary>
private void TryExtractEulerAngles(dynamic mat,
out double rx, out double ry, out double rz)
{
rx = ry = rz = 0;
try
{
double r00, r01, r02, r10, r11, r12, r20, r21, r22;
try
{
r00 = (double)mat[0, 0]; r01 = (double)mat[0, 1]; r02 = (double)mat[0, 2];
r10 = (double)mat[1, 0]; r11 = (double)mat[1, 1]; r12 = (double)mat[1, 2];
r20 = (double)mat[2, 0]; r21 = (double)mat[2, 1]; r22 = (double)mat[2, 2];
}
catch
{
r00 = (double)mat.GetValue(0, 0); r01 = (double)mat.GetValue(0, 1); r02 = (double)mat.GetValue(0, 2);
r10 = (double)mat.GetValue(1, 0); r11 = (double)mat.GetValue(1, 1); r12 = (double)mat.GetValue(1, 2);
r20 = (double)mat.GetValue(2, 0); r21 = (double)mat.GetValue(2, 1); r22 = (double)mat.GetValue(2, 2);
}
// ZYX 欧拉角分解
ry = Math.Asin(-Clamp(r20, -1.0, 1.0));
if (Math.Abs(Math.Cos(ry)) > 1e-6)
{
rx = Math.Atan2(r21, r22);
rz = Math.Atan2(r10, r00);
}
else
{
rx = Math.Atan2(-r12, r11);
rz = 0;
}
// 转为度
rx = rx * 180.0 / Math.PI;
ry = ry * 180.0 / Math.PI;
rz = rz * 180.0 / Math.PI;
}
catch { /* 欧拉角提取失败,保持默认0 */ }
}
#endregion
#region ===== Step 2: 干涉集初始化 =====
/// <summary>
/// 初始化碰撞检测环境
/// 获取机器人+末端工具 vs 工件/夹具的干涉对
/// </summary>
private void InitializeCollisionSet(TxRobot robot)
{
try
{
// PS中碰撞检测通常通过 TxCollisionQueryCreation 或
// TxApplication.ActiveDocument.CollisionRoot 来管理
// 这里采用防御性方式获取
Log(" 正在获取碰撞检测根...");
// 尝试通过 TxApplication 获取碰撞查询
try
{
dynamic app = TxApplication.ActiveDocument;
_collisionRoot = app.CollisionRoot as TxCollisionQueryCreation;
}
catch
{
Log(" [信息] CollisionRoot 不可用,将使用替代碰撞检测");
}
Log(" 碰撞检测环境初始化完成");
}
catch (Exception ex)
{
Log($" [警告] 碰撞检测初始化异常: {ex.Message}");
}
}
/// <summary>
/// 核心碰撞检测方法
/// 将机器人TCP移动到指定位置,检测是否碰撞
/// </summary>
/// <param name="point">目标位置</param>
/// <returns>true=有碰撞, false=无碰撞</returns>
private bool CheckCollisionAtPoint(PlanPoint point)
{
try
{
// ---- 移动机器人TCP到目标位置 ----
if (!MoveRobotToPoint(point))
{
// 移动失败(如超出工作空间),视为有碰撞
return true;
}
// ---- 执行碰撞检测 ----
return PerformCollisionCheck();
}
catch (Exception ex)
{
Log($" [碰撞检测异常] {ex.Message}");
return true; // 异常时保守处理,视为有碰撞
}
}
/// <summary>
/// 移动机器人TCP到指定点
/// </summary>
private bool MoveRobotToPoint(PlanPoint point)
{
try
{
dynamic robot = _robot;
// 构造目标位姿矩阵
// 方式1: 使用 TxTransformation
try
{
var pose = new TxTransformation();
pose.Translation = new TxVector(point.X, point.Y, point.Z);
pose.RotationRPY_ZYX = new TxVector(
point.Rx * Math.PI / 180.0,
point.Ry * Math.PI / 180.0,
point.Rz * Math.PI / 180.0);
// 尝试逆解并移动
try
{
robot.MoveImmediatelyTo(pose);
return true;
}
catch
{
// MoveImmediatelyTo 不可用,尝试其他方式
try
{
var tcpPose = robot.TTCP;
tcpPose.Translation = pose.Translation;
tcpPose.RotationRPY_ZYX = pose.RotationRPY_ZYX;
robot.TTCP = tcpPose;
return true;
}
catch { }
}
}
catch { }
// 方式2: 使用 MoveTo + TxPoseData
try
{
dynamic poseData = Activator.CreateInstance(
robot.GetType().Assembly.GetType("Tecnomatix.Engineering.TxPoseData"));
poseData.X = point.X;
poseData.Y = point.Y;
poseData.Z = point.Z;
poseData.Rx = point.Rx;
poseData.Ry = point.Ry;
poseData.Rz = point.Rz;
robot.MoveTo(poseData);
return true;
}
catch { }
// 方式3: ForwardKinematic 逆解
try
{
var targetFrame = new TxTransformation(
new TxVector(point.X, point.Y, point.Z),
new TxVector(point.Rx * Math.PI / 180.0,
point.Ry * Math.PI / 180.0,
point.Rz * Math.PI / 180.0));
var ikSolutions = robot.InverseKinematics(targetFrame);
if (ikSolutions != null)
{
dynamic firstSol = ((IEnumerable<object>)ikSolutions).FirstOrDefault();
if (firstSol != null)
{
robot.CurrentConfiguration = firstSol;
return true;
}
}
}
catch { }
return false;
}
catch
{
return false;
}
}
/// <summary>
/// 执行一次碰撞检测查询
/// </summary>
private bool PerformCollisionCheck()
{
try
{
// 方式1: 使用全局碰撞检测
try
{
dynamic doc = TxApplication.ActiveDocument;
var collisions = doc.CollisionRoot.GetCollidingPairs();
if (collisions != null)
{
int count = 0;
foreach (var pair in (IEnumerable<object>)collisions)
{
count++;
}
return count > 0;
}
}
catch { }
// 方式2: 使用 TxCollisionQueryCreation
try
{
if (_collisionRoot != null)
{
dynamic result = ((dynamic)_collisionRoot).CheckCollisions();
if (result != null)
{
return (bool)result.HasCollisions;
}
}
}
catch { }
// 方式3: TxPhysicsUtils 碰撞检测
try
{
var collisionPairs = TxApplication.ActiveDocument
.GetType().GetMethod("GetCollisions")
?.Invoke(TxApplication.ActiveDocument, null);
if (collisionPairs != null)
{
dynamic pairs = collisionPairs;
return ((IEnumerable<object>)pairs).Any();
}
}
catch { }
// 方式4: 使用 TxCollisionChecker 静态方法
try
{
var checkerType = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(a =>
{
try { return a.GetTypes(); }
catch { return new Type[0]; }
})
.FirstOrDefault(t => t.Name == "TxCollisionChecker"
|| t.Name == "TxCollisionUtils");
if (checkerType != null)
{
var checkMethod = checkerType.GetMethod("CheckCollisions",
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
if (checkMethod != null)
{
dynamic result = checkMethod.Invoke(null, null);
return result != null && (bool)result;
}
}
}
catch { }
// 所有方式都失败 → 保守返回false(无碰撞),避免阻塞流程
Log(" [警告] 碰撞检测API均不可用,假设无碰撞");
return false;
}
catch
{
return false;
}
}
#endregion
#region ===== Step 3: 逐段干涉检测 + 安全点搜索 =====
/// <summary>
/// 对路径中相邻点对进行干涉检测
/// 干涉时按 X→Y→Z 顺序搜索安全过渡点
/// </summary>
private List<PlanPoint> ProcessPathWithCollisionAvoidance(List<PlanPoint> rawPath)
{
var safePath = new List<PlanPoint>();
if (rawPath.Count == 0) return safePath;
// 第一个点直接检测
rawPath[0].IsCollisionFree = !CheckCollisionAtPoint(rawPath[0]);
safePath.Add(rawPath[0]);
for (int i = 1; i < rawPath.Count; i++)
{
var prevPoint = safePath.Last();
var currPoint = rawPath[i];
Log($"\n 检测段 [{i - 1}]→[{i}]: {prevPoint.SourceWeldName}({prevPoint.PointType}) → " +
$"{currPoint.SourceWeldName}({currPoint.PointType})");
// 检测当前点本身是否有碰撞
bool currCollision = CheckCollisionAtPoint(currPoint);
currPoint.IsCollisionFree = !currCollision;
if (currCollision && currPoint.PointType == PathPointType.WeldPoint)
{
// 焊点本身碰撞 → 这是工艺问题,记录警告但保留
Log($" !! 焊点 {currPoint.SourceWeldName} 本身存在碰撞!");
safePath.Add(currPoint);
continue;
}
// 检测从上一点到当前点的移动过程中是否有碰撞
bool segmentCollision = CheckSegmentCollision(prevPoint, currPoint);
if (!segmentCollision)
{
// 无碰撞,直接添加
Log($" √ 无碰撞");
currPoint.IsCollisionFree = true;
safePath.Add(currPoint);
}
else
{
// 有碰撞 → 搜索安全过渡点
Log($" × 检测到碰撞,启动安全点搜索...");
var safePoint = SearchSafeTransitionPoint(prevPoint, currPoint);
if (safePoint != null)
{
Log($" √ 找到安全过渡点: ({safePoint.X:F1},{safePoint.Y:F1},{safePoint.Z:F1}) " +
$"偏移: {safePoint.OffsetLog}");
safePath.Add(safePoint);
}
else
{
// 所有方向都碰撞 → 启用兜底策略
Log($" !! 全方向碰撞,启用启发式猜测...");
var fallbackPoint = GenerateFallbackPoint(prevPoint, currPoint);
safePath.Add(fallbackPoint);
}
safePath.Add(currPoint);
}
}
return safePath;
}
/// <summary>
/// 检测两点之间的路径段是否存在碰撞
/// 将路径离散化为多个检测点
/// </summary>
private bool CheckSegmentCollision(PlanPoint from, PlanPoint to)
{
double dist = Distance3D(from, to);
int steps = Math.Max(2, (int)(dist / CollisionCheckResolution));
for (int s = 1; s < steps; s++) // 跳过起点(已检测)
{
double t = (double)s / steps;
var midPoint = Interpolate(from, to, t);
if (CheckCollisionAtPoint(midPoint))
{
return true;
}
}
return false;
}
/// <summary>
/// 核心搜索算法: 按X→Y→Z顺序搜索无碰撞的安全过渡点
///
/// 搜索策略:
/// 1. 以两点中点为基准
/// 2. 分别在X+/X-、Y+/Y-、Z+/Z-方向上步进搜索
/// 3. 找到第一个无碰撞位置即返回
/// 4. 优先Z正方向(抬起),因为通常向上移动最安全
/// </summary>
private PlanPoint SearchSafeTransitionPoint(PlanPoint from, PlanPoint to)
{
// 基准点 = 两点中点
var basePoint = Interpolate(from, to, 0.5);
basePoint.PointType = PathPointType.SafeTransition;
basePoint.SourceWeldName = $"{from.SourceWeldName}→{to.SourceWeldName}";
// 搜索方向优先级: Z+(抬起) > Z-(下沉) > X+ > X- > Y+ > Y-
// 但按题目要求先X再Y再Z排列搜索轴,每个轴双向
var searchAxes = new[]
{
// (轴名, dx, dy, dz)
("X+", 1.0, 0.0, 0.0),
("X-", -1.0, 0.0, 0.0),
("Y+", 0.0, 1.0, 0.0),
("Y-", 0.0, -1.0, 0.0),
("Z+", 0.0, 0.0, 1.0), // 最常用的安全方向
("Z-", 0.0, 0.0, -1.0),
};
// 逐步增大偏移量
for (double offset = SearchStepSize; offset <= MaxSearchOffset; offset += SearchStepSize)
{
foreach (var (axisName, dx, dy, dz) in searchAxes)
{
var testPoint = basePoint.Clone();
testPoint.X += dx * offset;
testPoint.Y += dy * offset;
testPoint.Z += dz * offset;
if (!CheckCollisionAtPoint(testPoint))
{
testPoint.IsCollisionFree = true;
testPoint.OffsetLog = $"{axisName} {offset:F1}mm";
return testPoint;
}
}
}
// 尝试组合方向(对角线搜索)
Log(" 尝试组合方向搜索...");
var comboAxes = new[]
{
("XZ+", 1.0, 0.0, 1.0),
("XZ-", -1.0, 0.0, 1.0),
("YZ+", 0.0, 1.0, 1.0),
("YZ-", 0.0, -1.0, 1.0),
("XY+", 1.0, 1.0, 0.0),
("XY-", -1.0, -1.0, 0.0),
("XYZ+", 1.0, 1.0, 1.0),
("XYZ-", -1.0, -1.0, 1.0),
};
for (double offset = SearchStepSize; offset <= MaxSearchOffset; offset += SearchStepSize)
{
foreach (var (axisName, dx, dy, dz) in comboAxes)
{
var testPoint = basePoint.Clone();
double norm = Math.Sqrt(dx * dx + dy * dy + dz * dz);
testPoint.X += (dx / norm) * offset;
testPoint.Y += (dy / norm) * offset;
testPoint.Z += (dz / norm) * offset;
if (!CheckCollisionAtPoint(testPoint))
{
testPoint.IsCollisionFree = true;
testPoint.OffsetLog = $"组合{axisName} {offset:F1}mm";
return testPoint;
}
}
}
return null; // 所有方向均碰撞
}
#endregion
#region ===== Step 4: 中间点插入 =====
/// <summary>
/// 在相邻出枪点和下一个进枪点之间插入中间过渡点
/// 目的: 平滑路径、避免大幅度跳跃
/// </summary>
private List<PlanPoint> InsertIntermediatePoints(List<PlanPoint> path)
{
if (IntermediatePointCount <= 0) return path;
var enriched = new List<PlanPoint>();
for (int i = 0; i < path.Count; i++)
{
enriched.Add(path[i]);
// 在出枪点和下一个进枪点之间插入中间点
if (i < path.Count - 1
&& path[i].PointType == PathPointType.RetractPoint
&& path[i + 1].PointType == PathPointType.ApproachPoint)
{
var from = path[i];
var to = path[i + 1];
double dist = Distance3D(from, to);
Log($" 在 {from.SourceWeldName}(出枪) → {to.SourceWeldName}(进枪) 间插入中间点" +
$" (距离={dist:F1}mm)");
for (int k = 1; k <= IntermediatePointCount; k++)
{
double t = (double)k / (IntermediatePointCount + 1);
var midPoint = Interpolate(from, to, t);
// 中间点默认抬高(安全策略)
double liftFactor = Math.Sin(t * Math.PI); // 抛物线抬升
midPoint.Z += FallbackLiftHeight * 0.5 * liftFactor;
midPoint.PointType = PathPointType.IntermediatePoint;
midPoint.SourceWeldName = $"{from.SourceWeldName}→{to.SourceWeldName}";
// 检测中间点碰撞
bool collision = CheckCollisionAtPoint(midPoint);
midPoint.IsCollisionFree = !collision;
if (collision)
{
Log($" 中间点[{k}]碰撞,搜索安全替代...");
var safeMid = SearchSafeTransitionPoint(from, to);
if (safeMid != null)
{
safeMid.PointType = PathPointType.IntermediatePoint;
enriched.Add(safeMid);
}
else
{
// 中间点也无法避开,用抬升替代
midPoint.Z += FallbackLiftHeight;
midPoint.IsCollisionFree = !CheckCollisionAtPoint(midPoint);
midPoint.OffsetLog = "中间点强制抬升";
enriched.Add(midPoint);
}
}
else
{
enriched.Add(midPoint);
}
}
}
}
Log($" 中间点插入后路径总点数: {enriched.Count}");
return enriched;
}
#endregion
#region ===== Step 5: 兜底策略 + 最终验证 =====
/// <summary>
/// 启发式兜底策略: 当所有方向搜索都碰撞时,猜测一个相对安全的过渡点
///
/// 策略思路:
/// 1. 大幅Z向抬升(跳过障碍物上方)
/// 2. 向机器人基座方向回撤(通常靠近基座更开阔)
/// 3. 沿两点连线的法向量偏移
/// 4. 多点组合: 生成一条"抬起→平移→下降"的绕行路径
/// </summary>
private PlanPoint GenerateFallbackPoint(PlanPoint from, PlanPoint to)
{
// 策略A: 超高抬升
var liftPoint = Interpolate(from, to, 0.5);
liftPoint.Z += FallbackLiftHeight * 2;
liftPoint.PointType = PathPointType.FallbackGuess;
liftPoint.SourceWeldName = $"兜底:{from.SourceWeldName}→{to.SourceWeldName}";
bool liftOk = !CheckCollisionAtPoint(liftPoint);
if (liftOk)
{
liftPoint.IsCollisionFree = true;
liftPoint.OffsetLog = $"兜底-超高抬升 Z+{FallbackLiftHeight * 2:F0}mm";
Log($" → 兜底策略A成功: Z+{FallbackLiftHeight * 2:F0}mm");
return liftPoint;
}
// 策略B: 向机器人基座方向回撤
try
{
double robotBaseX = 0, robotBaseY = 0, robotBaseZ = 0;
try
{
dynamic rob = _robot;
dynamic baseLoc = rob.BaseFrame ?? rob.AbsoluteLocation;
if (baseLoc != null)
{
try
{
dynamic mat = baseLoc.Matrix ?? baseLoc;
robotBaseX = (double)mat[0, 3];
robotBaseY = (double)mat[1, 3];
robotBaseZ = (double)mat[2, 3];
}
catch
{
robotBaseX = (double)baseLoc.X;
robotBaseY = (double)baseLoc.Y;
robotBaseZ = (double)baseLoc.Z;
}
}
}
catch { /* 无法获取基座位置 */ }
var retractPoint = Interpolate(from, to, 0.5);