CalibrationAutoViewModel.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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. else if (Robot.Brand== Enums.RobotBrand.XYZ_Platform)
  167. {
  168. var res = await AutoTechP0(SelectCalibration.CalibTechP0Mode);
  169. if (!res)
  170. {
  171. MessageBox.Show(Lang.自动示教P0失败);
  172. return;
  173. }
  174. }
  175. /// <summary>
  176. /// 机器人与相机之间的旋转矩阵
  177. /// </summary>
  178. Matrix<double> RobotCameraRotationMatrix = Matrix<double>.Build.DenseOfArray(SelectCalibration.RobotCameraRotationMatrix);
  179. //1
  180. P1 = RobotCameraRotationMatrix.Inverse() * P1 + P0;
  181. //2
  182. P2 = RobotCameraRotationMatrix.Inverse() * P2 + P0;
  183. //3
  184. P3 = RobotCameraRotationMatrix.Inverse() * P3 + P0;
  185. //4
  186. P4 = RobotCameraRotationMatrix.Inverse() * P4 + P0;
  187. //5
  188. P5 = RobotCameraRotationMatrix.Inverse() * P5 + P0;
  189. //6
  190. P6 = RobotCameraRotationMatrix.Inverse() * P6 + P0;
  191. //7
  192. P7 = RobotCameraRotationMatrix.Inverse() * P7 + P0;
  193. //8
  194. P8 = RobotCameraRotationMatrix.Inverse() * P8 + P0;
  195. //9
  196. P9 = RobotCameraRotationMatrix.Inverse() * P9 + P0;
  197. List<RPoint> rPoints = new List<RPoint>();
  198. rPoints.Add(new RPoint()
  199. {
  200. Number = 1,
  201. X = (float)P1[0],
  202. Y = (float)P1[1],
  203. Z = SelectCalibration.CenterPoint.Z,
  204. U = SelectCalibration.CenterPoint.U,
  205. V = SelectCalibration.CenterPoint.V,
  206. W = SelectCalibration.CenterPoint.W,
  207. Hand = SelectCalibration.CenterPoint.Hand,
  208. Local = SelectCalibration.CenterPoint.Local,
  209. Tool = SelectCalibration.CenterPoint.Tool
  210. });
  211. rPoints.Add(new RPoint()
  212. {
  213. Number = 2,
  214. X = (float)P2[0],
  215. Y = (float)P2[1],
  216. Z = SelectCalibration.CenterPoint.Z,
  217. U = SelectCalibration.CenterPoint.U,
  218. V = SelectCalibration.CenterPoint.V,
  219. W = SelectCalibration.CenterPoint.W,
  220. Hand = SelectCalibration.CenterPoint.Hand,
  221. Local = SelectCalibration.CenterPoint.Local,
  222. Tool = SelectCalibration.CenterPoint.Tool
  223. });
  224. rPoints.Add(new RPoint()
  225. {
  226. Number = 3,
  227. X = (float)P3[0],
  228. Y = (float)P3[1],
  229. Z = SelectCalibration.CenterPoint.Z,
  230. U = SelectCalibration.CenterPoint.U,
  231. V = SelectCalibration.CenterPoint.V,
  232. W = SelectCalibration.CenterPoint.W,
  233. Hand = SelectCalibration.CenterPoint.Hand,
  234. Local = SelectCalibration.CenterPoint.Local,
  235. Tool = SelectCalibration.CenterPoint.Tool
  236. });
  237. rPoints.Add(new RPoint()
  238. {
  239. Number = 4,
  240. X = (float)P4[0],
  241. Y = (float)P4[1],
  242. Z = SelectCalibration.CenterPoint.Z,
  243. U = SelectCalibration.CenterPoint.U,
  244. V = SelectCalibration.CenterPoint.V,
  245. W = SelectCalibration.CenterPoint.W,
  246. Hand = SelectCalibration.CenterPoint.Hand,
  247. Local = SelectCalibration.CenterPoint.Local,
  248. Tool = SelectCalibration.CenterPoint.Tool
  249. });
  250. rPoints.Add(new RPoint()
  251. {
  252. Number = 5,
  253. X = (float)P5[0],
  254. Y = (float)P5[1],
  255. Z = SelectCalibration.CenterPoint.Z,
  256. U = SelectCalibration.CenterPoint.U,
  257. V = SelectCalibration.CenterPoint.V,
  258. W = SelectCalibration.CenterPoint.W,
  259. Hand = SelectCalibration.CenterPoint.Hand,
  260. Local = SelectCalibration.CenterPoint.Local,
  261. Tool = SelectCalibration.CenterPoint.Tool
  262. });
  263. rPoints.Add(new RPoint()
  264. {
  265. Number = 6,
  266. X = (float)P6[0],
  267. Y = (float)P6[1],
  268. Z = SelectCalibration.CenterPoint.Z,
  269. U = SelectCalibration.CenterPoint.U,
  270. V = SelectCalibration.CenterPoint.V,
  271. W = SelectCalibration.CenterPoint.W,
  272. Hand = SelectCalibration.CenterPoint.Hand,
  273. Local = SelectCalibration.CenterPoint.Local,
  274. Tool = SelectCalibration.CenterPoint.Tool
  275. });
  276. rPoints.Add(new RPoint()
  277. {
  278. Number = 7,
  279. X = (float)P7[0],
  280. Y = (float)P7[1],
  281. Z = SelectCalibration.CenterPoint.Z,
  282. U = SelectCalibration.CenterPoint.U,
  283. V = SelectCalibration.CenterPoint.V,
  284. W = SelectCalibration.CenterPoint.W,
  285. Hand = SelectCalibration.CenterPoint.Hand,
  286. Local = SelectCalibration.CenterPoint.Local,
  287. Tool = SelectCalibration.CenterPoint.Tool
  288. });
  289. rPoints.Add(new RPoint()
  290. {
  291. Number = 8,
  292. X = (float)P8[0],
  293. Y = (float)P8[1],
  294. Z = SelectCalibration.CenterPoint.Z,
  295. U = SelectCalibration.CenterPoint.U,
  296. V = SelectCalibration.CenterPoint.V,
  297. W = SelectCalibration.CenterPoint.W,
  298. Hand = SelectCalibration.CenterPoint.Hand,
  299. Local = SelectCalibration.CenterPoint.Local,
  300. Tool = SelectCalibration.CenterPoint.Tool
  301. });
  302. rPoints.Add(new RPoint()
  303. {
  304. Number = 9,
  305. X = (float)P9[0],
  306. Y = (float)P9[1],
  307. Z = SelectCalibration.CenterPoint.Z,
  308. U = SelectCalibration.CenterPoint.U,
  309. V = SelectCalibration.CenterPoint.V,
  310. W = SelectCalibration.CenterPoint.W,
  311. Hand = SelectCalibration.CenterPoint.Hand,
  312. Local = SelectCalibration.CenterPoint.Local,
  313. Tool = SelectCalibration.CenterPoint.Tool
  314. });
  315. List<PointF> PixelPoslist = new List<PointF>();
  316. if (SelectCalibration.CalibPoints == null)
  317. {
  318. SelectCalibration.CalibPoints = new System.Collections.ObjectModel.ObservableCollection<RobotPixelPoint>();
  319. }
  320. else
  321. {
  322. SelectCalibration.CalibPoints.Clear();
  323. }
  324. //移动相机标定时,需要先记住特征点的绝对坐标(相对于机器人坐标系) P0
  325. if (SelectCalibration.CameraMount == Enums.CameraMount.MobileJ4)
  326. {
  327. await TechP0();
  328. }
  329. for (int i = 0; i < rPoints.Count; i++)
  330. {
  331. //移动机器人至i
  332. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  333. {
  334. await Robot.CalibMotionAsync(rPoints[i], 0);
  335. //关闭吸气
  336. await Robot.CalibOutIOAsync(false);
  337. //移动机器人至待机位置
  338. await Robot.CalibMotionAsync(SelectCalibration.HomePoint, 0);
  339. }
  340. else
  341. {
  342. await Robot.CalibMotionAsync(rPoints[i], rPoints[i].Z);
  343. }
  344. PixelPoslist.Add(Photo());
  345. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  346. {
  347. //移动机器人至i
  348. await Robot.CalibMotionAsync(rPoints[i], 0);
  349. //打开吸气
  350. await Robot.CalibOutIOAsync(true);
  351. }
  352. SelectCalibration.CalibPoints.Add(new RobotPixelPoint()
  353. {
  354. Number = i + 1,
  355. Robot = new System.Drawing.PointF(rPoints[i].X, rPoints[i].Y),
  356. Pixel = new System.Drawing.PointF(PixelPoslist[i].X, PixelPoslist[i].Y)
  357. }); ; ;
  358. }
  359. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  360. {
  361. //移动机器人至待机位置
  362. await Robot.CalibMotionAsync(SelectCalibration.HomePoint, 0);
  363. }
  364. Mat matPixel = new Mat(9, 2, MatType.CV_64F);
  365. Mat matRobot = new Mat(9, 2, MatType.CV_64F);
  366. for (int i = 0; i < 9; i++)
  367. {
  368. matPixel.Set<double>(i, 0, PixelPoslist[i].X);
  369. matPixel.Set<double>(i, 1, PixelPoslist[i].Y);
  370. //如果是移动相机拍照需要将机器人的9点进行一个变换,为了达到Mark点动,相机不动的目的
  371. if (SelectCalibration.CameraMount == Enums.CameraMount.MobileJ4)
  372. {
  373. //计算每个点相对于第一个点移动的偏移量,将这个偏移量
  374. double ox = rPoints[i].X - rPoints[0].X;
  375. double oy = rPoints[i].Y - rPoints[0].Y;
  376. matRobot.Set<double>(i, 0, SelectCalibration.MarkPoint.X - ox);
  377. matRobot.Set<double>(i, 1, SelectCalibration.MarkPoint.Y - oy);
  378. }
  379. else if (SelectCalibration.CameraMount == Enums.CameraMount.MobileDown_XYPlatform)
  380. {
  381. //计算每个点相对于第一个点移动的偏移量,将这个偏移量
  382. double ox = rPoints[i].X - rPoints[0].X;
  383. double oy = rPoints[i].Y - rPoints[0].Y;
  384. matRobot.Set<double>(i, 0, SelectCalibration.MarkPoint.X - ox);
  385. matRobot.Set<double>(i, 1, SelectCalibration.MarkPoint.Y - oy);
  386. }
  387. else
  388. {
  389. //将tool 0下的坐标转换成Tool n下的坐标
  390. if (SelectCalibration.Tool != null)
  391. {
  392. ToolCoord toolCoord = new ToolCoord();
  393. toolCoord.SetTool(SelectCalibration.Tool.X, SelectCalibration.Tool.Y);
  394. double u1 = rPoints[i].U;
  395. if (Robot.Brand == Enums.RobotBrand.Schneider)
  396. {
  397. u1 *= -1;
  398. }
  399. var trans = toolCoord.GetToolnCoord(Vector<double>.Build.Dense(new double[] { rPoints[i].X, rPoints[i].Y }), u1);
  400. matRobot.Set<double>(i, 0, trans[0]);
  401. matRobot.Set<double>(i, 1, trans[1]);
  402. }
  403. else
  404. {
  405. matRobot.Set<double>(i, 0, rPoints[i].X);
  406. matRobot.Set<double>(i, 1, rPoints[i].Y);
  407. }
  408. }
  409. }
  410. Mat mat2d = Cv2.EstimateAffine2D(matPixel, matRobot);
  411. 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) } };
  412. Matrix<double> matrix = Matrix<double>.Build.DenseOfArray(vv);
  413. SelectCalibration.AffineTransformationMaterial = matrix.ToArray();
  414. //mat2d.Get<double>(0, 0):表示x方向上的缩放。大于 1,则表示进行了放大操作;小于 1,则表示进行了缩小操作;等于 1,则表示没有进行缩放操作。
  415. //mat2d.Get<double>(0, 1):表示垂直错切参数,与M[1,0]一起用于计算旋转角度
  416. //mat2d.Get<double>(0, 2):表示x方向上的平移。
  417. //mat2d.Get<double>(1, 0):表示水平错切参数,与M[0,1]一起用于计算旋转角度。
  418. //mat2d.Get<double>(1, 1):表示y方向上的缩放。大于 1,则表示进行了放大操作;小于 1,则表示进行了缩小操作;等于 1,则表示没有进行缩放操作。
  419. //mat2d.Get<double>(1, 2):表示y方向上的平移。
  420. //double RotationAngle= Math.Atan2(mat2d.Get<double>(1, 0), mat2d.Get<double>(0, 1))*180/Math.PI; //旋转角度
  421. //RMS(均方根值) 标定偏差
  422. double sumX = 0, sumY = 0;
  423. List<double> differenceX = new List<double>();
  424. List<double> differenceY = new List<double>();
  425. for (int i = 0; i < 9; i++)
  426. {
  427. var pixel = Vector<double>.Build.Dense(new double[] { PixelPoslist[i].X, PixelPoslist[i].Y, 1 });
  428. if (SelectCalibration.CameraMount == Enums.CameraMount.MobileJ4)
  429. {
  430. //计算每个点相对于第一个点移动的偏移量,Mark点减去这个偏移量及是图像中对应的实际坐标
  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 if (SelectCalibration.CameraMount == Enums.CameraMount.MobileDown_XYPlatform)
  437. {
  438. double ox = rPoints[i].X - rPoints[0].X;
  439. double oy = rPoints[i].Y - rPoints[0].Y;
  440. differenceX.Add((SelectCalibration.MarkPoint.X - ox) - (matrix * pixel)[0]);
  441. differenceY.Add((SelectCalibration.MarkPoint.Y - oy) - (matrix * pixel)[1]);
  442. }
  443. else
  444. {
  445. //将tool 0下的坐标转换成Tool n下的坐标
  446. if (SelectCalibration.Tool != null)
  447. {
  448. ToolCoord toolCoord = new ToolCoord();
  449. toolCoord.SetTool(SelectCalibration.Tool.X, SelectCalibration.Tool.Y);
  450. double u1 = rPoints[i].U;
  451. if (Robot.Brand == Enums.RobotBrand.Schneider)
  452. {
  453. u1 *= -1;
  454. }
  455. var trans = toolCoord.GetToolnCoord(Vector<double>.Build.Dense(new double[] { rPoints[i].X, rPoints[i].Y }), u1);
  456. differenceX.Add(trans[0] - (matrix * pixel)[0]);
  457. differenceY.Add(trans[1] - (matrix * pixel)[1]);
  458. }
  459. else
  460. {
  461. differenceX.Add(rPoints[i].X - (matrix * pixel)[0]);
  462. differenceY.Add(rPoints[i].Y - (matrix * pixel)[1]);
  463. }
  464. }
  465. sumX += Math.Pow(differenceX[differenceX.Count - 1], 2);
  466. sumY += Math.Pow(differenceY[differenceY.Count - 1], 2);
  467. }
  468. double rmsX, rmsY;
  469. rmsX = Math.Sqrt(sumX / 9);
  470. rmsY = Math.Sqrt(sumY / 9);
  471. CalibrationResult calibrationResult = new CalibrationResult();
  472. calibrationResult.ScaleX = mat2d.Get<double>(0, 0);
  473. calibrationResult.ScaleY = mat2d.Get<double>(1, 1);
  474. calibrationResult.TranslationX = mat2d.Get<double>(0, 2);
  475. calibrationResult.TranslationY = mat2d.Get<double>(1, 2);
  476. calibrationResult.Rotate = Math.Atan2(mat2d.Get<double>(1, 0), mat2d.Get<double>(0, 1)) * 180 / Math.PI;
  477. calibrationResult.RMSX = rmsX;
  478. calibrationResult.RMSY = rmsY;
  479. calibrationResult.MaxErrorValueX = differenceX.Max();
  480. calibrationResult.MaxErrorValueY = differenceY.Max();
  481. SelectCalibration.CalibrationResult = calibrationResult;
  482. DialogParameters parameters = new DialogParameters();
  483. parameters.Add("Result", calibrationResult);
  484. _dialogService.Show("ShowCalibrationResult", parameters, rst =>
  485. {
  486. //对话框关闭之后的回调函数,可以在这解析结果。
  487. ButtonResult result1 = rst.Result;
  488. if (result1 == ButtonResult.OK)
  489. {
  490. NavigationParameters param = new NavigationParameters();
  491. param.Add("SelectCalibration", SelectCalibration);
  492. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationVerification",param);
  493. }
  494. });
  495. //MessageBox.Show($"校准完成:RmsX:{rmsX}-RmsY:{rmsY}");
  496. }
  497. catch (Exception ex)
  498. {
  499. LogHelper.WriteLogError("9点校准时出错!", ex);
  500. MessageBox.Show(ex.Message);
  501. }
  502. finally
  503. {
  504. IsRunning = true;
  505. }
  506. }
  507. /// <summary>
  508. /// 记录P0点(移动相机校准时有效)
  509. /// </summary>
  510. async Task TechP0()
  511. {
  512. //移动机器人至中心位置
  513. await Robot.CalibMotionAsync(SelectCalibration.CenterPoint, 0);
  514. //将当前机器人Tool 0下的坐标转换成工具坐标下的坐标,并记录下来
  515. ToolCoord toolCoord = new ToolCoord();
  516. toolCoord.SetTool(SelectCalibration.Tool.X, SelectCalibration.Tool.Y);
  517. Vector<double> p = Vector<double>.Build.Dense(new double[] { SelectCalibration.CenterPoint.X, SelectCalibration.CenterPoint.Y }); ;
  518. var pn = toolCoord.GetToolnCoord(p, SelectCalibration.CenterPoint.U);
  519. if (SelectCalibration.MarkPoint == null)
  520. {
  521. SelectCalibration.MarkPoint = new RPoint();
  522. }
  523. SelectCalibration.MarkPoint = SelectCalibration.CenterPoint.Clone();
  524. SelectCalibration.MarkPoint.X = (float)pn[0];
  525. SelectCalibration.MarkPoint.Y = (float)pn[1];
  526. }
  527. /// <summary>
  528. /// 下一步
  529. /// </summary>
  530. void ExecuteNextCommand()
  531. {
  532. NavigationParameters param = new NavigationParameters();
  533. param.Add("SelectCalibration", SelectCalibration);
  534. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationVerification", param);
  535. }
  536. /// <summary>
  537. /// 上一步
  538. /// </summary>
  539. void ExecuteBackCommand()
  540. {
  541. NavigationParameters param = new NavigationParameters();
  542. param.Add("SelectCalibration", SelectCalibration);
  543. if (SelectCalibration.CameraMount == Enums.CameraMount.MobileDown_XYPlatform)
  544. {
  545. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationTeachCenterPoint",param);
  546. }
  547. else if (Robot.Brand == Enums.RobotBrand.XYZ_Platform)
  548. {
  549. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationTeachCenterPoint", param);
  550. }
  551. else
  552. {
  553. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationSelectTool", param);
  554. }
  555. }
  556. /// <summary>
  557. /// 相机拍照
  558. /// </summary>
  559. /// <returns></returns>
  560. /// <exception cref="Exception"></exception>
  561. private PointF Photo()
  562. {
  563. if (Feeder != null)
  564. {
  565. Feeder.OpenLight();
  566. Feeder.SetLightTimeOut((ushort)SelectCalibration.Brightness);
  567. }
  568. Task.Delay(200).Wait();
  569. //采集图像
  570. if (Camera != null)
  571. {
  572. SelectCalibration.ToolBlock.Inputs["InputImage"].Value = Camera.Grab();
  573. Image = (ICogImage)SelectCalibration.ToolBlock.Inputs["InputImage"].Value;
  574. }
  575. Graphic = null;
  576. SelectCalibration.ToolBlock.Run();
  577. CogToolBlockTerminalCollection outputCollection = SelectCalibration.ToolBlock.Outputs;
  578. Graphic = outputCollection["Graphic"].Value as CogGraphicCollection;
  579. bool Found = (bool)(outputCollection["Found"].Value);
  580. if (Found)
  581. {
  582. var strpoints = outputCollection["Point"].Value.ToString();
  583. string[] items = strpoints.Split(',');
  584. return new PointF(float.Parse(items[0]), float.Parse(items[1]));
  585. }
  586. else
  587. {
  588. throw new Exception(Lang.拍照失败);
  589. }
  590. }
  591. /// <summary>
  592. /// 自动示教P0点
  593. /// </summary>
  594. /// <returns></returns>
  595. private async Task<bool> AutoTechP0(Enums.CalibTechP0Mode calibTechP0)
  596. {
  597. // 移动机器人至中心位置
  598. await Robot.CalibMotionAsync(SelectCalibration.CenterPoint, 0);
  599. //计算相机与机器人坐标系的旋转矩阵
  600. var rst = await MoveRobotThreePointComputCroodTrans(SelectCalibration.CenterPoint, ((float)SelectCalibration.Height) / 3);
  601. SelectCalibration.RobotCameraRotationMatrix = rst.Item1.ToArray();
  602. SelectCalibration.PixelScaleX = rst.Item2;
  603. SelectCalibration.PixelScaleY = rst.Item3;
  604. if (calibTechP0== Enums.CalibTechP0Mode.TechP0)
  605. {
  606. return true;
  607. }
  608. var imagecenter = Camera.Grab();
  609. PointF P0 = new PointF((float)(imagecenter.Width / 2), (float)(imagecenter.Height / 2)); //中心点像素坐标
  610. //先找相机中心点,校准中心点坐标
  611. bool IsFinish = false;
  612. var curPixel = Photo();
  613. var curpos = Robot.GetRobotPos();
  614. for (int i = 0; i < 10; i++)
  615. {
  616. //计算需要移动的偏移量,目的是逼近中心点
  617. double offsetx = (P0.X - curPixel.X) * rst.Item2;
  618. double offsety = (P0.Y - curPixel.Y) * rst.Item3;
  619. var pos = rst.Item1.Inverse() * Vector<double>.Build.Dense(new double[] { offsetx, offsety });
  620. curpos.X += (float)pos[0];
  621. curpos.Y += (float)pos[1];
  622. //机器人移动
  623. await Robot.CalibMotionAsync(curpos, curpos.Z);
  624. curPixel = Photo();
  625. offsetx = P0.X - curPixel.X;
  626. offsety = P0.Y - curPixel.Y;
  627. //await Console.Out.WriteLineAsync($"移动后的差异:{offsetx},{offsety}");
  628. if (Math.Abs(offsetx) < 1 && Math.Abs(offsetx) < 1)
  629. {
  630. IsFinish = true;
  631. break;
  632. }
  633. }
  634. if (IsFinish == true)
  635. {
  636. SelectCalibration.CenterPoint = Robot.GetRobotPos();
  637. SelectCalibration.MarkPoint= SelectCalibration.CenterPoint.Clone();
  638. return true;
  639. }
  640. else
  641. {
  642. return false;
  643. }
  644. }
  645. /// <summary>
  646. /// 移动机器人3个点计算旋转矩阵
  647. /// </summary>
  648. /// <returns>(旋转矩阵,PixelScaleX,PixelScaleY)</returns>
  649. private async Task<Tuple<Matrix<double>, double, double>> MoveRobotThreePointComputCroodTrans(RPoint rPoint, float distance)
  650. {
  651. //-------------------------------------------P0-----------------------------------
  652. //移动机器人至中心点
  653. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  654. {
  655. await Robot.CalibMotionAsync(rPoint, 0);
  656. //关闭吸气
  657. await Robot.CalibOutIOAsync(false);
  658. }
  659. else
  660. {
  661. await Robot.CalibMotionAsync(rPoint, rPoint.Z);
  662. }
  663. RPoint RobotP0 = await Robot.GetRobotPosAsync();
  664. RPoint RobotP1 = RobotP0.Clone();
  665. RPoint RobotP2 = RobotP0.Clone();
  666. RobotP1.X += distance;
  667. RobotP2.Y += distance;
  668. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  669. {
  670. //移动机器人至待机位置
  671. await Robot.CalibMotionAsync(SelectCalibration.HomePoint, 0);
  672. }
  673. var P0 = Photo();
  674. //-------------------------------------------P1-----------------------------------
  675. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  676. {
  677. //移动机器人至P0
  678. await Robot.CalibMotionAsync(RobotP0, 0);
  679. //打开吸气
  680. await Robot.CalibOutIOAsync(true);
  681. //移动机器人至P1
  682. await Robot.CalibMotionAsync(RobotP1, 0);
  683. //关闭吸气
  684. await Robot.CalibOutIOAsync(false);
  685. //移动机器人至待机位置
  686. await Robot.CalibMotionAsync(SelectCalibration.HomePoint, 0);
  687. }
  688. else
  689. {
  690. //移动机器人至P1
  691. await Robot.CalibMotionAsync(RobotP1, RobotP1.Z);
  692. }
  693. var P1 = Photo();
  694. //-------------------------------------------P2-----------------------------------
  695. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  696. {
  697. //移动机器人至P1
  698. await Robot.CalibMotionAsync(RobotP1, 0);
  699. //打开吸气
  700. await Robot.CalibOutIOAsync(true);
  701. //移动机器人至P2
  702. await Robot.CalibMotionAsync(RobotP2, 0);
  703. //关闭吸气
  704. await Robot.CalibOutIOAsync(false);
  705. //移动机器人至待机位置
  706. await Robot.CalibMotionAsync(SelectCalibration.HomePoint, 0);
  707. }
  708. else
  709. {
  710. //移动机器人至P2
  711. await Robot.CalibMotionAsync(RobotP2, RobotP2.Z);
  712. }
  713. var P2 = Photo();
  714. //-------------------------------------------P0--------------------------------------
  715. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  716. {
  717. //移动机器人至P2
  718. await Robot.CalibMotionAsync(RobotP2, 0);
  719. //打开吸气
  720. await Robot.CalibOutIOAsync(true);
  721. //移动机器人至P0
  722. await Robot.CalibMotionAsync(RobotP0, 0);
  723. //关闭吸气
  724. await Robot.CalibOutIOAsync(false);
  725. //移动机器人至待机位置
  726. await Robot.CalibMotionAsync(SelectCalibration.HomePoint, 0);
  727. }
  728. else
  729. {
  730. //移动机器人至P0
  731. await Robot.CalibMotionAsync(RobotP0, RobotP0.Z);
  732. }
  733. //-----------------------------------------计算旋转矩阵------------------------------
  734. Matrix<double> M = ComputRotationMatrix(P0, P1, P2);
  735. //Theta = Acos(M11)
  736. //相机与机器人坐标系角度:Theta * 180 / PI
  737. //-----------------------------------------计算像素和毫米比例-----------------------
  738. Vector<double> P11 = Vector<double>.Build.Dense(new double[] { distance, 0 });
  739. Vector<double> P21 = Vector<double>.Build.Dense(new double[] { P1.X, P1.Y }) - Vector<double>.Build.Dense(new double[] { P0.X, P0.Y });
  740. double PixelScaleX = Math.Abs((M * P11)[0] / P21[0]); //mm/pixel
  741. double PixelScaleY = Math.Abs((M * P11)[1] / P21[1]); //mm/pixel
  742. return new Tuple<Matrix<double>, double, double>(M, PixelScaleX, PixelScaleY);
  743. }
  744. /// <summary>
  745. /// 计算旋转矩阵
  746. /// </summary>
  747. /// <param name="P0"></param>
  748. /// <param name="P1"></param>
  749. /// <param name="P2"></param>
  750. /// <returns></returns>
  751. private Matrix<double> ComputRotationMatrix(PointF P0, PointF P1, PointF P2)
  752. {
  753. Vector<double> Point0 = Vector<double>.Build.Dense(new double[] { (double)P0.X, (double)P0.Y });
  754. Vector<double> Point1 = Vector<double>.Build.Dense(new double[] { (double)P1.X, (double)P1.Y });
  755. Vector<double> Point2 = Vector<double>.Build.Dense(new double[] { (double)P2.X, (double)P2.Y });
  756. //2.计算坐标系的基向量:
  757. Vector<double> E1 = (Point1 - Point0) / (Point1 - Point0).L2Norm();
  758. Vector<double> E2 = (Point2 - Point0 - E1.DotProduct(Point2 - Point0) * E1) / (Point2 - Point0 - E1.DotProduct(Point2 - Point0) * E1).L2Norm();
  759. //3.计算旋转矩阵:我们可以使用基向量组成的矩阵作为旋转矩阵。具体地,我们可以将基向量 e1 和 e2 分别作为新坐标系下的 $x$ 和 $y$ 轴,然后组成一个矩阵,再对这个矩阵求逆即可得到旋转矩阵。代码实现:
  760. // Compute rotation matrix
  761. Matrix<double> R = Matrix<double>.Build.DenseOfColumnVectors(E1, E2);
  762. return R;
  763. }
  764. #endregion
  765. #region 继承
  766. /// <summary>
  767. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  768. /// </summary>
  769. /// <param name="navigationContext"></param>
  770. /// <param name="continuationCallback"></param>
  771. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  772. {
  773. continuationCallback(true);
  774. }
  775. /// <summary>
  776. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  777. /// </summary>
  778. /// <param name="navigationContext"></param>
  779. /// <exception cref="NotImplementedException"></exception>
  780. public async void OnNavigatedTo(NavigationContext navigationContext)
  781. {
  782. SelectCalibration = navigationContext.Parameters.GetValue<CalibrationInfo>("SelectCalibration");
  783. if (SelectCalibration != null)
  784. {
  785. Robot = _robotService.GetRobot(SelectCalibration.RobotId);
  786. if (SelectCalibration.FeederId != Guid.Empty)
  787. {
  788. Feeder = _feederService.GetFeeder(SelectCalibration.FeederId);
  789. }
  790. }
  791. Camera = _cameraService.GetCamera(SelectCalibration.CameraID);
  792. }
  793. /// <summary>
  794. /// 是否允许导航到此视图模型。
  795. /// </summary>
  796. /// <param name="navigationContext"></param>
  797. /// <returns></returns>
  798. public bool IsNavigationTarget(NavigationContext navigationContext)
  799. {
  800. return true;
  801. }
  802. /// <summary>
  803. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  804. /// </summary>
  805. /// <param name="navigationContext"></param>
  806. public void OnNavigatedFrom(NavigationContext navigationContext)
  807. {
  808. }
  809. #endregion
  810. private bool _IsAllowEdit = false;
  811. public bool IsAllowEdit
  812. {
  813. get { return _IsAllowEdit; }
  814. set { SetProperty(ref _IsAllowEdit, value); }
  815. }
  816. private void LoginChange(Models.User user)
  817. {
  818. if (user.userPart == Enums.UserPart.Operator)
  819. {
  820. IsAllowEdit = false;
  821. }
  822. else
  823. {
  824. IsAllowEdit = true;
  825. }
  826. }
  827. }
  828. }