RobotManual.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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(Lang.连接机器人时出错,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(Lang.断开机器人时出错, 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(Lang.机器人点击操作时出错, 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(Lang.重置机器人时出错, 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. var items = ((string)obj).Split(':');
  254. //如果移动距离大于10mm,则提示用户确认
  255. double distance = double.Parse(items[1]);
  256. if (Math.Abs(distance) >= 10)
  257. {
  258. if (MessageBox.Show(string.Format(Lang.确认要点动距离0mm吗,distance), Lang.确认点动, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  259. {
  260. return;
  261. }
  262. }
  263. Robot.Timeout = 6000000;
  264. switch (items[0])
  265. {
  266. case "X+":
  267. await Robot.JogAsync("X", distance);
  268. break;
  269. case "X-":
  270. await Robot.JogAsync("X", -distance);
  271. break;
  272. case "Y+":
  273. await Robot.JogAsync("Y", distance);
  274. break;
  275. case "Y-":
  276. await Robot.JogAsync("Y", -distance);
  277. break;
  278. case "Z+":
  279. await Robot.JogAsync("Z", distance);
  280. break;
  281. case "Z-":
  282. await Robot.JogAsync("Z", -distance);
  283. break;
  284. case "U+":
  285. await Robot.JogAsync("U", distance);
  286. break;
  287. case "U-":
  288. await Robot.JogAsync("U", -distance);
  289. break;
  290. case "V+":
  291. await Robot.JogAsync("V", distance);
  292. break;
  293. case "V-":
  294. await Robot.JogAsync("V", -distance);
  295. break;
  296. case "W+":
  297. await Robot.JogAsync("W", distance);
  298. break;
  299. case "W-":
  300. await Robot.JogAsync("W", -distance);
  301. break;
  302. default:
  303. break;
  304. }
  305. Robot.Timeout = 5000;
  306. ShowMsg($"{Lang.完成}!");
  307. }
  308. catch (Exception ex)
  309. {
  310. LogHelper.WriteLogError(Lang.机器点动时出错, ex);
  311. ShowMsg(ex.Message);
  312. }
  313. }
  314. /// <summary>
  315. /// 锁定刹车
  316. /// </summary>
  317. private async void ExecuteSLockCommand()
  318. {
  319. if (Robot == null || !Robot.IsConnected) return;
  320. try
  321. {
  322. await Robot.SLockAsync();
  323. ShowMsg($"{Lang.完成}!");
  324. }
  325. catch (Exception ex)
  326. {
  327. LogHelper.WriteLogError(Lang.机器人锁定刹车时出错, ex);
  328. ShowMsg(ex.Message);
  329. }
  330. }
  331. /// <summary>
  332. /// 释放刹车
  333. /// </summary>
  334. private async void ExecutesFreeCommand()
  335. {
  336. if (Robot == null || !Robot.IsConnected) return;
  337. try
  338. {
  339. await Robot.SFreeAsync();
  340. ShowMsg($"{Lang.完成}!");
  341. }
  342. catch (Exception ex)
  343. {
  344. LogHelper.WriteLogError(Lang.机器人释放刹车时出错, ex);
  345. ShowMsg(ex.Message);
  346. }
  347. }
  348. private bool CanExecute
  349. {
  350. get
  351. {
  352. if (Robot != null)
  353. {
  354. return Robot.CanExecute;
  355. }
  356. else
  357. {
  358. return false;
  359. }
  360. }
  361. }
  362. #endregion
  363. #region 属性通知
  364. /// <summary>
  365. /// Occurs when a property value changes.
  366. /// </summary>
  367. public event PropertyChangedEventHandler PropertyChanged;
  368. /// <summary>
  369. /// Checks if a property already matches a desired value. Sets the property and
  370. /// notifies listeners only when necessary.
  371. /// </summary>
  372. /// <typeparam name="T">Type of the property.</typeparam>
  373. /// <param name="storage">Reference to a property with both getter and setter.</param>
  374. /// <param name="value">Desired value for the property.</param>
  375. /// <param name="propertyName">Name of the property used to notify listeners. This
  376. /// value is optional and can be provided automatically when invoked from compilers that
  377. /// support CallerMemberName.</param>
  378. /// <returns>True if the value was changed, false if the existing value matched the
  379. /// desired value.</returns>
  380. protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
  381. {
  382. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  383. storage = value;
  384. RaisePropertyChanged(propertyName);
  385. return true;
  386. }
  387. /// <summary>
  388. /// Checks if a property already matches a desired value. Sets the property and
  389. /// notifies listeners only when necessary.
  390. /// </summary>
  391. /// <typeparam name="T">Type of the property.</typeparam>
  392. /// <param name="storage">Reference to a property with both getter and setter.</param>
  393. /// <param name="value">Desired value for the property.</param>
  394. /// <param name="propertyName">Name of the property used to notify listeners. This
  395. /// value is optional and can be provided automatically when invoked from compilers that
  396. /// support CallerMemberName.</param>
  397. /// <param name="onChanged">Action that is called after the property value has been changed.</param>
  398. /// <returns>True if the value was changed, false if the existing value matched the
  399. /// desired value.</returns>
  400. protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
  401. {
  402. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  403. storage = value;
  404. onChanged?.Invoke();
  405. RaisePropertyChanged(propertyName);
  406. return true;
  407. }
  408. /// <summary>
  409. /// Raises this object's PropertyChanged event.
  410. /// </summary>
  411. /// <param name="propertyName">Name of the property used to notify listeners. This
  412. /// value is optional and can be provided automatically when invoked from compilers
  413. /// that support <see cref="CallerMemberNameAttribute"/>.</param>
  414. protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
  415. {
  416. OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
  417. }
  418. /// <summary>
  419. /// Raises this object's PropertyChanged event.
  420. /// </summary>
  421. /// <param name="args">The PropertyChangedEventArgs</param>
  422. protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
  423. {
  424. PropertyChanged?.Invoke(this, args);
  425. }
  426. #endregion
  427. }
  428. }