添加 MX-PD-盘古 项目文件
将 MX-PD-盘古 - new 目录下的所有文件添加到主仓库
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
using MainShell.Common;
|
||||
using MainShell.Hardware;
|
||||
using MainShell.Log;
|
||||
using MainShell.PageCalib.OriginCalib.Model;
|
||||
using MainShell.PageCalib.OriginCalib.Service;
|
||||
using Stylet;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
namespace MainShell.PageCalib.OriginCalib.ViewModel
|
||||
{
|
||||
public class AvoidanceAxisViewModel : PropertyChangedBase
|
||||
{
|
||||
private readonly HardwareManager _hardware;
|
||||
private readonly OriginCalibrationMotionService _motionService;
|
||||
private readonly AvoidanceAxisItem _item;
|
||||
private readonly Action<AvoidanceAxisViewModel> _removeCallback;
|
||||
private bool _canTeach = true;
|
||||
|
||||
private const string DialogCaption = "原点标定";
|
||||
|
||||
public AvoidanceAxisItem Item => _item;
|
||||
|
||||
public string AxisName
|
||||
{
|
||||
get { return _item.AxisName; }
|
||||
set { _item.AxisName = value; NotifyOfPropertyChange(nameof(AxisName)); }
|
||||
}
|
||||
|
||||
public double Position
|
||||
{
|
||||
get { return _item.Position; }
|
||||
set { _item.Position = value; NotifyOfPropertyChange(nameof(Position)); }
|
||||
}
|
||||
|
||||
public bool CanTeach
|
||||
{
|
||||
get { return _canTeach; }
|
||||
set { SetAndNotify(ref _canTeach, value); }
|
||||
}
|
||||
|
||||
public AvoidanceAxisViewModel(AvoidanceAxisItem item, HardwareManager hardware, OriginCalibrationMotionService motionService, Action<AvoidanceAxisViewModel> removeCallback)
|
||||
{
|
||||
_item = item ?? throw new ArgumentNullException(nameof(item));
|
||||
_hardware = hardware ?? throw new ArgumentNullException(nameof(hardware));
|
||||
_motionService = motionService ?? throw new ArgumentNullException(nameof(motionService));
|
||||
_removeCallback = removeCallback;
|
||||
}
|
||||
|
||||
public void TeachPosition()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_hardware.AxesDic != null && _hardware.AxesDic.ContainsKey(AxisName))
|
||||
{
|
||||
Position = Math.Round(_hardware.AxesDic[AxisName].State.ActualPos, 3);
|
||||
string.Format("原点标定避让轴“{0}”示教位置成功:{1}", AxisName, Position).LogInfo();
|
||||
}
|
||||
else
|
||||
{
|
||||
string.Format("原点标定示教避让位置时未找到轴“{0}”。", AxisName).LogSysError();
|
||||
LocalizedMessageBox.ShowFormat(MessageKey.OriginCalibAxisNotFound, MessageKey.TitleWarning, MessageBoxButton.OK, MessageBoxImage.Warning, AxisName);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
string.Format("原点标定读取轴“{0}”位置失败:{1}", AxisName, ex.Message).LogSysError();
|
||||
LocalizedMessageBox.ShowFormat(MessageKey.OriginCalibReadPositionFailed, MessageKey.TitleError, MessageBoxButton.OK, MessageBoxImage.Error, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task MoveToPosition()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_hardware.AxesDic != null && _hardware.AxesDic.ContainsKey(AxisName))
|
||||
{
|
||||
await _motionService.MoveAxisAsync(AxisName, Position);
|
||||
}
|
||||
else
|
||||
{
|
||||
string.Format("原点标定移动避让轴时未找到轴“{0}”。", AxisName).LogSysError();
|
||||
LocalizedMessageBox.ShowFormat(MessageKey.OriginCalibAxisNotFound, MessageKey.TitleWarning, MessageBoxButton.OK, MessageBoxImage.Warning, AxisName);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
string.Format("原点标定移动避让轴“{0}”失败:{1}", AxisName, ex.Message).LogSysError();
|
||||
LocalizedMessageBox.ShowFormat(MessageKey.OriginCalibMoveFailed, MessageKey.TitleError, MessageBoxButton.OK, MessageBoxImage.Error, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public void Remove()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_removeCallback != null)
|
||||
{
|
||||
string.Format("原点标定正在删除避让轴“{0}”。", AxisName).LogInfo();
|
||||
_removeCallback(this);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
string.Format("原点标定删除避让轴“{0}”失败:{1}", AxisName, ex.Message).LogSysError();
|
||||
LocalizedMessageBox.ShowFormat(MessageKey.OriginCalibDeleteAxisFailed, MessageKey.TitleError, MessageBoxButton.OK, MessageBoxImage.Error, ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
using MainShell.PageCalib.OriginCalib.Model;
|
||||
using Stylet;
|
||||
using System;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace MainShell.PageCalib.OriginCalib.ViewModel
|
||||
{
|
||||
public class CalibrationAxisViewModel : PropertyChangedBase
|
||||
{
|
||||
private static readonly double OffsetTolerance = 0.05;
|
||||
private static readonly Brush InactiveBrush = CreateFrozenBrush(0xCC, 0xCC, 0xCC);
|
||||
private static readonly Brush InToleranceBrush = CreateFrozenBrush(0x28, 0xA7, 0x45);
|
||||
private static readonly Brush WarningBrush = CreateFrozenBrush(0xFD, 0x7E, 0x14);
|
||||
|
||||
private readonly CalibrationAxisItem _item;
|
||||
|
||||
public CalibrationAxisItem Item => _item;
|
||||
|
||||
public string AxisName
|
||||
{
|
||||
get { return _item.AxisName; }
|
||||
set { _item.AxisName = value; NotifyOfPropertyChange(nameof(AxisName)); }
|
||||
}
|
||||
|
||||
public double TargetPosition
|
||||
{
|
||||
get { return _item.TargetPosition; }
|
||||
set
|
||||
{
|
||||
_item.TargetPosition = value;
|
||||
NotifyOfPropertyChange(nameof(TargetPosition));
|
||||
UpdateOffset();
|
||||
}
|
||||
}
|
||||
|
||||
public double LastPosition
|
||||
{
|
||||
get { return _item.LastPosition; }
|
||||
set
|
||||
{
|
||||
_item.LastPosition = value;
|
||||
NotifyOfPropertyChange(nameof(LastPosition));
|
||||
UpdateOffset();
|
||||
}
|
||||
}
|
||||
|
||||
public double Offset
|
||||
{
|
||||
get { return _item.Offset; }
|
||||
private set
|
||||
{
|
||||
_item.Offset = value;
|
||||
NotifyOfPropertyChange(nameof(Offset));
|
||||
NotifyOfPropertyChange(nameof(OffsetText));
|
||||
NotifyOfPropertyChange(nameof(OffsetBrush));
|
||||
NotifyOfPropertyChange(nameof(StatusBrush));
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsCalibrated
|
||||
{
|
||||
get { return _item.IsCalibrated; }
|
||||
set
|
||||
{
|
||||
_item.IsCalibrated = value;
|
||||
NotifyOfPropertyChange(nameof(IsCalibrated));
|
||||
NotifyOfPropertyChange(nameof(OffsetText));
|
||||
NotifyOfPropertyChange(nameof(OffsetBrush));
|
||||
NotifyOfPropertyChange(nameof(StatusBrush));
|
||||
}
|
||||
}
|
||||
|
||||
public string OffsetText
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!IsCalibrated) return "-";
|
||||
return Offset >= 0 ? string.Format("+{0:F3}", Offset) : string.Format("{0:F3}", Offset);
|
||||
}
|
||||
}
|
||||
|
||||
public Brush OffsetBrush
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!IsCalibrated) return Brushes.Gray;
|
||||
return Math.Abs(Offset) <= OffsetTolerance ? InToleranceBrush : WarningBrush;
|
||||
}
|
||||
}
|
||||
|
||||
public Brush StatusBrush
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!IsCalibrated) return InactiveBrush;
|
||||
return Math.Abs(Offset) <= OffsetTolerance ? InToleranceBrush : WarningBrush;
|
||||
}
|
||||
}
|
||||
|
||||
public CalibrationAxisViewModel(CalibrationAxisItem item)
|
||||
{
|
||||
_item = item ?? throw new ArgumentNullException(nameof(item));
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
LastPosition = 0;
|
||||
IsCalibrated = false;
|
||||
}
|
||||
|
||||
private void UpdateOffset()
|
||||
{
|
||||
Offset = Math.Round(_item.LastPosition, 3);
|
||||
}
|
||||
|
||||
private static Brush CreateFrozenBrush(byte red, byte green, byte blue)
|
||||
{
|
||||
var brush = new SolidColorBrush(Color.FromRgb(red, green, blue));
|
||||
brush.Freeze();
|
||||
return brush;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,348 @@
|
||||
using MainShell.Common;
|
||||
using MainShell.Common.VisionTemple.ViewModel;
|
||||
using MainShell.Hardware;
|
||||
using MainShell.Log;
|
||||
using MainShell.Motion;
|
||||
using MainShell.PageCalib.OriginCalib.Model;
|
||||
using MainShell.PageCalib.OriginCalib.Service;
|
||||
using MainShell.Resources.CustomControl;
|
||||
using MwFramework.Device;
|
||||
using Stylet;
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
namespace MainShell.PageCalib.OriginCalib.ViewModel
|
||||
{
|
||||
public class OriginCalibModuleViewModel : PropertyChangedBase
|
||||
{
|
||||
private readonly HardwareManager _hardware;
|
||||
private readonly OriginCalibrationMotionService _motionService;
|
||||
private readonly OriginCalibModuleItem _item;
|
||||
private readonly VisionTempleWindowViewModel _visionTempleWindowViewModel;
|
||||
|
||||
private int _index;
|
||||
private bool _isCalibrating;
|
||||
|
||||
public OriginCalibModuleItem Item => _item;
|
||||
|
||||
public int Index
|
||||
{
|
||||
get { return _index; }
|
||||
set { SetAndNotify(ref _index, value); }
|
||||
}
|
||||
|
||||
public string ModuleName
|
||||
{
|
||||
get { return _item.ModuleName; }
|
||||
set { _item.ModuleName = value; NotifyOfPropertyChange(nameof(ModuleName)); }
|
||||
}
|
||||
|
||||
public bool IsIndependent
|
||||
{
|
||||
get { return _item.IsIndependent; }
|
||||
set
|
||||
{
|
||||
_item.IsIndependent = value;
|
||||
NotifyOfPropertyChange(nameof(IsIndependent));
|
||||
NotifyOfPropertyChange(nameof(ShowIndependentBadge));
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsCalibrating
|
||||
{
|
||||
get { return _isCalibrating; }
|
||||
set
|
||||
{
|
||||
SetAndNotify(ref _isCalibrating, value);
|
||||
NotifyOfPropertyChange(nameof(CanStartCalib));
|
||||
|
||||
// 标定进行中禁用避让示教和删除操作
|
||||
foreach (AvoidanceAxisViewModel axis in AvoidanceAxes)
|
||||
{
|
||||
axis.CanTeach = !value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool CanStartCalib
|
||||
{
|
||||
get { return !IsCalibrating; }
|
||||
}
|
||||
|
||||
public bool HasAvoidanceAxes
|
||||
{
|
||||
get { return AvoidanceAxes.Count > 0; }
|
||||
}
|
||||
|
||||
public System.Windows.Visibility ShowIndependentBadge
|
||||
{
|
||||
get { return IsIndependent ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed; }
|
||||
}
|
||||
|
||||
public System.Windows.Visibility ShowAvoidanceBadge
|
||||
{
|
||||
get { return HasAvoidanceAxes ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed; }
|
||||
}
|
||||
|
||||
public ObservableCollection<AvoidanceAxisViewModel> AvoidanceAxes { get; } = new ObservableCollection<AvoidanceAxisViewModel>();
|
||||
public ObservableCollection<CalibrationAxisViewModel> CalibrationAxes { get; } = new ObservableCollection<CalibrationAxisViewModel>();
|
||||
|
||||
public OriginCalibModuleViewModel(
|
||||
OriginCalibModuleItem item,
|
||||
HardwareManager hardware,
|
||||
OriginCalibrationMotionService motionService,
|
||||
VisionTempleWindowViewModel visionTempleWindowViewModel)
|
||||
{
|
||||
_item = item ?? throw new ArgumentNullException(nameof(item));
|
||||
_hardware = hardware ?? throw new ArgumentNullException(nameof(hardware));
|
||||
_motionService = motionService ?? throw new ArgumentNullException(nameof(motionService));
|
||||
_visionTempleWindowViewModel = visionTempleWindowViewModel ?? throw new ArgumentNullException(nameof(visionTempleWindowViewModel));
|
||||
|
||||
foreach (AvoidanceAxisItem axisItem in item.AvoidanceAxes)
|
||||
{
|
||||
AvoidanceAxes.Add(new AvoidanceAxisViewModel(axisItem, hardware, motionService, OnRemoveAvoidanceAxis));
|
||||
}
|
||||
|
||||
foreach (CalibrationAxisItem calibItem in item.CalibrationAxes)
|
||||
{
|
||||
CalibrationAxes.Add(new CalibrationAxisViewModel(calibItem));
|
||||
}
|
||||
}
|
||||
|
||||
public void AddAvoidanceAxis()
|
||||
{
|
||||
AvoidanceAxisItem newItem = new AvoidanceAxisItem { AxisName = "NewAxis", Position = 0.0 };
|
||||
_item.AvoidanceAxes.Add(newItem);
|
||||
AvoidanceAxisViewModel newVm = new AvoidanceAxisViewModel(newItem, _hardware, _motionService, OnRemoveAvoidanceAxis);
|
||||
newVm.CanTeach = !_isCalibrating;
|
||||
AvoidanceAxes.Add(newVm);
|
||||
NotifyOfPropertyChange(nameof(HasAvoidanceAxes));
|
||||
NotifyOfPropertyChange(nameof(ShowAvoidanceBadge));
|
||||
}
|
||||
|
||||
private void OnRemoveAvoidanceAxis(AvoidanceAxisViewModel vm)
|
||||
{
|
||||
if (_isCalibrating)
|
||||
{
|
||||
"原点标定进行中,无法删除避让轴。".LogSysError();
|
||||
LocalizedMessageBox.Show(MessageKey.OriginCalibDeleteAxisDuringCalibration, MessageKey.TitleWarning, MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
string.Format("原点标定模块“{1}”准备删除避让轴“{0}”。", vm.AxisName, ModuleName).LogInfo();
|
||||
_item.AvoidanceAxes.Remove(vm.Item);
|
||||
AvoidanceAxes.Remove(vm);
|
||||
NotifyOfPropertyChange(nameof(HasAvoidanceAxes));
|
||||
NotifyOfPropertyChange(nameof(ShowAvoidanceBadge));
|
||||
string.Format("原点标定删除避让轴成功,当前剩余 {0} 个避让轴。", AvoidanceAxes.Count).LogInfo();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
string.Format("原点标定删除避让轴“{0}”失败:{1}", vm.AxisName, ex.Message).LogSysError();
|
||||
LocalizedMessageBox.ShowFormat(MessageKey.OriginCalibDeleteAxisFailed, MessageKey.TitleError, MessageBoxButton.OK, MessageBoxImage.Error, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public void TeachCalibPositions()
|
||||
{
|
||||
foreach (CalibrationAxisViewModel calibAxis in CalibrationAxes)
|
||||
{
|
||||
if (_hardware.AxesDic != null && _hardware.AxesDic.ContainsKey(calibAxis.AxisName))
|
||||
{
|
||||
calibAxis.TargetPosition = Math.Round(_hardware.AxesDic[calibAxis.AxisName].State.ActualPos, 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void MakeTemplate()
|
||||
{
|
||||
try
|
||||
{
|
||||
if(Item.ModuleName== OriginCalibModuleNames.GantryX2)
|
||||
{
|
||||
_visionTempleWindowViewModel.SetCamera(CameraType.TopWideCamera);
|
||||
}
|
||||
else
|
||||
{
|
||||
_visionTempleWindowViewModel.SetCamera(CameraType.TopPositionCamera);
|
||||
}
|
||||
_visionTempleWindowViewModel.TemplatePath = _item.TemplatePath;
|
||||
$"原点标定模块“{ModuleName}”打开视觉模板制作窗口。".LogInfo();
|
||||
_visionTempleWindowViewModel.ShowWindow();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
$"原点标定模块“{ModuleName}”打开视觉模板制作窗口失败:{ex.Message}".LogSysError();
|
||||
LocalizedMessageBox.ShowFormat(
|
||||
MessageKey.OriginCalibOpenVisionTemplateFailed,
|
||||
MessageKey.TitleError,
|
||||
MessageBoxButton.OK,
|
||||
MessageBoxImage.Error,
|
||||
ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task StartCalib()
|
||||
{
|
||||
if (IsCalibrating)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
IsCalibrating = true;
|
||||
LoadingService.Instance.Show(string.Format("模块 {0} 正在标定...", ModuleName), true, true);
|
||||
try
|
||||
{
|
||||
string.Format(
|
||||
"原点标定模块“{0}”开始标定,避让轴数量:{1},标定轴数量:{2}。",
|
||||
ModuleName,
|
||||
AvoidanceAxes.Count,
|
||||
CalibrationAxes.Count).LogInfo();
|
||||
|
||||
var calibrationResult = await _motionService.ExecuteAsync(
|
||||
AvoidanceAxes.Select(x => x.Item),
|
||||
CalibrationAxes.Select(x => x.Item),
|
||||
LoadingService.Instance.Token);
|
||||
|
||||
// 执行模块特定的后处理逻辑,通常在运动完成后、标定写入前
|
||||
if (_item.PostProcessor != null)
|
||||
{
|
||||
string.Format(
|
||||
"原点标定模块“{0}”执行后处理“{1}”。",
|
||||
ModuleName,
|
||||
_item.PostProcessor.Name).LogInfo();
|
||||
|
||||
try
|
||||
{
|
||||
await _item.PostProcessor.ExecuteAsync(calibrationResult, LoadingService.Instance.Token, new CenterRecognitionParameters
|
||||
{
|
||||
TemplatePath = _item.TemplatePath,
|
||||
Type = CenterRecognitionType.Template
|
||||
});
|
||||
string.Format(
|
||||
"原点标定模块“{0}”后处理“{1}”完成。",
|
||||
ModuleName,
|
||||
_item.PostProcessor.Name).LogInfo();
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
string.Format(
|
||||
"原点标定模块“{0}”后处理“{1}”被取消。",
|
||||
ModuleName,
|
||||
_item.PostProcessor.Name).LogInfo();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
string.Format(
|
||||
"原点标定模块“{0}”后处理“{1}”失败:{2}",
|
||||
ModuleName,
|
||||
_item.PostProcessor.Name,
|
||||
ex.Message).LogSysError();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (CalibrationAxisViewModel calibAxis in CalibrationAxes)
|
||||
{
|
||||
IAxis axis = ResolveCalibrationAxis(calibAxis.AxisName);
|
||||
if (axis != null)
|
||||
{
|
||||
calibAxis.LastPosition = Math.Round(GetActualPosition(axis), 3);
|
||||
calibAxis.IsCalibrated = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
double lastPosition;
|
||||
if (calibrationResult.ActualPositions.TryGetValue(calibAxis.AxisName, out lastPosition))
|
||||
{
|
||||
calibAxis.LastPosition = lastPosition;
|
||||
calibAxis.IsCalibrated = true;
|
||||
}
|
||||
}
|
||||
|
||||
string.Format("原点标定模块“{0}”标定完成。", ModuleName).LogInfo();
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
string.Format("原点标定模块“{0}”取消标定。", ModuleName).LogInfo();
|
||||
LocalizedMessageBox.Show(MessageKey.OriginCalibCalibrationCanceled, MessageKey.TitleWarning, MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
string.Format("原点标定模块“{0}”标定失败:{1}", ModuleName, ex.Message).LogSysError();
|
||||
LocalizedMessageBox.ShowFormat(MessageKey.OriginCalibCalibrationFailed, MessageKey.TitleError, MessageBoxButton.OK, MessageBoxImage.Error, ex.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
LoadingService.Instance.Hide();
|
||||
IsCalibrating = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void ResetModule()
|
||||
{
|
||||
foreach (CalibrationAxisViewModel calibAxis in CalibrationAxes)
|
||||
{
|
||||
calibAxis.Reset();
|
||||
}
|
||||
|
||||
string.Format("原点标定模块“{0}”已复位。", ModuleName).LogInfo();
|
||||
}
|
||||
|
||||
private IAxis ResolveCalibrationAxis(string axisName)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(axisName))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (_hardware.AxesDic != null && _hardware.AxesDic.ContainsKey(axisName))
|
||||
{
|
||||
return _hardware.AxesDic[axisName];
|
||||
}
|
||||
|
||||
if (string.Equals(axisName, "X1", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return _hardware.Axis_X1;
|
||||
}
|
||||
|
||||
if (string.Equals(axisName, "Y1", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return _hardware.Axis_Y1;
|
||||
}
|
||||
|
||||
if (string.Equals(axisName, "X2", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return _hardware.Axis_X2;
|
||||
}
|
||||
|
||||
if (string.Equals(axisName, "Y2", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return _hardware.Axis_Y2;
|
||||
}
|
||||
|
||||
if (string.Equals(axisName, "WSX3", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return _hardware.Axis_WS_X3;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static double GetActualPosition(IAxis axis)
|
||||
{
|
||||
if (axis == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return axis.State != null ? axis.State.ActualPos : axis.GetPositionImmediate();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
using MainShell.Common;
|
||||
using MainShell.Common.Display.ViewModel;
|
||||
using MainShell.Common.VisionTemple.ViewModel;
|
||||
using MainShell.EventArgsFolder;
|
||||
using MainShell.Hardware;
|
||||
using MainShell.Models;
|
||||
using MainShell.Motion;
|
||||
using MainShell.PageCalib.OriginCalib.Model;
|
||||
using MainShell.PageCalib.OriginCalib.Service;
|
||||
using MaxwellFramework.Core.Interfaces;
|
||||
using MwFramework.Device;
|
||||
using Stylet;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
|
||||
namespace MainShell.PageCalib.OriginCalib.ViewModel
|
||||
{
|
||||
public class OriginCalibViewModel : BaseScreen, IPage
|
||||
{
|
||||
public string Name { get; set; } = "OriginCalib";
|
||||
|
||||
private readonly OriginCalibSetting _setting;
|
||||
private readonly HardwareManager _hardware;
|
||||
private readonly OriginCalibrationMotionService _originCalibrationMotionService;
|
||||
private readonly ApproachAlignmentService _approachAlignmentService;
|
||||
private readonly VisionTempleWindowViewModel _visionTempleWindowViewModel;
|
||||
private readonly IEventAggregator _eventAggregator;
|
||||
private bool _isInitialized;
|
||||
|
||||
private const string DialogCaption = "原点标定";
|
||||
|
||||
public ObservableCollection<OriginCalibModuleViewModel> Modules { get; } = new ObservableCollection<OriginCalibModuleViewModel>();
|
||||
|
||||
private CameraAxisViewModel _cameraAxisViewModel;
|
||||
public CameraAxisViewModel CameraAxisViewModel
|
||||
{
|
||||
get { return _cameraAxisViewModel; }
|
||||
private set { SetAndNotify(ref _cameraAxisViewModel, value); }
|
||||
}
|
||||
|
||||
public OriginCalibViewModel(
|
||||
HardwareManager hardware,
|
||||
OriginCalibrationMotionService originCalibrationMotionService,
|
||||
ApproachAlignmentService approachAlignmentService,
|
||||
VisionTempleWindowViewModel visionTempleWindowViewModel,
|
||||
IEventAggregator eventAggregator)
|
||||
{
|
||||
_hardware = hardware ?? throw new ArgumentNullException(nameof(hardware));
|
||||
_originCalibrationMotionService = originCalibrationMotionService ?? throw new ArgumentNullException(nameof(originCalibrationMotionService));
|
||||
_approachAlignmentService = approachAlignmentService ?? throw new ArgumentNullException(nameof(approachAlignmentService));
|
||||
_visionTempleWindowViewModel = visionTempleWindowViewModel ?? throw new ArgumentNullException(nameof(visionTempleWindowViewModel));
|
||||
_eventAggregator = eventAggregator ?? throw new ArgumentNullException(nameof(eventAggregator));
|
||||
_setting = OriginCalibSetting.LoadOrCreate();
|
||||
}
|
||||
|
||||
protected override void OnViewLoaded()
|
||||
{
|
||||
base.OnViewLoaded();
|
||||
|
||||
if (_isInitialized)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CameraAxisViewModel = IoC.Get<CameraAxisViewModel>();
|
||||
CameraAxisViewModel.CameraAxisDevices.HardwareDeviceList = _hardware.CameraAxisManager.TopPositionAndWideCameraAxisDevices;
|
||||
|
||||
InitializePostProcessors();
|
||||
RebuildModuleViewModels();
|
||||
_isInitialized = true;
|
||||
}
|
||||
|
||||
private void InitializePostProcessors()
|
||||
{
|
||||
foreach (OriginCalibModuleItem module in _setting.Modules)
|
||||
{
|
||||
if (module == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
module.PostProcessor = CreateDefaultPostProcessor(module);
|
||||
}
|
||||
}
|
||||
|
||||
private ICalibrationPostProcessor CreateDefaultPostProcessor(OriginCalibModuleItem module)
|
||||
{
|
||||
if (module == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(module.TemplatePath) || module.CalibrationAxes == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
List<ApproachAlignmentAxis> alignmentAxes = module.CalibrationAxes
|
||||
.Where(item => item != null)
|
||||
.Select(item => item.AxisName)
|
||||
.Where(axisName => !string.IsNullOrWhiteSpace(axisName))
|
||||
.Select(axisName => new ApproachAlignmentAxis(axisName, 0.005))
|
||||
.ToList();
|
||||
|
||||
if (alignmentAxes.Count == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new SingleAlignmentPostProcessor(
|
||||
_approachAlignmentService,
|
||||
alignmentAxes,
|
||||
CameraType.TopPositionCamera);
|
||||
}
|
||||
|
||||
|
||||
private void RebuildModuleViewModels()
|
||||
{
|
||||
Modules.Clear();
|
||||
for (int i = 0; i < _setting.Modules.Count; i++)
|
||||
{
|
||||
OriginCalibModuleViewModel moduleVm = new OriginCalibModuleViewModel(
|
||||
_setting.Modules[i],
|
||||
_hardware,
|
||||
_originCalibrationMotionService,
|
||||
_visionTempleWindowViewModel);
|
||||
moduleVm.Index = i + 1;
|
||||
Modules.Add(moduleVm);
|
||||
}
|
||||
}
|
||||
|
||||
public void SaveConfig()
|
||||
{
|
||||
try
|
||||
{
|
||||
_setting.Write();
|
||||
_eventAggregator.Publish(CreateOriginCalibDataChangedEventArgs());
|
||||
LocalizedMessageBox.Show(MessageKey.OriginCalibConfigSaved, MessageKey.TitleInfo, MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LocalizedMessageBox.ShowFormat(MessageKey.OriginCalibConfigSaveFailed, MessageKey.TitleError, MessageBoxButton.OK, MessageBoxImage.Error, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private OriginCalibDataChangedEventArgs CreateOriginCalibDataChangedEventArgs()
|
||||
{
|
||||
List<OriginCalibAxisOffsetChangedItem> axisOffsets = new List<OriginCalibAxisOffsetChangedItem>();
|
||||
|
||||
foreach (OriginCalibModuleItem module in _setting.Modules)
|
||||
{
|
||||
if (module == null || module.CalibrationAxes == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (CalibrationAxisItem calibrationAxis in module.CalibrationAxes)
|
||||
{
|
||||
if (calibrationAxis == null || string.IsNullOrWhiteSpace(calibrationAxis.AxisName))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
axisOffsets.Add(new OriginCalibAxisOffsetChangedItem(calibrationAxis.AxisName, calibrationAxis.Offset));
|
||||
}
|
||||
}
|
||||
|
||||
return new OriginCalibDataChangedEventArgs(axisOffsets);
|
||||
}
|
||||
|
||||
public void ResetAll()
|
||||
{
|
||||
foreach (OriginCalibModuleViewModel module in Modules)
|
||||
{
|
||||
module.ResetModule();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user