RobotManual.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. _configService=((PrismApplication)(App.Current)).Container.Resolve<IConfigService>();
  42. InitializeComponent();
  43. DataContext = this;
  44. this.Loaded += RobotManual_Loaded;
  45. }
  46. private void RobotManual_Loaded(object sender, RoutedEventArgs e)
  47. {
  48. _configService = ((PrismApplication)(App.Current)).Container.Resolve<IConfigService>();
  49. }
  50. #region 属性
  51. private IRobot _Robot;
  52. public IRobot Robot
  53. {
  54. get { return _Robot; }
  55. set { SetProperty(ref _Robot,value);
  56. try
  57. {
  58. if (value!=null)
  59. {
  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. _configService.UpdateRobotStepDistance(Robot.Id, value);
  90. }
  91. catch (Exception)
  92. {
  93. }
  94. }
  95. }
  96. private ObservableCollection<int> _Tools = new ObservableCollection<int>();
  97. /// <summary>
  98. /// 机器人Tool集合
  99. /// </summary>
  100. public ObservableCollection<int> Tools
  101. {
  102. get { return _Tools; }
  103. set { SetProperty(ref _Tools, value); }
  104. }
  105. private int _SelectTool;
  106. /// <summary>
  107. /// 选中的Tool
  108. /// </summary>
  109. public int SelectTool
  110. {
  111. get { return _SelectTool; }
  112. set
  113. {
  114. SetProperty(ref _SelectTool, value);
  115. if (value >= 0)
  116. {
  117. if (Robot != null && Robot.IsConnected)
  118. {
  119. try
  120. {
  121. Robot.SelectToolAsync(value);
  122. }
  123. catch (Exception)
  124. {
  125. }
  126. }
  127. }
  128. }
  129. }
  130. #endregion
  131. #region 命令
  132. private DelegateCommand _ConnectCommand;
  133. public DelegateCommand ConnectCommand =>
  134. _ConnectCommand ?? (_ConnectCommand = new DelegateCommand(ExecuteConnectCommand));
  135. private DelegateCommand _DisConnectCommand;
  136. public DelegateCommand DisConnectCommand =>
  137. _DisConnectCommand ?? (_DisConnectCommand = new DelegateCommand(ExecuteDisConnectCommand));
  138. private DelegateCommand<string> _MotorCommand;
  139. public DelegateCommand<string> MotorCommand =>
  140. _MotorCommand ?? (_MotorCommand = new DelegateCommand<string>(ExecuteMotorCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(()=>Robot).ObservesProperty(() => Robot.CanExecute));
  141. private DelegateCommand _ResetCommand;
  142. public DelegateCommand ResetCommand =>
  143. _ResetCommand ?? (_ResetCommand = new DelegateCommand(ExecuteResetCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  144. private DelegateCommand<object> _JogCommand;
  145. public DelegateCommand<object> JogCommand =>
  146. _JogCommand ?? (_JogCommand = new DelegateCommand<object>(ExecuteJogCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  147. private DelegateCommand _sFreeCommand;
  148. public DelegateCommand sFreeCommand =>
  149. _sFreeCommand ?? (_sFreeCommand = new DelegateCommand(ExecutesFreeCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  150. private DelegateCommand _SLockCommand;
  151. public DelegateCommand SLockCommand =>
  152. _SLockCommand ?? (_SLockCommand = new DelegateCommand(ExecuteSLockCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  153. #endregion
  154. #region 事件
  155. #endregion
  156. #region 方法
  157. private void ShowMsg(string msg)
  158. {
  159. if (snackbar.MessageQueue==null)
  160. {
  161. snackbar.MessageQueue = new MaterialDesignThemes.Wpf.SnackbarMessageQueue();
  162. }
  163. snackbar.MessageQueue.Clear();
  164. snackbar.MessageQueue?.Enqueue(
  165. msg,
  166. null,
  167. null,
  168. null,
  169. false,
  170. true,
  171. TimeSpan.FromSeconds(1));
  172. }
  173. /// <summary>
  174. /// 连接机器人
  175. /// </summary>
  176. private async void ExecuteConnectCommand()
  177. {
  178. if (Robot == null || !Robot.IsConnected) return;
  179. try
  180. {
  181. await Robot.ConnectAsync();
  182. ShowMsg($"{Lang.完成}!");
  183. }
  184. catch (Exception ex)
  185. {
  186. LogHelper.WriteLogError("连接机器人时出错!",ex);
  187. ShowMsg(ex.Message);
  188. }
  189. }
  190. /// <summary>
  191. /// 断开机器人
  192. /// </summary>
  193. private void ExecuteDisConnectCommand()
  194. {
  195. if (Robot == null || !Robot.IsConnected) return;
  196. try
  197. {
  198. Robot.Disconnect();
  199. ShowMsg($"{Lang.完成}!");
  200. }
  201. catch (Exception ex)
  202. {
  203. LogHelper.WriteLogError("断开机器人时出错!", ex);
  204. ShowMsg(ex.Message);
  205. }
  206. }
  207. /// <summary>
  208. /// 电机
  209. /// </summary>
  210. /// <param name="obj"></param>
  211. private async void ExecuteMotorCommand(string obj)
  212. {
  213. if (Robot == null || !Robot.IsConnected) return;
  214. try
  215. {
  216. await Robot.MotorAsync(Convert.ToBoolean(obj));
  217. ShowMsg($"{Lang.完成}!");
  218. }
  219. catch (Exception ex)
  220. {
  221. LogHelper.WriteLogError("机器人点击操作时出错!", ex);
  222. ShowMsg(ex.Message);
  223. }
  224. }
  225. /// <summary>
  226. /// 重置
  227. /// </summary>
  228. private async void ExecuteResetCommand()
  229. {
  230. if (Robot == null || !Robot.IsConnected) return;
  231. try
  232. {
  233. await Robot.ResetAsync();
  234. ShowMsg($"{Lang.完成}!");
  235. }
  236. catch (Exception ex)
  237. {
  238. LogHelper.WriteLogError("重置机器人时出错!", ex);
  239. ShowMsg(ex.Message);
  240. }
  241. }
  242. /// <summary>
  243. /// 点动
  244. /// </summary>
  245. /// <param name="obj"></param>
  246. private async void ExecuteJogCommand(object obj)
  247. {
  248. if (Robot == null || !Robot.IsConnected) return;
  249. try
  250. {
  251. Robot.Timeout = 6000000;
  252. var items = ((string)obj).Split(':');
  253. switch (items[0])
  254. {
  255. case "X+":
  256. await Robot.JogAsync("X", double.Parse(items[1]));
  257. break;
  258. case "X-":
  259. await Robot.JogAsync("X", -double.Parse(items[1]));
  260. break;
  261. case "Y+":
  262. await Robot.JogAsync("Y", double.Parse(items[1]));
  263. break;
  264. case "Y-":
  265. await Robot.JogAsync("Y", -double.Parse(items[1]));
  266. break;
  267. case "Z+":
  268. await Robot.JogAsync("Z", double.Parse(items[1]));
  269. break;
  270. case "Z-":
  271. await Robot.JogAsync("Z", -double.Parse(items[1]));
  272. break;
  273. case "U+":
  274. await Robot.JogAsync("U", double.Parse(items[1]));
  275. break;
  276. case "U-":
  277. await Robot.JogAsync("U", -double.Parse(items[1]));
  278. break;
  279. case "V+":
  280. await Robot.JogAsync("V", double.Parse(items[1]));
  281. break;
  282. case "V-":
  283. await Robot.JogAsync("V", -double.Parse(items[1]));
  284. break;
  285. case "W+":
  286. await Robot.JogAsync("W", double.Parse(items[1]));
  287. break;
  288. case "W-":
  289. await Robot.JogAsync("W", -double.Parse(items[1]));
  290. break;
  291. default:
  292. break;
  293. }
  294. Robot.Timeout = 5000;
  295. ShowMsg($"{Lang.完成}!");
  296. }
  297. catch (Exception ex)
  298. {
  299. LogHelper.WriteLogError("机器点动时出错!", ex);
  300. ShowMsg(ex.Message);
  301. }
  302. }
  303. /// <summary>
  304. /// 锁定刹车
  305. /// </summary>
  306. private async void ExecuteSLockCommand()
  307. {
  308. if (Robot == null || !Robot.IsConnected) return;
  309. try
  310. {
  311. await Robot.SLockAsync();
  312. ShowMsg($"{Lang.完成}!");
  313. }
  314. catch (Exception ex)
  315. {
  316. LogHelper.WriteLogError("机器人锁定刹车时出错!", ex);
  317. ShowMsg(ex.Message);
  318. }
  319. }
  320. /// <summary>
  321. /// 释放刹车
  322. /// </summary>
  323. private async void ExecutesFreeCommand()
  324. {
  325. if (Robot == null || !Robot.IsConnected) return;
  326. try
  327. {
  328. await Robot.SFreeAsync();
  329. ShowMsg($"{Lang.完成}!");
  330. }
  331. catch (Exception ex)
  332. {
  333. LogHelper.WriteLogError("机器人释放刹车时出错!", ex);
  334. ShowMsg(ex.Message);
  335. }
  336. }
  337. private bool CanExecute
  338. {
  339. get
  340. {
  341. if (Robot != null)
  342. {
  343. return Robot.CanExecute;
  344. }
  345. else
  346. {
  347. return false;
  348. }
  349. }
  350. }
  351. #endregion
  352. #region 属性通知
  353. /// <summary>
  354. /// Occurs when a property value changes.
  355. /// </summary>
  356. public event PropertyChangedEventHandler PropertyChanged;
  357. /// <summary>
  358. /// Checks if a property already matches a desired value. Sets the property and
  359. /// notifies listeners only when necessary.
  360. /// </summary>
  361. /// <typeparam name="T">Type of the property.</typeparam>
  362. /// <param name="storage">Reference to a property with both getter and setter.</param>
  363. /// <param name="value">Desired value for the property.</param>
  364. /// <param name="propertyName">Name of the property used to notify listeners. This
  365. /// value is optional and can be provided automatically when invoked from compilers that
  366. /// support CallerMemberName.</param>
  367. /// <returns>True if the value was changed, false if the existing value matched the
  368. /// desired value.</returns>
  369. protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
  370. {
  371. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  372. storage = value;
  373. RaisePropertyChanged(propertyName);
  374. return true;
  375. }
  376. /// <summary>
  377. /// Checks if a property already matches a desired value. Sets the property and
  378. /// notifies listeners only when necessary.
  379. /// </summary>
  380. /// <typeparam name="T">Type of the property.</typeparam>
  381. /// <param name="storage">Reference to a property with both getter and setter.</param>
  382. /// <param name="value">Desired value for the property.</param>
  383. /// <param name="propertyName">Name of the property used to notify listeners. This
  384. /// value is optional and can be provided automatically when invoked from compilers that
  385. /// support CallerMemberName.</param>
  386. /// <param name="onChanged">Action that is called after the property value has been changed.</param>
  387. /// <returns>True if the value was changed, false if the existing value matched the
  388. /// desired value.</returns>
  389. protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
  390. {
  391. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  392. storage = value;
  393. onChanged?.Invoke();
  394. RaisePropertyChanged(propertyName);
  395. return true;
  396. }
  397. /// <summary>
  398. /// Raises this object's PropertyChanged event.
  399. /// </summary>
  400. /// <param name="propertyName">Name of the property used to notify listeners. This
  401. /// value is optional and can be provided automatically when invoked from compilers
  402. /// that support <see cref="CallerMemberNameAttribute"/>.</param>
  403. protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
  404. {
  405. OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
  406. }
  407. /// <summary>
  408. /// Raises this object's PropertyChanged event.
  409. /// </summary>
  410. /// <param name="args">The PropertyChangedEventArgs</param>
  411. protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
  412. {
  413. PropertyChanged?.Invoke(this, args);
  414. }
  415. #endregion
  416. }
  417. }