using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Windows; using TeamAAS.Global.Devices; using TeamAAS.Motion.Models; namespace TeamAAS.Views { public partial class EditMotionSafetyDialog { public ObservableCollection Rows { get; } = new ObservableCollection(); public List Result { get; private set; } public int EstopDiIndex { get; set; } = -1; public int EstopDiBankIndex { get; set; } public bool EstopActiveHigh { get; set; } = true; public int StopDiIndex { get; set; } = -1; public int StopDiBankIndex { get; set; } public bool StopActiveHigh { get; set; } = true; public MotionIoBank EstopDiBank => EstopDiBankIndex == 1 ? MotionIoBank.EtherCat : MotionIoBank.Local; public MotionIoBank StopDiBank => StopDiBankIndex == 1 ? MotionIoBank.EtherCat : MotionIoBank.Local; public EditMotionSafetyDialog(MotionDeviceConfig cfg, IEnumerable axes) { EstopDiIndex = cfg != null ? cfg.EstopDiIndex : -1; EstopDiBankIndex = cfg != null && cfg.EstopDiBank == MotionIoBank.EtherCat ? 1 : 0; EstopActiveHigh = cfg == null || cfg.EstopActiveHigh; StopDiIndex = cfg != null ? cfg.StopDiIndex : -1; StopDiBankIndex = cfg != null && cfg.StopDiBank == MotionIoBank.EtherCat ? 1 : 0; StopActiveHigh = cfg == null || cfg.StopActiveHigh; InitializeComponent(); DataContext = this; foreach (var axisCfg in axes ?? Enumerable.Empty()) Rows.Add(SafetyEditRow.FromConfig(axisCfg)); } private void BtnOK_Click(object sender, RoutedEventArgs e) { foreach (var row in Rows) { if (row.SoftLimitEnabled && row.SoftLimitNeg == row.SoftLimitPos) { DialogHelper.ShowWarning($"{row.Name} 的正负软限位不能相同"); return; } if (row.MaxVelocity < 0) { DialogHelper.ShowWarning($"{row.Name} 的最高速度不能为负"); return; } } if (EstopDiIndex < -1 || StopDiIndex < -1) { DialogHelper.ShowWarning("急停/停止 DI 填 -1 表示不启用,不能小于 -1"); return; } Result = Rows.Select(r => r.ToConfig()).ToList(); DialogResult = true; } } public class SafetyEditRow : INotifyPropertyChanged { private bool _softLimitEnabled; private double _softLimitNeg; private double _softLimitPos; private double _maxVelocity; public int AxisNo { get; set; } public string Name { get; set; } public string KindText { get; set; } public bool SoftLimitEnabled { get => _softLimitEnabled; set { _softLimitEnabled = value; OnChanged(nameof(SoftLimitEnabled)); } } public double SoftLimitNeg { get => _softLimitNeg; set { _softLimitNeg = value; OnChanged(nameof(SoftLimitNeg)); } } public double SoftLimitPos { get => _softLimitPos; set { _softLimitPos = value; OnChanged(nameof(SoftLimitPos)); } } public double MaxVelocity { get => _maxVelocity; set { _maxVelocity = value; OnChanged(nameof(MaxVelocity)); } } public event PropertyChangedEventHandler PropertyChanged; private void OnChanged(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); public static SafetyEditRow FromConfig(MotionAxisConfig cfg) { cfg = cfg ?? new MotionAxisConfig(); return new SafetyEditRow { AxisNo = cfg.AxisNo, Name = cfg.Name, KindText = cfg.KindText, SoftLimitEnabled = cfg.SoftLimitEnabled, SoftLimitNeg = cfg.SoftLimitNeg, SoftLimitPos = cfg.SoftLimitPos, MaxVelocity = cfg.MaxVelocity }; } public MotionAxisConfig ToConfig() { return new MotionAxisConfig { AxisNo = AxisNo, Name = Name, SoftLimitEnabled = SoftLimitEnabled, SoftLimitNeg = SoftLimitNeg, SoftLimitPos = SoftLimitPos, MaxVelocity = MaxVelocity }; } } }