PlcRobotManual.xaml.cs 17 KB

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