CalibrationTeachCenterPointViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. using Prism.Commands;
  2. using Prism.Events;
  3. using Prism.Ioc;
  4. using Prism.Mvvm;
  5. using Prism.Regions;
  6. using Prism.Services.Dialogs;
  7. using SixLabors.Fonts.Unicode;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Security.Cryptography;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using Team.FFFeederService.Interfaces;
  15. using TeamAAS_VP;
  16. using TeamAAS_VP.Core;
  17. using TeamAAS_VP.Events;
  18. using TeamAAS_VP.Interfaces;
  19. using TeamAAS_VP.Models;
  20. using TeamAAS_VP.Models.Calibration;
  21. using TeamAAS_VP.Resources.Languages;
  22. using TeamAAS_VP.Services;
  23. using TeamAAS_VP.ViewModels.Product;
  24. namespace TeamAAS_VP.ViewModels.Calibration
  25. {
  26. public class CalibrationTeachCenterPointViewModel : BindableBase, IConfirmNavigationRequest
  27. {
  28. IRegionManager _regionManager;
  29. IRegionNavigationService _regionNavigationService;
  30. IContainerProvider _container;
  31. IDialogService _dialogService;
  32. IEventAggregator _eventAggregator;
  33. IConfigService _configService;
  34. IRobotService _robotService;
  35. ICameraService _cameraService;
  36. IFeederService _feederService;
  37. ISystemDatabaseService _systemDatabaseService;
  38. #region 属性
  39. private CalibrationInfo _SelectCalibration;
  40. public CalibrationInfo SelectCalibration
  41. {
  42. get { return _SelectCalibration; }
  43. set { SetProperty(ref _SelectCalibration, value); }
  44. }
  45. private bool _IsShowTechHome = false;
  46. public bool IsShowTechHome
  47. {
  48. get { return _IsShowTechHome; }
  49. set { SetProperty(ref _IsShowTechHome, value); }
  50. }
  51. private bool _IsShowTechP0ModeSelect = true;
  52. /// <summary>
  53. /// 是否显示P0模式选择
  54. /// </summary>
  55. public bool IsShowTechP0ModeSelect
  56. {
  57. get { return _IsShowTechP0ModeSelect; }
  58. set { SetProperty(ref _IsShowTechP0ModeSelect, value); }
  59. }
  60. public Management management { get; set; }
  61. private IRobot _Robot;
  62. public IRobot Robot
  63. {
  64. get { return _Robot; }
  65. set { SetProperty(ref _Robot, value); }
  66. }
  67. #endregion
  68. #region 命令
  69. private DelegateCommand _LoadedCommand;
  70. public DelegateCommand LoadedCommand =>
  71. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  72. private DelegateCommand _NextCommand;
  73. public DelegateCommand NextCommand =>
  74. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  75. private DelegateCommand _BackCommand;
  76. public DelegateCommand BackCommand =>
  77. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  78. private DelegateCommand _TechCenterCommand;
  79. public DelegateCommand TechCenterCommand =>
  80. _TechCenterCommand ?? (_TechCenterCommand = new DelegateCommand(ExecuteTechCenterCommand));
  81. private DelegateCommand _TechHomeCommand;
  82. public DelegateCommand TechHomeCommand =>
  83. _TechHomeCommand ?? (_TechHomeCommand = new DelegateCommand(ExecuteTechHomeCommand));
  84. private DelegateCommand<object> _BlowOrInhalControlCommand;
  85. public DelegateCommand<object> BlowOrInhalControlCommand =>
  86. _BlowOrInhalControlCommand ?? (_BlowOrInhalControlCommand = new DelegateCommand<object>(ExecuteBlowOrInhalControlCommand));
  87. private DelegateCommand _TechMarkPointCommand;
  88. public DelegateCommand TechMarkPointCommand =>
  89. _TechMarkPointCommand ?? (_TechMarkPointCommand = new DelegateCommand(ExecuteTechMarkPointCommand));
  90. private DelegateCommand _MoveCenterCommand;
  91. public DelegateCommand MoveCenterCommand =>
  92. _MoveCenterCommand ?? (_MoveCenterCommand = new DelegateCommand(ExecuteMoveCenterCommand));
  93. #endregion
  94. #region 事件
  95. #endregion
  96. public CalibrationTeachCenterPointViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService,
  97. IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, ISystemDatabaseService systemDatabaseService)
  98. {
  99. _regionManager = regionManager;
  100. _regionNavigationService = regionNavigationService;
  101. _container = container;
  102. _dialogService = dialogService;
  103. _eventAggregator = ea;
  104. _configService = configService;
  105. _robotService = robotService;
  106. _cameraService = cameraService;
  107. _feederService = feederService;
  108. _systemDatabaseService = systemDatabaseService;
  109. }
  110. #region 方法
  111. void ExecuteLoadedCommand()
  112. {
  113. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  114. {
  115. IsAllowEdit = false;
  116. }
  117. else
  118. {
  119. IsAllowEdit = true;
  120. }
  121. }
  122. /// <summary>
  123. /// 下一步
  124. /// </summary>
  125. void ExecuteNextCommand()
  126. {
  127. NavigationParameters param = new NavigationParameters();
  128. param.Add("SelectCalibration", SelectCalibration);
  129. if (SelectCalibration.CameraMount == Enums.CameraMount.MobileDown_XYPlatform)
  130. {
  131. if (SelectCalibration.CalibTechP0Mode == Enums.CalibTechP0Mode.CameraFindP0)
  132. {
  133. _regionManager.RequestNavigate("CalibrationRegionContext", "FixedUpCameraFindP0", param);
  134. return;
  135. }
  136. else
  137. {
  138. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationAuto", param);
  139. }
  140. }
  141. else if (Robot.Brand == Enums.RobotBrand.XYZ_Platform)
  142. {
  143. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationAuto", param);
  144. }
  145. else
  146. {
  147. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationSelectTool", param);
  148. }
  149. }
  150. /// <summary>
  151. /// 上一步
  152. /// </summary>
  153. void ExecuteBackCommand()
  154. {
  155. NavigationParameters param = new NavigationParameters();
  156. param.Add("SelectCalibration", SelectCalibration);
  157. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationEditVision", param);
  158. }
  159. /// <summary>
  160. /// 示教中心点
  161. /// </summary>
  162. async void ExecuteTechCenterCommand()
  163. {
  164. if (Robot == null || !Robot.IsConnected) return;
  165. try
  166. {
  167. var curpos = await Robot.GetRobotPosAsync();
  168. if (SelectCalibration.CenterPoint == null)
  169. {
  170. SelectCalibration.CenterPoint = new RPoint();
  171. }
  172. SelectCalibration.CenterPoint.X = curpos.X;
  173. SelectCalibration.CenterPoint.Y = curpos.Y;
  174. SelectCalibration.CenterPoint.Z = curpos.Z;
  175. SelectCalibration.CenterPoint.U = curpos.U;
  176. SelectCalibration.CenterPoint.V = curpos.V;
  177. SelectCalibration.CenterPoint.W = curpos.W;
  178. SelectCalibration.CenterPoint.Hand = curpos.Hand;
  179. SelectCalibration.CenterPoint.Local = curpos.Local;
  180. SelectCalibration.CenterPoint.Tool = curpos.Tool;
  181. LogHelper.WriteLogInfo(string.Format(Lang.相机校准示教中心点界面示教待机点X0Y1Z2, SelectCalibration.CenterPoint.X, SelectCalibration.CenterPoint.Y, SelectCalibration.CenterPoint.Z));
  182. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = string.Format(Lang.成功X0Y1Z2, SelectCalibration.CenterPoint.X, SelectCalibration.CenterPoint.Y, SelectCalibration.CenterPoint.Z), Duration = 1 });
  183. }
  184. catch (Exception ex)
  185. {
  186. LogHelper.WriteLogError(Lang.相机校准示教中心点界面示教中心点时出错, ex);
  187. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  188. }
  189. }
  190. /// <summary>
  191. /// 示教待机点
  192. /// </summary>
  193. async void ExecuteTechHomeCommand()
  194. {
  195. if (Robot == null || !Robot.IsConnected) return;
  196. try
  197. {
  198. var curpos = await Robot.GetRobotPosAsync();
  199. if (SelectCalibration.HomePoint == null)
  200. {
  201. SelectCalibration.HomePoint = new RPoint();
  202. }
  203. SelectCalibration.HomePoint.X = curpos.X;
  204. SelectCalibration.HomePoint.Y = curpos.Y;
  205. SelectCalibration.HomePoint.Z = curpos.Z;
  206. SelectCalibration.HomePoint.U = curpos.U;
  207. SelectCalibration.HomePoint.V = curpos.V;
  208. SelectCalibration.HomePoint.W = curpos.W;
  209. SelectCalibration.HomePoint.Hand = curpos.Hand;
  210. SelectCalibration.HomePoint.Local = curpos.Local;
  211. SelectCalibration.HomePoint.Tool = curpos.Tool;
  212. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.成功, Duration = 1 });
  213. }
  214. catch (Exception ex)
  215. {
  216. LogHelper.WriteLogError(Lang.相机校准示教中心点界面示教待机点时出错, ex);
  217. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  218. }
  219. }
  220. /// <summary>
  221. /// 夹爪打开/关闭
  222. /// </summary>
  223. /// <param name="obj"></param>
  224. async void ExecuteBlowOrInhalControlCommand(object obj)
  225. {
  226. try
  227. {
  228. await Robot.CalibOutIOAsync(!(bool)obj);
  229. }
  230. catch (Exception ex)
  231. {
  232. LogHelper.WriteLogError(Lang.相机校准夹爪打开关闭点时出错, ex);
  233. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  234. }
  235. }
  236. /// <summary>
  237. /// 示教P0
  238. /// </summary>
  239. async void ExecuteTechMarkPointCommand()
  240. {
  241. if (Robot == null || !Robot.IsConnected) return;
  242. try
  243. {
  244. var curpos = await Robot.GetRobotPosAsync();
  245. if (SelectCalibration.MarkPoint == null)
  246. {
  247. SelectCalibration.MarkPoint = new RPoint();
  248. }
  249. SelectCalibration.MarkPoint.X = curpos.X;
  250. SelectCalibration.MarkPoint.Y = curpos.Y;
  251. SelectCalibration.MarkPoint.Z = curpos.Z;
  252. SelectCalibration.MarkPoint.U = curpos.U;
  253. SelectCalibration.MarkPoint.V = curpos.V;
  254. SelectCalibration.MarkPoint.W = curpos.W;
  255. SelectCalibration.MarkPoint.Hand = curpos.Hand;
  256. SelectCalibration.MarkPoint.Local = curpos.Local;
  257. SelectCalibration.MarkPoint.Tool = curpos.Tool;
  258. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.成功, Duration = 1 });
  259. }
  260. catch (Exception ex)
  261. {
  262. LogHelper.WriteLogError(Lang.相机校准示教中心点界面示教P0时出错, ex);
  263. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  264. }
  265. }
  266. /// <summary>
  267. /// 移动至中心点
  268. /// </summary>
  269. async void ExecuteMoveCenterCommand()
  270. {
  271. if (Robot == null || !Robot.IsConnected) return;
  272. try
  273. {
  274. if (SelectCalibration.CenterPoint.X==0 && SelectCalibration.CenterPoint.Y==0)
  275. {
  276. MessageBox.Show(Lang.检测到机器人的XY为0可能点位未示教将不允许移动机器人);
  277. return;
  278. }
  279. if (MessageBox.Show(Lang.是否移动机器人至中心点请确认安全,Lang.移动机器人,MessageBoxButton.YesNo,MessageBoxImage.Asterisk)!= MessageBoxResult.Yes)
  280. {
  281. return;
  282. }
  283. await Robot.CalibMotionAsync(SelectCalibration.CenterPoint,0);
  284. LogHelper.WriteLogInfo(Lang.相机校准移动机器人至中心点界面示教待机点);
  285. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = string.Format(Lang.成功X0Y1Z2, SelectCalibration.CenterPoint.X, SelectCalibration.CenterPoint.Y, SelectCalibration.CenterPoint.Z), Duration = 1 });
  286. }
  287. catch (Exception ex)
  288. {
  289. LogHelper.WriteLogError(Lang.相机校准示教中心点界面示教中心点时出错, ex);
  290. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  291. }
  292. }
  293. #endregion
  294. #region 继承
  295. /// <summary>
  296. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  297. /// </summary>
  298. /// <param name="navigationContext"></param>
  299. /// <param name="continuationCallback"></param>
  300. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  301. {
  302. continuationCallback(true);
  303. }
  304. /// <summary>
  305. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  306. /// </summary>
  307. /// <param name="navigationContext"></param>
  308. /// <exception cref="NotImplementedException"></exception>
  309. public async void OnNavigatedTo(NavigationContext navigationContext)
  310. {
  311. SelectCalibration = navigationContext.Parameters.GetValue<CalibrationInfo>("SelectCalibration");
  312. if (SelectCalibration != null)
  313. {
  314. Robot = _robotService.GetRobot(SelectCalibration.RobotId);
  315. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  316. {
  317. IsShowTechHome = true;
  318. }
  319. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedUp)
  320. {
  321. IsShowTechP0ModeSelect = false;
  322. }
  323. else if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  324. {
  325. IsShowTechP0ModeSelect = false;
  326. }
  327. else if (SelectCalibration.CameraMount == Enums.CameraMount.MobileJ4)
  328. {
  329. IsShowTechP0ModeSelect = true;
  330. }
  331. else if (SelectCalibration.CameraMount == Enums.CameraMount.MobileJ2)
  332. {
  333. IsShowTechP0ModeSelect = true;
  334. }
  335. else if (SelectCalibration.CameraMount == Enums.CameraMount.MobileDown_XYPlatform)
  336. {
  337. IsShowTechP0ModeSelect = true;
  338. }
  339. }
  340. else
  341. {
  342. IsShowTechHome = false;
  343. }
  344. }
  345. /// <summary>
  346. /// 是否允许导航到此视图模型。
  347. /// </summary>
  348. /// <param name="navigationContext"></param>
  349. /// <returns></returns>
  350. public bool IsNavigationTarget(NavigationContext navigationContext)
  351. {
  352. return true;
  353. }
  354. /// <summary>
  355. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  356. /// </summary>
  357. /// <param name="navigationContext"></param>
  358. public void OnNavigatedFrom(NavigationContext navigationContext)
  359. {
  360. }
  361. #endregion
  362. private bool _IsAllowEdit = false;
  363. public bool IsAllowEdit
  364. {
  365. get { return _IsAllowEdit; }
  366. set { SetProperty(ref _IsAllowEdit, value); }
  367. }
  368. private void LoginChange(Models.User user)
  369. {
  370. if (user.userPart == Enums.UserPart.Operator)
  371. {
  372. IsAllowEdit = false;
  373. }
  374. else
  375. {
  376. IsAllowEdit = true;
  377. }
  378. }
  379. }
  380. }