CalibrationVerificationViewModel.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. using Cognex.VisionPro;
  2. using Cognex.VisionPro.ToolBlock;
  3. using MathNet.Numerics.LinearAlgebra;
  4. using MathNet.Numerics.LinearAlgebra.Complex;
  5. using Prism.Commands;
  6. using Prism.Events;
  7. using Prism.Ioc;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using Prism.Services.Dialogs;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Drawing;
  14. using System.Linq;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. using Team.FFFeederService.Interfaces;
  18. using TeamAAS_VP.Core;
  19. using TeamAAS_VP.Events;
  20. using TeamAAS_VP.Interfaces;
  21. using TeamAAS_VP.Models;
  22. using TeamAAS_VP;
  23. using TeamAAS_VP.Resources.Languages;
  24. namespace TeamAAS_VP.ViewModels.Calibration
  25. {
  26. public class CalibrationVerificationViewModel : BindableBase
  27. {
  28. IRegionManager _regionManager;
  29. IRegionNavigationService _regionNavigationService;
  30. IContainerProvider _container;
  31. IDialogService _dialogService;
  32. IEventAggregator _eventAggregator;
  33. #region 属性
  34. private ICogImage _Image;
  35. public ICogImage Image
  36. {
  37. get { return _Image; }
  38. set { SetProperty(ref _Image, value); }
  39. }
  40. private Cognex.VisionPro.CogGraphicCollection _Graphic;
  41. public Cognex.VisionPro.CogGraphicCollection Graphic
  42. {
  43. get { return _Graphic; }
  44. set { SetProperty(ref _Graphic, value); }
  45. }
  46. private CalibrationInfo _SelectCalibration;
  47. public CalibrationInfo SelectCalibration
  48. {
  49. get { return _SelectCalibration; }
  50. set { SetProperty(ref _SelectCalibration, value); }
  51. }
  52. public Management management { get; set; }
  53. public RobotService robotService { get; set; }
  54. public IRobot Robot { get; set; }
  55. public IVoiceCoilMotorFeeder Feeder { get; set; }
  56. private bool _IsRunning = true;
  57. public bool IsRunning
  58. {
  59. get { return _IsRunning; }
  60. set { SetProperty(ref _IsRunning, value); }
  61. }
  62. private ICamera _Camera;
  63. public ICamera Camera
  64. {
  65. get { return _Camera; }
  66. set { SetProperty(ref _Camera, value); }
  67. }
  68. #endregion
  69. #region 命令
  70. private DelegateCommand _LoadedCommand;
  71. public DelegateCommand LoadedCommand =>
  72. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  73. private DelegateCommand _NextCommand;
  74. public DelegateCommand NextCommand =>
  75. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand).ObservesCanExecute(() => IsRunning));
  76. private DelegateCommand _BackCommand;
  77. public DelegateCommand BackCommand =>
  78. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand).ObservesCanExecute(() => IsRunning));
  79. private DelegateCommand _StartTestCommand;
  80. public DelegateCommand StartTestCommand =>
  81. _StartTestCommand ?? (_StartTestCommand = new DelegateCommand(ExecuteStartTestCommand).ObservesCanExecute(() => IsRunning));
  82. #endregion
  83. #region 事件
  84. #endregion
  85. public CalibrationVerificationViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService)
  86. {
  87. _regionManager = regionManager;
  88. _regionNavigationService = regionNavigationService;
  89. _container = container;
  90. _dialogService = dialogService;
  91. _eventAggregator = ea;
  92. SelectCalibration = _container.Resolve<CalibrationHomeViewModel>().SelectCalibration;
  93. }
  94. #region 方法
  95. void ExecuteLoadedCommand()
  96. {
  97. if (_container.Resolve<Management>().CurrentUser.userPart == Enums.UserPart.Operator)
  98. {
  99. IsAllowEdit = false;
  100. }
  101. else
  102. {
  103. IsAllowEdit = true;
  104. }
  105. try
  106. {
  107. SelectCalibration = _container.Resolve<CalibrationHomeViewModel>().SelectCalibration;
  108. management = _container.Resolve<Management>();
  109. robotService = management.RobotService;
  110. if (SelectCalibration != null)
  111. {
  112. Robot = robotService.GetRobot(SelectCalibration.RobotId);
  113. if (SelectCalibration.FeederId != Guid.Empty)
  114. {
  115. Feeder = management.FeederService.GetFeeder(SelectCalibration.FeederId);
  116. }
  117. }
  118. Camera = management.CameraService.GetCamera(SelectCalibration.CameraID);
  119. }
  120. catch (Exception ex)
  121. {
  122. LogHelper.WriteLogError("相机校准-校准验证界面示加载时出错!", ex);
  123. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  124. }
  125. }
  126. /// <summary>
  127. /// 测试校准
  128. /// </summary>
  129. async void ExecuteStartTestCommand()
  130. {
  131. try
  132. {
  133. if (MessageBox.Show(Lang.是否开始测试, Lang.自动校准, MessageBoxButton.OKCancel, MessageBoxImage.Question) != MessageBoxResult.OK) return;
  134. IsRunning = false;
  135. double IntervalX = SelectCalibration.Width*0.90 / 2;
  136. double IntervalY = SelectCalibration.Height * 0.90 / 2;
  137. Vector<double> P1 = Vector<double>.Build.Dense(new double[] { -IntervalX, -IntervalY });
  138. Vector<double> P2 = Vector<double>.Build.Dense(new double[] { IntervalX, -IntervalY });
  139. Vector<double> P3 = Vector<double>.Build.Dense(new double[] { 0, 0 });
  140. Vector<double> P4 = Vector<double>.Build.Dense(new double[] { -IntervalX, IntervalY });
  141. Vector<double> P5 = Vector<double>.Build.Dense(new double[] { IntervalX, IntervalY });
  142. //移动相机标定时,需要先记住特征点的绝对坐标(相对于机器人坐标系) P0
  143. //if (SelectCalibration.CameraMount == Enums.CameraMount.MobileJ4)
  144. //{
  145. // P1[0] *= -1; P1[1] *= -1;
  146. // P2[0] *= -1; P2[1] *= -1;
  147. // P3[0] *= -1; P3[1] *= -1;
  148. // P4[0] *= -1; P4[1] *= -1;
  149. // P5[0] *= -1; P5[1] *= -1;
  150. //}
  151. var P0 = Vector<double>.Build.Dense(new double[] { SelectCalibration.CenterPoint.X, SelectCalibration.CenterPoint.Y });
  152. /// <summary>
  153. /// 机器人与相机之间的旋转矩阵
  154. /// </summary>
  155. Matrix<double> RobotCameraRotationMatrix = Matrix<double>.Build.DenseOfArray(SelectCalibration.RobotCameraRotationMatrix);
  156. //1
  157. P1 = RobotCameraRotationMatrix.Inverse() * P1 + P0;
  158. //2
  159. P2 = RobotCameraRotationMatrix.Inverse() * P2 + P0;
  160. //3
  161. P3 = RobotCameraRotationMatrix.Inverse() * P3 + P0;
  162. //4
  163. P4 = RobotCameraRotationMatrix.Inverse() * P4 + P0;
  164. //5
  165. P5 = RobotCameraRotationMatrix.Inverse() * P5 + P0;
  166. List<RPoint> rPoints = new List<RPoint>();
  167. rPoints.Add(new RPoint()
  168. {
  169. Number = 1,
  170. X = (float)P1[0],
  171. Y = (float)P1[1],
  172. Z = SelectCalibration.CenterPoint.Z,
  173. U = SelectCalibration.CenterPoint.U,
  174. V = SelectCalibration.CenterPoint.V,
  175. W = SelectCalibration.CenterPoint.W,
  176. Hand = SelectCalibration.CenterPoint.Hand,
  177. Local = SelectCalibration.CenterPoint.Local,
  178. Tool = SelectCalibration.CenterPoint.Tool
  179. });
  180. rPoints.Add(new RPoint()
  181. {
  182. Number = 2,
  183. X = (float)P2[0],
  184. Y = (float)P2[1],
  185. Z = SelectCalibration.CenterPoint.Z,
  186. U = SelectCalibration.CenterPoint.U,
  187. V = SelectCalibration.CenterPoint.V,
  188. W = SelectCalibration.CenterPoint.W,
  189. Hand = SelectCalibration.CenterPoint.Hand,
  190. Local = SelectCalibration.CenterPoint.Local,
  191. Tool = SelectCalibration.CenterPoint.Tool
  192. });
  193. rPoints.Add(new RPoint()
  194. {
  195. Number = 3,
  196. X = (float)P3[0],
  197. Y = (float)P3[1],
  198. Z = SelectCalibration.CenterPoint.Z,
  199. U = SelectCalibration.CenterPoint.U,
  200. V = SelectCalibration.CenterPoint.V,
  201. W = SelectCalibration.CenterPoint.W,
  202. Hand = SelectCalibration.CenterPoint.Hand,
  203. Local = SelectCalibration.CenterPoint.Local,
  204. Tool = SelectCalibration.CenterPoint.Tool
  205. });
  206. rPoints.Add(new RPoint()
  207. {
  208. Number = 4,
  209. X = (float)P4[0],
  210. Y = (float)P4[1],
  211. Z = SelectCalibration.CenterPoint.Z,
  212. U = SelectCalibration.CenterPoint.U,
  213. V = SelectCalibration.CenterPoint.V,
  214. W = SelectCalibration.CenterPoint.W,
  215. Hand = SelectCalibration.CenterPoint.Hand,
  216. Local = SelectCalibration.CenterPoint.Local,
  217. Tool = SelectCalibration.CenterPoint.Tool
  218. });
  219. rPoints.Add(new RPoint()
  220. {
  221. Number = 5,
  222. X = (float)P5[0],
  223. Y = (float)P5[1],
  224. Z = SelectCalibration.CenterPoint.Z,
  225. U = SelectCalibration.CenterPoint.U,
  226. V = SelectCalibration.CenterPoint.V,
  227. W = SelectCalibration.CenterPoint.W,
  228. Hand = SelectCalibration.CenterPoint.Hand,
  229. Local = SelectCalibration.CenterPoint.Local,
  230. Tool = SelectCalibration.CenterPoint.Tool
  231. });
  232. List<Vector<double>> TestPos = new List<Vector<double>>();
  233. for (int i = 0; i < rPoints.Count; i++)
  234. {
  235. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  236. {
  237. //移动机器人至i
  238. await Robot.CalibMotionAsync(rPoints[i], 0);
  239. //关闭吸气
  240. await Robot.CalibOutIOAsync(false);
  241. //移动机器人至待机位置
  242. await Robot.CalibMotionAsync(SelectCalibration.HomePoint, 0);
  243. }
  244. else
  245. {
  246. //移动机器人至i
  247. await Robot.CalibMotionAsync(rPoints[i], rPoints[i].Z);
  248. }
  249. var pixelpos = Photo();
  250. Matrix<double> matrix = Matrix<double>.Build.DenseOfArray(SelectCalibration.AffineTransformationMaterial);
  251. var rpos = matrix * Vector<double>.Build.Dense(new double[] { pixelpos.X, pixelpos.Y ,1});
  252. TestPos.Add(rpos);
  253. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  254. {
  255. //移动机器人至i
  256. await Robot.CalibMotionAsync(rPoints[i], 0);
  257. //打开吸气
  258. await Robot.CalibOutIOAsync(true);
  259. }
  260. }
  261. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  262. {
  263. //移动机器人至待机位置
  264. await Robot.CalibMotionAsync(SelectCalibration.HomePoint, 0);
  265. }
  266. //RMS(均方根值) 标定偏差
  267. double sumX = 0, sumY = 0;
  268. List<double> differenceX = new List<double>();
  269. List<double> differenceY = new List<double>();
  270. for (int i = 0; i < 5; i++)
  271. {
  272. if (SelectCalibration.CameraMount == Enums.CameraMount.MobileJ4)
  273. {
  274. //像素直接转换成mm时的点位
  275. double Robot_X, Robot_Y;
  276. Robot_X = TestPos[i][0];
  277. Robot_Y= TestPos[i][1];
  278. //机器人当前TOOL 0下的坐标
  279. double curpos_x = 0, curpos_y = 0, curpos_u = 0;
  280. curpos_x = rPoints[i].X;
  281. curpos_y = rPoints[i].Y;
  282. curpos_u = rPoints[i].U;
  283. //得到旋转矩阵
  284. double angle = Math.PI * (curpos_u - SelectCalibration.MarkPoint.U) / 180;
  285. // 创建T矩阵
  286. Matrix<double> rotationMatrix = Matrix<double>.Build.DenseOfArray(new double[,]
  287. {
  288. { Math.Cos(angle), -Math.Sin(angle) },
  289. { Math.Sin(angle), Math.Cos(angle) }
  290. });
  291. Vector<double> p3 = Vector<double>.Build.Dense(new double[] { Robot_X - SelectCalibration.CalibPoints[0].Robot.X, Robot_Y - SelectCalibration.CalibPoints[0].Robot.Y });
  292. Vector<double> phere = Vector<double>.Build.Dense(new double[] { curpos_x, curpos_y });
  293. var cc = rotationMatrix * p3 + phere;
  294. differenceX.Add(cc[0] - SelectCalibration.MarkPoint.X);
  295. differenceY.Add(cc[1] - SelectCalibration.MarkPoint.Y);
  296. }
  297. else
  298. {
  299. //将tool 0下的坐标转换成Tool n下的坐标
  300. if (SelectCalibration.Tool != null)
  301. {
  302. ToolCoord toolCoord = new ToolCoord();
  303. toolCoord.SetTool(SelectCalibration.Tool.X, SelectCalibration.Tool.Y);
  304. double u1 = rPoints[i].U;
  305. if (Robot.Brand == Enums.RobotBrand.Schneider)
  306. {
  307. u1 *= -1;
  308. }
  309. var trans = toolCoord.GetToolnCoord(Vector<double>.Build.Dense(new double[] { rPoints[i].X, rPoints[i].Y }), u1);
  310. differenceX.Add(trans[0] - TestPos[i][0]);
  311. differenceY.Add(trans[1] - TestPos[i][1]);
  312. }
  313. else
  314. {
  315. differenceX.Add(rPoints[i].X - TestPos[i][0]);
  316. differenceY.Add(rPoints[i].Y - TestPos[i][1]);
  317. }
  318. }
  319. sumX += Math.Pow(differenceX[differenceX.Count - 1], 2);
  320. sumY += Math.Pow(differenceY[differenceY.Count - 1], 2);
  321. }
  322. double rmsX, rmsY;
  323. rmsX = Math.Sqrt(sumX / 5);
  324. rmsY = Math.Sqrt(sumY / 5);
  325. CalibrationTestResult calibrationTestResult = new CalibrationTestResult();
  326. calibrationTestResult.MaxErrorValueX = differenceX.Max();
  327. calibrationTestResult.MinErrorValueX = differenceX.Min();
  328. calibrationTestResult.MaxErrorValueY = differenceY.Max();
  329. calibrationTestResult.MinErrorValueY = differenceY.Min();
  330. calibrationTestResult.RMSEX = rmsX;
  331. calibrationTestResult.RMSEY = rmsY;
  332. SelectCalibration.CalibrationTestResult = calibrationTestResult;
  333. MessageBox.Show(Lang.验证完成);
  334. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationResult");
  335. }
  336. catch (Exception ex)
  337. {
  338. LogHelper.WriteLogError("相机校准-校准验证界面示加载时出错!", ex);
  339. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  340. MessageBox.Show(ex.Message);
  341. }
  342. finally
  343. {
  344. IsRunning = true;
  345. }
  346. }
  347. /// <summary>
  348. /// 下一步
  349. /// </summary>
  350. void ExecuteNextCommand()
  351. {
  352. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationResult");
  353. }
  354. /// <summary>
  355. /// 上一步
  356. /// </summary>
  357. void ExecuteBackCommand()
  358. {
  359. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationAuto");
  360. }
  361. /// <summary>
  362. /// 相机拍照
  363. /// </summary>
  364. /// <returns></returns>
  365. /// <exception cref="Exception"></exception>
  366. private PointF Photo()
  367. {
  368. if (Feeder != null)
  369. {
  370. Feeder.OpenLight();
  371. Feeder.SetLightTimeOut((ushort)SelectCalibration.Brightness);
  372. }
  373. Task.Delay(200).Wait();
  374. //采集图像
  375. if (Camera != null)
  376. {
  377. SelectCalibration.ToolBlock.Inputs["InputImage"].Value = Camera.Grab();
  378. Image = (ICogImage)SelectCalibration.ToolBlock.Inputs["InputImage"].Value;
  379. }
  380. Graphic = null;
  381. SelectCalibration.ToolBlock.Run();
  382. CogToolBlockTerminalCollection outputCollection = SelectCalibration.ToolBlock.Outputs;
  383. Graphic = outputCollection["Graphic"].Value as CogGraphicCollection;
  384. bool Found = (bool)(outputCollection["Found"].Value);
  385. if (Found)
  386. {
  387. var strpoints = outputCollection["Point"].Value.ToString();
  388. string[] items = strpoints.Split(',');
  389. return new PointF(float.Parse(items[0]), float.Parse(items[1]));
  390. }
  391. else
  392. {
  393. throw new Exception(Lang.拍照失败);
  394. }
  395. }
  396. #endregion
  397. private bool _IsAllowEdit = false;
  398. public bool IsAllowEdit
  399. {
  400. get { return _IsAllowEdit; }
  401. set { SetProperty(ref _IsAllowEdit, value); }
  402. }
  403. private void LoginChange(Models.User user)
  404. {
  405. if (user.userPart == Enums.UserPart.Operator)
  406. {
  407. IsAllowEdit = false;
  408. }
  409. else
  410. {
  411. IsAllowEdit = true;
  412. }
  413. }
  414. }
  415. }