FixedUpCameraFindP0ViewModel.cs 16 KB

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