CalibrationAutoViewModel.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. using MathNet.Numerics.LinearAlgebra;
  2. using NPOI.SS.Formula.Functions;
  3. using OpenCvSharp;
  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.Tasks;
  14. using System.Windows;
  15. using Team.FFFeederService.Interfaces;
  16. using TeamAAS_VP.Events;
  17. using TeamAAS_VP.Core;
  18. using TeamAAS_VP.Interfaces;
  19. using TeamAAS_VP.Models;
  20. using static NPOI.POIFS.Crypt.CryptoFunctions;
  21. using TeamAAS_VP;
  22. using Cognex.VisionPro;
  23. using System.Drawing;
  24. using Cognex.VisionPro.ToolBlock;
  25. using TeamAAS_VP.Resources.Languages;
  26. namespace TeamAAS_VP.ViewModels.Calibration
  27. {
  28. public class CalibrationAutoViewModel : BindableBase
  29. {
  30. IRegionManager _regionManager;
  31. IRegionNavigationService _regionNavigationService;
  32. IContainerProvider _container;
  33. IDialogService _dialogService;
  34. IEventAggregator _eventAggregator;
  35. #region 属性
  36. private ICogImage _Image;
  37. public ICogImage Image
  38. {
  39. get { return _Image; }
  40. set { SetProperty(ref _Image, value); }
  41. }
  42. private Cognex.VisionPro.CogGraphicCollection _Graphic;
  43. public Cognex.VisionPro.CogGraphicCollection Graphic
  44. {
  45. get { return _Graphic; }
  46. set { SetProperty(ref _Graphic, value); }
  47. }
  48. private CalibrationInfo _SelectCalibration;
  49. public CalibrationInfo SelectCalibration
  50. {
  51. get { return _SelectCalibration; }
  52. set { SetProperty(ref _SelectCalibration, value); }
  53. }
  54. public Management management { get; set; }
  55. public RobotService robotService { get; set; }
  56. public IRobot Robot { get; set; }
  57. public IVoiceCoilMotorFeeder Feeder { 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 _LoadedCommand;
  73. public DelegateCommand LoadedCommand =>
  74. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  75. private DelegateCommand _NextCommand;
  76. public DelegateCommand NextCommand =>
  77. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand).ObservesCanExecute(() => IsRunning));
  78. private DelegateCommand _BackCommand;
  79. public DelegateCommand BackCommand =>
  80. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand).ObservesCanExecute(() => IsRunning));
  81. private DelegateCommand _StartCalibCommand;
  82. public DelegateCommand StartCalibCommand =>
  83. _StartCalibCommand ?? (_StartCalibCommand = new DelegateCommand(ExecuteStartCalibCommand).ObservesCanExecute(() => IsRunning));
  84. #endregion
  85. #region 事件
  86. #endregion
  87. public CalibrationAutoViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService)
  88. {
  89. _regionManager = regionManager;
  90. _regionNavigationService = regionNavigationService;
  91. _container = container;
  92. _dialogService = dialogService;
  93. _eventAggregator = ea;
  94. SelectCalibration = _container.Resolve<CalibrationHomeViewModel>().SelectCalibration;
  95. }
  96. #region 方法
  97. void ExecuteLoadedCommand()
  98. {
  99. try
  100. {
  101. if (_container.Resolve<Management>().CurrentUser.userPart == Enums.UserPart.Operator)
  102. {
  103. IsAllowEdit = false;
  104. }
  105. else
  106. {
  107. IsAllowEdit = true;
  108. }
  109. management = _container.Resolve<Management>();
  110. robotService = management.RobotService;
  111. SelectCalibration = _container.Resolve<CalibrationHomeViewModel>().SelectCalibration;
  112. if (SelectCalibration != null)
  113. {
  114. Robot = robotService.GetRobot(SelectCalibration.RobotId);
  115. if (SelectCalibration.FeederId != Guid.Empty)
  116. {
  117. Feeder = management.FeederService.GetFeeder(SelectCalibration.FeederId);
  118. }
  119. }
  120. Camera = management.CameraService.GetCamera(SelectCalibration.CameraID);
  121. }
  122. catch (Exception ex)
  123. {
  124. LogHelper.WriteLogError("9点校准界面加载时出错!", ex);
  125. MessageBox.Show(ex.ToString());
  126. }
  127. }
  128. async void ExecuteStartCalibCommand()
  129. {
  130. try
  131. {
  132. if (MessageBox.Show(Lang.是否开始自动校准, $"{Lang.自动校准}", MessageBoxButton.OKCancel, MessageBoxImage.Question) != MessageBoxResult.OK) return;
  133. IsRunning = false;
  134. double IntervalX = SelectCalibration.Width / 2;
  135. double IntervalY = SelectCalibration.Height / 2;
  136. Vector<double> P1 = Vector<double>.Build.Dense(new double[] { -IntervalX, -IntervalY });
  137. Vector<double> P2 = Vector<double>.Build.Dense(new double[] { 0, -IntervalY });
  138. Vector<double> P3 = Vector<double>.Build.Dense(new double[] { IntervalX, -IntervalY });
  139. Vector<double> P4 = Vector<double>.Build.Dense(new double[] { IntervalX, 0 });
  140. Vector<double> P5 = Vector<double>.Build.Dense(new double[] { 0, 0 });
  141. Vector<double> P6 = Vector<double>.Build.Dense(new double[] { -IntervalX, 0 });
  142. Vector<double> P7 = Vector<double>.Build.Dense(new double[] { -IntervalX, IntervalY });
  143. Vector<double> P8 = Vector<double>.Build.Dense(new double[] { 0, IntervalY });
  144. Vector<double> P9 = Vector<double>.Build.Dense(new double[] { IntervalX, IntervalY });
  145. //移动相机标定时,需要先记住特征点的绝对坐标(相对于机器人坐标系) P0
  146. //if (SelectCalibration.CameraMount == Enums.CameraMount.MobileJ4)
  147. //{
  148. // P1[0] *=-1; P1[1] *= -1;
  149. // P2[0] *= -1; P2[1] *= -1;
  150. // P3[0] *= -1; P3[1] *= -1;
  151. // P4[0] *= -1; P4[1] *= -1;
  152. // P5[0] *= -1; P5[1] *= -1;
  153. // P6[0] *= -1; P6[1] *= -1;
  154. // P7[0] *= -1; P7[1] *= -1;
  155. // P8[0] *= -1; P8[1] *= -1;
  156. // P9[0] *= -1; P9[1] *= -1;
  157. //}
  158. var P0 = Vector<double>.Build.Dense(new double[] { SelectCalibration.CenterPoint.X, SelectCalibration.CenterPoint.Y });
  159. if (SelectCalibration.CameraMount == Enums.CameraMount.MobileDown_XYPlatform)
  160. {
  161. var res = await AutoTechP0(SelectCalibration.CalibTechP0Mode);
  162. if (!res)
  163. {
  164. MessageBox.Show(Lang.自动示教P0失败);
  165. return;
  166. }
  167. }
  168. /// <summary>
  169. /// 机器人与相机之间的旋转矩阵
  170. /// </summary>
  171. Matrix<double> RobotCameraRotationMatrix = Matrix<double>.Build.DenseOfArray(SelectCalibration.RobotCameraRotationMatrix);
  172. //1
  173. P1 = RobotCameraRotationMatrix.Inverse() * P1 + P0;
  174. //2
  175. P2 = RobotCameraRotationMatrix.Inverse() * P2 + P0;
  176. //3
  177. P3 = RobotCameraRotationMatrix.Inverse() * P3 + P0;
  178. //4
  179. P4 = RobotCameraRotationMatrix.Inverse() * P4 + P0;
  180. //5
  181. P5 = RobotCameraRotationMatrix.Inverse() * P5 + P0;
  182. //6
  183. P6 = RobotCameraRotationMatrix.Inverse() * P6 + P0;
  184. //7
  185. P7 = RobotCameraRotationMatrix.Inverse() * P7 + P0;
  186. //8
  187. P8 = RobotCameraRotationMatrix.Inverse() * P8 + P0;
  188. //9
  189. P9 = RobotCameraRotationMatrix.Inverse() * P9 + P0;
  190. List<RPoint> rPoints = new List<RPoint>();
  191. rPoints.Add(new RPoint()
  192. {
  193. Number = 1,
  194. X = (float)P1[0],
  195. Y = (float)P1[1],
  196. Z = SelectCalibration.CenterPoint.Z,
  197. U = SelectCalibration.CenterPoint.U,
  198. V = SelectCalibration.CenterPoint.V,
  199. W = SelectCalibration.CenterPoint.W,
  200. Hand = SelectCalibration.CenterPoint.Hand,
  201. Local = SelectCalibration.CenterPoint.Local,
  202. Tool = SelectCalibration.CenterPoint.Tool
  203. });
  204. rPoints.Add(new RPoint()
  205. {
  206. Number = 2,
  207. X = (float)P2[0],
  208. Y = (float)P2[1],
  209. Z = SelectCalibration.CenterPoint.Z,
  210. U = SelectCalibration.CenterPoint.U,
  211. V = SelectCalibration.CenterPoint.V,
  212. W = SelectCalibration.CenterPoint.W,
  213. Hand = SelectCalibration.CenterPoint.Hand,
  214. Local = SelectCalibration.CenterPoint.Local,
  215. Tool = SelectCalibration.CenterPoint.Tool
  216. });
  217. rPoints.Add(new RPoint()
  218. {
  219. Number = 3,
  220. X = (float)P3[0],
  221. Y = (float)P3[1],
  222. Z = SelectCalibration.CenterPoint.Z,
  223. U = SelectCalibration.CenterPoint.U,
  224. V = SelectCalibration.CenterPoint.V,
  225. W = SelectCalibration.CenterPoint.W,
  226. Hand = SelectCalibration.CenterPoint.Hand,
  227. Local = SelectCalibration.CenterPoint.Local,
  228. Tool = SelectCalibration.CenterPoint.Tool
  229. });
  230. rPoints.Add(new RPoint()
  231. {
  232. Number = 4,
  233. X = (float)P4[0],
  234. Y = (float)P4[1],
  235. Z = SelectCalibration.CenterPoint.Z,
  236. U = SelectCalibration.CenterPoint.U,
  237. V = SelectCalibration.CenterPoint.V,
  238. W = SelectCalibration.CenterPoint.W,
  239. Hand = SelectCalibration.CenterPoint.Hand,
  240. Local = SelectCalibration.CenterPoint.Local,
  241. Tool = SelectCalibration.CenterPoint.Tool
  242. });
  243. rPoints.Add(new RPoint()
  244. {
  245. Number = 5,
  246. X = (float)P5[0],
  247. Y = (float)P5[1],
  248. Z = SelectCalibration.CenterPoint.Z,
  249. U = SelectCalibration.CenterPoint.U,
  250. V = SelectCalibration.CenterPoint.V,
  251. W = SelectCalibration.CenterPoint.W,
  252. Hand = SelectCalibration.CenterPoint.Hand,
  253. Local = SelectCalibration.CenterPoint.Local,
  254. Tool = SelectCalibration.CenterPoint.Tool
  255. });
  256. rPoints.Add(new RPoint()
  257. {
  258. Number = 6,
  259. X = (float)P6[0],
  260. Y = (float)P6[1],
  261. Z = SelectCalibration.CenterPoint.Z,
  262. U = SelectCalibration.CenterPoint.U,
  263. V = SelectCalibration.CenterPoint.V,
  264. W = SelectCalibration.CenterPoint.W,
  265. Hand = SelectCalibration.CenterPoint.Hand,
  266. Local = SelectCalibration.CenterPoint.Local,
  267. Tool = SelectCalibration.CenterPoint.Tool
  268. });
  269. rPoints.Add(new RPoint()
  270. {
  271. Number = 7,
  272. X = (float)P7[0],
  273. Y = (float)P7[1],
  274. Z = SelectCalibration.CenterPoint.Z,
  275. U = SelectCalibration.CenterPoint.U,
  276. V = SelectCalibration.CenterPoint.V,
  277. W = SelectCalibration.CenterPoint.W,
  278. Hand = SelectCalibration.CenterPoint.Hand,
  279. Local = SelectCalibration.CenterPoint.Local,
  280. Tool = SelectCalibration.CenterPoint.Tool
  281. });
  282. rPoints.Add(new RPoint()
  283. {
  284. Number = 8,
  285. X = (float)P8[0],
  286. Y = (float)P8[1],
  287. Z = SelectCalibration.CenterPoint.Z,
  288. U = SelectCalibration.CenterPoint.U,
  289. V = SelectCalibration.CenterPoint.V,
  290. W = SelectCalibration.CenterPoint.W,
  291. Hand = SelectCalibration.CenterPoint.Hand,
  292. Local = SelectCalibration.CenterPoint.Local,
  293. Tool = SelectCalibration.CenterPoint.Tool
  294. });
  295. rPoints.Add(new RPoint()
  296. {
  297. Number = 9,
  298. X = (float)P9[0],
  299. Y = (float)P9[1],
  300. Z = SelectCalibration.CenterPoint.Z,
  301. U = SelectCalibration.CenterPoint.U,
  302. V = SelectCalibration.CenterPoint.V,
  303. W = SelectCalibration.CenterPoint.W,
  304. Hand = SelectCalibration.CenterPoint.Hand,
  305. Local = SelectCalibration.CenterPoint.Local,
  306. Tool = SelectCalibration.CenterPoint.Tool
  307. });
  308. List<PointF> PixelPoslist = new List<PointF>();
  309. if (SelectCalibration.CalibPoints == null)
  310. {
  311. SelectCalibration.CalibPoints = new System.Collections.ObjectModel.ObservableCollection<RobotPixelPoint>();
  312. }
  313. else
  314. {
  315. SelectCalibration.CalibPoints.Clear();
  316. }
  317. //移动相机标定时,需要先记住特征点的绝对坐标(相对于机器人坐标系) P0
  318. if (SelectCalibration.CameraMount == Enums.CameraMount.MobileJ4)
  319. {
  320. await TechP0();
  321. }
  322. for (int i = 0; i < rPoints.Count; i++)
  323. {
  324. //移动机器人至i
  325. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  326. {
  327. await Robot.CalibMotionAsync(rPoints[i], 0);
  328. //关闭吸气
  329. await Robot.CalibOutIOAsync(false);
  330. //移动机器人至待机位置
  331. await Robot.CalibMotionAsync(SelectCalibration.HomePoint, 0);
  332. }
  333. else
  334. {
  335. await Robot.CalibMotionAsync(rPoints[i], rPoints[i].Z);
  336. }
  337. PixelPoslist.Add(Photo());
  338. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  339. {
  340. //移动机器人至i
  341. await Robot.CalibMotionAsync(rPoints[i], 0);
  342. //打开吸气
  343. await Robot.CalibOutIOAsync(true);
  344. }
  345. SelectCalibration.CalibPoints.Add(new RobotPixelPoint()
  346. {
  347. Number = i + 1,
  348. Robot = new System.Drawing.PointF(rPoints[i].X, rPoints[i].Y),
  349. Pixel = new System.Drawing.PointF(PixelPoslist[i].X, PixelPoslist[i].Y)
  350. }); ; ;
  351. }
  352. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  353. {
  354. //移动机器人至待机位置
  355. await Robot.CalibMotionAsync(SelectCalibration.HomePoint, 0);
  356. }
  357. Mat matPixel = new Mat(9, 2, MatType.CV_64F);
  358. Mat matRobot = new Mat(9, 2, MatType.CV_64F);
  359. for (int i = 0; i < 9; i++)
  360. {
  361. matPixel.Set<double>(i, 0, PixelPoslist[i].X);
  362. matPixel.Set<double>(i, 1, PixelPoslist[i].Y);
  363. //如果是移动相机拍照需要将机器人的9点进行一个变换,为了达到Mark点动,相机不动的目的
  364. if (SelectCalibration.CameraMount == Enums.CameraMount.MobileJ4)
  365. {
  366. //计算每个点相对于第一个点移动的偏移量,将这个偏移量
  367. double ox = rPoints[i].X - rPoints[0].X;
  368. double oy = rPoints[i].Y - rPoints[0].Y;
  369. matRobot.Set<double>(i, 0, SelectCalibration.MarkPoint.X - ox);
  370. matRobot.Set<double>(i, 1, SelectCalibration.MarkPoint.Y - oy);
  371. }
  372. else if (SelectCalibration.CameraMount == Enums.CameraMount.MobileDown_XYPlatform)
  373. {
  374. //计算每个点相对于第一个点移动的偏移量,将这个偏移量
  375. double ox = rPoints[i].X - rPoints[0].X;
  376. double oy = rPoints[i].Y - rPoints[0].Y;
  377. matRobot.Set<double>(i, 0, SelectCalibration.MarkPoint.X - ox);
  378. matRobot.Set<double>(i, 1, SelectCalibration.MarkPoint.Y - oy);
  379. }
  380. else
  381. {
  382. //将tool 0下的坐标转换成Tool n下的坐标
  383. if (SelectCalibration.Tool != null)
  384. {
  385. ToolCoord toolCoord = new ToolCoord();
  386. toolCoord.SetTool(SelectCalibration.Tool.X, SelectCalibration.Tool.Y);
  387. double u1 = rPoints[i].U;
  388. if (Robot.Brand == Enums.RobotBrand.Schneider)
  389. {
  390. u1 *= -1;
  391. }
  392. var trans = toolCoord.GetToolnCoord(Vector<double>.Build.Dense(new double[] { rPoints[i].X, rPoints[i].Y }), u1);
  393. matRobot.Set<double>(i, 0, trans[0]);
  394. matRobot.Set<double>(i, 1, trans[1]);
  395. }
  396. else
  397. {
  398. matRobot.Set<double>(i, 0, rPoints[i].X);
  399. matRobot.Set<double>(i, 1, rPoints[i].Y);
  400. }
  401. }
  402. }
  403. Mat mat2d = Cv2.EstimateAffine2D(matPixel, matRobot);
  404. double[,] vv = { { mat2d.Get<double>(0, 0), mat2d.Get<double>(0, 1), mat2d.Get<double>(0, 2) }, { mat2d.Get<double>(1, 0), mat2d.Get<double>(1, 1), mat2d.Get<double>(1, 2) } };
  405. Matrix<double> matrix = Matrix<double>.Build.DenseOfArray(vv);
  406. SelectCalibration.AffineTransformationMaterial = matrix.ToArray();
  407. //mat2d.Get<double>(0, 0):表示x方向上的缩放。大于 1,则表示进行了放大操作;小于 1,则表示进行了缩小操作;等于 1,则表示没有进行缩放操作。
  408. //mat2d.Get<double>(0, 1):表示垂直错切参数,与M[1,0]一起用于计算旋转角度
  409. //mat2d.Get<double>(0, 2):表示x方向上的平移。
  410. //mat2d.Get<double>(1, 0):表示水平错切参数,与M[0,1]一起用于计算旋转角度。
  411. //mat2d.Get<double>(1, 1):表示y方向上的缩放。大于 1,则表示进行了放大操作;小于 1,则表示进行了缩小操作;等于 1,则表示没有进行缩放操作。
  412. //mat2d.Get<double>(1, 2):表示y方向上的平移。
  413. //double RotationAngle= Math.Atan2(mat2d.Get<double>(1, 0), mat2d.Get<double>(0, 1))*180/Math.PI; //旋转角度
  414. //RMS(均方根值) 标定偏差
  415. double sumX = 0, sumY = 0;
  416. List<double> differenceX = new List<double>();
  417. List<double> differenceY = new List<double>();
  418. for (int i = 0; i < 9; i++)
  419. {
  420. var pixel = Vector<double>.Build.Dense(new double[] { PixelPoslist[i].X, PixelPoslist[i].Y, 1 });
  421. if (SelectCalibration.CameraMount == Enums.CameraMount.MobileJ4)
  422. {
  423. //计算每个点相对于第一个点移动的偏移量,Mark点减去这个偏移量及是图像中对应的实际坐标
  424. double ox = rPoints[i].X - rPoints[0].X;
  425. double oy = rPoints[i].Y - rPoints[0].Y;
  426. differenceX.Add((SelectCalibration.MarkPoint.X - ox) - (matrix * pixel)[0]);
  427. differenceY.Add((SelectCalibration.MarkPoint.Y - oy) - (matrix * pixel)[1]);
  428. }
  429. else if (SelectCalibration.CameraMount == Enums.CameraMount.MobileDown_XYPlatform)
  430. {
  431. double ox = rPoints[i].X - rPoints[0].X;
  432. double oy = rPoints[i].Y - rPoints[0].Y;
  433. differenceX.Add((SelectCalibration.MarkPoint.X - ox) - (matrix * pixel)[0]);
  434. differenceY.Add((SelectCalibration.MarkPoint.Y - oy) - (matrix * pixel)[1]);
  435. }
  436. else
  437. {
  438. //将tool 0下的坐标转换成Tool n下的坐标
  439. if (SelectCalibration.Tool != null)
  440. {
  441. ToolCoord toolCoord = new ToolCoord();
  442. toolCoord.SetTool(SelectCalibration.Tool.X, SelectCalibration.Tool.Y);
  443. double u1 = rPoints[i].U;
  444. if (Robot.Brand == Enums.RobotBrand.Schneider)
  445. {
  446. u1 *= -1;
  447. }
  448. var trans = toolCoord.GetToolnCoord(Vector<double>.Build.Dense(new double[] { rPoints[i].X, rPoints[i].Y }), u1);
  449. differenceX.Add(trans[0] - (matrix * pixel)[0]);
  450. differenceY.Add(trans[1] - (matrix * pixel)[1]);
  451. }
  452. else
  453. {
  454. differenceX.Add(rPoints[i].X - (matrix * pixel)[0]);
  455. differenceY.Add(rPoints[i].Y - (matrix * pixel)[1]);
  456. }
  457. }
  458. sumX += Math.Pow(differenceX[differenceX.Count - 1], 2);
  459. sumY += Math.Pow(differenceY[differenceY.Count - 1], 2);
  460. }
  461. double rmsX, rmsY;
  462. rmsX = Math.Sqrt(sumX / 9);
  463. rmsY = Math.Sqrt(sumY / 9);
  464. CalibrationResult calibrationResult = new CalibrationResult();
  465. calibrationResult.ScaleX = mat2d.Get<double>(0, 0);
  466. calibrationResult.ScaleY = mat2d.Get<double>(1, 1);
  467. calibrationResult.TranslationX = mat2d.Get<double>(0, 2);
  468. calibrationResult.TranslationY = mat2d.Get<double>(1, 2);
  469. calibrationResult.Rotate = Math.Atan2(mat2d.Get<double>(1, 0), mat2d.Get<double>(0, 1)) * 180 / Math.PI;
  470. calibrationResult.RMSX = rmsX;
  471. calibrationResult.RMSY = rmsY;
  472. calibrationResult.MaxErrorValueX = differenceX.Max();
  473. calibrationResult.MaxErrorValueY = differenceY.Max();
  474. SelectCalibration.CalibrationResult = calibrationResult;
  475. DialogParameters parameters = new DialogParameters();
  476. parameters.Add("Result", calibrationResult);
  477. _dialogService.Show("ShowCalibrationResult", parameters, rst =>
  478. {
  479. //对话框关闭之后的回调函数,可以在这解析结果。
  480. ButtonResult result1 = rst.Result;
  481. if (result1 == ButtonResult.OK)
  482. {
  483. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationVerification");
  484. }
  485. });
  486. //MessageBox.Show($"校准完成:RmsX:{rmsX}-RmsY:{rmsY}");
  487. }
  488. catch (Exception ex)
  489. {
  490. LogHelper.WriteLogError("9点校准时出错!", ex);
  491. MessageBox.Show(ex.Message);
  492. }
  493. finally
  494. {
  495. IsRunning = true;
  496. }
  497. }
  498. /// <summary>
  499. /// 记录P0点(移动相机校准时有效)
  500. /// </summary>
  501. async Task TechP0()
  502. {
  503. //移动机器人至中心位置
  504. await Robot.CalibMotionAsync(SelectCalibration.CenterPoint, 0);
  505. //将当前机器人Tool 0下的坐标转换成工具坐标下的坐标,并记录下来
  506. ToolCoord toolCoord = new ToolCoord();
  507. toolCoord.SetTool(SelectCalibration.Tool.X, SelectCalibration.Tool.Y);
  508. Vector<double> p = Vector<double>.Build.Dense(new double[] { SelectCalibration.CenterPoint.X, SelectCalibration.CenterPoint.Y }); ;
  509. var pn = toolCoord.GetToolnCoord(p, SelectCalibration.CenterPoint.U);
  510. if (SelectCalibration.MarkPoint == null)
  511. {
  512. SelectCalibration.MarkPoint = new RPoint();
  513. }
  514. SelectCalibration.MarkPoint = SelectCalibration.CenterPoint.Clone();
  515. SelectCalibration.MarkPoint.X = (float)pn[0];
  516. SelectCalibration.MarkPoint.Y = (float)pn[1];
  517. }
  518. /// <summary>
  519. /// 下一步
  520. /// </summary>
  521. void ExecuteNextCommand()
  522. {
  523. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationVerification");
  524. }
  525. /// <summary>
  526. /// 上一步
  527. /// </summary>
  528. void ExecuteBackCommand()
  529. {
  530. if (SelectCalibration.CameraMount == Enums.CameraMount.MobileDown_XYPlatform)
  531. {
  532. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationTeachCenterPoint");
  533. }
  534. else
  535. {
  536. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationSelectTool");
  537. }
  538. }
  539. /// <summary>
  540. /// 相机拍照
  541. /// </summary>
  542. /// <returns></returns>
  543. /// <exception cref="Exception"></exception>
  544. private PointF Photo()
  545. {
  546. if (Feeder != null)
  547. {
  548. Feeder.OpenLight();
  549. Feeder.SetLightTimeOut((ushort)SelectCalibration.Brightness);
  550. }
  551. Task.Delay(200).Wait();
  552. //采集图像
  553. if (Camera != null)
  554. {
  555. SelectCalibration.ToolBlock.Inputs["InputImage"].Value = Camera.Grab();
  556. Image = (ICogImage)SelectCalibration.ToolBlock.Inputs["InputImage"].Value;
  557. }
  558. Graphic = null;
  559. SelectCalibration.ToolBlock.Run();
  560. CogToolBlockTerminalCollection outputCollection = SelectCalibration.ToolBlock.Outputs;
  561. Graphic = outputCollection["Graphic"].Value as CogGraphicCollection;
  562. bool Found = (bool)(outputCollection["Found"].Value);
  563. if (Found)
  564. {
  565. var strpoints = outputCollection["Point"].Value.ToString();
  566. string[] items = strpoints.Split(',');
  567. return new PointF(float.Parse(items[0]), float.Parse(items[1]));
  568. }
  569. else
  570. {
  571. throw new Exception(Lang.拍照失败);
  572. }
  573. }
  574. /// <summary>
  575. /// 自动示教P0点
  576. /// </summary>
  577. /// <returns></returns>
  578. private async Task<bool> AutoTechP0(Enums.CalibTechP0Mode calibTechP0)
  579. {
  580. // 移动机器人至中心位置
  581. await Robot.CalibMotionAsync(SelectCalibration.CenterPoint, 0);
  582. //计算相机与机器人坐标系的旋转矩阵
  583. var rst = await MoveRobotThreePointComputCroodTrans(SelectCalibration.CenterPoint, ((float)SelectCalibration.Height) / 3);
  584. SelectCalibration.RobotCameraRotationMatrix = rst.Item1.ToArray();
  585. SelectCalibration.PixelScaleX = rst.Item2;
  586. SelectCalibration.PixelScaleY = rst.Item3;
  587. if (calibTechP0== Enums.CalibTechP0Mode.TechP0)
  588. {
  589. return true;
  590. }
  591. var imagecenter = Camera.Grab();
  592. PointF P0 = new PointF((float)(imagecenter.Width / 2), (float)(imagecenter.Height / 2)); //中心点像素坐标
  593. //先找相机中心点,校准中心点坐标
  594. bool IsFinish = false;
  595. var curPixel = Photo();
  596. var curpos = Robot.GetRobotPos();
  597. for (int i = 0; i < 10; i++)
  598. {
  599. //计算需要移动的偏移量,目的是逼近中心点
  600. double offsetx = (P0.X - curPixel.X) * rst.Item2;
  601. double offsety = (P0.Y - curPixel.Y) * rst.Item3;
  602. var pos = rst.Item1.Inverse() * Vector<double>.Build.Dense(new double[] { offsetx, offsety });
  603. curpos.X += (float)pos[0];
  604. curpos.Y += (float)pos[1];
  605. //机器人移动
  606. await Robot.CalibMotionAsync(curpos, curpos.Z);
  607. curPixel = Photo();
  608. offsetx = P0.X - curPixel.X;
  609. offsety = P0.Y - curPixel.Y;
  610. //await Console.Out.WriteLineAsync($"移动后的差异:{offsetx},{offsety}");
  611. if (Math.Abs(offsetx) < 1 && Math.Abs(offsetx) < 1)
  612. {
  613. IsFinish = true;
  614. break;
  615. }
  616. }
  617. if (IsFinish == true)
  618. {
  619. SelectCalibration.CenterPoint = Robot.GetRobotPos();
  620. SelectCalibration.MarkPoint= SelectCalibration.CenterPoint.Clone();
  621. return true;
  622. }
  623. else
  624. {
  625. return false;
  626. }
  627. }
  628. /// <summary>
  629. /// 移动机器人3个点计算旋转矩阵
  630. /// </summary>
  631. /// <returns>(旋转矩阵,PixelScaleX,PixelScaleY)</returns>
  632. private async Task<Tuple<Matrix<double>, double, double>> MoveRobotThreePointComputCroodTrans(RPoint rPoint, float distance)
  633. {
  634. //-------------------------------------------P0-----------------------------------
  635. //移动机器人至中心点
  636. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  637. {
  638. await Robot.CalibMotionAsync(rPoint, 0);
  639. //关闭吸气
  640. await Robot.CalibOutIOAsync(false);
  641. }
  642. else
  643. {
  644. await Robot.CalibMotionAsync(rPoint, rPoint.Z);
  645. }
  646. RPoint RobotP0 = await Robot.GetRobotPosAsync();
  647. RPoint RobotP1 = RobotP0.Clone();
  648. RPoint RobotP2 = RobotP0.Clone();
  649. RobotP1.X += distance;
  650. RobotP2.Y += distance;
  651. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  652. {
  653. //移动机器人至待机位置
  654. await Robot.CalibMotionAsync(SelectCalibration.HomePoint, 0);
  655. }
  656. var P0 = Photo();
  657. //-------------------------------------------P1-----------------------------------
  658. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  659. {
  660. //移动机器人至P0
  661. await Robot.CalibMotionAsync(RobotP0, 0);
  662. //打开吸气
  663. await Robot.CalibOutIOAsync(true);
  664. //移动机器人至P1
  665. await Robot.CalibMotionAsync(RobotP1, 0);
  666. //关闭吸气
  667. await Robot.CalibOutIOAsync(false);
  668. //移动机器人至待机位置
  669. await Robot.CalibMotionAsync(SelectCalibration.HomePoint, 0);
  670. }
  671. else
  672. {
  673. //移动机器人至P1
  674. await Robot.CalibMotionAsync(RobotP1, RobotP1.Z);
  675. }
  676. var P1 = Photo();
  677. //-------------------------------------------P2-----------------------------------
  678. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  679. {
  680. //移动机器人至P1
  681. await Robot.CalibMotionAsync(RobotP1, 0);
  682. //打开吸气
  683. await Robot.CalibOutIOAsync(true);
  684. //移动机器人至P2
  685. await Robot.CalibMotionAsync(RobotP2, 0);
  686. //关闭吸气
  687. await Robot.CalibOutIOAsync(false);
  688. //移动机器人至待机位置
  689. await Robot.CalibMotionAsync(SelectCalibration.HomePoint, 0);
  690. }
  691. else
  692. {
  693. //移动机器人至P2
  694. await Robot.CalibMotionAsync(RobotP2, RobotP2.Z);
  695. }
  696. var P2 = Photo();
  697. //-------------------------------------------P0--------------------------------------
  698. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  699. {
  700. //移动机器人至P2
  701. await Robot.CalibMotionAsync(RobotP2, 0);
  702. //打开吸气
  703. await Robot.CalibOutIOAsync(true);
  704. //移动机器人至P0
  705. await Robot.CalibMotionAsync(RobotP0, 0);
  706. //关闭吸气
  707. await Robot.CalibOutIOAsync(false);
  708. //移动机器人至待机位置
  709. await Robot.CalibMotionAsync(SelectCalibration.HomePoint, 0);
  710. }
  711. else
  712. {
  713. //移动机器人至P0
  714. await Robot.CalibMotionAsync(RobotP0, RobotP0.Z);
  715. }
  716. //-----------------------------------------计算旋转矩阵------------------------------
  717. Matrix<double> M = ComputRotationMatrix(P0, P1, P2);
  718. //Theta = Acos(M11)
  719. //相机与机器人坐标系角度:Theta * 180 / PI
  720. //-----------------------------------------计算像素和毫米比例-----------------------
  721. Vector<double> P11 = Vector<double>.Build.Dense(new double[] { distance, 0 });
  722. Vector<double> P21 = Vector<double>.Build.Dense(new double[] { P1.X, P1.Y }) - Vector<double>.Build.Dense(new double[] { P0.X, P0.Y });
  723. double PixelScaleX = Math.Abs((M * P11)[0] / P21[0]); //mm/pixel
  724. double PixelScaleY = Math.Abs((M * P11)[1] / P21[1]); //mm/pixel
  725. return new Tuple<Matrix<double>, double, double>(M, PixelScaleX, PixelScaleY);
  726. }
  727. /// <summary>
  728. /// 计算旋转矩阵
  729. /// </summary>
  730. /// <param name="P0"></param>
  731. /// <param name="P1"></param>
  732. /// <param name="P2"></param>
  733. /// <returns></returns>
  734. private Matrix<double> ComputRotationMatrix(PointF P0, PointF P1, PointF P2)
  735. {
  736. Vector<double> Point0 = Vector<double>.Build.Dense(new double[] { (double)P0.X, (double)P0.Y });
  737. Vector<double> Point1 = Vector<double>.Build.Dense(new double[] { (double)P1.X, (double)P1.Y });
  738. Vector<double> Point2 = Vector<double>.Build.Dense(new double[] { (double)P2.X, (double)P2.Y });
  739. //2.计算坐标系的基向量:
  740. Vector<double> E1 = (Point1 - Point0) / (Point1 - Point0).L2Norm();
  741. Vector<double> E2 = (Point2 - Point0 - E1.DotProduct(Point2 - Point0) * E1) / (Point2 - Point0 - E1.DotProduct(Point2 - Point0) * E1).L2Norm();
  742. //3.计算旋转矩阵:我们可以使用基向量组成的矩阵作为旋转矩阵。具体地,我们可以将基向量 e1 和 e2 分别作为新坐标系下的 $x$ 和 $y$ 轴,然后组成一个矩阵,再对这个矩阵求逆即可得到旋转矩阵。代码实现:
  743. // Compute rotation matrix
  744. Matrix<double> R = Matrix<double>.Build.DenseOfColumnVectors(E1, E2);
  745. return R;
  746. }
  747. #endregion
  748. private bool _IsAllowEdit = false;
  749. public bool IsAllowEdit
  750. {
  751. get { return _IsAllowEdit; }
  752. set { SetProperty(ref _IsAllowEdit, value); }
  753. }
  754. private void LoginChange(Models.User user)
  755. {
  756. if (user.userPart == Enums.UserPart.Operator)
  757. {
  758. IsAllowEdit = false;
  759. }
  760. else
  761. {
  762. IsAllowEdit = true;
  763. }
  764. }
  765. }
  766. }