CalibrationVerificationViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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("相机校准-校准验证界面示加载时出错!", 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. ICogImage image = Camera.Grab();
  198. if (SelectCalibration.IsDistortionCorrection && SelectCalibration.CalibCheckerboardTool != null)
  199. {
  200. SelectCalibration.CalibCheckerboardTool.InputImage = image;
  201. SelectCalibration.CalibCheckerboardTool.Run();
  202. image = SelectCalibration.CalibCheckerboardTool.OutputImage;
  203. }
  204. SelectCalibration.ToolBlock.Inputs["InputImage"].Value = image;
  205. }
  206. App.Current.Dispatcher.Invoke(() =>
  207. {
  208. Image = (ICogImage)SelectCalibration.ToolBlock.Inputs["InputImage"].Value;
  209. Graphic = null;
  210. });
  211. SelectCalibration.ToolBlock.Run();
  212. CogToolBlockTerminalCollection outputCollection = SelectCalibration.ToolBlock.Outputs;
  213. App.Current.Dispatcher.Invoke(() =>
  214. {
  215. Graphic = outputCollection["Graphic"].Value as CogGraphicCollection;
  216. });
  217. bool Found = (bool)(outputCollection["Found"].Value);
  218. if (Found)
  219. {
  220. var strpoints = outputCollection["Point"].Value.ToString();
  221. string[] items = strpoints.Split(',');
  222. double x = double.Parse(items[0]);
  223. double y = double.Parse(items[1]);
  224. return (true, x, y, 0, Image.Width, Image.Height);
  225. }
  226. else
  227. {
  228. return (false, 0, 0, 0, Image.Width, Image.Height);
  229. }
  230. }
  231. #endregion
  232. #region 继承
  233. /// <summary>
  234. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  235. /// </summary>
  236. /// <param name="navigationContext"></param>
  237. /// <param name="continuationCallback"></param>
  238. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  239. {
  240. continuationCallback(true);
  241. }
  242. /// <summary>
  243. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  244. /// </summary>
  245. /// <param name="navigationContext"></param>
  246. /// <exception cref="NotImplementedException"></exception>
  247. public async void OnNavigatedTo(NavigationContext navigationContext)
  248. {
  249. SelectCalibration = navigationContext.Parameters.GetValue<CalibrationInfo>("SelectCalibration");
  250. if (SelectCalibration != null)
  251. {
  252. Robot = _robotService.GetRobot(SelectCalibration.RobotId);
  253. if (SelectCalibration.FeederId != Guid.Empty)
  254. {
  255. Feeder = _feederService.GetFeeder(SelectCalibration.FeederId);
  256. }
  257. }
  258. Camera = _cameraService.GetCamera(SelectCalibration.CameraID);
  259. _cameraCalibrationService.CaptureAndProcessCallback = null;
  260. _cameraCalibrationService.CaptureAndProcessCallback= CaptureAndProcess;
  261. }
  262. /// <summary>
  263. /// 是否允许导航到此视图模型。
  264. /// </summary>
  265. /// <param name="navigationContext"></param>
  266. /// <returns></returns>
  267. public bool IsNavigationTarget(NavigationContext navigationContext)
  268. {
  269. return true;
  270. }
  271. /// <summary>
  272. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  273. /// </summary>
  274. /// <param name="navigationContext"></param>
  275. public void OnNavigatedFrom(NavigationContext navigationContext)
  276. {
  277. _cameraCalibrationService.CaptureAndProcessCallback = null;
  278. }
  279. #endregion
  280. private bool _IsAllowEdit = false;
  281. public bool IsAllowEdit
  282. {
  283. get { return _IsAllowEdit; }
  284. set { SetProperty(ref _IsAllowEdit, value); }
  285. }
  286. private void LoginChange(Models.User user)
  287. {
  288. if (user.userPart == Enums.UserPart.Operator)
  289. {
  290. IsAllowEdit = false;
  291. }
  292. else
  293. {
  294. IsAllowEdit = true;
  295. }
  296. }
  297. }
  298. }