EditMotionSafetyDialog.xaml.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Windows;
  6. using TeamAAS.Global.Devices;
  7. using TeamAAS.Motion.Models;
  8. namespace TeamAAS.Views
  9. {
  10. public partial class EditMotionSafetyDialog
  11. {
  12. public ObservableCollection<SafetyEditRow> Rows { get; } = new ObservableCollection<SafetyEditRow>();
  13. public List<MotionAxisConfig> Result { get; private set; }
  14. public int EstopDiIndex { get; set; } = -1;
  15. public int EstopDiBankIndex { get; set; }
  16. public bool EstopActiveHigh { get; set; } = true;
  17. public int StopDiIndex { get; set; } = -1;
  18. public int StopDiBankIndex { get; set; }
  19. public bool StopActiveHigh { get; set; } = true;
  20. public MotionIoBank EstopDiBank => EstopDiBankIndex == 1 ? MotionIoBank.EtherCat : MotionIoBank.Local;
  21. public MotionIoBank StopDiBank => StopDiBankIndex == 1 ? MotionIoBank.EtherCat : MotionIoBank.Local;
  22. public EditMotionSafetyDialog(MotionDeviceConfig cfg, IEnumerable<MotionAxisConfig> axes)
  23. {
  24. EstopDiIndex = cfg != null ? cfg.EstopDiIndex : -1;
  25. EstopDiBankIndex = cfg != null && cfg.EstopDiBank == MotionIoBank.EtherCat ? 1 : 0;
  26. EstopActiveHigh = cfg == null || cfg.EstopActiveHigh;
  27. StopDiIndex = cfg != null ? cfg.StopDiIndex : -1;
  28. StopDiBankIndex = cfg != null && cfg.StopDiBank == MotionIoBank.EtherCat ? 1 : 0;
  29. StopActiveHigh = cfg == null || cfg.StopActiveHigh;
  30. InitializeComponent();
  31. DataContext = this;
  32. foreach (var axisCfg in axes ?? Enumerable.Empty<MotionAxisConfig>())
  33. Rows.Add(SafetyEditRow.FromConfig(axisCfg));
  34. }
  35. private void BtnOK_Click(object sender, RoutedEventArgs e)
  36. {
  37. foreach (var row in Rows)
  38. {
  39. if (row.SoftLimitEnabled && row.SoftLimitNeg == row.SoftLimitPos)
  40. {
  41. DialogHelper.ShowWarning($"{row.Name} 的正负软限位不能相同");
  42. return;
  43. }
  44. if (row.MaxVelocity < 0)
  45. {
  46. DialogHelper.ShowWarning($"{row.Name} 的最高速度不能为负");
  47. return;
  48. }
  49. }
  50. if (EstopDiIndex < -1 || StopDiIndex < -1)
  51. {
  52. DialogHelper.ShowWarning("急停/停止 DI 填 -1 表示不启用,不能小于 -1");
  53. return;
  54. }
  55. Result = Rows.Select(r => r.ToConfig()).ToList();
  56. DialogResult = true;
  57. }
  58. }
  59. public class SafetyEditRow : INotifyPropertyChanged
  60. {
  61. private bool _softLimitEnabled;
  62. private double _softLimitNeg;
  63. private double _softLimitPos;
  64. private double _maxVelocity;
  65. public int AxisNo { get; set; }
  66. public string Name { get; set; }
  67. public string KindText { get; set; }
  68. public bool SoftLimitEnabled
  69. {
  70. get => _softLimitEnabled;
  71. set { _softLimitEnabled = value; OnChanged(nameof(SoftLimitEnabled)); }
  72. }
  73. public double SoftLimitNeg
  74. {
  75. get => _softLimitNeg;
  76. set { _softLimitNeg = value; OnChanged(nameof(SoftLimitNeg)); }
  77. }
  78. public double SoftLimitPos
  79. {
  80. get => _softLimitPos;
  81. set { _softLimitPos = value; OnChanged(nameof(SoftLimitPos)); }
  82. }
  83. public double MaxVelocity
  84. {
  85. get => _maxVelocity;
  86. set { _maxVelocity = value; OnChanged(nameof(MaxVelocity)); }
  87. }
  88. public event PropertyChangedEventHandler PropertyChanged;
  89. private void OnChanged(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  90. public static SafetyEditRow FromConfig(MotionAxisConfig cfg)
  91. {
  92. cfg = cfg ?? new MotionAxisConfig();
  93. return new SafetyEditRow
  94. {
  95. AxisNo = cfg.AxisNo,
  96. Name = cfg.Name,
  97. KindText = cfg.KindText,
  98. SoftLimitEnabled = cfg.SoftLimitEnabled,
  99. SoftLimitNeg = cfg.SoftLimitNeg,
  100. SoftLimitPos = cfg.SoftLimitPos,
  101. MaxVelocity = cfg.MaxVelocity
  102. };
  103. }
  104. public MotionAxisConfig ToConfig()
  105. {
  106. return new MotionAxisConfig
  107. {
  108. AxisNo = AxisNo,
  109. Name = Name,
  110. SoftLimitEnabled = SoftLimitEnabled,
  111. SoftLimitNeg = SoftLimitNeg,
  112. SoftLimitPos = SoftLimitPos,
  113. MaxVelocity = MaxVelocity
  114. };
  115. }
  116. }
  117. }