CalibIndependentCameraViewModel.cs 11 KB

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