RobotManual.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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?.Enqueue(
  164. msg,
  165. null,
  166. null,
  167. null,
  168. false,
  169. true,
  170. TimeSpan.FromSeconds(1));
  171. }
  172. /// <summary>
  173. /// 连接机器人
  174. /// </summary>
  175. private async void ExecuteConnectCommand()
  176. {
  177. if (Robot == null || !Robot.IsConnected) return;
  178. try
  179. {
  180. await Robot.ConnectAsync();
  181. ShowMsg($"{Lang.完成}!");
  182. }
  183. catch (Exception ex)
  184. {
  185. LogHelper.WriteLogError("连接机器人时出错!",ex);
  186. ShowMsg(ex.Message);
  187. }
  188. }
  189. /// <summary>
  190. /// 断开机器人
  191. /// </summary>
  192. private void ExecuteDisConnectCommand()
  193. {
  194. if (Robot == null || !Robot.IsConnected) return;
  195. try
  196. {
  197. Robot.Disconnect();
  198. ShowMsg($"{Lang.完成}!");
  199. }
  200. catch (Exception ex)
  201. {
  202. LogHelper.WriteLogError("断开机器人时出错!", ex);
  203. ShowMsg(ex.Message);
  204. }
  205. }
  206. /// <summary>
  207. /// 电机
  208. /// </summary>
  209. /// <param name="obj"></param>
  210. private async void ExecuteMotorCommand(string obj)
  211. {
  212. if (Robot == null || !Robot.IsConnected) return;
  213. try
  214. {
  215. await Robot.MotorAsync(Convert.ToBoolean(obj));
  216. ShowMsg($"{Lang.完成}!");
  217. }
  218. catch (Exception ex)
  219. {
  220. LogHelper.WriteLogError("机器人点击操作时出错!", ex);
  221. ShowMsg(ex.Message);
  222. }
  223. }
  224. /// <summary>
  225. /// 重置
  226. /// </summary>
  227. private async void ExecuteResetCommand()
  228. {
  229. if (Robot == null || !Robot.IsConnected) return;
  230. try
  231. {
  232. await Robot.ResetAsync();
  233. ShowMsg($"{Lang.完成}!");
  234. }
  235. catch (Exception ex)
  236. {
  237. LogHelper.WriteLogError("重置机器人时出错!", ex);
  238. ShowMsg(ex.Message);
  239. }
  240. }
  241. /// <summary>
  242. /// 点动
  243. /// </summary>
  244. /// <param name="obj"></param>
  245. private async void ExecuteJogCommand(object obj)
  246. {
  247. if (Robot == null || !Robot.IsConnected) return;
  248. try
  249. {
  250. Robot.Timeout = 6000000;
  251. var items = ((string)obj).Split(':');
  252. switch (items[0])
  253. {
  254. case "X+":
  255. await Robot.JogAsync("X", double.Parse(items[1]));
  256. break;
  257. case "X-":
  258. await Robot.JogAsync("X", -double.Parse(items[1]));
  259. break;
  260. case "Y+":
  261. await Robot.JogAsync("Y", double.Parse(items[1]));
  262. break;
  263. case "Y-":
  264. await Robot.JogAsync("Y", -double.Parse(items[1]));
  265. break;
  266. case "Z+":
  267. await Robot.JogAsync("Z", double.Parse(items[1]));
  268. break;
  269. case "Z-":
  270. await Robot.JogAsync("Z", -double.Parse(items[1]));
  271. break;
  272. case "U+":
  273. await Robot.JogAsync("U", double.Parse(items[1]));
  274. break;
  275. case "U-":
  276. await Robot.JogAsync("U", -double.Parse(items[1]));
  277. break;
  278. case "V+":
  279. await Robot.JogAsync("V", double.Parse(items[1]));
  280. break;
  281. case "V-":
  282. await Robot.JogAsync("V", -double.Parse(items[1]));
  283. break;
  284. case "W+":
  285. await Robot.JogAsync("W", double.Parse(items[1]));
  286. break;
  287. case "W-":
  288. await Robot.JogAsync("W", -double.Parse(items[1]));
  289. break;
  290. default:
  291. break;
  292. }
  293. Robot.Timeout = 5000;
  294. ShowMsg($"{Lang.完成}!");
  295. }
  296. catch (Exception ex)
  297. {
  298. LogHelper.WriteLogError("机器点动时出错!", ex);
  299. ShowMsg(ex.Message);
  300. }
  301. }
  302. /// <summary>
  303. /// 锁定刹车
  304. /// </summary>
  305. private async void ExecuteSLockCommand()
  306. {
  307. if (Robot == null || !Robot.IsConnected) return;
  308. try
  309. {
  310. await Robot.SLockAsync();
  311. ShowMsg($"{Lang.完成}!");
  312. }
  313. catch (Exception ex)
  314. {
  315. LogHelper.WriteLogError("机器人锁定刹车时出错!", ex);
  316. ShowMsg(ex.Message);
  317. }
  318. }
  319. /// <summary>
  320. /// 释放刹车
  321. /// </summary>
  322. private async void ExecutesFreeCommand()
  323. {
  324. if (Robot == null || !Robot.IsConnected) return;
  325. try
  326. {
  327. await Robot.SFreeAsync();
  328. ShowMsg($"{Lang.完成}!");
  329. }
  330. catch (Exception ex)
  331. {
  332. LogHelper.WriteLogError("机器人释放刹车时出错!", ex);
  333. ShowMsg(ex.Message);
  334. }
  335. }
  336. private bool CanExecute
  337. {
  338. get
  339. {
  340. if (Robot != null)
  341. {
  342. return Robot.CanExecute;
  343. }
  344. else
  345. {
  346. return false;
  347. }
  348. }
  349. }
  350. #endregion
  351. #region 属性通知
  352. /// <summary>
  353. /// Occurs when a property value changes.
  354. /// </summary>
  355. public event PropertyChangedEventHandler PropertyChanged;
  356. /// <summary>
  357. /// Checks if a property already matches a desired value. Sets the property and
  358. /// notifies listeners only when necessary.
  359. /// </summary>
  360. /// <typeparam name="T">Type of the property.</typeparam>
  361. /// <param name="storage">Reference to a property with both getter and setter.</param>
  362. /// <param name="value">Desired value for the property.</param>
  363. /// <param name="propertyName">Name of the property used to notify listeners. This
  364. /// value is optional and can be provided automatically when invoked from compilers that
  365. /// support CallerMemberName.</param>
  366. /// <returns>True if the value was changed, false if the existing value matched the
  367. /// desired value.</returns>
  368. protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
  369. {
  370. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  371. storage = value;
  372. RaisePropertyChanged(propertyName);
  373. return true;
  374. }
  375. /// <summary>
  376. /// Checks if a property already matches a desired value. Sets the property and
  377. /// notifies listeners only when necessary.
  378. /// </summary>
  379. /// <typeparam name="T">Type of the property.</typeparam>
  380. /// <param name="storage">Reference to a property with both getter and setter.</param>
  381. /// <param name="value">Desired value for the property.</param>
  382. /// <param name="propertyName">Name of the property used to notify listeners. This
  383. /// value is optional and can be provided automatically when invoked from compilers that
  384. /// support CallerMemberName.</param>
  385. /// <param name="onChanged">Action that is called after the property value has been changed.</param>
  386. /// <returns>True if the value was changed, false if the existing value matched the
  387. /// desired value.</returns>
  388. protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
  389. {
  390. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  391. storage = value;
  392. onChanged?.Invoke();
  393. RaisePropertyChanged(propertyName);
  394. return true;
  395. }
  396. /// <summary>
  397. /// Raises this object's PropertyChanged event.
  398. /// </summary>
  399. /// <param name="propertyName">Name of the property used to notify listeners. This
  400. /// value is optional and can be provided automatically when invoked from compilers
  401. /// that support <see cref="CallerMemberNameAttribute"/>.</param>
  402. protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
  403. {
  404. OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
  405. }
  406. /// <summary>
  407. /// Raises this object's PropertyChanged event.
  408. /// </summary>
  409. /// <param name="args">The PropertyChangedEventArgs</param>
  410. protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
  411. {
  412. PropertyChanged?.Invoke(this, args);
  413. }
  414. #endregion
  415. }
  416. }