RobotManual.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. using Prism.Commands;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. using TeamAAS_VP.Events;
  19. using TeamAAS_VP.Core;
  20. using TeamAAS_VP.Interfaces;
  21. using TeamAAS_VP.ViewModels;
  22. using TeamAAS_VP;
  23. using System.Collections.ObjectModel;
  24. using TeamAAS_VP.Enums;
  25. using TeamAAS_VP.Core.Robots;
  26. using TeamAAS_VP.Resources.Languages;
  27. namespace TeamAAS_VP.Controls
  28. {
  29. /// <summary>
  30. /// RobotManual.xaml 的交互逻辑
  31. /// </summary>
  32. public partial class RobotManual : UserControl,INotifyPropertyChanged
  33. {
  34. public RobotManual()
  35. {
  36. InitializeComponent();
  37. DataContext = this;
  38. this.Loaded += RobotManual_Loaded;
  39. }
  40. private void RobotManual_Loaded(object sender, RoutedEventArgs e)
  41. {
  42. }
  43. #region 属性
  44. private IRobot _Robot;
  45. public IRobot Robot
  46. {
  47. get { return _Robot; }
  48. set { SetProperty(ref _Robot,value);
  49. try
  50. {
  51. var config = FileHelper.ReadApplicationConfiguration();
  52. if (config.RobotJogSetpDistance.TryGetValue(value.Id,out double distance))
  53. {
  54. this.Distance = distance;
  55. }
  56. if (value is FanucRobot)
  57. {
  58. Tools = new ObservableCollection<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
  59. SelectTool = Robot.SelectedTool;
  60. }
  61. else
  62. {
  63. Tools = new ObservableCollection<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
  64. SelectTool = Robot.SelectedTool;
  65. }
  66. }
  67. catch (Exception)
  68. {
  69. }
  70. }
  71. }
  72. private double _Distance=1;
  73. public double Distance
  74. {
  75. get { return _Distance; }
  76. set { SetProperty(ref _Distance, value);
  77. try
  78. {
  79. if (Robot==null)
  80. {
  81. return;
  82. }
  83. var config = FileHelper.ReadApplicationConfiguration();
  84. if (config.RobotJogSetpDistance.ContainsKey(Robot.Id))
  85. {
  86. config.RobotJogSetpDistance[Robot.Id] =value;
  87. }
  88. else
  89. {
  90. config.RobotJogSetpDistance.Add(Robot.Id,value);
  91. }
  92. FileHelper.SaveApplicationConfiguration(config);
  93. }
  94. catch (Exception)
  95. {
  96. }
  97. }
  98. }
  99. private ObservableCollection<int> _Tools = new ObservableCollection<int>();
  100. /// <summary>
  101. /// 机器人Tool集合
  102. /// </summary>
  103. public ObservableCollection<int> Tools
  104. {
  105. get { return _Tools; }
  106. set { SetProperty(ref _Tools, value); }
  107. }
  108. private int _SelectTool;
  109. /// <summary>
  110. /// 选中的Tool
  111. /// </summary>
  112. public int SelectTool
  113. {
  114. get { return _SelectTool; }
  115. set
  116. {
  117. SetProperty(ref _SelectTool, value);
  118. if (value >= 0)
  119. {
  120. if (Robot != null && Robot.IsConnected)
  121. {
  122. try
  123. {
  124. Robot.SelectToolAsync(value);
  125. }
  126. catch (Exception)
  127. {
  128. }
  129. }
  130. }
  131. }
  132. }
  133. #endregion
  134. #region 命令
  135. private DelegateCommand _ConnectCommand;
  136. public DelegateCommand ConnectCommand =>
  137. _ConnectCommand ?? (_ConnectCommand = new DelegateCommand(ExecuteConnectCommand));
  138. private DelegateCommand _DisConnectCommand;
  139. public DelegateCommand DisConnectCommand =>
  140. _DisConnectCommand ?? (_DisConnectCommand = new DelegateCommand(ExecuteDisConnectCommand));
  141. private DelegateCommand<string> _MotorCommand;
  142. public DelegateCommand<string> MotorCommand =>
  143. _MotorCommand ?? (_MotorCommand = new DelegateCommand<string>(ExecuteMotorCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(()=>Robot).ObservesProperty(() => Robot.CanExecute));
  144. private DelegateCommand _ResetCommand;
  145. public DelegateCommand ResetCommand =>
  146. _ResetCommand ?? (_ResetCommand = new DelegateCommand(ExecuteResetCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  147. private DelegateCommand<object> _JogCommand;
  148. public DelegateCommand<object> JogCommand =>
  149. _JogCommand ?? (_JogCommand = new DelegateCommand<object>(ExecuteJogCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  150. private DelegateCommand _sFreeCommand;
  151. public DelegateCommand sFreeCommand =>
  152. _sFreeCommand ?? (_sFreeCommand = new DelegateCommand(ExecutesFreeCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  153. private DelegateCommand _SLockCommand;
  154. public DelegateCommand SLockCommand =>
  155. _SLockCommand ?? (_SLockCommand = new DelegateCommand(ExecuteSLockCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  156. #endregion
  157. #region 事件
  158. #endregion
  159. #region 方法
  160. private void ShowMsg(string msg)
  161. {
  162. if (snackbar.MessageQueue==null)
  163. {
  164. snackbar.MessageQueue = new MaterialDesignThemes.Wpf.SnackbarMessageQueue();
  165. }
  166. snackbar.MessageQueue?.Enqueue(
  167. msg,
  168. null,
  169. null,
  170. null,
  171. false,
  172. true,
  173. TimeSpan.FromSeconds(1));
  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. }