CalibrationTeachCenterPointViewModel.cs 19 KB

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