RobotManual.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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;
  22. using TeamAAS_VP.Core;
  23. using TeamAAS_VP.Core.Robots;
  24. using TeamAAS_VP.Enums;
  25. using TeamAAS_VP.Events;
  26. using TeamAAS_VP.Interfaces;
  27. using TeamAAS_VP.Resources.Languages;
  28. using TeamAAS_VP.Services;
  29. using TeamAAS_VP.ViewModels;
  30. using TeamAAS_VP.Views;
  31. namespace TeamAAS_VP.Controls
  32. {
  33. /// <summary>
  34. /// RobotManual.xaml 的交互逻辑
  35. /// </summary>
  36. public partial class RobotManual : UserControl,INotifyPropertyChanged
  37. {
  38. IConfigService _configService;
  39. public RobotManual()
  40. {
  41. InitializeComponent();
  42. DataContext = this;
  43. this.Loaded += RobotManual_Loaded;
  44. }
  45. private void RobotManual_Loaded(object sender, RoutedEventArgs e)
  46. {
  47. }
  48. #region 属性
  49. private IRobot _Robot;
  50. public IRobot Robot
  51. {
  52. get { return _Robot; }
  53. set { SetProperty(ref _Robot,value);
  54. try
  55. {
  56. if (value!=null)
  57. {
  58. if (_configService==null)
  59. _configService = ((PrismApplication)(App.Current)).Container.Resolve<IConfigService>();
  60. this.Distance = _configService.GetRobotStepDistance(value.Id);
  61. }
  62. if (value is FanucRobot)
  63. {
  64. Tools = new ObservableCollection<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
  65. SelectTool = Robot.SelectedTool;
  66. }
  67. else
  68. {
  69. Tools = new ObservableCollection<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
  70. SelectTool = Robot.SelectedTool;
  71. }
  72. }
  73. catch (Exception)
  74. {
  75. }
  76. }
  77. }
  78. private double _Distance=1;
  79. public double Distance
  80. {
  81. get { return _Distance; }
  82. set { SetProperty(ref _Distance, value);
  83. try
  84. {
  85. if (Robot==null)
  86. {
  87. return;
  88. }
  89. if (_configService == null)
  90. _configService = ((PrismApplication)(App.Current)).Container.Resolve<IConfigService>();
  91. _configService.UpdateRobotStepDistance(Robot.Id, value);
  92. }
  93. catch (Exception)
  94. {
  95. }
  96. }
  97. }
  98. private ObservableCollection<int> _Tools = new ObservableCollection<int>();
  99. /// <summary>
  100. /// 机器人Tool集合
  101. /// </summary>
  102. public ObservableCollection<int> Tools
  103. {
  104. get { return _Tools; }
  105. set { SetProperty(ref _Tools, value); }
  106. }
  107. private int _SelectTool;
  108. /// <summary>
  109. /// 选中的Tool
  110. /// </summary>
  111. public int SelectTool
  112. {
  113. get { return _SelectTool; }
  114. set
  115. {
  116. SetProperty(ref _SelectTool, value);
  117. if (value >= 0)
  118. {
  119. if (Robot != null && Robot.IsConnected)
  120. {
  121. try
  122. {
  123. Robot.SelectToolAsync(value);
  124. }
  125. catch (Exception)
  126. {
  127. }
  128. }
  129. }
  130. }
  131. }
  132. #endregion
  133. #region 命令
  134. private DelegateCommand _ConnectCommand;
  135. public DelegateCommand ConnectCommand =>
  136. _ConnectCommand ?? (_ConnectCommand = new DelegateCommand(ExecuteConnectCommand));
  137. private DelegateCommand _DisConnectCommand;
  138. public DelegateCommand DisConnectCommand =>
  139. _DisConnectCommand ?? (_DisConnectCommand = new DelegateCommand(ExecuteDisConnectCommand));
  140. private DelegateCommand<string> _MotorCommand;
  141. public DelegateCommand<string> MotorCommand =>
  142. _MotorCommand ?? (_MotorCommand = new DelegateCommand<string>(ExecuteMotorCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(()=>Robot).ObservesProperty(() => Robot.CanExecute));
  143. private DelegateCommand _ResetCommand;
  144. public DelegateCommand ResetCommand =>
  145. _ResetCommand ?? (_ResetCommand = new DelegateCommand(ExecuteResetCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  146. private DelegateCommand<object> _JogCommand;
  147. public DelegateCommand<object> JogCommand =>
  148. _JogCommand ?? (_JogCommand = new DelegateCommand<object>(ExecuteJogCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  149. private DelegateCommand _sFreeCommand;
  150. public DelegateCommand sFreeCommand =>
  151. _sFreeCommand ?? (_sFreeCommand = new DelegateCommand(ExecutesFreeCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  152. private DelegateCommand _SLockCommand;
  153. public DelegateCommand SLockCommand =>
  154. _SLockCommand ?? (_SLockCommand = new DelegateCommand(ExecuteSLockCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  155. #endregion
  156. #region 事件
  157. #endregion
  158. #region 方法
  159. private void ShowMsg(string msg)
  160. {
  161. if (snackbar.MessageQueue==null)
  162. {
  163. snackbar.MessageQueue = new MaterialDesignThemes.Wpf.SnackbarMessageQueue();
  164. }
  165. snackbar.MessageQueue.Clear();
  166. snackbar.MessageQueue?.Enqueue(
  167. msg,
  168. null,
  169. null,
  170. null,
  171. false,
  172. true,
  173. TimeSpan.FromSeconds(0.4));
  174. }
  175. /// <summary>
  176. /// 连接机器人
  177. /// </summary>
  178. private async void ExecuteConnectCommand()
  179. {
  180. if (Robot == null || !Robot.IsConnected) return;
  181. try
  182. {
  183. await Robot.ConnectAsync();
  184. ShowMsg($"{Lang.完成}!");
  185. }
  186. catch (Exception ex)
  187. {
  188. LogHelper.WriteLogError("连接机器人时出错!",ex);
  189. ShowMsg(ex.Message);
  190. }
  191. }
  192. /// <summary>
  193. /// 断开机器人
  194. /// </summary>
  195. private void ExecuteDisConnectCommand()
  196. {
  197. if (Robot == null || !Robot.IsConnected) return;
  198. try
  199. {
  200. Robot.Disconnect();
  201. ShowMsg($"{Lang.完成}!");
  202. }
  203. catch (Exception ex)
  204. {
  205. LogHelper.WriteLogError("断开机器人时出错!", ex);
  206. ShowMsg(ex.Message);
  207. }
  208. }
  209. /// <summary>
  210. /// 电机
  211. /// </summary>
  212. /// <param name="obj"></param>
  213. private async void ExecuteMotorCommand(string obj)
  214. {
  215. if (Robot == null || !Robot.IsConnected) return;
  216. try
  217. {
  218. await Robot.MotorAsync(Convert.ToBoolean(obj));
  219. ShowMsg($"{Lang.完成}!");
  220. }
  221. catch (Exception ex)
  222. {
  223. LogHelper.WriteLogError("机器人点击操作时出错!", ex);
  224. ShowMsg(ex.Message);
  225. }
  226. }
  227. /// <summary>
  228. /// 重置
  229. /// </summary>
  230. private async void ExecuteResetCommand()
  231. {
  232. if (Robot == null || !Robot.IsConnected) return;
  233. try
  234. {
  235. await Robot.ResetAsync();
  236. ShowMsg($"{Lang.完成}!");
  237. }
  238. catch (Exception ex)
  239. {
  240. LogHelper.WriteLogError("重置机器人时出错!", ex);
  241. ShowMsg(ex.Message);
  242. }
  243. }
  244. /// <summary>
  245. /// 点动
  246. /// </summary>
  247. /// <param name="obj"></param>
  248. private async void ExecuteJogCommand(object obj)
  249. {
  250. if (Robot == null || !Robot.IsConnected) return;
  251. try
  252. {
  253. Robot.Timeout = 6000000;
  254. var items = ((string)obj).Split(':');
  255. switch (items[0])
  256. {
  257. case "X+":
  258. await Robot.JogAsync("X", double.Parse(items[1]));
  259. break;
  260. case "X-":
  261. await Robot.JogAsync("X", -double.Parse(items[1]));
  262. break;
  263. case "Y+":
  264. await Robot.JogAsync("Y", double.Parse(items[1]));
  265. break;
  266. case "Y-":
  267. await Robot.JogAsync("Y", -double.Parse(items[1]));
  268. break;
  269. case "Z+":
  270. await Robot.JogAsync("Z", double.Parse(items[1]));
  271. break;
  272. case "Z-":
  273. await Robot.JogAsync("Z", -double.Parse(items[1]));
  274. break;
  275. case "U+":
  276. await Robot.JogAsync("U", double.Parse(items[1]));
  277. break;
  278. case "U-":
  279. await Robot.JogAsync("U", -double.Parse(items[1]));
  280. break;
  281. case "V+":
  282. await Robot.JogAsync("V", double.Parse(items[1]));
  283. break;
  284. case "V-":
  285. await Robot.JogAsync("V", -double.Parse(items[1]));
  286. break;
  287. case "W+":
  288. await Robot.JogAsync("W", double.Parse(items[1]));
  289. break;
  290. case "W-":
  291. await Robot.JogAsync("W", -double.Parse(items[1]));
  292. break;
  293. default:
  294. break;
  295. }
  296. Robot.Timeout = 5000;
  297. ShowMsg($"{Lang.完成}!");
  298. }
  299. catch (Exception ex)
  300. {
  301. LogHelper.WriteLogError("机器点动时出错!", ex);
  302. ShowMsg(ex.Message);
  303. }
  304. }
  305. /// <summary>
  306. /// 锁定刹车
  307. /// </summary>
  308. private async void ExecuteSLockCommand()
  309. {
  310. if (Robot == null || !Robot.IsConnected) return;
  311. try
  312. {
  313. await Robot.SLockAsync();
  314. ShowMsg($"{Lang.完成}!");
  315. }
  316. catch (Exception ex)
  317. {
  318. LogHelper.WriteLogError("机器人锁定刹车时出错!", ex);
  319. ShowMsg(ex.Message);
  320. }
  321. }
  322. /// <summary>
  323. /// 释放刹车
  324. /// </summary>
  325. private async void ExecutesFreeCommand()
  326. {
  327. if (Robot == null || !Robot.IsConnected) return;
  328. try
  329. {
  330. await Robot.SFreeAsync();
  331. ShowMsg($"{Lang.完成}!");
  332. }
  333. catch (Exception ex)
  334. {
  335. LogHelper.WriteLogError("机器人释放刹车时出错!", ex);
  336. ShowMsg(ex.Message);
  337. }
  338. }
  339. private bool CanExecute
  340. {
  341. get
  342. {
  343. if (Robot != null)
  344. {
  345. return Robot.CanExecute;
  346. }
  347. else
  348. {
  349. return false;
  350. }
  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. }