CalibrationTeachCenterPointViewModel.cs 16 KB

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