TestProductPageViewModel.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. using Cognex.VisionPro;
  2. using Cognex.VisionPro.ToolBlock;
  3. using MaterialDesignThemes.Wpf;
  4. using MathNet.Numerics.LinearAlgebra;
  5. using Prism.Commands;
  6. using Prism.Events;
  7. using Prism.Ioc;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Collections.ObjectModel;
  13. using System.Linq;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using Team.FFFeederService;
  17. using Team.FFFeederService.Interfaces;
  18. using TeamAAS_VP.Controls;
  19. using TeamAAS_VP.Core;
  20. using TeamAAS_VP.Enums;
  21. using TeamAAS_VP.Interfaces;
  22. using TeamAAS_VP.Models;
  23. using TeamAAS_VP.Models.Product;
  24. using TeamAAS_VP.Models.Robot;
  25. using TeamAAS_VP;
  26. using TeamAAS_VP.Resources.Languages;
  27. namespace TeamAAS_VP.ViewModels.Product
  28. {
  29. public class TestProductPageViewModel : BindableBase
  30. {
  31. #region 字段
  32. IRegionManager _regionManager;
  33. IEventAggregator _eventAggregator;
  34. IContainerProvider _container;
  35. IFeederService _feederService;
  36. ICameraService _cameraService;
  37. IRobotService _robotService;
  38. IConfigService _configService;
  39. ICalibrationService _calibrationService;
  40. #endregion
  41. #region 属性
  42. private Management _management;
  43. public Management management
  44. {
  45. get { return _management; }
  46. set { SetProperty(ref _management,value); }
  47. }
  48. private ProductModel _SelectProduct;
  49. /// <summary>
  50. /// 选中的产品
  51. /// </summary>
  52. public ProductModel SelectProduct
  53. {
  54. get { return _SelectProduct; }
  55. set { SetProperty(ref _SelectProduct, value); }
  56. }
  57. //------------------------------------------------------------------------------------------------------------Feeder 取料--------------------------------------------------------------------
  58. private ProcedureModel _SelectProcedure;
  59. /// <summary>
  60. /// 选中的流程
  61. /// </summary>
  62. public ProcedureModel SelectProcedure
  63. {
  64. get { return _SelectProcedure; }
  65. set
  66. {
  67. SetProperty(ref _SelectProcedure, value);
  68. if (value != null)
  69. {
  70. Robot = _robotService.GetRobot(value.RobotId);
  71. Camera=_cameraService.GetCamera(value.CameraId);
  72. Feeder= _feederService.GetFeeder(value.FeederId);
  73. }
  74. }
  75. }
  76. private ObservableCollection<ProcedureModel> _ProcedureModels;
  77. /// <summary>
  78. /// 流程集合
  79. /// </summary>
  80. public ObservableCollection<ProcedureModel> ProcedureModels
  81. {
  82. get { return _ProcedureModels; }
  83. set { SetProperty(ref _ProcedureModels, value); }
  84. }
  85. private RPoint _SelectPoint;
  86. public RPoint SelectPoint
  87. {
  88. get { return _SelectPoint; }
  89. set { SetProperty(ref _SelectPoint, value); }
  90. }
  91. private IRobot _Robot;
  92. public IRobot Robot
  93. {
  94. get { return _Robot; }
  95. set { SetProperty(ref _Robot, value); }
  96. }
  97. private ICamera _Camera;
  98. public ICamera Camera
  99. {
  100. get { return _Camera; }
  101. set { SetProperty(ref _Camera, value); }
  102. }
  103. public IVoiceCoilMotorFeeder Feeder { get; set; }
  104. private int _Inhal = -1;
  105. public int Inhal
  106. {
  107. get { return _Inhal; }
  108. set { SetProperty(ref _Inhal, value); }
  109. }
  110. private int _Blow = -1;
  111. public int Blow
  112. {
  113. get { return _Blow; }
  114. set { SetProperty(ref _Blow, value); }
  115. }
  116. private ICogImage _Image;
  117. public ICogImage Image
  118. {
  119. get { return _Image; }
  120. set { SetProperty(ref _Image, value); }
  121. }
  122. private Cognex.VisionPro.CogGraphicCollection _Graphic;
  123. public Cognex.VisionPro.CogGraphicCollection Graphic
  124. {
  125. get { return _Graphic; }
  126. set { SetProperty(ref _Graphic, value); }
  127. }
  128. //------------------------------------------------------------------------------------------------------------下相机定位--------------------------------------------------------------------
  129. private ProcedureModel _SelectProcedure1;
  130. /// <summary>
  131. /// 选中的流程
  132. /// </summary>
  133. public ProcedureModel SelectProcedure1
  134. {
  135. get { return _SelectProcedure1; }
  136. set
  137. {
  138. SetProperty(ref _SelectProcedure1, value);
  139. if (value != null)
  140. {
  141. Robot1 = _robotService.GetRobot(value.RobotId);
  142. Camera1 = _cameraService.GetCamera(value.CameraId);
  143. Feeder1 = _feederService.GetFeeder(value.FeederId);
  144. }
  145. }
  146. }
  147. private ObservableCollection<ProcedureModel> _ProcedureModels1;
  148. /// <summary>
  149. /// 流程集合
  150. /// </summary>
  151. public ObservableCollection<ProcedureModel> ProcedureModels1
  152. {
  153. get { return _ProcedureModels1; }
  154. set { SetProperty(ref _ProcedureModels1, value); }
  155. }
  156. private RPoint _SelectPoint1;
  157. public RPoint SelectPoint1
  158. {
  159. get { return _SelectPoint1; }
  160. set { SetProperty(ref _SelectPoint1, value); }
  161. }
  162. private bool _IsCreateTool=true;
  163. public bool IsCreateTool
  164. {
  165. get { return _IsCreateTool; }
  166. set { SetProperty(ref _IsCreateTool, value); }
  167. }
  168. public IRobot Robot1 { get; set; }
  169. public ICamera Camera1 { get; set; }
  170. public IVoiceCoilMotorFeeder Feeder1 { get; set; }
  171. private int _Inhal1 = -1;
  172. public int Inhal1
  173. {
  174. get { return _Inhal1; }
  175. set { SetProperty(ref _Inhal1, value); }
  176. }
  177. private int _Blow1 = -1;
  178. public int Blow1
  179. {
  180. get { return _Blow1; }
  181. set { SetProperty(ref _Blow1, value); }
  182. }
  183. private ICogImage _Image1;
  184. public ICogImage Image1
  185. {
  186. get { return _Image1; }
  187. set { SetProperty(ref _Image1, value); }
  188. }
  189. private Cognex.VisionPro.CogGraphicCollection _Graphic1;
  190. public Cognex.VisionPro.CogGraphicCollection Graphic1
  191. {
  192. get { return _Graphic1; }
  193. set { SetProperty(ref _Graphic1, value); }
  194. }
  195. private bool _IsRunning = true;
  196. public bool IsRunning
  197. {
  198. get { return _IsRunning; }
  199. set { SetProperty(ref _IsRunning, value); }
  200. }
  201. #endregion
  202. #region 命令
  203. private DelegateCommand _LoadedCommand;
  204. public DelegateCommand LoadedCommand =>
  205. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  206. private DelegateCommand _AutoPickCommand;
  207. public DelegateCommand AutoPickCommand =>
  208. _AutoPickCommand ?? (_AutoPickCommand = new DelegateCommand(ExecuteAutoPickCommand).ObservesCanExecute(() => IsRunning));
  209. private DelegateCommand _AutoPhotoCommand;
  210. public DelegateCommand AutoPhotoCommand =>
  211. _AutoPhotoCommand ?? (_AutoPhotoCommand = new DelegateCommand(ExecuteAutoPhotoCommand).ObservesCanExecute(() => IsRunning));
  212. private DelegateCommand<object> _BlowOrInhalControlCommand;
  213. public DelegateCommand<object> BlowOrInhalControlCommand =>
  214. _BlowOrInhalControlCommand ?? (_BlowOrInhalControlCommand = new DelegateCommand<object>(ExecuteBlowOrInhalControlCommand).ObservesCanExecute(() => IsRunning));
  215. private DelegateCommand<object> _BlowOrInhalControl1Command;
  216. public DelegateCommand<object> BlowOrInhalControl1Command =>
  217. _BlowOrInhalControl1Command ?? (_BlowOrInhalControl1Command = new DelegateCommand<object>(ExecuteBlowOrInhalControl1Command).ObservesCanExecute(() => IsRunning));
  218. #endregion
  219. #region 事件
  220. #endregion
  221. public TestProductPageViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container,
  222. IFeederService feederService,ICameraService cameraService,IRobotService robotService, IConfigService configService,
  223. ICalibrationService calibrationService)
  224. {
  225. _regionManager = regionManager;
  226. _eventAggregator = ea;
  227. _container = container;
  228. _feederService = feederService;
  229. _cameraService = cameraService;
  230. _robotService = robotService;
  231. _configService = configService;
  232. _calibrationService = calibrationService;
  233. management = _container.Resolve<Management>();
  234. }
  235. #region 方法
  236. void ExecuteLoadedCommand()
  237. {
  238. management = _container.Resolve<Management>();
  239. var productsViewModel = _container.Resolve<ProductsViewModel>();
  240. //SelectProduct = productsViewModel.SelectProduct;
  241. ProcedureModels = new ObservableCollection<ProcedureModel>();
  242. foreach (var item in SelectProduct.CameraProcedures)
  243. {
  244. foreach (var item1 in item.ProcedureModels)
  245. {
  246. if (item1.RobotId!=Guid.Empty && item1.FeederId!= Guid.Empty && item1.CameraId!= Guid.Empty)
  247. {
  248. ProcedureModels.Add(item1);
  249. }
  250. if (item1.RobotId != Guid.Empty && item1.FeederId == Guid.Empty && item1.CameraId != Guid.Empty)
  251. {
  252. ProcedureModels1.Add(item1);
  253. }
  254. }
  255. }
  256. }
  257. async void ExecuteBlowOrInhalControlCommand(object isInhal)
  258. {
  259. try
  260. {
  261. if (Robot == null || !Robot.IsConnected)
  262. {
  263. return;
  264. }
  265. IsRunning = false;
  266. Robot.Timeout = 10000;
  267. //发送校准的参数
  268. await Robot.CalibParameAsync(new PickInfo() { Blow= Blow , Inhal= Inhal, PickPlaceModel= PickPlaceModel.Inhal}, 20, 50, false, 300, 300);
  269. await Robot.CalibOutIOAsync(!(bool)isInhal);
  270. }
  271. catch (Exception ex)
  272. {
  273. LogHelper.WriteLogError(Lang.示教取料点时出错, ex);
  274. MessageBox.Show(ex.Message);
  275. }
  276. finally
  277. {
  278. IsRunning = true;
  279. }
  280. }
  281. async void ExecuteBlowOrInhalControl1Command(object isInhal)
  282. {
  283. try
  284. {
  285. if (Robot == null || !Robot.IsConnected)
  286. {
  287. return;
  288. }
  289. IsRunning = false;
  290. Robot.Timeout = 10000;
  291. //发送校准的参数
  292. await Robot.CalibParameAsync(new PickInfo() { Blow = Blow1, Inhal = Inhal1, PickPlaceModel = PickPlaceModel.Inhal }, 20, 50, false, 300, 300);
  293. await Robot.CalibOutIOAsync(!(bool)isInhal);
  294. }
  295. catch (Exception ex)
  296. {
  297. LogHelper.WriteLogError(Lang.示教取料点时出错, ex);
  298. MessageBox.Show(ex.Message);
  299. }
  300. finally
  301. {
  302. IsRunning = true;
  303. }
  304. }
  305. /// <summary>
  306. /// 自动取料
  307. /// </summary>
  308. async void ExecuteAutoPickCommand()
  309. {
  310. if (SelectPoint==null || SelectProcedure==null)
  311. {
  312. return;
  313. }
  314. if (!(Robot!=null && Robot.IsConnected))
  315. {
  316. return;
  317. }
  318. try
  319. {
  320. if (Robot == null || !Robot.IsConnected)
  321. {
  322. return;
  323. }
  324. IsRunning = false;
  325. Robot.Timeout = 1000000;
  326. await Robot.SLockAsync();
  327. //发送校准的参数
  328. await Robot.CalibParameAsync(new PickInfo() { Blow = Blow, Inhal = Inhal, PickPlaceModel = PickPlaceModel.Inhal }, 20, 50, false, 300, 300);
  329. var pos = SelectPoint.Clone();
  330. //pos.Z = 0;
  331. //移动机器人至待机位置
  332. await Robot.CalibMotionAsync(pos, 0);
  333. //打开光源
  334. await Feeder.OpenLightAsync();
  335. await Feeder.SetLightValueAsync((ushort)SelectProcedure.LightValue);
  336. var calib = _calibrationService.GetAllCalibrations().FirstOrDefault(cal => cal.Id == SelectProcedure.CalibrationId);
  337. if (calib == null)
  338. {
  339. throw new Exception(Lang.没有可用的校准);
  340. }
  341. //关闭吸气
  342. await Robot.CalibOutIOAsync(false);
  343. await Task.Delay(300);
  344. //移动机器人至待机位置
  345. await Robot.CalibMotionAsync(pos, 0);
  346. var campos = await Photo(SelectProcedure, Camera);
  347. double angle = campos.Item3;
  348. if (calib.CameraMount == CameraMount.FixedDown || calib.CameraMount == CameraMount.MobileJ2 || calib.CameraMount == CameraMount.MobileJ4)
  349. {
  350. angle *= -1;
  351. }
  352. //得到校准矩阵
  353. Matrix<double> matrix = Matrix<double>.Build.DenseOfArray(calib.AffineTransformationMaterial);
  354. //将像素坐标转换成机器人坐标
  355. var rpos = matrix * Vector<double>.Build.Dense(new double[] { campos.Item1, campos.Item2, 1 });
  356. RPoint Target = pos.Clone();
  357. Target.X = (float)rpos[0];
  358. Target.Y = (float)rpos[1];
  359. Target.Z = (float)SelectProcedure.ToolInfo[0].OffsetZ;
  360. Target.U = (float)(angle + SelectProcedure.ToolInfo[0].OffsetU);
  361. Target.Tool = 2;
  362. //取料
  363. await Robot.CalibMotionAsync(Target, 0);
  364. await Task.Delay(200);
  365. await Robot.CalibOutIOAsync(true);
  366. //移动机器人至待机位置
  367. await Robot.CalibMotionAsync(pos, 0);
  368. //打开光源
  369. await Feeder.CloseLightAsync();
  370. MessageBox.Show(Lang.完成);
  371. }
  372. catch (Exception ex)
  373. {
  374. MessageBox.Show(ex.ToString());
  375. }
  376. finally
  377. {
  378. IsRunning = true;
  379. }
  380. }
  381. /// <summary>
  382. /// 自动下相机拍照
  383. /// </summary>
  384. async void ExecuteAutoPhotoCommand()
  385. {
  386. if (SelectPoint1 == null || SelectProcedure1 == null)
  387. {
  388. return;
  389. }
  390. if (!(Robot1 != null && Robot1.IsConnected))
  391. {
  392. return;
  393. }
  394. try
  395. {
  396. if (Robot1 == null || !Robot1.IsConnected)
  397. {
  398. return;
  399. }
  400. IsRunning = false;
  401. Robot1.Timeout = 1000000;
  402. await Robot1.SLockAsync();
  403. //发送校准的参数
  404. await Robot1.CalibParameAsync(new PickInfo() { Blow = Blow1, Inhal = Inhal1, PickPlaceModel = PickPlaceModel.Inhal }, 20, 50, false, 300, 300);
  405. var pos = SelectPoint1.Clone();
  406. //pos.Z = 0;
  407. //移动机器人至待机位置
  408. await Robot1.CalibMotionAsync(pos, 0);
  409. var calib = _calibrationService.GetAllCalibrations().FirstOrDefault(cal => cal.Id == SelectProcedure1.CalibrationId);
  410. if (calib == null)
  411. {
  412. throw new Exception(Lang.没有可用的校准);
  413. }
  414. var campos = await Photo(SelectProcedure1, Camera1);
  415. double angle = campos.Item3;
  416. if (calib.CameraMount == CameraMount.FixedDown || calib.CameraMount == CameraMount.MobileJ2 || calib.CameraMount == CameraMount.MobileJ4)
  417. {
  418. angle *= -1;
  419. }
  420. //得到校准矩阵
  421. Matrix<double> matrix = Matrix<double>.Build.DenseOfArray(calib.AffineTransformationMaterial);
  422. //将像素坐标转换成机器人坐标
  423. var rpos = matrix * Vector<double>.Build.Dense(new double[] { campos.Item1, campos.Item2, 1 });
  424. await Robot1.SelectToolAsync(1);
  425. var curpos=await Robot1.GetRobotPosAsync();
  426. //根据机器人当前的点位计算工具坐标
  427. ToolCoord tool = new ToolCoord();
  428. tool.ComputeTool(curpos.X, curpos.Y, curpos.U, rpos[0], rpos[1]);
  429. rpos = Vector<double>.Build.Dense(new double[] { tool.X, tool.Y });
  430. await Robot1.SetToolAsync(3, rpos[0], rpos[1]);
  431. MessageBox.Show(Lang.完成);
  432. }
  433. catch (Exception ex)
  434. {
  435. MessageBox.Show(ex.ToString());
  436. }
  437. finally
  438. {
  439. IsRunning = true;
  440. }
  441. }
  442. /// <summary>
  443. /// 相机拍照
  444. /// </summary>
  445. /// <returns></returns>
  446. /// <exception cref="Exception"></exception>
  447. private async Task<Tuple<double, double, double>> Photo(ProcedureModel SelectProcedure, ICamera Camera)
  448. {
  449. if (Feeder != null)
  450. {
  451. await Feeder.OpenLightAsync();
  452. await Feeder.SetLightValueAsync((ushort)SelectProcedure.LightValue);
  453. await Task.Delay(200);
  454. }
  455. //采集图像
  456. if (Camera != null)
  457. {
  458. SelectProcedure.ToolBlock.Inputs["InputImage"].Value = Camera.Grab();
  459. Image = (ICogImage)SelectProcedure.ToolBlock.Inputs["InputImage"].Value;
  460. }
  461. Graphic = null;
  462. SelectProcedure.ToolBlock.Run();
  463. if (Feeder != null)
  464. {
  465. await Feeder.CloseLightAsync();
  466. }
  467. CogToolBlockTerminalCollection outputCollection = SelectProcedure.ToolBlock.Outputs;
  468. Graphic = outputCollection["Graphic"].Value as CogGraphicCollection;
  469. bool Found = (bool)(outputCollection["Found"].Value);
  470. if (Found)
  471. {
  472. var strpoints = outputCollection["Point"].Value.ToString();
  473. string[] items = strpoints.Split(',');
  474. var calib = _calibrationService.GetAllCalibrations().FirstOrDefault(cal => cal.Id == SelectProcedure.CalibrationId);
  475. if (calib == null)
  476. {
  477. throw new Exception(Lang.没有可用的校准);
  478. }
  479. Matrix<double> matrix = Matrix<double>.Build.DenseOfArray(calib.AffineTransformationMaterial);
  480. double x = double.Parse(items[0]);
  481. double y = double.Parse(items[1]);
  482. double angle = double.Parse(items[2]);
  483. var rpos = matrix * Vector<double>.Build.Dense(new double[] { x, y, 1 });
  484. return new Tuple<double, double, double>(rpos[0], rpos[1], angle);
  485. }
  486. else
  487. {
  488. throw new Exception(Lang.拍照失败);
  489. }
  490. }
  491. #endregion
  492. }
  493. }