PlcRobotManual.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. using Prism.Commands;
  2. using Prism.Ioc;
  3. using Prism.Unity;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using System.ComponentModel;
  8. using System.Linq;
  9. using System.Runtime.CompilerServices;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Data;
  15. using System.Windows.Documents;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. using System.Windows.Navigation;
  20. using System.Windows.Shapes;
  21. using TeamAAS_VP.Core.PLCs;
  22. using TeamAAS_VP.Core.Robots;
  23. using TeamAAS_VP.Interfaces;
  24. using TeamAAS_VP.Resources.Languages;
  25. namespace TeamAAS_VP.Controls
  26. {
  27. /// <summary>
  28. /// PlcRobotManual.xaml 的交互逻辑
  29. /// </summary>
  30. public partial class PlcRobotManual : UserControl, INotifyPropertyChanged
  31. {
  32. IConfigService _configService;
  33. public PlcRobotManual()
  34. {
  35. InitializeComponent();
  36. DataContext = this;
  37. }
  38. #region 属性
  39. private ObservableCollection<Axis> _AdditionalAxisList;
  40. public ObservableCollection<Axis> AdditionalAxisList
  41. {
  42. get { return _AdditionalAxisList; }
  43. set { SetProperty(ref _AdditionalAxisList, value); }
  44. }
  45. private XYZU_Robot _Robot;
  46. public XYZU_Robot Robot
  47. {
  48. get { return _Robot; }
  49. set
  50. {
  51. SetProperty(ref _Robot, value);
  52. try
  53. {
  54. if (value != null)
  55. {
  56. if (_configService == null)
  57. _configService = ((PrismApplication)(App.Current)).Container.Resolve<IConfigService>();
  58. this.Distance = _configService.GetRobotStepDistance(value.Id);
  59. }
  60. }
  61. catch (Exception)
  62. {
  63. }
  64. }
  65. }
  66. private double _Distance = 1;
  67. public double Distance
  68. {
  69. get { return _Distance; }
  70. set
  71. {
  72. SetProperty(ref _Distance, value);
  73. try
  74. {
  75. if (Robot == null)
  76. {
  77. return;
  78. }
  79. if (_configService == null)
  80. _configService = ((PrismApplication)(App.Current)).Container.Resolve<IConfigService>();
  81. _configService.UpdateRobotStepDistance(Robot.Id, value);
  82. }
  83. catch (Exception)
  84. {
  85. }
  86. }
  87. }
  88. #endregion
  89. #region 命令
  90. private DelegateCommand<string> _MotorCommand;
  91. public DelegateCommand<string> MotorCommand =>
  92. _MotorCommand ?? (_MotorCommand = new DelegateCommand<string>(ExecuteMotorCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  93. private DelegateCommand _ResetCommand;
  94. public DelegateCommand ResetCommand =>
  95. _ResetCommand ?? (_ResetCommand = new DelegateCommand(ExecuteResetCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  96. private DelegateCommand<object> _JogCommand;
  97. public DelegateCommand<object> JogCommand =>
  98. _JogCommand ?? (_JogCommand = new DelegateCommand<object>(ExecuteJogCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  99. #endregion
  100. #region 事件
  101. #endregion
  102. #region 方法
  103. private void ShowMsg(string msg)
  104. {
  105. if (snackbar.MessageQueue == null)
  106. {
  107. snackbar.MessageQueue = new MaterialDesignThemes.Wpf.SnackbarMessageQueue();
  108. }
  109. snackbar.MessageQueue.Clear();
  110. snackbar.MessageQueue?.Enqueue(
  111. msg,
  112. null,
  113. null,
  114. null,
  115. false,
  116. true,
  117. TimeSpan.FromSeconds(0.4));
  118. }
  119. /// <summary>
  120. /// 电机
  121. /// </summary>
  122. /// <param name="obj"></param>
  123. private async void ExecuteMotorCommand(string obj)
  124. {
  125. if (Robot == null || !Robot.IsConnected) return;
  126. try
  127. {
  128. await Robot.MotorAsync(Convert.ToBoolean(obj));
  129. ShowMsg($"{Lang.完成}!");
  130. }
  131. catch (Exception ex)
  132. {
  133. LogHelper.WriteLogError("机器人点击操作时出错!", ex);
  134. ShowMsg(ex.Message);
  135. }
  136. }
  137. /// <summary>
  138. /// 重置
  139. /// </summary>
  140. private async void ExecuteResetCommand()
  141. {
  142. if (Robot == null || !Robot.IsConnected) return;
  143. try
  144. {
  145. await Robot.ResetAsync();
  146. ShowMsg($"{Lang.完成}!");
  147. }
  148. catch (Exception ex)
  149. {
  150. LogHelper.WriteLogError("重置机器人时出错!", ex);
  151. ShowMsg(ex.Message);
  152. }
  153. }
  154. /// <summary>
  155. /// 点动
  156. /// </summary>
  157. /// <param name="obj"></param>
  158. private async void ExecuteJogCommand(object obj)
  159. {
  160. if (Robot == null || !Robot.IsConnected) return;
  161. try
  162. {
  163. var items = ((string)obj).Split(':');
  164. //如果移动距离大于10mm,则提示用户确认
  165. double distance = double.Parse(items[1]);
  166. if (Math.Abs(distance) >= 10)
  167. {
  168. if (MessageBox.Show($"确认要点动距离 {distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  169. {
  170. return;
  171. }
  172. }
  173. Robot.Timeout = 6000000;
  174. switch (items[0])
  175. {
  176. case "X+":
  177. await Robot.JogAsync("X", distance);
  178. break;
  179. case "X-":
  180. await Robot.JogAsync("X", -distance);
  181. break;
  182. case "Y+":
  183. await Robot.JogAsync("Y", distance);
  184. break;
  185. case "Y-":
  186. await Robot.JogAsync("Y", -distance);
  187. break;
  188. case "Z+":
  189. await Robot.JogAsync("Z", distance);
  190. break;
  191. case "Z-":
  192. await Robot.JogAsync("Z", -distance);
  193. break;
  194. case "U+":
  195. await Robot.JogAsync("U", distance);
  196. break;
  197. case "U-":
  198. await Robot.JogAsync("U", -distance);
  199. break;
  200. case "V+":
  201. await Robot.JogAsync("V", distance);
  202. break;
  203. case "V-":
  204. await Robot.JogAsync("V", -distance);
  205. break;
  206. case "W+":
  207. await Robot.JogAsync("W", distance);
  208. break;
  209. case "W-":
  210. await Robot.JogAsync("W", -distance);
  211. break;
  212. default:
  213. break;
  214. }
  215. Robot.Timeout = 5000;
  216. ShowMsg($"{Lang.完成}!");
  217. }
  218. catch (Exception ex)
  219. {
  220. LogHelper.WriteLogError("机器点动时出错!", ex);
  221. ShowMsg(ex.Message);
  222. }
  223. }
  224. private bool CanExecute
  225. {
  226. get
  227. {
  228. if (Robot != null)
  229. {
  230. return Robot.CanExecute;
  231. }
  232. else
  233. {
  234. return false;
  235. }
  236. }
  237. }
  238. #endregion
  239. #region 属性通知
  240. /// <summary>
  241. /// Occurs when a property value changes.
  242. /// </summary>
  243. public event PropertyChangedEventHandler PropertyChanged;
  244. /// <summary>
  245. /// Checks if a property already matches a desired value. Sets the property and
  246. /// notifies listeners only when necessary.
  247. /// </summary>
  248. /// <typeparam name="T">Type of the property.</typeparam>
  249. /// <param name="storage">Reference to a property with both getter and setter.</param>
  250. /// <param name="value">Desired value for the property.</param>
  251. /// <param name="propertyName">Name of the property used to notify listeners. This
  252. /// value is optional and can be provided automatically when invoked from compilers that
  253. /// support CallerMemberName.</param>
  254. /// <returns>True if the value was changed, false if the existing value matched the
  255. /// desired value.</returns>
  256. protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
  257. {
  258. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  259. storage = value;
  260. RaisePropertyChanged(propertyName);
  261. return true;
  262. }
  263. /// <summary>
  264. /// Checks if a property already matches a desired value. Sets the property and
  265. /// notifies listeners only when necessary.
  266. /// </summary>
  267. /// <typeparam name="T">Type of the property.</typeparam>
  268. /// <param name="storage">Reference to a property with both getter and setter.</param>
  269. /// <param name="value">Desired value for the property.</param>
  270. /// <param name="propertyName">Name of the property used to notify listeners. This
  271. /// value is optional and can be provided automatically when invoked from compilers that
  272. /// support CallerMemberName.</param>
  273. /// <param name="onChanged">Action that is called after the property value has been changed.</param>
  274. /// <returns>True if the value was changed, false if the existing value matched the
  275. /// desired value.</returns>
  276. protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
  277. {
  278. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  279. storage = value;
  280. onChanged?.Invoke();
  281. RaisePropertyChanged(propertyName);
  282. return true;
  283. }
  284. /// <summary>
  285. /// Raises this object's PropertyChanged event.
  286. /// </summary>
  287. /// <param name="propertyName">Name of the property used to notify listeners. This
  288. /// value is optional and can be provided automatically when invoked from compilers
  289. /// that support <see cref="CallerMemberNameAttribute"/>.</param>
  290. protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
  291. {
  292. OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
  293. }
  294. /// <summary>
  295. /// Raises this object's PropertyChanged event.
  296. /// </summary>
  297. /// <param name="args">The PropertyChangedEventArgs</param>
  298. protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
  299. {
  300. PropertyChanged?.Invoke(this, args);
  301. }
  302. #endregion
  303. }
  304. }