CalibrationAutoViewModel.cs 39 KB

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