PlcRobotManual.xaml.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. //private DelegateCommand<string> _CameraAxisModerOnCommand;
  115. //public DelegateCommand<string> CameraAxisModerOnCommand =>
  116. // _CameraAxisModerOnCommand ?? (_CameraAxisModerOnCommand = new DelegateCommand<string>(ExecuteCameraAxisModerOnCommand));
  117. //private DelegateCommand<string> _CameraAxisModerOffCommand;
  118. //public DelegateCommand<string> CameraAxisModerOffCommand =>
  119. // _CameraAxisModerOffCommand ?? (_CameraAxisModerOffCommand = new DelegateCommand<string>(ExecuteCameraAxisModerOffCommand));
  120. //private DelegateCommand<string> _CameraAxisJogCommand;
  121. //public DelegateCommand<string> CameraAxisJogCommand =>
  122. // _CameraAxisJogCommand ?? (_CameraAxisJogCommand = new DelegateCommand<string>(ExecuteCameraAxisJogCommand));
  123. #endregion
  124. #region 事件
  125. #endregion
  126. #region 方法
  127. private void ShowMsg(string msg)
  128. {
  129. if (snackbar.MessageQueue == null)
  130. {
  131. snackbar.MessageQueue = new MaterialDesignThemes.Wpf.SnackbarMessageQueue();
  132. }
  133. snackbar.MessageQueue.Clear();
  134. snackbar.MessageQueue?.Enqueue(
  135. msg,
  136. null,
  137. null,
  138. null,
  139. false,
  140. true,
  141. TimeSpan.FromSeconds(0.4));
  142. }
  143. /// <summary>
  144. /// 电机
  145. /// </summary>
  146. /// <param name="obj"></param>
  147. private async void ExecuteMotorCommand(string obj)
  148. {
  149. if (Robot == null || !Robot.IsConnected) return;
  150. try
  151. {
  152. await Robot.MotorAsync(Convert.ToBoolean(obj));
  153. ShowMsg($"{Lang.完成}!");
  154. }
  155. catch (Exception ex)
  156. {
  157. LogHelper.WriteLogError("机器人点击操作时出错!", ex);
  158. ShowMsg(ex.Message);
  159. }
  160. }
  161. /// <summary>
  162. /// 重置
  163. /// </summary>
  164. private async void ExecuteResetCommand()
  165. {
  166. if (Robot == null || !Robot.IsConnected) return;
  167. try
  168. {
  169. await Robot.ResetAsync();
  170. ShowMsg($"{Lang.完成}!");
  171. }
  172. catch (Exception ex)
  173. {
  174. LogHelper.WriteLogError("重置机器人时出错!", ex);
  175. ShowMsg(ex.Message);
  176. }
  177. }
  178. /// <summary>
  179. /// 点动
  180. /// </summary>
  181. /// <param name="obj"></param>
  182. private async void ExecuteJogCommand(object obj)
  183. {
  184. if (Robot == null || !Robot.IsConnected) return;
  185. try
  186. {
  187. var items = ((string)obj).Split(':');
  188. //如果移动距离大于10mm,则提示用户确认
  189. double distance = double.Parse(items[1]);
  190. if (Math.Abs(distance) >= 10)
  191. {
  192. if (MessageBox.Show($"确认要点动距离 {distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  193. {
  194. return;
  195. }
  196. }
  197. Robot.Timeout = 6000000;
  198. switch (items[0])
  199. {
  200. case "X+":
  201. await Robot.JogAsync("X", distance);
  202. break;
  203. case "X-":
  204. await Robot.JogAsync("X", -distance);
  205. break;
  206. case "Y+":
  207. await Robot.JogAsync("Y", distance);
  208. break;
  209. case "Y-":
  210. await Robot.JogAsync("Y", -distance);
  211. break;
  212. case "Z+":
  213. await Robot.JogAsync("Z", distance);
  214. break;
  215. case "Z-":
  216. await Robot.JogAsync("Z", -distance);
  217. break;
  218. case "U+":
  219. await Robot.JogAsync("U", distance);
  220. break;
  221. case "U-":
  222. await Robot.JogAsync("U", -distance);
  223. break;
  224. case "V+":
  225. await Robot.JogAsync("V", distance);
  226. break;
  227. case "V-":
  228. await Robot.JogAsync("V", -distance);
  229. break;
  230. case "W+":
  231. await Robot.JogAsync("W", distance);
  232. break;
  233. case "W-":
  234. await Robot.JogAsync("W", -distance);
  235. break;
  236. default:
  237. break;
  238. }
  239. Robot.Timeout = 5000;
  240. ShowMsg($"{Lang.完成}!");
  241. }
  242. catch (Exception ex)
  243. {
  244. LogHelper.WriteLogError("机器点动时出错!", ex);
  245. ShowMsg(ex.Message);
  246. }
  247. }
  248. private bool CanExecute
  249. {
  250. get
  251. {
  252. if (Robot != null)
  253. {
  254. return Robot.CanExecute;
  255. }
  256. else
  257. {
  258. return false;
  259. }
  260. }
  261. }
  262. /// <summary>
  263. /// 附加轴+点动
  264. /// </summary>
  265. /// <param name="obj"></param>
  266. async void ExecuteAdditionalAxisJogPlusCommand(object obj)
  267. {
  268. if (Robot == null || !Robot.IsConnected) return;
  269. try
  270. {
  271. if (obj is AxisExtended axis)
  272. {
  273. //如果移动距离大于10mm,则提示用户确认
  274. if (Math.Abs(Distance) >= 10)
  275. {
  276. if (MessageBox.Show($"确认要点动距离 {Distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  277. {
  278. return;
  279. }
  280. }
  281. await Robot.AdditionalAxisJogAsync(axis.Name, Distance);
  282. ShowMsg($"{Lang.完成}!");
  283. }
  284. }
  285. catch (Exception ex)
  286. {
  287. LogHelper.WriteLogError("附加轴点动+时出错!", ex);
  288. ShowMsg(ex.Message);
  289. }
  290. }
  291. /// <summary>
  292. /// 附加轴-点动
  293. /// </summary>
  294. /// <param name="obj"></param>
  295. async void ExecuteAdditionalAxisJogMinusCommand(object obj)
  296. {
  297. if (Robot == null || !Robot.IsConnected) return;
  298. try
  299. {
  300. if (obj is AxisExtended axis)
  301. {
  302. //如果移动距离大于10mm,则提示用户确认
  303. if (Math.Abs(Distance) >= 10)
  304. {
  305. if (MessageBox.Show($"确认要点动距离 {Distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  306. {
  307. return;
  308. }
  309. }
  310. await Robot.AdditionalAxisJogNegAsync(axis.Name, Distance);
  311. ShowMsg($"{Lang.完成}!");
  312. }
  313. }
  314. catch (Exception ex)
  315. {
  316. LogHelper.WriteLogError("附加轴点动-时出错!", ex);
  317. ShowMsg(ex.Message);
  318. }
  319. }
  320. /// <summary>
  321. /// 附加轴上使能
  322. /// </summary>
  323. /// <param name="obj"></param>
  324. async void ExecuteAdditionalAxisMotorOffCommand(object obj)
  325. {
  326. if (Robot == null || !Robot.IsConnected) return;
  327. try
  328. {
  329. if (obj is AxisExtended axis)
  330. {
  331. await Robot.AdditionalAxisMotorAsync(axis.Name, true);
  332. ShowMsg($"{Lang.完成}!");
  333. }
  334. }
  335. catch (Exception ex)
  336. {
  337. LogHelper.WriteLogError("附加轴上使能时出错!", ex);
  338. ShowMsg(ex.Message);
  339. }
  340. }
  341. /// <summary>
  342. /// 附加轴下使能
  343. /// </summary>
  344. /// <param name="obj"></param>
  345. async void ExecuteAdditionalAxisMotorOnCommand(object obj)
  346. {
  347. if (Robot == null || !Robot.IsConnected) return;
  348. try
  349. {
  350. if (obj is AxisExtended axis)
  351. {
  352. await Robot.AdditionalAxisMotorAsync(axis.Name, true);
  353. ShowMsg($"{Lang.完成}!");
  354. }
  355. }
  356. catch (Exception ex)
  357. {
  358. LogHelper.WriteLogError("附加轴下使能时出错!", ex);
  359. ShowMsg(ex.Message);
  360. }
  361. }
  362. #region 相机控制
  363. ///// <summary>
  364. ///// 轴电机下使能操作
  365. ///// </summary>
  366. ///// <param name="parameter"></param>
  367. //async void ExecuteCameraAxisModerOffCommand(string parameter)
  368. //{
  369. // try
  370. // {
  371. // int cameraIndex = int.Parse(parameter);
  372. // var isSuccess = await Robot.CameraMotorAsync(cameraIndex, false);
  373. // if (isSuccess)
  374. // {
  375. // ShowMsg($"{Lang.完成}!");
  376. // }
  377. // else
  378. // {
  379. // ShowMsg("相机轴下使能失败!");
  380. // }
  381. // }
  382. // catch (Exception ex)
  383. // {
  384. // LogHelper.WriteLogError("相机轴下使能时出错!", ex);
  385. // ShowMsg(ex.Message);
  386. // }
  387. //}
  388. ///// <summary>
  389. ///// 轴电机上使能操作
  390. ///// </summary>
  391. ///// <param name="parameter"></param>
  392. //async void ExecuteCameraAxisModerOnCommand(string parameter)
  393. //{
  394. // try
  395. // {
  396. // int cameraIndex = int.Parse(parameter);
  397. // var isSuccess = await Robot.CameraMotorAsync(cameraIndex, true);
  398. // if (isSuccess)
  399. // {
  400. // ShowMsg($"{Lang.完成}!");
  401. // }
  402. // else
  403. // {
  404. // ShowMsg("相机轴上使能失败!");
  405. // }
  406. // }
  407. // catch (Exception ex)
  408. // {
  409. // LogHelper.WriteLogError("相机轴上使能时出错!", ex);
  410. // ShowMsg(ex.Message);
  411. // }
  412. //}
  413. ///// <summary>
  414. ///// 相机轴点动命令
  415. ///// </summary>
  416. ///// <param name="parameter"></param>
  417. //async void ExecuteCameraAxisJogCommand(string parameter)
  418. //{
  419. // try
  420. // {
  421. // int cameraIndex = int.Parse(parameter.Split(',')[0]);
  422. // double jogDistance = Distance;
  423. // string axisName = "";
  424. // switch (parameter.Split(',')[1])
  425. // {
  426. // case "X+":
  427. // jogDistance = Distance;
  428. // axisName = "X";
  429. // break;
  430. // case "X-":
  431. // jogDistance = -Distance;
  432. // axisName = "X";
  433. // break;
  434. // case "Y+":
  435. // jogDistance = Distance;
  436. // axisName = "Y";
  437. // break;
  438. // case "Y-":
  439. // jogDistance = -Distance;
  440. // axisName = "Y";
  441. // break;
  442. // default:
  443. // break;
  444. // }
  445. // bool isSuccess = await Robot.CameraJogAsync(cameraIndex, axisName, jogDistance);
  446. // if (isSuccess)
  447. // {
  448. // ShowMsg($"{Lang.完成}!");
  449. // }
  450. // else
  451. // {
  452. // ShowMsg("相机轴点动失败!");
  453. // }
  454. // }
  455. // catch (Exception ex)
  456. // {
  457. // LogHelper.WriteLogError("相机轴点动时出错!", ex);
  458. // ShowMsg(ex.Message);
  459. // }
  460. //}
  461. #endregion
  462. #endregion
  463. #region 属性通知
  464. /// <summary>
  465. /// Occurs when a property value changes.
  466. /// </summary>
  467. public event PropertyChangedEventHandler PropertyChanged;
  468. /// <summary>
  469. /// Checks if a property already matches a desired value. Sets the property and
  470. /// notifies listeners only when necessary.
  471. /// </summary>
  472. /// <typeparam name="T">Type of the property.</typeparam>
  473. /// <param name="storage">Reference to a property with both getter and setter.</param>
  474. /// <param name="value">Desired value for the property.</param>
  475. /// <param name="propertyName">Name of the property used to notify listeners. This
  476. /// value is optional and can be provided automatically when invoked from compilers that
  477. /// support CallerMemberName.</param>
  478. /// <returns>True if the value was changed, false if the existing value matched the
  479. /// desired value.</returns>
  480. protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
  481. {
  482. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  483. storage = value;
  484. RaisePropertyChanged(propertyName);
  485. return true;
  486. }
  487. /// <summary>
  488. /// Checks if a property already matches a desired value. Sets the property and
  489. /// notifies listeners only when necessary.
  490. /// </summary>
  491. /// <typeparam name="T">Type of the property.</typeparam>
  492. /// <param name="storage">Reference to a property with both getter and setter.</param>
  493. /// <param name="value">Desired value for the property.</param>
  494. /// <param name="propertyName">Name of the property used to notify listeners. This
  495. /// value is optional and can be provided automatically when invoked from compilers that
  496. /// support CallerMemberName.</param>
  497. /// <param name="onChanged">Action that is called after the property value has been changed.</param>
  498. /// <returns>True if the value was changed, false if the existing value matched the
  499. /// desired value.</returns>
  500. protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
  501. {
  502. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  503. storage = value;
  504. onChanged?.Invoke();
  505. RaisePropertyChanged(propertyName);
  506. return true;
  507. }
  508. /// <summary>
  509. /// Raises this object's PropertyChanged event.
  510. /// </summary>
  511. /// <param name="propertyName">Name of the property used to notify listeners. This
  512. /// value is optional and can be provided automatically when invoked from compilers
  513. /// that support <see cref="CallerMemberNameAttribute"/>.</param>
  514. protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
  515. {
  516. OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
  517. }
  518. /// <summary>
  519. /// Raises this object's PropertyChanged event.
  520. /// </summary>
  521. /// <param name="args">The PropertyChangedEventArgs</param>
  522. protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
  523. {
  524. PropertyChanged?.Invoke(this, args);
  525. }
  526. #endregion
  527. }
  528. }