TestProductPageViewModel.cs 19 KB

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