| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581 |
- using Prism.Commands;
- using Prism.Ioc;
- using Prism.Mvvm;
- using Prism.Unity;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using TeamAAS_VP.Core.PLCs;
- using TeamAAS_VP.Core.Robots;
- using TeamAAS_VP.Interfaces;
- using TeamAAS_VP.Resources.Languages;
- namespace TeamAAS_VP.Controls
- {
- /// <summary>
- /// PlcRobotManual.xaml 的交互逻辑
- /// </summary>
- public partial class PlcRobotManual : UserControl, INotifyPropertyChanged
- {
- IConfigService _configService;
- public PlcRobotManual()
- {
- InitializeComponent();
- DataContext = this;
- }
- #region 属性
- private ObservableCollection<AxisExtended> _AdditionalAxisList;
- public ObservableCollection<AxisExtended> AdditionalAxisList
- {
- get { return _AdditionalAxisList; }
- set { SetProperty(ref _AdditionalAxisList, value); }
- }
- private XYZU_Robot _Robot;
- public XYZU_Robot Robot
- {
- get { return _Robot; }
- set
- {
- SetProperty(ref _Robot, value);
- try
- {
- if (value != null)
- {
- if (_configService == null)
- _configService = ((PrismApplication)(App.Current)).Container.Resolve<IConfigService>();
- this.Distance = _configService.GetRobotStepDistance(value.Id);
- }
- }
- catch (Exception)
- {
- }
- }
- }
- private double _Distance = 1;
- public double Distance
- {
- get { return _Distance; }
- set
- {
- SetProperty(ref _Distance, value);
- try
- {
- if (Robot == null)
- {
- return;
- }
- if (_configService == null)
- _configService = ((PrismApplication)(App.Current)).Container.Resolve<IConfigService>();
- _configService.UpdateRobotStepDistance(Robot.Id, value);
- }
- catch (Exception)
- {
- }
- }
- }
- #endregion
- #region 命令
- private DelegateCommand<string> _MotorCommand;
- public DelegateCommand<string> MotorCommand =>
- _MotorCommand ?? (_MotorCommand = new DelegateCommand<string>(ExecuteMotorCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
- private DelegateCommand _ResetCommand;
- public DelegateCommand ResetCommand =>
- _ResetCommand ?? (_ResetCommand = new DelegateCommand(ExecuteResetCommand).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<object> _AdditionalAxisJogPlusCommand;
- public DelegateCommand<object> AdditionalAxisJogPlusCommand =>
- _AdditionalAxisJogPlusCommand ?? (_AdditionalAxisJogPlusCommand = new DelegateCommand<object>(ExecuteAdditionalAxisJogPlusCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
- //附加轴,轴点动-命令
- private DelegateCommand<object> _AdditionalAxisJogMinusCommand;
- public DelegateCommand<object> AdditionalAxisJogMinusCommand =>
- _AdditionalAxisJogMinusCommand ?? (_AdditionalAxisJogMinusCommand = new DelegateCommand<object>(ExecuteAdditionalAxisJogMinusCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
- private DelegateCommand<object> _AdditionalAxisMotorOnCommand;
- public DelegateCommand<object> AdditionalAxisMotorOnCommand =>
- _AdditionalAxisMotorOnCommand ?? (_AdditionalAxisMotorOnCommand = new DelegateCommand<object>(ExecuteAdditionalAxisMotorOnCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
- private DelegateCommand<object> _AdditionalAxisMotorOffCommand;
- public DelegateCommand<object> AdditionalAxisMotorOffCommand =>
- _AdditionalAxisMotorOffCommand ?? (_AdditionalAxisMotorOffCommand = new DelegateCommand<object>(ExecuteAdditionalAxisMotorOffCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
- private DelegateCommand<string> _CameraAxisModerOnCommand;
- public DelegateCommand<string> CameraAxisModerOnCommand =>
- _CameraAxisModerOnCommand ?? (_CameraAxisModerOnCommand = new DelegateCommand<string>(ExecuteCameraAxisModerOnCommand));
- private DelegateCommand<string> _CameraAxisModerOffCommand;
- public DelegateCommand<string> CameraAxisModerOffCommand =>
- _CameraAxisModerOffCommand ?? (_CameraAxisModerOffCommand = new DelegateCommand<string>(ExecuteCameraAxisModerOffCommand));
- private DelegateCommand<string> _CameraAxisJogCommand;
- public DelegateCommand<string> CameraAxisJogCommand =>
- _CameraAxisJogCommand ?? (_CameraAxisJogCommand = new DelegateCommand<string>(ExecuteCameraAxisJogCommand));
- #endregion
- #region 事件
- #endregion
- #region 方法
- private void ShowMsg(string msg)
- {
- if (snackbar.MessageQueue == null)
- {
- snackbar.MessageQueue = new MaterialDesignThemes.Wpf.SnackbarMessageQueue();
- }
- snackbar.MessageQueue.Clear();
- snackbar.MessageQueue?.Enqueue(
- msg,
- null,
- null,
- null,
- false,
- true,
- TimeSpan.FromSeconds(0.4));
- }
- /// <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));
- ShowMsg($"{Lang.完成}!");
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("机器人点击操作时出错!", ex);
- ShowMsg(ex.Message);
- }
- }
- /// <summary>
- /// 重置
- /// </summary>
- private async void ExecuteResetCommand()
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- await Robot.ResetAsync();
- ShowMsg($"{Lang.完成}!");
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("重置机器人时出错!", ex);
- ShowMsg(ex.Message);
- }
- }
- /// <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;
- ShowMsg($"{Lang.完成}!");
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("机器点动时出错!", ex);
- ShowMsg(ex.Message);
- }
- }
- private bool CanExecute
- {
- get
- {
- if (Robot != null)
- {
- return Robot.CanExecute;
- }
- else
- {
- return false;
- }
- }
- }
- /// <summary>
- /// 附加轴+点动
- /// </summary>
- /// <param name="obj"></param>
- async void ExecuteAdditionalAxisJogPlusCommand(object obj)
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- if (obj is AxisExtended axis)
- {
- //如果移动距离大于10mm,则提示用户确认
- if (Math.Abs(Distance) >= 10)
- {
- if (MessageBox.Show($"确认要点动距离 {Distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- }
- await Robot.AdditionalAxisJogAsync(axis.Name, Distance);
- ShowMsg($"{Lang.完成}!");
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("附加轴点动+时出错!", ex);
- ShowMsg(ex.Message);
- }
- }
- /// <summary>
- /// 附加轴-点动
- /// </summary>
- /// <param name="obj"></param>
- async void ExecuteAdditionalAxisJogMinusCommand(object obj)
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- if (obj is AxisExtended axis)
- {
- //如果移动距离大于10mm,则提示用户确认
- if (Math.Abs(Distance) >= 10)
- {
- if (MessageBox.Show($"确认要点动距离 {Distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- }
- await Robot.AdditionalAxisJogNegAsync(axis.Name, Distance);
- ShowMsg($"{Lang.完成}!");
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("附加轴点动-时出错!", ex);
- ShowMsg(ex.Message);
- }
- }
- /// <summary>
- /// 附加轴上使能
- /// </summary>
- /// <param name="obj"></param>
- async void ExecuteAdditionalAxisMotorOffCommand(object obj)
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- if (obj is AxisExtended axis)
- {
- await Robot.AdditionalAxisMotorAsync(axis.Name, true);
- ShowMsg($"{Lang.完成}!");
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("附加轴上使能时出错!", ex);
- ShowMsg(ex.Message);
- }
- }
- /// <summary>
- /// 附加轴下使能
- /// </summary>
- /// <param name="obj"></param>
- async void ExecuteAdditionalAxisMotorOnCommand(object obj)
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- if (obj is AxisExtended axis)
- {
- await Robot.AdditionalAxisMotorAsync(axis.Name, true);
- ShowMsg($"{Lang.完成}!");
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("附加轴下使能时出错!", ex);
- ShowMsg(ex.Message);
- }
- }
- #region 相机控制
- /// <summary>
- /// 轴电机下使能操作
- /// </summary>
- /// <param name="parameter"></param>
- async void ExecuteCameraAxisModerOffCommand(string parameter)
- {
- try
- {
- int cameraIndex = int.Parse(parameter);
- var isSuccess = await Robot.CameraMotorAsync(cameraIndex, false);
- if (isSuccess)
- {
- ShowMsg($"{Lang.完成}!");
- }
- else
- {
- ShowMsg("相机轴下使能失败!");
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("相机轴下使能时出错!", ex);
- ShowMsg(ex.Message);
- }
- }
- /// <summary>
- /// 轴电机上使能操作
- /// </summary>
- /// <param name="parameter"></param>
- async void ExecuteCameraAxisModerOnCommand(string parameter)
- {
- try
- {
- int cameraIndex = int.Parse(parameter);
- var isSuccess = await Robot.CameraMotorAsync(cameraIndex, true);
- if (isSuccess)
- {
- ShowMsg($"{Lang.完成}!");
- }
- else
- {
- ShowMsg("相机轴上使能失败!");
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("相机轴上使能时出错!", ex);
- ShowMsg(ex.Message);
- }
- }
- /// <summary>
- /// 相机轴点动命令
- /// </summary>
- /// <param name="parameter"></param>
- async void ExecuteCameraAxisJogCommand(string parameter)
- {
- try
- {
- int cameraIndex = int.Parse(parameter.Split(',')[0]);
- double jogDistance = Distance;
- string axisName = "";
- switch (parameter.Split(',')[1])
- {
- case "X+":
- jogDistance = Distance;
- axisName = "X";
- break;
- case "X-":
- jogDistance = -Distance;
- axisName = "X";
- break;
- case "Y+":
- jogDistance = Distance;
- axisName = "Y";
- break;
- case "Y-":
- jogDistance = -Distance;
- axisName = "Y";
- break;
- default:
- break;
- }
- bool isSuccess = await Robot.CameraJogAsync(cameraIndex, axisName, jogDistance);
- if (isSuccess)
- {
- ShowMsg($"{Lang.完成}!");
- }
- else
- {
- ShowMsg("相机轴点动失败!");
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("相机轴点动时出错!", ex);
- ShowMsg(ex.Message);
- }
- }
- #endregion
- #endregion
- #region 属性通知
- /// <summary>
- /// Occurs when a property value changes.
- /// </summary>
- public event PropertyChangedEventHandler PropertyChanged;
- /// <summary>
- /// Checks if a property already matches a desired value. Sets the property and
- /// notifies listeners only when necessary.
- /// </summary>
- /// <typeparam name="T">Type of the property.</typeparam>
- /// <param name="storage">Reference to a property with both getter and setter.</param>
- /// <param name="value">Desired value for the property.</param>
- /// <param name="propertyName">Name of the property used to notify listeners. This
- /// value is optional and can be provided automatically when invoked from compilers that
- /// support CallerMemberName.</param>
- /// <returns>True if the value was changed, false if the existing value matched the
- /// desired value.</returns>
- protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
- {
- if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
- storage = value;
- RaisePropertyChanged(propertyName);
- return true;
- }
- /// <summary>
- /// Checks if a property already matches a desired value. Sets the property and
- /// notifies listeners only when necessary.
- /// </summary>
- /// <typeparam name="T">Type of the property.</typeparam>
- /// <param name="storage">Reference to a property with both getter and setter.</param>
- /// <param name="value">Desired value for the property.</param>
- /// <param name="propertyName">Name of the property used to notify listeners. This
- /// value is optional and can be provided automatically when invoked from compilers that
- /// support CallerMemberName.</param>
- /// <param name="onChanged">Action that is called after the property value has been changed.</param>
- /// <returns>True if the value was changed, false if the existing value matched the
- /// desired value.</returns>
- protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
- {
- if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
- storage = value;
- onChanged?.Invoke();
- RaisePropertyChanged(propertyName);
- return true;
- }
- /// <summary>
- /// Raises this object's PropertyChanged event.
- /// </summary>
- /// <param name="propertyName">Name of the property used to notify listeners. This
- /// value is optional and can be provided automatically when invoked from compilers
- /// that support <see cref="CallerMemberNameAttribute"/>.</param>
- protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
- {
- OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
- }
- /// <summary>
- /// Raises this object's PropertyChanged event.
- /// </summary>
- /// <param name="args">The PropertyChangedEventArgs</param>
- protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
- {
- PropertyChanged?.Invoke(this, args);
- }
- #endregion
- }
- }
|