台架驱动

This commit is contained in:
hsc
2026-07-17 17:36:08 +08:00
parent c4ec348217
commit 32d62de6f8
10 changed files with 1494 additions and 626 deletions

View File

@@ -1,413 +1,485 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Threading;
using BenchMovementModule.HardwareDrive;
using Logger;
using UIShare.ViewModelBase;
namespace BenchMovementModule.ViewModels
{
public class BenchMovementViewModel : NavigateViewModelBase, IRegionMemberLifetime, IDisposable
{
#region
#region
public bool KeepAlive => true;
// --- 1. 台架位置区 ---
private double _posX;
public double PosX
// ===== GantryControlTcp 实例 =====
private GantryControlTcp? _gantry;
// ===== 连接状态 =====
private bool _isConnected;
public bool IsConnected
{
get => _posX;
set => SetProperty(ref _posX, value);
get => _isConnected;
set => SetProperty(ref _isConnected, value);
}
private double _posY;
public double PosY
private string _connectionStatus = "未连接";
public string ConnectionStatus
{
get => _posY;
set => SetProperty(ref _posY, value);
get => _connectionStatus;
set => SetProperty(ref _connectionStatus, value);
}
private double _posZ;
public double PosZ
{
get => _posZ;
set => SetProperty(ref _posZ, value);
}
// ===== 6轴绝对位置显示相对原点的实时位置只读=====
private int _absPos1;
public int AbsPos1 { get => _absPos1; set => SetProperty(ref _absPos1, value); }
// --- 2. 信号回读区 ---
private double _pos1X;
public double Pos1X
{
get => _pos1X;
set => SetProperty(ref _pos1X, value);
}
private int _absPos2;
public int AbsPos2 { get => _absPos2; set => SetProperty(ref _absPos2, value); }
private int _pos1Quality;
public int Pos1Quality
{
get => _pos1Quality;
set => SetProperty(ref _pos1Quality, value);
}
private int _absPos3;
public int AbsPos3 { get => _absPos3; set => SetProperty(ref _absPos3, value); }
private double _pos2X;
public double Pos2X
{
get => _pos2X;
set => SetProperty(ref _pos2X, value);
}
private int _absPos4;
public int AbsPos4 { get => _absPos4; set => SetProperty(ref _absPos4, value); }
private int _pos2Quality;
public int Pos2Quality
{
get => _pos2Quality;
set => SetProperty(ref _pos2Quality, value);
}
private int _absPos5;
public int AbsPos5 { get => _absPos5; set => SetProperty(ref _absPos5, value); }
private double _pos1Y;
public double Pos1Y
{
get => _pos1Y;
set => SetProperty(ref _pos1Y, value);
}
private int _absPos6;
public int AbsPos6 { get => _absPos6; set => SetProperty(ref _absPos6, value); }
private double _pos2Y;
public double Pos2Y
{
get => _pos2Y;
set => SetProperty(ref _pos2Y, value);
}
// ===== 寸动目标位置 =====
private int _targetX;
public int TargetX { get => _targetX; set => SetProperty(ref _targetX, value); }
// --- 3. 扫描参数区 ---
private double _scanStepX = 10;
public double ScanStepX
{
get => _scanStepX;
set => SetProperty(ref _scanStepX, value);
}
private int _targetY;
public int TargetY { get => _targetY; set => SetProperty(ref _targetY, value); }
private double _scanStartX = -120;
public double ScanStartX
{
get => _scanStartX;
set => SetProperty(ref _scanStartX, value);
}
private int _targetZ;
public int TargetZ { get => _targetZ; set => SetProperty(ref _targetZ, value); }
private double _scanEndX = 120;
public double ScanEndX
{
get => _scanEndX;
set => SetProperty(ref _scanEndX, value);
}
// ===== 扫描参数 =====
private int _scanStepX = 1000;
public int ScanStepX { get => _scanStepX; set => SetProperty(ref _scanStepX, value); }
private double _scanStepY = 10;
public double ScanStepY
{
get => _scanStepY;
set => SetProperty(ref _scanStepY, value);
}
private int _scanStartX;
public int ScanStartX { get => _scanStartX; set => SetProperty(ref _scanStartX, value); }
private double _scanStartY = -120;
public double ScanStartY
{
get => _scanStartY;
set => SetProperty(ref _scanStartY, value);
}
private int _scanEndX = 50000;
public int ScanEndX { get => _scanEndX; set => SetProperty(ref _scanEndX, value); }
private double _scanEndY = 120;
public double ScanEndY
{
get => _scanEndY;
set => SetProperty(ref _scanEndY, value);
}
private int _scanStepY = 1000;
public int ScanStepY { get => _scanStepY; set => SetProperty(ref _scanStepY, value); }
private double _scanStepZ = 1;
public double ScanStepZ
{
get => _scanStepZ;
set => SetProperty(ref _scanStepZ, value);
}
private int _scanStartY;
public int ScanStartY { get => _scanStartY; set => SetProperty(ref _scanStartY, value); }
private double _scanStartZ = 0;
public double ScanStartZ
{
get => _scanStartZ;
set => SetProperty(ref _scanStartZ, value);
}
private int _scanEndY = 50000;
public int ScanEndY { get => _scanEndY; set => SetProperty(ref _scanEndY, value); }
private double _scanEndZ = 0;
public double ScanEndZ
{
get => _scanEndZ;
set => SetProperty(ref _scanEndZ, value);
}
private int _scanStepZ = 100;
public int ScanStepZ { get => _scanStepZ; set => SetProperty(ref _scanStepZ, value); }
// --- 4. 寸动参数区 ---
private double _jogX;
public double JogX
{
get => _jogX;
set => SetProperty(ref _jogX, value);
}
private int _scanStartZ;
public int ScanStartZ { get => _scanStartZ; set => SetProperty(ref _scanStartZ, value); }
private double _jogY;
public double JogY
{
get => _jogY;
set => SetProperty(ref _jogY, value);
}
private int _scanEndZ = 20000;
public int ScanEndZ { get => _scanEndZ; set => SetProperty(ref _scanEndZ, value); }
private double _jogZ;
public double JogZ
{
get => _jogZ;
set => SetProperty(ref _jogZ, value);
}
// ===== 速度设定(脉冲)=====
private int _speedX = 5000;
public int SpeedX { get => _speedX; set => SetProperty(ref _speedX, value); }
// --- 5. 设置页网络及PLC参数 ---
private string _plcIp = "192.168.0.30";
public string PlcIp
{
get => _plcIp;
set => SetProperty(ref _plcIp, value);
}
private int _speedY = 5000;
public int SpeedY { get => _speedY; set => SetProperty(ref _speedY, value); }
private int _plcPort = 502;
public int PlcPort
{
get => _plcPort;
set => SetProperty(ref _plcPort, value);
}
private int _speedZ = 2000;
public int SpeedZ { get => _speedZ; set => SetProperty(ref _speedZ, value); }
// --- 6. 设置页:速度与零点设定 ---
private double _speedX;
public double SpeedX
{
get => _speedX;
set => SetProperty(ref _speedX, value);
}
private int _homeSpeed = 3000;
public int HomeSpeed { get => _homeSpeed; set => SetProperty(ref _homeSpeed, value); }
private double _speedY;
public double SpeedY
{
get => _speedY;
set => SetProperty(ref _speedY, value);
}
// ===== 零点设定 =====
private int _homeX;
public int HomeX { get => _homeX; set => SetProperty(ref _homeX, value); }
private double _speedZ;
public double SpeedZ
{
get => _speedZ;
set => SetProperty(ref _speedZ, value);
}
private int _homeY;
public int HomeY { get => _homeY; set => SetProperty(ref _homeY, value); }
private double _zeroX;
public double ZeroX
{
get => _zeroX;
set => SetProperty(ref _zeroX, value);
}
private double _zeroY;
public double ZeroY
{
get => _zeroY;
set => SetProperty(ref _zeroY, value);
}
private double _zeroZ;
public double ZeroZ
{
get => _zeroZ;
set => SetProperty(ref _zeroZ, value);
}
// --- 7. 设置页CAN设定信号映射 ---
private int _canChannel = 0;
public int CanChannel
{
get => _canChannel;
set => SetProperty(ref _canChannel, value);
}
private string _canMsgId = "0X0";
public string CanMsgId
{
get => _canMsgId;
set => SetProperty(ref _canMsgId, value);
}
private string _canMsgName = string.Empty;
public string CanMsgName
{
get => _canMsgName;
set => SetProperty(ref _canMsgName, value);
}
private string _sigPos1X = string.Empty;
public string SigPos1X
{
get => _sigPos1X;
set => SetProperty(ref _sigPos1X, value);
}
private string _sigPos2X = string.Empty;
public string SigPos2X
{
get => _sigPos2X;
set => SetProperty(ref _sigPos2X, value);
}
private string _sigPos1Y = string.Empty;
public string SigPos1Y
{
get => _sigPos1Y;
set => SetProperty(ref _sigPos1Y, value);
}
private string _sigPos2Y = string.Empty;
public string SigPos2Y
{
get => _sigPos2Y;
set => SetProperty(ref _sigPos2Y, value);
}
private string _sigPos1Quality = string.Empty;
public string SigPos1Quality
{
get => _sigPos1Quality;
set => SetProperty(ref _sigPos1Quality, value);
}
private string _sigPos2Quality = string.Empty;
public string SigPos2Quality
{
get => _sigPos2Quality;
set => SetProperty(ref _sigPos2Quality, value);
}
private int _homeZ;
public int HomeZ { get => _homeZ; set => SetProperty(ref _homeZ, value); }
#endregion
#region
// 扫描控制面板命令
public ICommand StartScanCommand { get; }
public ICommand StopScanCommand { get; }
public ICommand GoHomeCommand { get; }
// 寸动执行单轴移动
public ICommand OpenConfigCommand { get; }
public ICommand ConnectCommand { get; }
public ICommand DisconnectCommand { get; }
public ICommand MoveXCommand { get; }
public ICommand MoveYCommand { get; }
public ICommand MoveZCommand { get; }
// 全局控制
public ICommand MoveAllCommand { get; }
public ICommand GoHomeCommand { get; }
public ICommand EmergencyStopCommand { get; }
// 参数设置应用命令
public ICommand ApplyNetworkCommand { get; }
public ICommand StopXCommand { get; }
public ICommand StopYCommand { get; }
public ICommand StopZCommand { get; }
public ICommand SetSpeedCommand { get; }
public ICommand SetZeroCommand { get; }
public ICommand SetHomeCommand { get; }
public ICommand ReadStatusCommand { get; }
#endregion
#region
private readonly IContainerProvider _containerProvider;
private readonly DispatcherTimer _pollTimer;
private bool _isInitiated = false;
private string _testStatus = string.Empty;
#endregion
public BenchMovementViewModel(IContainerProvider containerProvider) : base(containerProvider)
{
_containerProvider = containerProvider;
// 创建 GantryControlTcp 实例(默认配置,用户可通过弹窗修改)
_gantry = new GantryControlTcp("192.168.0.30", 502);
// 初始化扫描控制命令绑定
StartScanCommand = new DelegateCommand(OnStartScan);
StopScanCommand = new DelegateCommand(OnStopScan);
GoHomeCommand = new DelegateCommand(OnGoHome);
// 初始化寸动命令绑定
OpenConfigCommand = new DelegateCommand(OnOpenConfig);
ConnectCommand = new DelegateCommand(OnConnect);
DisconnectCommand = new DelegateCommand(OnDisconnect);
MoveXCommand = new DelegateCommand(OnMoveX);
MoveYCommand = new DelegateCommand(OnMoveY);
MoveZCommand = new DelegateCommand(OnMoveZ);
// 急停与设置保存
MoveAllCommand = new DelegateCommand(OnMoveAll);
GoHomeCommand = new DelegateCommand(OnGoHome);
EmergencyStopCommand = new DelegateCommand(OnEmergencyStop);
ApplyNetworkCommand = new DelegateCommand(OnApplyNetwork);
StopXCommand = new DelegateCommand(OnStopX);
StopYCommand = new DelegateCommand(OnStopY);
StopZCommand = new DelegateCommand(OnStopZ);
SetSpeedCommand = new DelegateCommand(OnSetSpeed);
SetZeroCommand = new DelegateCommand(OnSetZero);
SetHomeCommand = new DelegateCommand(OnSetHome);
ReadStatusCommand = new DelegateCommand(OnReadStatus);
// 位置轮询定时器500ms
_pollTimer = new DispatcherTimer(DispatcherPriority.Background)
{
Interval = TimeSpan.FromMilliseconds(500)
};
_pollTimer.Tick += OnPollTick;
}
#region
#region
private void OnStartScan()
private void OnOpenConfig()
{
// TODO: 等协议到了处理开始扫描
var param = new DialogParameters();
param.Add("Gantry", _gantry);
_dialogService.ShowDialog("GantryConfigDialog", param, result =>
{
UpdateConnectionStatus();
});
}
private void OnStopScan()
private async void OnConnect()
{
// TODO: 等协议到了处理停止扫描
if (_gantry == null) return;
try
{
ConnectionStatus = "连接中...";
bool ok = await _gantry.ConnectAsync();
if (ok)
{
IsConnected = true;
ConnectionStatus = "已连接";
_pollTimer.Start();
await ReadDeviceParametersAsync();
LoggerHelper.Info("[BenchMovement] 台架连接成功");
}
else
{
ConnectionStatus = "连接失败";
}
}
catch (Exception ex)
{
LoggerHelper.Error($"[BenchMovement] 连接失败: {ex.Message}");
ConnectionStatus = $"连接失败: {ex.Message}";
}
UpdateConnectionStatus();
}
private void OnGoHome()
private void OnDisconnect()
{
// TODO: 等协议到了处理台架回原点逻辑
if (_gantry == null) return;
try
{
_pollTimer.Stop();
_gantry.Close();
IsConnected = false;
ConnectionStatus = "未连接";
LoggerHelper.Info("[BenchMovement] 台架已断开");
}
catch (Exception ex)
{
LoggerHelper.Error($"[BenchMovement] 断开失败: {ex.Message}");
}
UpdateConnectionStatus();
}
private void OnMoveX()
private async void OnMoveX()
{
// TODO: 执行单轴X向绝对位置寸动
if (!EnsureConnected()) return;
try
{
await _gantry!.SetAxisTargetPositionAsync(1, TargetX);
await _gantry.StartSingleAxisAsync(1);
LoggerHelper.Info($"[BenchMovement] X轴移动到 {TargetX}");
}
catch (Exception ex)
{
LoggerHelper.Error($"[BenchMovement] X轴移动失败: {ex.Message}");
ShowErrorMessageBox($"X轴移动失败: {ex.Message}", () => { });
}
}
private void OnMoveY()
private async void OnMoveY()
{
// TODO: 执行单轴Y向绝对位置寸动
if (!EnsureConnected()) return;
try
{
await _gantry!.SetAxisTargetPositionAsync(2, TargetY);
await _gantry.StartSingleAxisAsync(2);
LoggerHelper.Info($"[BenchMovement] Y轴移动到 {TargetY}");
}
catch (Exception ex)
{
LoggerHelper.Error($"[BenchMovement] Y轴移动失败: {ex.Message}");
ShowErrorMessageBox($"Y轴移动失败: {ex.Message}", () => { });
}
}
private void OnMoveZ()
private async void OnMoveZ()
{
// TODO: 执行单轴Z向绝对位置寸动
if (!EnsureConnected()) return;
try
{
await _gantry!.SetAxisTargetPositionAsync(3, TargetZ);
await _gantry.StartSingleAxisAsync(3);
LoggerHelper.Info($"[BenchMovement] Z轴移动到 {TargetZ}");
}
catch (Exception ex)
{
LoggerHelper.Error($"[BenchMovement] Z轴移动失败: {ex.Message}");
ShowErrorMessageBox($"Z轴移动失败: {ex.Message}", () => { });
}
}
private void OnEmergencyStop()
private async void OnMoveAll()
{
// TODO: 全局急停高优先级报文/IO发送
if (!EnsureConnected()) return;
try
{
await _gantry!.SetAxisTargetPositionAsync(1, TargetX);
await _gantry.SetAxisTargetPositionAsync(2, TargetY);
await _gantry.SetAxisTargetPositionAsync(3, TargetZ);
await _gantry.StartAllAxesAsync();
LoggerHelper.Info($"[BenchMovement] 三轴同时移动: X={TargetX}, Y={TargetY}, Z={TargetZ}");
}
catch (Exception ex)
{
LoggerHelper.Error($"[BenchMovement] 三轴移动失败: {ex.Message}");
ShowErrorMessageBox($"三轴移动失败: {ex.Message}", () => { });
}
}
private void OnApplyNetwork()
private async void OnGoHome()
{
// TODO: 应用PLC的IP和端口网络连接配置
if (!EnsureConnected()) return;
try
{
await _gantry!.HomeAsync();
LoggerHelper.Info("[BenchMovement] 回原点已触发");
}
catch (Exception ex)
{
LoggerHelper.Error($"[BenchMovement] 回原点失败: {ex.Message}");
ShowErrorMessageBox($"回原点失败: {ex.Message}", () => { });
}
}
private void OnSetSpeed()
private async void OnEmergencyStop()
{
// TODO: 发送三轴移动速度设定参数
if (!EnsureConnected()) return;
try
{
await _gantry!.SetEStopStateAsync(true);
LoggerHelper.Info("[BenchMovement] 急停已触发");
}
catch (Exception ex)
{
LoggerHelper.Error($"[BenchMovement] 急停失败: {ex.Message}");
}
}
private void OnSetZero()
private async void OnStopX()
{
// TODO: 发送台架绝对位置零点校准设定
if (!EnsureConnected()) return;
try
{
await _gantry!.SetStopStateAsync(1, true);
await Task.Delay(100);
await _gantry.SetStopStateAsync(1, false);
}
catch (Exception ex)
{
LoggerHelper.Error($"[BenchMovement] X轴停止失败: {ex.Message}");
}
}
private async void OnStopY()
{
if (!EnsureConnected()) return;
try
{
await _gantry!.SetStopStateAsync(2, true);
await Task.Delay(100);
await _gantry.SetStopStateAsync(2, false);
}
catch (Exception ex)
{
LoggerHelper.Error($"[BenchMovement] Y轴停止失败: {ex.Message}");
}
}
private async void OnStopZ()
{
if (!EnsureConnected()) return;
try
{
await _gantry!.SetStopStateAsync(3, true);
await Task.Delay(100);
await _gantry.SetStopStateAsync(3, false);
}
catch (Exception ex)
{
LoggerHelper.Error($"[BenchMovement] Z轴停止失败: {ex.Message}");
}
}
private async void OnSetSpeed()
{
if (!EnsureConnected()) return;
try
{
await _gantry!.SetAxisSpeedAsync(1, SpeedX);
await _gantry.SetAxisSpeedAsync(2, SpeedY);
await _gantry.SetAxisSpeedAsync(3, SpeedZ);
await _gantry.SetHomePositionSpeedAsync(HomeSpeed);
LoggerHelper.Info($"[BenchMovement] 速度设定完成: X={SpeedX}, Y={SpeedY}, Z={SpeedZ}, 回零={HomeSpeed}");
}
catch (Exception ex)
{
LoggerHelper.Error($"[BenchMovement] 速度设定失败: {ex.Message}");
ShowErrorMessageBox($"速度设定失败: {ex.Message}", () => { });
}
}
private async void OnSetHome()
{
if (!EnsureConnected()) return;
try
{
await _gantry!.SetHomePositionAsync(HomeX, HomeY, HomeZ);
LoggerHelper.Info($"[BenchMovement] 零点设定完成: X={HomeX}, Y={HomeY}, Z={HomeZ}");
}
catch (Exception ex)
{
LoggerHelper.Error($"[BenchMovement] 零点设定失败: {ex.Message}");
ShowErrorMessageBox($"零点设定失败: {ex.Message}", () => { });
}
}
private async void OnReadStatus()
{
await ReadDeviceParametersAsync();
}
#endregion
#region
#region
private bool EnsureConnected()
{
if (_gantry == null || !_gantry.IsConnected)
{
ShowErrorMessageBox("设备未连接,请先连接台架", () => { });
return false;
}
return true;
}
private void UpdateConnectionStatus()
{
IsConnected = _gantry?.IsConnected ?? false;
ConnectionStatus = IsConnected ? "已连接" : "未连接";
}
private async Task ReadDeviceParametersAsync()
{
if (_gantry == null || !_gantry.IsConnected) return;
try
{
var (x, y, z) = await _gantry.GetHomePositionAsync();
HomeX = x; HomeY = y; HomeZ = z;
SpeedX = await _gantry.GetAxisSpeedAsync(1);
SpeedY = await _gantry.GetAxisSpeedAsync(2);
SpeedZ = await _gantry.GetAxisSpeedAsync(3);
HomeSpeed = await _gantry.GetHomePositionSpeedAsync();
// 读取 6 轴绝对位置
var positions = await _gantry.GetAllAbsolutePositionsAsync();
AbsPos1 = positions.Axis1; AbsPos2 = positions.Axis2; AbsPos3 = positions.Axis3;
AbsPos4 = positions.Axis4; AbsPos5 = positions.Axis5; AbsPos6 = positions.Axis6;
}
catch (Exception ex)
{
LoggerHelper.Error($"[BenchMovement] 读取设备参数失败: {ex.Message}");
}
}
private async void OnPollTick(object? sender, EventArgs e)
{
if (_gantry == null || !_gantry.IsConnected) return;
try
{
// 批量读取 6 轴绝对位置(一次性通信,高效)
var pos = await _gantry.GetAllAbsolutePositionsAsync();
AbsPos1 = pos.Axis1; AbsPos2 = pos.Axis2; AbsPos3 = pos.Axis3;
AbsPos4 = pos.Axis4; AbsPos5 = pos.Axis5; AbsPos6 = pos.Axis6;
}
catch { /* 忽略轮询错误 */ }
}
#endregion
#region
public override void OnNavigatedTo(NavigationContext navigationContext)
{
base.OnNavigatedTo(navigationContext);
if (!_isInitiated && navigationContext.Parameters.ContainsKey("Name"))
if (!_isInitiated)
{
_testStatus = navigationContext.Parameters.GetValue<string>("Name");
// TODO: 若后续需要从全局环境 Scope 字典中加载参数配置,可以在此处对齐获取:
// var scope = _globalInfo.ScopeDic[_testStatus];
_isInitiated = true;
}
}
public void Dispose()
{
// 用于界面关闭或Region注销时的资源释放例如解除事件订阅、断开临时Socket通道、清空通信缓冲
_pollTimer.Stop();
_gantry?.Close();
_gantry?.TcpDevice?.Dispose();
}
#endregion
}
}
}