| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717 |
- using MathNet.Numerics;
- using NPOI.OpenXml4Net.OPC.Internal;
- using NPOI.SS.Formula.Functions;
- using OxyPlot;
- using Prism.Commands;
- using Prism.Events;
- using Prism.Ioc;
- using Prism.Mvvm;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
- using TeamAAS_VP.Controls;
- using TeamAAS_VP.Core;
- using TeamAAS_VP.Core.PLCs;
- using TeamAAS_VP.Core.ScrewDriver;
- using TeamAAS_VP.Data;
- using TeamAAS_VP.Enums;
- using TeamAAS_VP.Events;
- using TeamAAS_VP.Interfaces;
- using TeamAAS_VP.Models;
- using TeamAAS_VP.Models.PLC;
- using TeamAAS_VP.Models.Torque;
- using TeamAAS_VP.Resources.Languages;
- using TeamAAS_VP.Services;
- using FileHelper = TeamAAS_VP.Core.FileHelper;
- namespace TeamAAS_VP.ViewModels.Home
- {
- public class TorqueCheckViewModel : BindableBase, IDialogAware
- {
- // 点检扭矩参数存放日志地址
- public static readonly string CheckTorqueConfigurationPath = "..//Log//ScrewFeedersConfiguration.cfg";
- IEventAggregator _eventAggregator;
- IConfigService _configService;
- IContainerProvider _container;
- IRobotService _robotService;
- IPlcService _plcService;
- TorqueService _torqueService;
- float Value { get; set; } = 0;
- #region 属性
- private Management _management;
- public Management management
- {
- get { return _management; }
- set { SetProperty(ref _management, value); }
- }
- private bool CanExecute
- {
- get
- {
- // 命令可执行仅当机器人可执行且已选择点位
- if (Robot != null && Robot.IsConnected && Robot.CanExecute)
- {
- return true;
- }
- return false;
- }
- }
- private IRobot _Robot;
- public IRobot Robot
- {
- get { return _Robot; }
- set { SetProperty(ref _Robot, value); }
- }
- private double _Distance = 1;
- public double Distance
- {
- get { return _Distance; }
- set
- {
- SetProperty(ref _Distance, value);
- try
- {
- if (Robot == null)
- {
- return;
- }
- _configService.UpdateRobotStepDistance(Robot.Id, value);
- }
- catch (Exception)
- {
- }
- }
- }
- private float _Z_Distance;
- public float Z_Distance
- {
- get { return _Z_Distance; }
- set { SetProperty(ref _Z_Distance, value); }
- }
- private float _torqueValue;
- public float TorqueValue
- {
- get { return _torqueValue; }
- set { SetProperty(ref _torqueValue, value); }
- }
- private string _TValue;
- public string TValue
- {
- get { return _TValue; }
- set
- {
- SetProperty(ref _TValue, value);
- }
- }
- private string _PValue;
- public string PValue
- {
- get { return _PValue; }
- set
- {
- SetProperty(ref _PValue, value);
- }
- }
- private string _SubValue;
- public string SubValue
- {
- get { return _SubValue; }
- set { SetProperty(ref _SubValue, value); }
- }
- private string _Result;
- public string Result
- {
- get { return _Result; }
- set { SetProperty(ref _Result, value); }
- }
- private string _FontColor;
- public string FontColor
- {
- get { return _FontColor; }
- set { SetProperty(ref _FontColor, value); }
- }
- private int _SelectedIndex;
- public int SelectedIndex
- {
- get { return _SelectedIndex; }
- set { SetProperty(ref _SelectedIndex, value); }
- }
- private ObservableCollection<PlcPoint_Torque> _Points = new ObservableCollection<PlcPoint_Torque>();
- /// <summary>
- /// 点位集合
- /// </summary>
- public ObservableCollection<PlcPoint_Torque> Points
- {
- get { return _Points; }
- set { SetProperty(ref _Points, value); }
- }
- private TorqueProduceModel _AllProductParameter = new TorqueProduceModel();
- /// <summary>
- /// 所有产品的参数信息
- /// </summary>
- public TorqueProduceModel AllProductParameter
- {
- get { return _AllProductParameter; }
- set { SetProperty(ref _AllProductParameter, value); }
- }
- private PlcPoint_Torque _selectedPointInDataGrid;
- public PlcPoint_Torque SelectedPointInDataGrid
- {
- get { return _selectedPointInDataGrid; }
- set
- {
- SetProperty(ref _selectedPointInDataGrid, value);
- // 可以同步到 SelectPoint 属性
- SelectedPoint = value;
- AllProductParameter.TorquePoints = _Points;
- }
- }
- private PlcPoint_Torque _SelectedPoint;
- public PlcPoint_Torque SelectedPoint
- {
- get { return _SelectedPoint; }
- set { SetProperty(ref _SelectedPoint, value); }
- }
- #endregion
- #region 命令
- private DelegateCommand _CancelCommand;
- public DelegateCommand CancelCommand =>
- _CancelCommand ?? (_CancelCommand = new DelegateCommand(ExecuteCancelCommand));
- private DelegateCommand _LoadedCommand;
- public DelegateCommand LoadedCommand =>
- _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
- private DelegateCommand _ConfirmCommand;
- public DelegateCommand ConfirmCommand =>
- _ConfirmCommand ?? (_ConfirmCommand = new DelegateCommand(ExecuteConfirmCommand));
- private DelegateCommand<string> _MotorCommand;
- public DelegateCommand<string> MotorCommand =>
- _MotorCommand ?? (_MotorCommand = new DelegateCommand<string>(ExecuteMotorCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
- private DelegateCommand<object> _JogCommand;
- public DelegateCommand<object> JogCommand =>
- _JogCommand ?? (_JogCommand = new DelegateCommand<object>(ExecuteJogCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
- private DelegateCommand _TechPointCommand;
- public DelegateCommand TechPointCommand =>
- _TechPointCommand ?? (_TechPointCommand = new DelegateCommand(ExecuteTechPointCommand));
- private DelegateCommand _ModifyCommand;
- public DelegateCommand ModifyCommand =>
- _ModifyCommand ?? (_ModifyCommand = new DelegateCommand(ExecuteModifyCommand));
- private DelegateCommand _WorkCommand;
- public DelegateCommand WorkCommand =>
- _WorkCommand ?? (_WorkCommand = new DelegateCommand(ExecuteWorkCommand));
- private DelegateCommand _AddPointCommand;
- public DelegateCommand AddPointCommand =>
- _AddPointCommand ?? (_AddPointCommand = new DelegateCommand(ExecuteAddPointCommand));
- private DelegateCommand _SavePointsCommand;
- public DelegateCommand SavePointsCommand =>
- _SavePointsCommand ?? (_SavePointsCommand = new DelegateCommand(ExecuteSavePointsCommand));
- private DelegateCommand _DeletePointCommand;
- public DelegateCommand DeletePointCommand =>
- _DeletePointCommand ?? (_DeletePointCommand = new DelegateCommand(ExecuteDeletePointCommand, CanExecuteDeletePointCommand).ObservesProperty(() => SelectedPointInDataGrid));
- #endregion
- #region 方法
- public TorqueCheckViewModel(IEventAggregator ea, IConfigService configService, TorqueService torqueService,
- IContainerProvider container, IRobotService robotService, IPlcService plcService)
- {
- _eventAggregator = ea;
- _configService = configService;
- _container = container;
- _robotService = robotService;
- _plcService = plcService;
- _torqueService = torqueService;
- management = _container.Resolve<Management>();
- }
- bool CanExecuteDeletePointCommand()
- {
- return SelectedPointInDataGrid != null;
- }
- void ExecuteCancelCommand()
- {
- IDialogParameters parameters = new DialogParameters();
- RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel, parameters));
- }
- void ExecuteConfirmCommand()
- {
- IDialogParameters parameters = new DialogParameters();
- RequestClose?.Invoke(new DialogResult(ButtonResult.OK, parameters));
- }
- /// <summary>
- /// 电机
- /// </summary>
- /// <param name="obj"></param>
- private async void ExecuteMotorCommand(string obj)
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- await Robot.MotorAsync(Convert.ToBoolean(obj));
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("机器人点击操作时出错!", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <summary>
- /// 点动
- /// </summary>
- /// <param name="obj"></param>
- private async void ExecuteJogCommand(object obj)
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- var items = ((string)obj).Split(':');
- //如果移动距离大于10mm,则提示用户确认
- double distance = double.Parse(items[1]);
- if (Math.Abs(distance) >= 10)
- {
- if (MessageBox.Show($"确认要点动距离 {distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- }
- Robot.Timeout = 6000000;
- switch (items[0])
- {
- case "X+":
- await Robot.JogAsync("X", distance);
- break;
- case "X-":
- await Robot.JogAsync("X", -distance);
- break;
- case "Y+":
- await Robot.JogAsync("Y", distance);
- break;
- case "Y-":
- await Robot.JogAsync("Y", -distance);
- break;
- case "Z+":
- await Robot.JogAsync("Z", distance);
- break;
- case "Z-":
- await Robot.JogAsync("Z", -distance);
- break;
- case "U+":
- await Robot.JogAsync("U", distance);
- break;
- case "U-":
- await Robot.JogAsync("U", -distance);
- break;
- case "V+":
- await Robot.JogAsync("V", distance);
- break;
- case "V-":
- await Robot.JogAsync("V", -distance);
- break;
- case "W+":
- await Robot.JogAsync("W", distance);
- break;
- case "W-":
- await Robot.JogAsync("W", -distance);
- break;
- default:
- break;
- }
- Robot.Timeout = 5000;
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("机器人点位-机器人点动时出错", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <summary>
- /// 示教点位
- /// </summary>
- async void ExecuteTechPointCommand()
- {
- //弹窗用户是否确认要示教该点位
- if (MessageBox.Show($"确认要示教点位吗?", "示教点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- if (SelectedPoint == null)
- {
- MessageBox.Show("请选择示教点位");
- return;
- }
- var curpos = await Robot.GetRobotPosAsync();
- SelectedPoint.X_Position = curpos.X;
- SelectedPoint.Y_Position = curpos.Y;
- SelectedPoint.Z_Position_Start = curpos.Z;
- //SelectedPoint.Z_Position_Stop = curpos.Z - (float)this.Z_Distance;
- SelectedPoint.Z_Position_Stop = 0;
- AllProductParameter.TorqueValue = this.TorqueValue;
- AllProductParameter.TorquePoints = Points;
- FileHelper.WriteJsonFile(AllProductParameter, FilePath.TorqueCheckPath);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- }
- /// <summary>
- /// 修改
- /// </summary>
- async void ExecuteModifyCommand()
- {
- if (MessageBox.Show($"确认要修改扭力阈值吗?", "修改", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- var res = FileHelper.ReadJsonFile<TorqueProduceModel>(FilePath.TorqueCheckPath);
- res.TorqueValue = this.TorqueValue;
- FileHelper.WriteJsonFile(res, FilePath.TorqueCheckPath);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 0.4 });
- }
- async void ExecuteWorkCommand()
- {
- if (!File.Exists(FilePath.TorqueCheckPath))
- {
- MessageBox.Show("还未示教,请先示教");
- return;
- }
- if (Robot == null || !Robot.IsConnected)
- {
- return;
- }
- if (MessageBox.Show($"确认要进行点检吗?", "执行点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- try
- {
- if (await MoveRobot())
- {
-
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("执行点检时出错!", ex);
- }
- }
- /// <summary>
- /// 点检扭矩总体流程
- /// </summary>
- /// <returns></returns>
- public async Task<bool> MoveRobot()
- {
- var Plc_Point = FileHelper.ReadJsonFile<TorqueProduceModel>(FilePath.TorqueCheckPath);
- PlcAddressConfig addressConfig = _configService.GetPlcAddresses();
- OpcUaClientPLC plc = _plcService.GetPlcByNumber(addressConfig.PlcNo) as OpcUaClientPLC;
- try
- {
- if (Plc_Point == null || addressConfig == null || plc == null)
- {
- return false;
- }
- //切换手动模式
- if (await plc.WriteNodeAsync<bool>(addressConfig.OP_AutoManual.Address, false))
- {
- //回设置的零点
- if (await _torqueService.Safety_Position_Async(Robot, plc, addressConfig, Plc_Point))
- {
- //执行抛螺丝动作
- if (await _torqueService.Throw_Screw_Async(Robot, plc, addressConfig, Plc_Point))
- {
- //执行取螺丝动作
- if (await _torqueService.Pick_Screw_Async(Robot, plc, addressConfig, Plc_Point))
- {
- //执行锁螺丝点位操作
- if (await _torqueService.Screw_Torque_Async(Robot, plc, addressConfig, Plc_Point))
- {
- //获取电批结果并显示曲线
- if (await WatchAsync(plc, addressConfig))
- {
- await DisplayResult();
- return true;
- }
- MessageBox.Show("未获取到电批结果"); return false;
- }
- MessageBox.Show("执行锁螺丝点位操作失败"); return false;
- }
- MessageBox.Show("执行取螺丝动作失败"); return false;
- }
- MessageBox.Show("执行抛螺丝动作失败"); return false;
- }
- MessageBox.Show("执行回安全点失败"); return false;
- }
- MessageBox.Show("点检失败");return false;
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("执行点检移动时出错!", ex);
- return false;
- }
- finally
- {
- await _torqueService.Safety_Position_Async(Robot, plc, addressConfig, Plc_Point);
- }
- }
- public async Task DisplayResult()
- {
- try
- {
- Thread.Sleep(100);
- TValue = management.SignalValue;
- double tvalue = double.Parse(TValue.Split(' ')[0]);
- double pvalue = Value;
- PValue = $"{Math.Round(pvalue, 3)} kgf.cm";
- SubValue = $"{Math.Round(Math.Abs(tvalue - pvalue), 3)} kgf.cm";
- double sub = Math.Abs(tvalue - pvalue);
- var res = FileHelper.ReadJsonFile<TorqueTest>(FilePath.TorqueCheckPath);
- if (res != null)
- {
- if (sub <= res.TorqueValue)
- {
- FontColor = "Green";
- Result = "OK";
- }
- else
- {
- FontColor = "Red";
- Result = "NG";
- }
- }
- else
- {
- if (sub <= TorqueValue)
- {
- FontColor = "Green";
- Result = "OK";
- }
- else
- {
- FontColor = "Red";
- Result = "NG";
- }
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("执行点检显示时出错!", ex);
- }
- }
- public void Save_Torque_Parameter()
- {
- }
- public async Task<bool> WatchAsync(OpcUaClientPLC plc, PlcAddressConfig addressConfig)
- {
- Thread.Sleep(100);
- object oking = null;
- object nging = null;
- int count = 0;
- while (count++ < 10)
- {
- oking = plc.ReadNode(addressConfig.Screw_OK.Address);
- nging = plc.ReadNode(addressConfig.Screw_NG.Address);
- if (oking != null && nging != null)
- {
- bool okin = (bool)oking; bool ngin = (bool)nging;
- //电批OKNG
- if (okin || ngin)
- {
- //获取电批结果
- bool isSucced = management.ScrewDriver.GetData();
- LockResult lockResult = new LockResult();
- lockResult.LockTurns = (float)management.ScrewDriver.Truns;
- lockResult.LockTorque = (float)management.ScrewDriver.TorqueValue;
- lockResult.WaveDatas = management.ScrewDriver.WaveDatas;
- App.Current.Dispatcher.Invoke(() =>
- {
- _eventAggregator.GetEvent<LockFinishNotification>().Publish(lockResult);
- });
- //await _systemDatabaseService.RecordLockResultAsync(lockResult);
- await plc.WriteNodeAsync(addressConfig.In_WorkDone.Address, (Int16)0);
- Value = lockResult.LockTorque;
- await plc.WriteNodeAsync(addressConfig.Check_ScrewTorque.Address, false);
- return true;
- }
- }
- }
- return false;
- }
- #endregion
- public string Title { get; set; } = "扭力点检";
- public event Action<IDialogResult> RequestClose;
- public bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
- }
- public void OnDialogOpened(IDialogParameters parameters)
- {
- try
- {
- var res = FileHelper.ReadJsonFile<TorqueTest>(FilePath.TorqueCheckPath);
- if (res != null)
- {
- res.TorqueValue = TorqueValue;
- }
- else
- {
- MessageBox.Show("还未示教,请先示教");
- }
- }
- catch (Exception)
- {
- MessageBox.Show("还未示教,请先示教");
- }
- if (Robot == null)
- {
- Robot = _robotService.GetRobotByNumber(1);
- }
- if (Robot != null && Robot.IsConnected)
- {
- this.Distance = _configService.GetRobotStepDistance(Robot.Id);
- Robot.EnterDebugMode = true;
- }
- else if (Robot != null)
- {
- Robot.EnterDebugMode = true;
- }
- }
- private void ExecuteLoadedCommand()
- {
- var res = FileHelper.ReadJsonFile<TorqueProduceModel>(FilePath.TorqueCheckPath);
- this.Points = res.TorquePoints;
- this.TorqueValue = res.TorqueValue;
- }
- #region 添加点位与删除点位
- /// <summary>
- /// 增加点位
- /// </summary>
- private void ExecuteAddPointCommand()
- {
- //添加点位
- Points.Add(new PlcPoint_Torque()
- {
- Number = Points.Count + 1,
- });
- }
- /// <summary>
- /// 移除点位
- /// </summary>
- private void ExecuteDeletePointCommand()
- {
- if (SelectedPoint == null)
- {
- return;
- }
- // 确认删除
- if (MessageBox.Show(Lang.确认要删除选中的点位吗, Lang.删除点位, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- int removedIndex = Points.IndexOf(SelectedPoint);
- Points.Remove(SelectedPoint);
- //重新编号
- for (int i = 0; i < Points.Count; i++)
- {
- Points[i].Number = i + 1;
- }
- // adjust selection
- if (Points.Count > 0)
- {
- int newIndex = Math.Min(removedIndex, Points.Count - 1);
- SelectedPointInDataGrid = Points[newIndex];
- }
- else
- {
- SelectedPointInDataGrid = null;
- }
- }
- #endregion
- private void ExecuteSavePointsCommand()
- {
- AllProductParameter.TorqueValue = this.TorqueValue;
- AllProductParameter.TorquePoints = Points;
- FileHelper.WriteJsonFile(AllProductParameter, FilePath.TorqueCheckPath);
- MessageBox.Show("参数保存成功");
- }
- }
- }
|