CalibrationVerificationViewModel.cs 21 KB

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