CalibrationTeachCenterPointViewModel.cs 19 KB

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