CalibrationVerificationViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using Cognex.VisionPro;
  2. using Cognex.VisionPro.ToolBlock;
  3. using MathNet.Numerics.LinearAlgebra;
  4. using MathNet.Numerics.LinearAlgebra.Complex;
  5. using Prism.Commands;
  6. using Prism.Events;
  7. using Prism.Ioc;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using Prism.Services.Dialogs;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Drawing;
  14. using System.Linq;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. using Team.FFFeederService.Interfaces;
  18. using TeamAAS_VP.Core;
  19. using TeamAAS_VP.Events;
  20. using TeamAAS_VP.Interfaces;
  21. using TeamAAS_VP.Models;
  22. using TeamAAS_VP;
  23. using TeamAAS_VP.Resources.Languages;
  24. using TeamAAS_VP.Models.Calibration;
  25. using TeamAAS_VP.Services;
  26. using System.Threading;
  27. namespace TeamAAS_VP.ViewModels.Calibration
  28. {
  29. public class CalibrationVerificationViewModel : BindableBase, IConfirmNavigationRequest
  30. {
  31. IRegionManager _regionManager;
  32. IRegionNavigationService _regionNavigationService;
  33. IContainerProvider _container;
  34. IDialogService _dialogService;
  35. IEventAggregator _eventAggregator;
  36. IConfigService _configService;
  37. IRobotService _robotService;
  38. ICameraService _cameraService;
  39. IFeederService _feederService;
  40. ISystemDatabaseService _systemDatabaseService;
  41. ICameraCalibrationService _cameraCalibrationService;
  42. private CancellationTokenSource _cts;
  43. #region 属性
  44. private ICogImage _Image;
  45. public ICogImage Image
  46. {
  47. get { return _Image; }
  48. set { SetProperty(ref _Image, value); }
  49. }
  50. private Cognex.VisionPro.CogGraphicCollection _Graphic;
  51. public Cognex.VisionPro.CogGraphicCollection Graphic
  52. {
  53. get { return _Graphic; }
  54. set { SetProperty(ref _Graphic, value); }
  55. }
  56. private CalibrationInfo _SelectCalibration;
  57. public CalibrationInfo SelectCalibration
  58. {
  59. get { return _SelectCalibration; }
  60. set { SetProperty(ref _SelectCalibration, value); }
  61. }
  62. public Management management { get; set; }
  63. public IRobot Robot { get; set; }
  64. public IVoiceCoilMotorFeeder Feeder { get; set; }
  65. private bool _IsRunning = true;
  66. public bool IsRunning
  67. {
  68. get { return _IsRunning; }
  69. set { SetProperty(ref _IsRunning, value); }
  70. }
  71. private ICamera _Camera;
  72. public ICamera Camera
  73. {
  74. get { return _Camera; }
  75. set { SetProperty(ref _Camera, value); }
  76. }
  77. #endregion
  78. #region 命令
  79. private DelegateCommand _LoadedCommand;
  80. public DelegateCommand LoadedCommand =>
  81. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  82. private DelegateCommand _NextCommand;
  83. public DelegateCommand NextCommand =>
  84. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand).ObservesCanExecute(() => IsRunning));
  85. private DelegateCommand _BackCommand;
  86. public DelegateCommand BackCommand =>
  87. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand).ObservesCanExecute(() => IsRunning));
  88. private DelegateCommand _StartTestCommand;
  89. public DelegateCommand StartTestCommand =>
  90. _StartTestCommand ?? (_StartTestCommand = new DelegateCommand(ExecuteStartTestCommand).ObservesCanExecute(() => IsRunning));
  91. #endregion
  92. #region 事件
  93. #endregion
  94. public CalibrationVerificationViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService,
  95. IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, ISystemDatabaseService systemDatabaseService, ICameraCalibrationService cameraCalibrationService)
  96. {
  97. _regionManager = regionManager;
  98. _regionNavigationService = regionNavigationService;
  99. _container = container;
  100. _dialogService = dialogService;
  101. _eventAggregator = ea;
  102. _configService = configService;
  103. _robotService = robotService;
  104. _cameraService = cameraService;
  105. _feederService = feederService;
  106. _systemDatabaseService = systemDatabaseService;
  107. _cameraCalibrationService = cameraCalibrationService;
  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. async void ExecuteStartTestCommand()
  125. {
  126. try
  127. {
  128. if (MessageBox.Show(Lang.是否开始测试, Lang.自动校准, MessageBoxButton.OKCancel, MessageBoxImage.Question) != MessageBoxResult.OK) return;
  129. _cts = new CancellationTokenSource();
  130. IsRunning = false;
  131. (bool IsSuccess, CalibrationTestResult Result) = await _cameraCalibrationService.ExecuteCalibrationValidation(SelectCalibration, Robot, _cts.Token);
  132. if (!IsSuccess)
  133. {
  134. MessageBox.Show(Lang.失败);
  135. return;
  136. }
  137. SelectCalibration.CalibrationTestResult = Result;
  138. MessageBox.Show(Lang.验证完成);
  139. NavigationParameters param = new NavigationParameters();
  140. param.Add("SelectCalibration", SelectCalibration);
  141. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationResult", param);
  142. }
  143. catch (Exception ex)
  144. {
  145. LogHelper.WriteLogError(Lang.相机校准校准验证界面示加载时出错, ex);
  146. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  147. MessageBox.Show(ex.Message);
  148. }
  149. finally
  150. {
  151. try
  152. {
  153. if (_cts != null && !_cts.IsCancellationRequested)
  154. _cts.Cancel();
  155. _cts.Dispose();
  156. _cts = null;
  157. }
  158. catch (Exception)
  159. {
  160. }
  161. IsRunning = true;
  162. }
  163. }
  164. /// <summary>
  165. /// 下一步
  166. /// </summary>
  167. void ExecuteNextCommand()
  168. {
  169. NavigationParameters param = new NavigationParameters();
  170. param.Add("SelectCalibration", SelectCalibration);
  171. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationResult", param);
  172. }
  173. /// <summary>
  174. /// 上一步
  175. /// </summary>
  176. void ExecuteBackCommand()
  177. {
  178. NavigationParameters param = new NavigationParameters();
  179. param.Add("SelectCalibration", SelectCalibration);
  180. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationAuto", param);
  181. }
  182. /// <summary>
  183. /// 相机拍照
  184. /// </summary>
  185. /// <returns></returns>
  186. private async Task<(bool IsSuccess, double X, double Y, double U, int ImageWidth, int ImageHeight)> CaptureAndProcess()
  187. {
  188. if (Feeder != null)
  189. {
  190. Feeder.OpenLight();
  191. Feeder.SetLightTimeOut((ushort)SelectCalibration.Brightness);
  192. }
  193. await Task.Delay(200);
  194. //采集图像
  195. if (Camera != null)
  196. {
  197. SelectCalibration.ToolBlock.Inputs["InputImage"].Value = Camera.Grab();
  198. }
  199. App.Current.Dispatcher.Invoke(() =>
  200. {
  201. Image = (ICogImage)SelectCalibration.ToolBlock.Inputs["InputImage"].Value;
  202. Graphic = null;
  203. });
  204. SelectCalibration.ToolBlock.Run();
  205. CogToolBlockTerminalCollection outputCollection = SelectCalibration.ToolBlock.Outputs;
  206. App.Current.Dispatcher.Invoke(() =>
  207. {
  208. Graphic = outputCollection["Graphic"].Value as CogGraphicCollection;
  209. });
  210. bool Found = (bool)(outputCollection["Found"].Value);
  211. if (Found)
  212. {
  213. var strpoints = outputCollection["Point"].Value.ToString();
  214. string[] items = strpoints.Split(',');
  215. double x = double.Parse(items[0]);
  216. double y = double.Parse(items[1]);
  217. return (true, x, y, 0, Image.Width, Image.Height);
  218. }
  219. else
  220. {
  221. return (false, 0, 0, 0, Image.Width, Image.Height);
  222. }
  223. }
  224. #endregion
  225. #region 继承
  226. /// <summary>
  227. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  228. /// </summary>
  229. /// <param name="navigationContext"></param>
  230. /// <param name="continuationCallback"></param>
  231. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  232. {
  233. continuationCallback(true);
  234. }
  235. /// <summary>
  236. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  237. /// </summary>
  238. /// <param name="navigationContext"></param>
  239. /// <exception cref="NotImplementedException"></exception>
  240. public async void OnNavigatedTo(NavigationContext navigationContext)
  241. {
  242. SelectCalibration = navigationContext.Parameters.GetValue<CalibrationInfo>("SelectCalibration");
  243. if (SelectCalibration != null)
  244. {
  245. Robot = _robotService.GetRobot(SelectCalibration.RobotId);
  246. if (SelectCalibration.FeederId != Guid.Empty)
  247. {
  248. Feeder = _feederService.GetFeeder(SelectCalibration.FeederId);
  249. }
  250. }
  251. Camera = _cameraService.GetCamera(SelectCalibration.CameraID);
  252. _cameraCalibrationService.CaptureAndProcessCallback = null;
  253. _cameraCalibrationService.CaptureAndProcessCallback= CaptureAndProcess;
  254. }
  255. /// <summary>
  256. /// 是否允许导航到此视图模型。
  257. /// </summary>
  258. /// <param name="navigationContext"></param>
  259. /// <returns></returns>
  260. public bool IsNavigationTarget(NavigationContext navigationContext)
  261. {
  262. return true;
  263. }
  264. /// <summary>
  265. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  266. /// </summary>
  267. /// <param name="navigationContext"></param>
  268. public void OnNavigatedFrom(NavigationContext navigationContext)
  269. {
  270. _cameraCalibrationService.CaptureAndProcessCallback = null;
  271. }
  272. #endregion
  273. private bool _IsAllowEdit = false;
  274. public bool IsAllowEdit
  275. {
  276. get { return _IsAllowEdit; }
  277. set { SetProperty(ref _IsAllowEdit, value); }
  278. }
  279. private void LoginChange(Models.User user)
  280. {
  281. if (user.userPart == Enums.UserPart.Operator)
  282. {
  283. IsAllowEdit = false;
  284. }
  285. else
  286. {
  287. IsAllowEdit = true;
  288. }
  289. }
  290. }
  291. }