FixedUpCameraFindP0ViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. using Cognex.VisionPro;
  2. using Cognex.VisionPro.ToolBlock;
  3. using Prism.Commands;
  4. using Prism.Events;
  5. using Prism.Ioc;
  6. using Prism.Mvvm;
  7. using Prism.Regions;
  8. using Prism.Services.Dialogs;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Collections.ObjectModel;
  12. using System.Drawing;
  13. using System.IO;
  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.Enums;
  20. using TeamAAS_VP.Events;
  21. using TeamAAS_VP.Interfaces;
  22. using TeamAAS_VP.Models;
  23. using TeamAAS_VP.Models.Calibration;
  24. using TeamAAS_VP.Services;
  25. namespace TeamAAS_VP.ViewModels.Calibration
  26. {
  27. public class FixedUpCameraFindP0ViewModel : BindableBase, IConfirmNavigationRequest
  28. {
  29. IRegionManager _regionManager;
  30. IRegionNavigationService _regionNavigationService;
  31. IContainerProvider _container;
  32. IDialogService _dialogService;
  33. IEventAggregator _eventAggregator;
  34. IConfigService _configService;
  35. IRobotService _robotService;
  36. ICameraService _cameraService;
  37. IFeederService _feederService;
  38. ISystemDatabaseService _systemDatabaseService;
  39. ICalibrationService _calibrationServvice;
  40. #region 属性
  41. private CogToolBlockEditV2 _ToolBlockEdit;
  42. public CogToolBlockEditV2 ToolBlockEdit
  43. {
  44. get { return _ToolBlockEdit; }
  45. set { SetProperty(ref _ToolBlockEdit, value); }
  46. }
  47. private CalibrationInfo _SelectCalibration;
  48. public CalibrationInfo SelectCalibration
  49. {
  50. get { return _SelectCalibration; }
  51. set { SetProperty(ref _SelectCalibration, value); }
  52. }
  53. private IRobot _Robot;
  54. public IRobot Robot
  55. {
  56. get { return _Robot; }
  57. set { SetProperty(ref _Robot, value); }
  58. }
  59. private ICamera _Camera;
  60. public ICamera Camera
  61. {
  62. get { return _Camera; }
  63. set { SetProperty(ref _Camera, value); }
  64. }
  65. private float _ExposureTime;
  66. /// <summary>
  67. /// 相机曝光时间
  68. /// </summary>
  69. public float ExposureTime
  70. {
  71. get { return _ExposureTime; }
  72. set
  73. {
  74. SetProperty(ref _ExposureTime, value);
  75. SelectCalibration.ExposureTime = value;
  76. if (Camera != null)
  77. {
  78. try
  79. {
  80. SelectCalibration.FixedUpCameraFindP0Info.ExposureTime = value;
  81. Camera.SetExposureTime(value);
  82. }
  83. catch (Exception)
  84. {
  85. }
  86. }
  87. }
  88. }
  89. private float _Gain;
  90. /// <summary>
  91. /// 相机增益
  92. /// </summary>
  93. public float Gain
  94. {
  95. get { return _Gain; }
  96. set
  97. {
  98. SetProperty(ref _Gain, value);
  99. SelectCalibration.Gain = value;
  100. if (Camera != null)
  101. {
  102. try
  103. {
  104. SelectCalibration.FixedUpCameraFindP0Info.Gain = value;
  105. Camera.SetGain(value);
  106. }
  107. catch (Exception)
  108. {
  109. }
  110. }
  111. }
  112. }
  113. private ObservableCollection<CameraInfo> _Cameras;
  114. public ObservableCollection<CameraInfo> Cameras
  115. {
  116. get { return _Cameras; }
  117. set { SetProperty(ref _Cameras, value); }
  118. }
  119. private CameraInfo _SelectCamera;
  120. public CameraInfo SelectCamera
  121. {
  122. get { return _SelectCamera; }
  123. set
  124. {
  125. SetProperty(ref _SelectCamera, value);
  126. if (value != null)
  127. {
  128. Camera = _cameraService.GetCamera(value.Id);
  129. SelectCalibration.FixedUpCameraFindP0Info.CameraId = value.Id;
  130. SelectCalibration.FixedUpCameraFindP0Info.CameraId = value.Id;
  131. }
  132. }
  133. }
  134. private ObservableCollection<CalibrationInfo> _CalibrationList;
  135. public ObservableCollection<CalibrationInfo> CalibrationList
  136. {
  137. get { return _CalibrationList; }
  138. set { SetProperty(ref _CalibrationList, value); }
  139. }
  140. private CalibrationInfo _SelectedCalibration;
  141. public CalibrationInfo SelectedCalibration
  142. {
  143. get { return _SelectedCalibration; }
  144. set
  145. {
  146. SetProperty(ref _SelectedCalibration, value);
  147. }
  148. }
  149. #endregion
  150. #region 命令
  151. private DelegateCommand _LoadedCommand;
  152. public DelegateCommand LoadedCommand =>
  153. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  154. private DelegateCommand _NextCommand;
  155. public DelegateCommand NextCommand =>
  156. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  157. private DelegateCommand _BackCommand;
  158. public DelegateCommand BackCommand =>
  159. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  160. private DelegateCommand _RunVisionProcessCommand;
  161. public DelegateCommand RunVisionProcessCommand =>
  162. _RunVisionProcessCommand ?? (_RunVisionProcessCommand = new DelegateCommand(ExecuteRunVisionProcessCommand));
  163. private DelegateCommand _FindP0Command;
  164. public DelegateCommand FindP0Command =>
  165. _FindP0Command ?? (_FindP0Command = new DelegateCommand(ExecuteFindP0Command));
  166. #endregion
  167. #region 事件
  168. #endregion
  169. public FixedUpCameraFindP0ViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService,
  170. IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, ISystemDatabaseService systemDatabaseService, ICalibrationService calibrationService)
  171. {
  172. _regionManager = regionManager;
  173. _regionNavigationService = regionNavigationService;
  174. _container = container;
  175. _dialogService = dialogService;
  176. _eventAggregator = ea;
  177. _configService = configService;
  178. _robotService = robotService;
  179. _cameraService = cameraService;
  180. _feederService = feederService;
  181. _systemDatabaseService = systemDatabaseService;
  182. _calibrationServvice = calibrationService;
  183. }
  184. #region 方法
  185. void ExecuteLoadedCommand()
  186. {
  187. try
  188. {
  189. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  190. {
  191. IsAllowEdit = false;
  192. }
  193. else
  194. {
  195. IsAllowEdit = true;
  196. }
  197. }
  198. catch (Exception ex)
  199. {
  200. LogHelper.WriteLogError("相机校准加载视觉工具处理界面时出错!", ex);
  201. MessageBox.Show(ex.Message);
  202. }
  203. }
  204. /// <summary>
  205. /// 下一步
  206. /// </summary>
  207. void ExecuteNextCommand()
  208. {
  209. try
  210. {
  211. string prcPath = FilePath.CalibrationPath + "//" + SelectCalibration.Name + "//" + "FixedUpCameraTool.vpp";
  212. CogSerializer.SaveObjectToFile(ToolBlockEdit.Subject, prcPath);
  213. NavigationParameters param = new NavigationParameters();
  214. param.Add("SelectCalibration", SelectCalibration);
  215. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationAuto", param);
  216. }
  217. catch (Exception ex)
  218. {
  219. LogHelper.WriteLogError("相机校准-下相机示教P0界面点击下一步时出错!", ex);
  220. MessageBox.Show(ex.Message);
  221. }
  222. }
  223. /// <summary>
  224. /// 上一步
  225. /// </summary>
  226. void ExecuteBackCommand()
  227. {
  228. try
  229. {
  230. NavigationParameters param = new NavigationParameters();
  231. param.Add("SelectCalibration", SelectCalibration);
  232. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationTeachCenterPoint", param);
  233. }
  234. catch (Exception ex)
  235. {
  236. LogHelper.WriteLogError("相机校准-下相机示教P0界面点击上一步时出错!", ex);
  237. MessageBox.Show(ex.Message);
  238. }
  239. }
  240. /// <summary>
  241. /// 运行视觉流程
  242. /// </summary>
  243. void ExecuteRunVisionProcessCommand()
  244. {
  245. try
  246. {
  247. ToolBlockEdit.Subject.Inputs["InputImage"].Value = Camera.Grab();
  248. ToolBlockEdit.Subject.Run();
  249. }
  250. catch (Exception ex)
  251. {
  252. LogHelper.WriteLogError("相机校准-运行流程时出错时出错", ex);
  253. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  254. }
  255. }
  256. /// <summary>
  257. /// 自动计算P0点
  258. /// </summary>
  259. async void ExecuteFindP0Command()
  260. {
  261. if (Camera == null)
  262. {
  263. MessageBox.Show("请先选择相机!");
  264. return;
  265. }
  266. if (SelectedCalibration == null)
  267. {
  268. MessageBox.Show("请先选择标定!");
  269. return;
  270. }
  271. var result = await CaptureAndProcess();
  272. if (result.IsSuccess)
  273. {
  274. if (SelectCalibration.MarkPoint == null)
  275. {
  276. SelectCalibration.MarkPoint = new RPoint();
  277. }
  278. SelectCalibration.MarkPoint.X = (float) result.X;
  279. SelectCalibration.MarkPoint.Y = (float)result.Y;
  280. MessageBox.Show($"P0点计算成功!X:{result.X},Y:{result.Y}");
  281. }
  282. else
  283. {
  284. MessageBox.Show("未找到P0点,请调整工件位置后重试!");
  285. }
  286. }
  287. /// <summary>
  288. /// 相机拍照
  289. /// </summary>
  290. /// <returns></returns>
  291. private async Task<(bool IsSuccess, double X, double Y, double U, int ImageWidth, int ImageHeight)> CaptureAndProcess()
  292. {
  293. ICogImage image = null;
  294. //采集图像
  295. if (Camera != null)
  296. {
  297. image = Camera.Grab();
  298. ToolBlockEdit.Subject.Inputs["InputImage"].Value = image;
  299. }
  300. ToolBlockEdit.Subject.Run();
  301. CogToolBlockTerminalCollection outputCollection = ToolBlockEdit.Subject.Outputs;
  302. bool Found = (bool)(outputCollection["Found"].Value);
  303. if (Found)
  304. {
  305. var strpoints = outputCollection["Point"].Value.ToString();
  306. string[] items = strpoints.Split(',');
  307. double x = double.Parse(items[0]);
  308. double y = double.Parse(items[1]);
  309. //校准转换
  310. var calibResult = _calibrationServvice.ConvertPixelToPosition((x, y, 0), null, SelectedCalibration, Robot.Brand);
  311. if (calibResult.IsSucceed)
  312. {
  313. x = calibResult.X;
  314. y = calibResult.Y;
  315. return (true, x, y, 0, image.Width, image.Height);
  316. }
  317. return (false, 0, 0, 0, image.Width, image.Height);
  318. }
  319. else
  320. {
  321. return (false, 0, 0, 0, image.Width, image.Height);
  322. }
  323. }
  324. #endregion
  325. #region 继承
  326. /// <summary>
  327. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  328. /// </summary>
  329. /// <param name="navigationContext"></param>
  330. /// <param name="continuationCallback"></param>
  331. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  332. {
  333. continuationCallback(true);
  334. }
  335. /// <summary>
  336. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  337. /// </summary>
  338. /// <param name="navigationContext"></param>
  339. /// <exception cref="NotImplementedException"></exception>
  340. public async void OnNavigatedTo(NavigationContext navigationContext)
  341. {
  342. SelectCalibration = navigationContext.Parameters.GetValue<CalibrationInfo>("SelectCalibration");
  343. if (SelectCalibration.FixedUpCameraFindP0Info == null)
  344. {
  345. SelectCalibration.FixedUpCameraFindP0Info = new FixedUpCameraFindP0Info();
  346. }
  347. if (ToolBlockEdit.Subject==null)
  348. {
  349. string prcPath = FilePath.CalibrationPath + "//" + SelectCalibration.Name + "//" + "FixedUpCameraTool.vpp";
  350. if (File.Exists(prcPath))
  351. {
  352. ToolBlockEdit.Subject = CogSerializer.LoadObjectFromFile(prcPath) as CogToolBlock;
  353. }
  354. else
  355. {
  356. ToolBlockEdit.Subject = CogSerializer.LoadObjectFromFile(FilePath.CalibrationToolblockPath) as CogToolBlock;
  357. }
  358. }
  359. if (SelectCalibration.RobotId != Guid.Empty)
  360. {
  361. Robot = _robotService.GetRobot(SelectCalibration.RobotId);
  362. }
  363. Cameras = new ObservableCollection<CameraInfo>(_configService.GetAllCameras());
  364. if (SelectCalibration.FixedUpCameraFindP0Info.CameraId != Guid.Empty)
  365. {
  366. SelectCamera = Cameras.FirstOrDefault(c => c.Id == SelectCalibration.FixedUpCameraFindP0Info.CameraId);
  367. }
  368. CalibrationList = new ObservableCollection<CalibrationInfo>(_calibrationServvice.GetAllCalibrations());
  369. if (SelectCalibration.FixedUpCameraFindP0Info.CalibrationId != Guid.Empty)
  370. {
  371. SelectCalibration = CalibrationList.FirstOrDefault(c => c.Id == SelectCalibration.FixedUpCameraFindP0Info.CalibrationId);
  372. }
  373. ExposureTime = SelectCalibration.ExposureTime;
  374. Gain = SelectCalibration.Gain;
  375. }
  376. /// <summary>
  377. /// 是否允许导航到此视图模型。
  378. /// </summary>
  379. /// <param name="navigationContext"></param>
  380. /// <returns></returns>
  381. public bool IsNavigationTarget(NavigationContext navigationContext)
  382. {
  383. return true;
  384. }
  385. /// <summary>
  386. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  387. /// </summary>
  388. /// <param name="navigationContext"></param>
  389. public void OnNavigatedFrom(NavigationContext navigationContext)
  390. {
  391. }
  392. #endregion
  393. private bool _IsAllowEdit = false;
  394. public bool IsAllowEdit
  395. {
  396. get { return _IsAllowEdit; }
  397. set { SetProperty(ref _IsAllowEdit, value); }
  398. }
  399. private void LoginChange(Models.User user)
  400. {
  401. if (user.userPart == Enums.UserPart.Operator)
  402. {
  403. IsAllowEdit = false;
  404. }
  405. else
  406. {
  407. IsAllowEdit = true;
  408. }
  409. }
  410. }
  411. }