CameraCalibrationService.cs 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  1. using MaterialDesignThemes.Wpf;
  2. using MathNet.Numerics;
  3. using MathNet.Numerics.LinearAlgebra;
  4. using OpenCvSharp;
  5. using Prism.Regions;
  6. using Prism.Services.Dialogs;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Drawing;
  10. using System.Linq;
  11. using System.Security.Cryptography;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using TeamAAS_VP.Controls;
  16. using TeamAAS_VP.Core;
  17. using TeamAAS_VP.Interfaces;
  18. using TeamAAS_VP.Models;
  19. using TeamAAS_VP.Models.Calibration;
  20. using TeamAAS_VP.Models.Robot;
  21. using TeamAAS_VP.Resources.Languages;
  22. namespace TeamAAS_VP.Services
  23. {
  24. /// <summary>
  25. /// 相机标定服务的骨架实现。实际的标定算法和数据持久化由后续实现补充。
  26. /// </summary>
  27. public class CameraCalibrationService : ICameraCalibrationService
  28. {
  29. /// <summary>
  30. /// 触发相机拍照并进行图像处理的回调函数
  31. /// 在标定过程中需要获取图像特征点时调用此回调
  32. /// Tuple 字段含义:
  33. /// Item1 - IsSuccess: 拍照及图像处理是否成功;
  34. /// Item2 - X: 图像处理后得到的机器人目标 X(mm);
  35. /// Item3 - Y: 图像处理后得到的机器人目标 Y(mm);
  36. /// Item4 - U: 图像处理后得到的机器人目标 U(工具朝向)。
  37. /// </summary>
  38. public Func<Task<(bool IsSuccess, double X, double Y, double U, int ImageWidth, int ImageHeight)>> CaptureAndProcessCallback { get; set; }
  39. /// <summary>
  40. /// 固定向下相机校准工具坐标。
  41. /// 该方法用于在相机固定、朝下安装且工具不随 J4 移动的情况下标定相机与机器人工具坐标系之间的关系。
  42. /// </summary>
  43. /// <param name="calibrationInfo">包含标定时所需的相机参数、图像处理配置与标定点信息的对象。</param>
  44. /// <param name="robot">实现了 <see cref="IRobot"/> 的机器人抽象,用于获取/移动机器人位姿。</param>
  45. /// <param name="cancellationToken">用于取消操作的标记。</param>
  46. /// <returns>
  47. /// 一个包含以下字段的元组:
  48. /// IsSuccess - 标定是否成功;
  49. /// Matrix<double> - 图像与像素坐标系之间的旋转(或仿射)矩阵(行列顺序与具体实现约定);
  50. /// ToolX - 标定得到的工具坐标系在机器人基坐标系下的 X(单位与机器人一致);
  51. /// ToolY - 标定得到的工具坐标系在机器人基坐标系下的 Y(单位与机器人一致);
  52. /// PixelScaleX - 像素到物理距离在 X 方向的缩放因子(如 mm/px);
  53. /// PixelScaleY - 像素到物理距离在 Y 方向的缩放因子(如 mm/px)。
  54. /// </returns>
  55. public async Task<(bool IsSuccess, Matrix<double> RotationMatrix, double ToolX, double ToolY, double PixelScaleX, double PixelScaleY)> CalibrateFixedDownCameraTool(CalibrationInfo calibrationInfo, IRobot robot, CancellationToken cancellationToken)
  56. {
  57. double pixelScaleX = 0;
  58. double pixelScaleY = 0;
  59. Matrix<double> RobotCameraRotationMatrix = null;
  60. //移动机器人至待机位置
  61. bool isSuccess = await robot.CalibMotionAsync(calibrationInfo.HomePoint, 0);
  62. if (!isSuccess)
  63. {
  64. return (false, null, 0, 0, 0, 0);
  65. }
  66. if (cancellationToken.IsCancellationRequested)
  67. {
  68. return (false, null, 0, 0, 0, 0);
  69. }
  70. if (calibrationInfo.Pick.PickPlaceModel == Enums.PickPlaceModel.Inhal)
  71. {
  72. //打开吸气
  73. await robot.CalibOutIOAsync(true);
  74. }
  75. else
  76. {
  77. //张开夹爪
  78. //夹爪张开为false,夹爪闭合为True
  79. await robot.CalibOutIOAsync(false);
  80. }
  81. var view = new ShowMessage(Lang.提示, Lang.请将校准块放置在吸嘴中心);
  82. var result = await DialogHost.Show(view, "CalibToolDialog", null, null, null);
  83. if (!((bool)result)) return (false, null, 0, 0, 0, 0);
  84. if (calibrationInfo.Pick.PickPlaceModel == Enums.PickPlaceModel.Clamp)
  85. {
  86. //夹紧夹爪
  87. await robot.CalibOutIOAsync(true);
  88. }
  89. //计算相机与机器人坐标系的旋转矩阵
  90. var robotCameraRotationMatrix = await AutoIdentifyRobotCameraRotationMatrix(calibrationInfo, robot, cancellationToken);
  91. if (!robotCameraRotationMatrix.IsSuccess)
  92. {
  93. return (false, null, 0, 0, 0, 0);
  94. }
  95. pixelScaleX = robotCameraRotationMatrix.PixelScaleX;
  96. pixelScaleY = robotCameraRotationMatrix.PixelScaleY;
  97. RobotCameraRotationMatrix = robotCameraRotationMatrix.Item2;
  98. //相机拍照并处理图像
  99. var photoResult = await CaptureAndProcessCallback();
  100. if (!photoResult.IsSuccess)
  101. {
  102. return (false, null, 0, 0, 0, 0);
  103. }
  104. PointF P0 = new PointF((float)(photoResult.X), (float)(photoResult.Y));
  105. RPoint RobotU = calibrationInfo.CenterPoint.Clone();
  106. RobotU.U += (float)calibrationInfo.Angle;
  107. //移动机器人中心点
  108. isSuccess = await robot.CalibMotionAsync(calibrationInfo.CenterPoint, 0);
  109. if (!isSuccess)
  110. {
  111. return (false, null, 0, 0, 0, 0);
  112. }
  113. if (cancellationToken.IsCancellationRequested)
  114. {
  115. return (false, null, 0, 0, 0, 0);
  116. }
  117. //打开吸气
  118. await robot.CalibOutIOAsync(true);
  119. //机器人U轴旋转
  120. isSuccess = await robot.CalibMotionAsync(RobotU, 0);
  121. if (!isSuccess)
  122. {
  123. return (false, null, 0, 0, 0, 0);
  124. }
  125. if (cancellationToken.IsCancellationRequested)
  126. {
  127. return (false, null, 0, 0, 0, 0);
  128. }
  129. //关闭吸气
  130. await robot.CalibOutIOAsync(false);
  131. //移动机器人至待机位置
  132. isSuccess = await robot.CalibMotionAsync(calibrationInfo.HomePoint, 0);
  133. if (!isSuccess)
  134. {
  135. return (false, null, 0, 0, 0, 0);
  136. }
  137. if (cancellationToken.IsCancellationRequested)
  138. {
  139. return (false, null, 0, 0, 0, 0);
  140. }
  141. //相机拍照并处理图像
  142. photoResult = await CaptureAndProcessCallback();
  143. if (!photoResult.IsSuccess)
  144. {
  145. return (false, null, 0, 0, 0, 0);
  146. }
  147. PointF curPixel = new PointF((float)(photoResult.X), (float)(photoResult.Y));
  148. bool IsFinish = false;
  149. for (int i = 0; i < 10; i++)
  150. {
  151. //计算需要移动的偏移量,目的是逼近中心点
  152. double offsetx = (P0.X - curPixel.X) * pixelScaleX;
  153. double offsety = (P0.Y - curPixel.Y) * pixelScaleY;
  154. var pos = RobotCameraRotationMatrix.Inverse() * Vector<double>.Build.Dense(new double[] { offsetx, offsety });
  155. //移动机器人取标定块
  156. isSuccess = await robot.CalibMotionAsync(RobotU, 0);
  157. if (!isSuccess)
  158. {
  159. return (false, null, 0, 0, 0, 0);
  160. }
  161. if (cancellationToken.IsCancellationRequested)
  162. {
  163. return (false, null, 0, 0, 0, 0);
  164. }
  165. //打开吸气
  166. await robot.CalibOutIOAsync(true);
  167. RobotU.X += (float)pos[0];
  168. RobotU.Y += (float)pos[1];
  169. //机器人移动
  170. isSuccess = await robot.CalibMotionAsync(RobotU, 0);
  171. if (!isSuccess)
  172. {
  173. return (false, null, 0, 0, 0, 0);
  174. }
  175. if (cancellationToken.IsCancellationRequested)
  176. {
  177. return (false, null, 0, 0, 0, 0);
  178. }
  179. //关闭吸气
  180. await robot.CalibOutIOAsync(false);
  181. //移动机器人至待机位置
  182. isSuccess = await robot.CalibMotionAsync(calibrationInfo.HomePoint, 0);
  183. if (!isSuccess)
  184. {
  185. return (false, null, 0, 0, 0, 0);
  186. }
  187. if (cancellationToken.IsCancellationRequested)
  188. {
  189. return (false, null, 0, 0, 0, 0);
  190. }
  191. photoResult = await CaptureAndProcessCallback();
  192. if (!photoResult.IsSuccess)
  193. {
  194. return (false, null, 0, 0, 0, 0);
  195. }
  196. curPixel = new PointF((float)(photoResult.X), (float)(photoResult.Y));
  197. offsetx = P0.X - curPixel.X;
  198. offsety = P0.Y - curPixel.Y;
  199. //await Console.Out.WriteLineAsync($"移动后的差异:{offsetx},{offsety}");
  200. if (Math.Abs(offsetx) < 1 && Math.Abs(offsetx) < 1)
  201. {
  202. //移动机器人取标定块
  203. isSuccess = await robot.CalibMotionAsync(RobotU, 0);
  204. if (!isSuccess)
  205. {
  206. return (false, null, 0, 0, 0, 0);
  207. }
  208. if (cancellationToken.IsCancellationRequested)
  209. {
  210. return (false, null, 0, 0, 0, 0);
  211. }
  212. //打开吸气
  213. await robot.CalibOutIOAsync(true);
  214. //移动机器人至待机位置
  215. isSuccess = await robot.CalibMotionAsync(calibrationInfo.HomePoint, 0);
  216. if (!isSuccess)
  217. {
  218. return (false, null, 0, 0, 0, 0);
  219. }
  220. IsFinish = true;
  221. break;
  222. }
  223. if (cancellationToken.IsCancellationRequested)
  224. {
  225. return (false, null, 0, 0, 0, 0);
  226. }
  227. }
  228. if (IsFinish)
  229. {
  230. Vector<double> p0 = Vector<double>.Build.Dense(new double[] { calibrationInfo.CenterPoint.X, calibrationInfo.CenterPoint.Y });
  231. Vector<double> p1 = Vector<double>.Build.Dense(new double[] { RobotU.X, RobotU.Y });
  232. ToolCoord toolCoord = new ToolCoord();
  233. double u1 = calibrationInfo.CenterPoint.U;
  234. double u2 = RobotU.U;
  235. if (robot.Brand == Enums.RobotBrand.Schneider)
  236. {
  237. u1 *= -1;
  238. u2 *= -1;
  239. }
  240. toolCoord.ComputeTool(p0, p1, u1, u2);
  241. if (calibrationInfo.Tool == null)
  242. {
  243. calibrationInfo.Tool = new RobotTool();
  244. }
  245. calibrationInfo.Tool.X = toolCoord.X;
  246. calibrationInfo.Tool.Y = toolCoord.Y;
  247. return (true, RobotCameraRotationMatrix, calibrationInfo.Tool.X, calibrationInfo.Tool.Y, pixelScaleX, pixelScaleY);
  248. }
  249. else
  250. {
  251. return (false, null, 0, 0, 0, 0);
  252. }
  253. }
  254. /// <summary>
  255. /// J4 移动的向下相机校准工具坐标。
  256. /// 用于相机或工具随机器人第 4 轴(J4)运动时的标定,需考虑 J4 引入的偏转影响。
  257. /// </summary>
  258. /// <param name="calibrationInfo">标定所需配置信息。</param>
  259. /// <param name="robot">机器人接口。</param>
  260. /// <param name="cancellationToken">用于取消操作的标记。</param>
  261. /// <returns>同 <see cref="CalibrateFixedDownCameraTool"/> 的返回约定。</returns>
  262. public async Task<(bool IsSuccess, Matrix<double> RotationMatrix, double ToolX, double ToolY, double PixelScaleX, double PixelScaleY)> CalibrateJ4MovingDownCameraTool(CalibrationInfo calibrationInfo, IRobot robot, CancellationToken cancellationToken)
  263. {
  264. double pixelScaleX = 0;
  265. double pixelScaleY = 0;
  266. Matrix<double> RobotCameraRotationMatrix = null;
  267. //移动机器人至中心位置
  268. bool isSuccess = await robot.CalibMotionAsync(calibrationInfo.CenterPoint, 0);
  269. if (!isSuccess)
  270. {
  271. return (false, null, 0, 0, 0, 0);
  272. }
  273. //计算相机与机器人坐标系的旋转矩阵
  274. var robotCameraRotationMatrix = await AutoIdentifyRobotCameraRotationMatrix(calibrationInfo, robot, cancellationToken);
  275. if (!robotCameraRotationMatrix.IsSuccess)
  276. {
  277. return (false, null, 0, 0, 0, 0);
  278. }
  279. pixelScaleX = robotCameraRotationMatrix.PixelScaleX;
  280. pixelScaleY = robotCameraRotationMatrix.PixelScaleY;
  281. RobotCameraRotationMatrix = robotCameraRotationMatrix.Item2;
  282. //自动移动机器人至图像中心点位置
  283. var autoMoveResult = await AutoMoveRobotToImageCenter(robot, RobotCameraRotationMatrix, pixelScaleX, pixelScaleY, cancellationToken);
  284. if (!autoMoveResult.IsSuccess)
  285. {
  286. return (false, null, 0, 0, 0, 0);
  287. }
  288. calibrationInfo.CenterPoint.X = (float)autoMoveResult.X;
  289. calibrationInfo.CenterPoint.Y = (float)autoMoveResult.Y;
  290. RPoint RobotU = calibrationInfo.CenterPoint.Clone();
  291. RobotU.U += (float)calibrationInfo.Angle;
  292. //机器人U轴旋转
  293. isSuccess = await robot.CalibMotionAsync(RobotU, RobotU.Z);
  294. if (!isSuccess)
  295. {
  296. return (false, null, 0, 0, 0, 0);
  297. }
  298. if (cancellationToken.IsCancellationRequested)
  299. {
  300. return (false, null, 0, 0, 0, 0);
  301. }
  302. //相机拍照并处理图像
  303. var photoResult = await CaptureAndProcessCallback();
  304. if (!photoResult.IsSuccess)
  305. {
  306. return (false, null, 0, 0, 0, 0);
  307. }
  308. PointF curPixel = new PointF((float)(photoResult.X), (float)(photoResult.Y));
  309. autoMoveResult = await AutoMoveRobotToImageCenter(robot, RobotCameraRotationMatrix, pixelScaleX, pixelScaleY, cancellationToken);
  310. if (!autoMoveResult.IsSuccess)
  311. {
  312. return (false, null, 0, 0, 0, 0);
  313. }
  314. RobotU.X = (float)autoMoveResult.X;
  315. RobotU.Y = (float)autoMoveResult.Y;
  316. Vector<double> p0 = Vector<double>.Build.Dense(new double[] { calibrationInfo.CenterPoint.X, calibrationInfo.CenterPoint.Y });
  317. Vector<double> p1 = Vector<double>.Build.Dense(new double[] { RobotU.X, RobotU.Y });
  318. ToolCoord toolCoord = new ToolCoord();
  319. double u1 = calibrationInfo.CenterPoint.U;
  320. double u2 = RobotU.U;
  321. if (robot.Brand == Enums.RobotBrand.Schneider)
  322. {
  323. u1 *= -1;
  324. u2 *= -1;
  325. }
  326. toolCoord.ComputeTool(p0, p1, u1, u2);
  327. if (calibrationInfo.Tool == null)
  328. {
  329. calibrationInfo.Tool = new RobotTool();
  330. }
  331. calibrationInfo.Tool.X = toolCoord.X;
  332. calibrationInfo.Tool.Y = toolCoord.Y;
  333. //移动机器人至中心位置
  334. isSuccess = await robot.CalibMotionAsync(calibrationInfo.CenterPoint, calibrationInfo.CenterPoint.Z);
  335. if (!isSuccess)
  336. {
  337. return (false, null, 0, 0, 0, 0);
  338. }
  339. return (true, RobotCameraRotationMatrix, calibrationInfo.Tool.X, calibrationInfo.Tool.Y, pixelScaleX, pixelScaleY);
  340. }
  341. /// <summary>
  342. /// 固定向上相机校准工具坐标。
  343. /// 该方法用于相机固定、朝上安装的情况,标定过程与向下相机类似但需要考虑图像翻转/镜像等差异。
  344. /// </summary>
  345. /// <param name="calibrationInfo">标定配置信息。</param>
  346. /// <param name="robot">机器人接口。</param>
  347. /// <param name="cancellationToken">用于取消操作的标记。</param>
  348. /// <returns>同 <see cref="CalibrateFixedDownCameraTool"/> 的返回约定。</returns>
  349. public async Task<(bool IsSuccess, Matrix<double> RotationMatrix, double ToolX, double ToolY, double PixelScaleX, double PixelScaleY)> CalibrateFixedUpCameraTool(CalibrationInfo calibrationInfo, IRobot robot, CancellationToken cancellationToken)
  350. {
  351. double pixelScaleX = 0;
  352. double pixelScaleY = 0;
  353. Matrix<double> RobotCameraRotationMatrix = null;
  354. //移动机器人至中心位置
  355. bool isSuccess = await robot.CalibMotionAsync(calibrationInfo.CenterPoint, 0);
  356. if (!isSuccess)
  357. {
  358. return (false, null, 0, 0, 0, 0);
  359. }
  360. if (calibrationInfo.Pick.PickPlaceModel == Enums.PickPlaceModel.Inhal)
  361. {
  362. //打开吸气
  363. await robot.CalibOutIOAsync(true);
  364. }
  365. else
  366. {
  367. //张开夹爪
  368. await robot.CalibOutIOAsync(false);
  369. }
  370. var view = new ShowMessage(Lang.提示, Lang.请将校准块放置在吸嘴中心);
  371. var result = await DialogHost.Show(view, "CalibToolDialog", null, null, null);
  372. if (!((bool)result)) return (false, null, 0, 0, 0, 0);
  373. if (calibrationInfo.Pick.PickPlaceModel == Enums.PickPlaceModel.Clamp)
  374. {
  375. //夹紧夹爪
  376. await robot.CalibOutIOAsync(true);
  377. }
  378. //计算相机与机器人坐标系的旋转矩阵
  379. var robotCameraRotationMatrix = await AutoIdentifyRobotCameraRotationMatrix(calibrationInfo, robot, cancellationToken);
  380. if (!robotCameraRotationMatrix.IsSuccess)
  381. {
  382. return (false, null, 0, 0, 0, 0);
  383. }
  384. pixelScaleX = robotCameraRotationMatrix.PixelScaleX;
  385. pixelScaleY = robotCameraRotationMatrix.PixelScaleY;
  386. RobotCameraRotationMatrix = robotCameraRotationMatrix.Item2;
  387. //自动移动机器人至图像中心点位置
  388. var autoMoveResult = await AutoMoveRobotToImageCenter(robot, RobotCameraRotationMatrix, pixelScaleX, pixelScaleY, cancellationToken);
  389. if (!autoMoveResult.IsSuccess)
  390. {
  391. return (false, null, 0, 0, 0, 0);
  392. }
  393. calibrationInfo.CenterPoint.X = (float)autoMoveResult.X;
  394. calibrationInfo.CenterPoint.Y = (float)autoMoveResult.Y;
  395. RPoint RobotU = calibrationInfo.CenterPoint.Clone();
  396. RobotU.U += (float)calibrationInfo.Angle;
  397. //机器人U轴旋转
  398. isSuccess = await robot.CalibMotionAsync(RobotU, RobotU.Z);
  399. if (!isSuccess)
  400. {
  401. return (false, null, 0, 0, 0, 0);
  402. }
  403. if (cancellationToken.IsCancellationRequested)
  404. {
  405. return (false, null, 0, 0, 0, 0);
  406. }
  407. //相机拍照并处理图像
  408. var photoResult = await CaptureAndProcessCallback();
  409. if (!photoResult.IsSuccess)
  410. {
  411. return (false, null, 0, 0, 0, 0);
  412. }
  413. PointF curPixel = new PointF((float)(photoResult.X), (float)(photoResult.Y));
  414. autoMoveResult = await AutoMoveRobotToImageCenter(robot, RobotCameraRotationMatrix, pixelScaleX, pixelScaleY, cancellationToken);
  415. if (!autoMoveResult.IsSuccess)
  416. {
  417. return (false, null, 0, 0, 0, 0);
  418. }
  419. RobotU.X = (float)autoMoveResult.X;
  420. RobotU.Y = (float)autoMoveResult.Y;
  421. Vector<double> p0 = Vector<double>.Build.Dense(new double[] { calibrationInfo.CenterPoint.X, calibrationInfo.CenterPoint.Y });
  422. Vector<double> p1 = Vector<double>.Build.Dense(new double[] { RobotU.X, RobotU.Y });
  423. ToolCoord toolCoord = new ToolCoord();
  424. double u1 = calibrationInfo.CenterPoint.U;
  425. double u2 = RobotU.U;
  426. if (robot.Brand == Enums.RobotBrand.Schneider)
  427. {
  428. u1 *= -1;
  429. u2 *= -1;
  430. }
  431. toolCoord.ComputeTool(p0, p1, u1, u2);
  432. if (calibrationInfo.Tool == null)
  433. {
  434. calibrationInfo.Tool = new RobotTool();
  435. }
  436. calibrationInfo.Tool.X = toolCoord.X;
  437. calibrationInfo.Tool.Y = toolCoord.Y;
  438. //移动机器人至中心位置
  439. isSuccess = await robot.CalibMotionAsync(calibrationInfo.CenterPoint, calibrationInfo.CenterPoint.Z);
  440. if (!isSuccess)
  441. {
  442. return (false, null, 0, 0, 0, 0);
  443. }
  444. return (true, RobotCameraRotationMatrix, calibrationInfo.Tool.X, calibrationInfo.Tool.Y, pixelScaleX, pixelScaleY);
  445. }
  446. /// <summary>
  447. /// 通过未标定的相机校准工具坐标。
  448. /// 当相机内参未知或未可靠标定时,通过已知机器人位姿与图像特征的对应关系反推工具坐标与像素尺度。
  449. /// </summary>
  450. /// <param name="calibrationInfo">标定配置信息(可能包含初始猜测或参考点)。</param>
  451. /// <param name="robot">机器人接口。</param>
  452. /// <param name="cancellationToken">用于取消操作的标记。</param>
  453. /// <returns>同 <see cref="CalibrateFixedDownCameraTool"/> 的返回约定。</returns>
  454. public async Task<(bool IsSuccess, Matrix<double> RotationMatrix, double ToolX, double ToolY, double PixelScaleX, double PixelScaleY)> CalibrateUncalibratedCameraTool(CalibrationInfo calibrationInfo, IRobot robot, CancellationToken cancellationToken)
  455. {
  456. if (calibrationInfo == null) return (false, null, 0, 0, 0, 0);
  457. if (robot == null || !robot.IsConnected) return (false, null, 0, 0, 0, 0);
  458. double pixelScaleX = 0;
  459. double pixelScaleY = 0;
  460. Matrix<double> RobotCameraRotationMatrix = null;
  461. //发送校准的参数
  462. await robot.CalibParameAsync(calibrationInfo.Pick, calibrationInfo.Speed, calibrationInfo.Accel, calibrationInfo.Power, calibrationInfo.WaitSuction, calibrationInfo.WaitBlow);
  463. (bool IsSuccess, Matrix<double> RotationMatrix, double ToolX, double ToolY, double PixelScaleX, double PixelScaleY) calibresult;
  464. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown)
  465. {
  466. calibresult = await CalibrateFixedDownCameraTool(calibrationInfo, robot, cancellationToken);
  467. }
  468. else if (calibrationInfo.CameraMount == Enums.CameraMount.FixedUp)
  469. {
  470. calibresult = await CalibrateFixedUpCameraTool(calibrationInfo, robot, cancellationToken);
  471. }
  472. else if (calibrationInfo.CameraMount == Enums.CameraMount.MobileJ4)
  473. {
  474. calibresult = await CalibrateJ4MovingDownCameraTool(calibrationInfo, robot, cancellationToken);
  475. }
  476. else
  477. {
  478. return (false, null, 0, 0, 0, 0);
  479. }
  480. if (!calibresult.IsSuccess)
  481. {
  482. return (false, null, 0, 0, 0, 0);
  483. }
  484. pixelScaleX = calibresult.PixelScaleX;
  485. pixelScaleY = calibresult.PixelScaleY;
  486. RobotCameraRotationMatrix = calibresult.RotationMatrix;
  487. return (true, RobotCameraRotationMatrix, calibresult.ToolX, calibresult.ToolY, pixelScaleX, pixelScaleY);
  488. }
  489. /// <summary>
  490. /// 自动识别机器人与相机图像坐标系的旋转矩阵。
  491. /// 使用若干已知的机器人位姿与对应图像坐标点,估计二者之间的旋转变换与像素尺度。
  492. /// </summary>
  493. /// <param name="calibrationInfo">包含用于识别的点集与图像处理配置。</param>
  494. /// <param name="robot">机器人接口,用于获取对应位姿或移动机器人到指定位置以采集数据。</param>
  495. /// <param name="cancellationToken">用于取消操作的标记。</param>
  496. /// <returns>
  497. /// 元组字段:
  498. /// IsSuccess - 是否成功识别;
  499. /// Matrix<double> - 旋转(或仿射)矩阵;
  500. /// PixelScaleX - X 方向像素尺度(如 mm/px);
  501. /// PixelScaleY - Y 方向像素尺度(如 mm/px)。
  502. /// </returns>
  503. public async Task<(bool IsSuccess, Matrix<double> RotationMatrix, double PixelScaleX, double PixelScaleY)> AutoIdentifyRobotCameraRotationMatrix(CalibrationInfo calibrationInfo, IRobot robot, CancellationToken cancellationToken)
  504. {
  505. //移动距离,单位mm
  506. float distance = ((float)calibrationInfo.Height) / 3;
  507. bool isSuccess = false;
  508. //-------------------------------------------P0-----------------------------------
  509. //移动机器人至中心点
  510. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown && !calibrationInfo.FixedDownCameraNoReturnToHome)
  511. {
  512. isSuccess = await robot.CalibMotionAsync(calibrationInfo.CenterPoint, 0);
  513. if (!isSuccess)
  514. {
  515. return (false, null, 0, 0);
  516. }
  517. //关闭吸气
  518. await robot.CalibOutIOAsync(false);
  519. }
  520. else
  521. {
  522. isSuccess = await robot.CalibMotionAsync(calibrationInfo.CenterPoint, calibrationInfo.CenterPoint.Z);
  523. if (!isSuccess)
  524. {
  525. return (false, null, 0, 0);
  526. }
  527. }
  528. RPoint RobotP0 = await robot.GetRobotPosAsync();
  529. RPoint RobotP1 = RobotP0.Clone();
  530. RPoint RobotP2 = RobotP0.Clone();
  531. RobotP1.X += distance;
  532. RobotP2.Y += distance;
  533. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown && !calibrationInfo.FixedDownCameraNoReturnToHome)
  534. {
  535. //移动机器人至待机位置
  536. isSuccess = await robot.CalibMotionAsync(calibrationInfo.HomePoint, 0);
  537. if (!isSuccess)
  538. {
  539. return (false, null, 0, 0);
  540. }
  541. }
  542. //相机拍照并处理图像
  543. var result = await CaptureAndProcessCallback();
  544. if (!result.IsSuccess)
  545. {
  546. return (false, null, 0, 0);
  547. }
  548. PointF P0 = new PointF((float)result.X, (float)result.Y);
  549. //-------------------------------------------P1-----------------------------------
  550. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown && !calibrationInfo.FixedDownCameraNoReturnToHome)
  551. {
  552. //移动机器人至P0
  553. isSuccess = await robot.CalibMotionAsync(RobotP0, 0);
  554. if (!isSuccess)
  555. {
  556. return (false, null, 0, 0);
  557. }
  558. if (cancellationToken.IsCancellationRequested)
  559. {
  560. return (false, null, 0, 0);
  561. }
  562. //打开吸气
  563. await robot.CalibOutIOAsync(true);
  564. //移动机器人至P1
  565. isSuccess = await robot.CalibMotionAsync(RobotP1, 0);
  566. if (!isSuccess)
  567. {
  568. return (false, null, 0, 0);
  569. }
  570. if (cancellationToken.IsCancellationRequested)
  571. {
  572. return (false, null, 0, 0);
  573. }
  574. //关闭吸气
  575. await robot.CalibOutIOAsync(false);
  576. //移动机器人至待机位置
  577. isSuccess = await robot.CalibMotionAsync(calibrationInfo.HomePoint, 0);
  578. if (!isSuccess)
  579. {
  580. return (false, null, 0, 0);
  581. }
  582. }
  583. else
  584. {
  585. //移动机器人至P1
  586. isSuccess = await robot.CalibMotionAsync(RobotP1, RobotP1.Z);
  587. if (!isSuccess)
  588. {
  589. return (false, null, 0, 0);
  590. }
  591. }
  592. if (cancellationToken.IsCancellationRequested)
  593. {
  594. return (false, null, 0, 0);
  595. }
  596. //相机拍照并处理图像
  597. result = await CaptureAndProcessCallback();
  598. if (!result.IsSuccess)
  599. {
  600. return (false, null, 0, 0);
  601. }
  602. PointF P1 = new PointF((float)result.X, (float)result.Y);
  603. //-------------------------------------------P2-----------------------------------
  604. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown && !calibrationInfo.FixedDownCameraNoReturnToHome)
  605. {
  606. //移动机器人至P1
  607. isSuccess = await robot.CalibMotionAsync(RobotP1, 0);
  608. if (!isSuccess)
  609. {
  610. return (false, null, 0, 0);
  611. }
  612. if (cancellationToken.IsCancellationRequested)
  613. {
  614. return (false, null, 0, 0);
  615. }
  616. //打开吸气
  617. await robot.CalibOutIOAsync(true);
  618. //移动机器人至P2
  619. isSuccess = await robot.CalibMotionAsync(RobotP2, 0);
  620. if (!isSuccess)
  621. {
  622. return (false, null, 0, 0);
  623. }
  624. if (cancellationToken.IsCancellationRequested)
  625. {
  626. return (false, null, 0, 0);
  627. }
  628. //关闭吸气
  629. await robot.CalibOutIOAsync(false);
  630. //移动机器人至待机位置
  631. isSuccess = await robot.CalibMotionAsync(calibrationInfo.HomePoint, 0);
  632. if (!isSuccess)
  633. {
  634. return (false, null, 0, 0);
  635. }
  636. }
  637. else
  638. {
  639. //移动机器人至P2
  640. isSuccess = await robot.CalibMotionAsync(RobotP2, RobotP2.Z);
  641. if (!isSuccess)
  642. {
  643. return (false, null, 0, 0);
  644. }
  645. }
  646. if (cancellationToken.IsCancellationRequested)
  647. {
  648. return (false, null, 0, 0);
  649. }
  650. //相机拍照并处理图像
  651. result = await CaptureAndProcessCallback();
  652. if (!result.IsSuccess)
  653. {
  654. return (false, null, 0, 0);
  655. }
  656. PointF P2 = new PointF((float)result.X, (float)result.Y);
  657. //-------------------------------------------P0--------------------------------------
  658. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown && !calibrationInfo.FixedDownCameraNoReturnToHome)
  659. {
  660. //移动机器人至P2
  661. isSuccess = await robot.CalibMotionAsync(RobotP2, 0);
  662. if (!isSuccess)
  663. {
  664. return (false, null, 0, 0);
  665. }
  666. if (cancellationToken.IsCancellationRequested)
  667. {
  668. return (false, null, 0, 0);
  669. }
  670. //打开吸气
  671. await robot.CalibOutIOAsync(true);
  672. //移动机器人至P0
  673. isSuccess = await robot.CalibMotionAsync(RobotP0, 0);
  674. if (!isSuccess)
  675. {
  676. return (false, null, 0, 0);
  677. }
  678. if (cancellationToken.IsCancellationRequested)
  679. {
  680. return (false, null, 0, 0);
  681. }
  682. //关闭吸气
  683. await robot.CalibOutIOAsync(false);
  684. //移动机器人至待机位置
  685. isSuccess = await robot.CalibMotionAsync(calibrationInfo.HomePoint, 0);
  686. if (!isSuccess)
  687. {
  688. return (false, null, 0, 0);
  689. }
  690. }
  691. else
  692. {
  693. //移动机器人至P0
  694. isSuccess = await robot.CalibMotionAsync(RobotP0, RobotP0.Z);
  695. if (!isSuccess)
  696. {
  697. return (false, null, 0, 0);
  698. }
  699. }
  700. if (cancellationToken.IsCancellationRequested)
  701. {
  702. return (false, null, 0, 0);
  703. }
  704. //-----------------------------------------计算旋转矩阵------------------------------
  705. Matrix<double> M = CalculateImageRotationMatrix(P0, P1, P2);
  706. //Theta = Acos(M11)
  707. //相机与机器人坐标系角度:Theta * 180 / PI
  708. //-----------------------------------------计算像素和毫米比例-----------------------
  709. Vector<double> P11 = Vector<double>.Build.Dense(new double[] { distance, 0 });
  710. Vector<double> P21 = Vector<double>.Build.Dense(new double[] { P1.X, P1.Y }) - Vector<double>.Build.Dense(new double[] { P0.X, P0.Y });
  711. double PixelScaleX = Math.Abs((M * P11)[0] / P21[0]); //mm/pixel
  712. double PixelScaleY = Math.Abs((M * P11)[1] / P21[1]); //mm/pixel
  713. return (true, M, PixelScaleX, PixelScaleY);
  714. }
  715. /// <summary>
  716. /// 根据图像上的三个点计算图像坐标系的旋转矩阵。
  717. /// 通常通过已知三点在像素坐标系中的位置计算出局部旋转/仿射变换,用于后续坐标换算。
  718. /// </summary>
  719. /// <param name="P0">图像中的点 0(像素坐标)。</param>
  720. /// <param name="P1">图像中的点 1(像素坐标)。</param>
  721. /// <param name="P2">图像中的点 2(像素坐标)。</param>
  722. /// <returns>表示图像旋转或仿射变换的 3x3 或 2x2 矩阵(具体尺寸由实现决定)。</returns>
  723. public Matrix<double> CalculateImageRotationMatrix(PointF P0, PointF P1, PointF P2)
  724. {
  725. //1.将点转换为向量表示:
  726. Vector<double> Point0 = Vector<double>.Build.Dense(new double[] { (double)P0.X, (double)P0.Y });
  727. Vector<double> Point1 = Vector<double>.Build.Dense(new double[] { (double)P1.X, (double)P1.Y });
  728. Vector<double> Point2 = Vector<double>.Build.Dense(new double[] { (double)P2.X, (double)P2.Y });
  729. //2.计算坐标系的基向量:
  730. Vector<double> E1 = (Point1 - Point0) / (Point1 - Point0).L2Norm();
  731. Vector<double> E2 = (Point2 - Point0 - E1.DotProduct(Point2 - Point0) * E1) / (Point2 - Point0 - E1.DotProduct(Point2 - Point0) * E1).L2Norm();
  732. //3.计算旋转矩阵:我们可以使用基向量组成的矩阵作为旋转矩阵。具体地,我们可以将基向量 e1 和 e2 分别作为新坐标系下的 $x$ 和 $y$ 轴,然后组成一个矩阵,再对这个矩阵求逆即可得到旋转矩阵。代码实现:
  733. Matrix<double> R = Matrix<double>.Build.DenseOfColumnVectors(E1, E2);
  734. return R;
  735. }
  736. /// <summary>
  737. /// 自动移动机器人至图像中心点位置。
  738. /// 典型用例为将相机视野中心对应的工作点转换为机器人坐标并移动机器人到该点以便校准或抓取。
  739. /// </summary>
  740. /// <param name="robot">机器人接口,用于执行移动命令并返回当前位置。</param>
  741. /// <param name="matrix">图像坐标系到机器人坐标系的转换矩阵(旋转/仿射)。</param>
  742. /// <param name="PixelScaleX">像素到物理距离在 X 方向的缩放因子(如 mm/px)。</param>
  743. /// <param name="PixelScaleY">像素到物理距离在 Y 方向的缩放因子(如 mm/px)。</param>
  744. /// <param name="cancellationToken">用于取消操作的标记。</param>
  745. /// <returns>
  746. /// 元组字段:
  747. /// IsSuccess - 是否成功移动到图像中心;
  748. /// X - 移动后机器人在基坐标系下的 X;
  749. /// Y - 移动后机器人在基坐标系下的 Y;
  750. /// U - 移动后机器人的朝向(工具角度)。
  751. /// </returns>
  752. public async Task<(bool IsSuccess, double X, double Y, double U)> AutoMoveRobotToImageCenter(IRobot robot, Matrix<double> matrix, double PixelScaleX, double PixelScaleY, CancellationToken cancellationToken)
  753. {
  754. bool IsFinish = false;
  755. //相机拍照并处理图像
  756. var result = await CaptureAndProcessCallback();
  757. if (!result.IsSuccess)
  758. {
  759. return (false, 0, 0, 0);
  760. }
  761. PointF curPixel = new PointF((float)result.X, (float)result.Y);
  762. //中心点像素坐标
  763. PointF P0 = new PointF((float)(result.ImageWidth / 2), (float)(result.ImageHeight / 2));
  764. bool isSuccess = false;
  765. var curpos = robot.GetRobotPos();
  766. for (int i = 0; i < 10; i++)
  767. {
  768. //计算需要移动的偏移量,目的是逼近中心点
  769. double offsetx = (P0.X - curPixel.X) * PixelScaleX;
  770. double offsety = (P0.Y - curPixel.Y) * PixelScaleY;
  771. var pos = matrix.Inverse() * Vector<double>.Build.Dense(new double[] { offsetx, offsety });
  772. curpos.X += (float)pos[0];
  773. curpos.Y += (float)pos[1];
  774. //机器人移动
  775. isSuccess = await robot.CalibMotionAsync(curpos, curpos.Z);
  776. if (!isSuccess)
  777. {
  778. return (false, 0, 0, 0);
  779. }
  780. await Task.Delay(200);
  781. result = await CaptureAndProcessCallback();
  782. if (!result.IsSuccess)
  783. {
  784. return (false, 0, 0, 0);
  785. }
  786. curPixel = new PointF((float)result.X, (float)result.Y);
  787. offsetx = P0.X - curPixel.X;
  788. offsety = P0.Y - curPixel.Y;
  789. //await Console.Out.WriteLineAsync($"移动后的差异:{offsetx},{offsety}");
  790. if (Math.Abs(offsetx) < 3 && Math.Abs(offsetx) < 3)
  791. {
  792. IsFinish = true;
  793. break;
  794. }
  795. if (cancellationToken.IsCancellationRequested)
  796. {
  797. return (false, 0, 0, 0);
  798. }
  799. }
  800. if (IsFinish)
  801. {
  802. var finalPos = robot.GetRobotPos();
  803. return (true, finalPos.X, finalPos.Y, finalPos.U);
  804. }
  805. else
  806. {
  807. return (false, 0, 0, 0);
  808. }
  809. }
  810. /// <summary>
  811. /// 自动九点标定。
  812. /// 使用 9 个采样点进行仿射/透视变换求解,以提高标定精度并生成验证用的九点数据结构。
  813. /// </summary>
  814. /// <param name="calibrationInfo">九点标定的配置信息(点列、采集顺序、图像处理参数等)。</param>
  815. /// <param name="robot">机器人接口。</param>
  816. /// <param name="cancellationToken">用于取消操作的标记。</param>
  817. /// <returns>
  818. /// 元组字段:
  819. /// IsSuccess - 标定是否成功;
  820. /// AffineTransformationMaterial - 求解得到的仿射变换矩阵;
  821. /// NinePoint - 九点标定结果封装(像素点与对应的机器人位姿);
  822. /// Result - 详细的标定结果与统计信息(误差、残差等)。
  823. /// </returns>
  824. public async Task<(bool IsSuccess, Matrix<double> AffineTransformationMaterial, RobotPixelPoint[] NinePoint, CalibrationResult Result)> AutoNinePointCalibration(CalibrationInfo calibrationInfo, IRobot robot, CancellationToken cancellationToken)
  825. {
  826. if (calibrationInfo == null) return (false, null, null, null);
  827. if (robot == null || !robot.IsConnected) return (false, null, null, null);
  828. //发送校准的参数
  829. await robot.CalibParameAsync(calibrationInfo.Pick, calibrationInfo.Speed, calibrationInfo.Accel, calibrationInfo.Power, calibrationInfo.WaitSuction, calibrationInfo.WaitBlow);
  830. bool isSuccess = false;
  831. var P0 = Vector<double>.Build.Dense(new double[] { calibrationInfo.CenterPoint.X, calibrationInfo.CenterPoint.Y });
  832. if (robot.Brand == Enums.RobotBrand.XYZ_Platform)
  833. {
  834. //移动机器人至中心位置
  835. isSuccess = await robot.CalibMotionAsync(calibrationInfo.CenterPoint, 0);
  836. if (!isSuccess)
  837. {
  838. return (false, null, null, null);
  839. }
  840. if (cancellationToken.IsCancellationRequested)
  841. {
  842. return (false, null, null, null);
  843. }
  844. //计算相机与机器人坐标系的旋转矩阵
  845. var robotCameraRotationMatrix = await AutoIdentifyRobotCameraRotationMatrix(calibrationInfo, robot, cancellationToken);
  846. if (!robotCameraRotationMatrix.IsSuccess)
  847. {
  848. return (false, null, null, null);
  849. }
  850. //自动移动机器人至图像中心点位置
  851. var autoMoveResult = await AutoMoveRobotToImageCenter(robot, robotCameraRotationMatrix.RotationMatrix, robotCameraRotationMatrix.PixelScaleX, robotCameraRotationMatrix.PixelScaleY, cancellationToken);
  852. if (!autoMoveResult.IsSuccess)
  853. {
  854. return (false, null, null, null);
  855. }
  856. calibrationInfo.CenterPoint.X = (float)autoMoveResult.X;
  857. calibrationInfo.CenterPoint.Y = (float)autoMoveResult.Y;
  858. if (calibrationInfo.CameraMount != Enums.CameraMount.MobileDown_XYPlatform)
  859. {
  860. calibrationInfo.MarkPoint = calibrationInfo.CenterPoint.Clone();
  861. }
  862. calibrationInfo.RobotCameraRotationMatrix = robotCameraRotationMatrix.RotationMatrix.ToArray();
  863. }
  864. else if (calibrationInfo.CameraMount== Enums.CameraMount.MobileDown_XYPlatform)
  865. {
  866. //移动机器人至中心位置
  867. isSuccess = await robot.CalibMotionAsync(calibrationInfo.CenterPoint, 0);
  868. if (!isSuccess)
  869. {
  870. return (false, null, null, null);
  871. }
  872. if (cancellationToken.IsCancellationRequested)
  873. {
  874. return (false, null, null, null);
  875. }
  876. //计算相机与机器人坐标系的旋转矩阵
  877. var robotCameraRotationMatrix = await AutoIdentifyRobotCameraRotationMatrix(calibrationInfo, robot, cancellationToken);
  878. if (!robotCameraRotationMatrix.IsSuccess)
  879. {
  880. return (false, null, null, null);
  881. }
  882. //自动移动机器人至图像中心点位置
  883. var autoMoveResult = await AutoMoveRobotToImageCenter(robot, robotCameraRotationMatrix.RotationMatrix, robotCameraRotationMatrix.PixelScaleX, robotCameraRotationMatrix.PixelScaleY, cancellationToken);
  884. if (!autoMoveResult.IsSuccess)
  885. {
  886. return (false, null, null, null);
  887. }
  888. calibrationInfo.CenterPoint.X = (float)autoMoveResult.X;
  889. calibrationInfo.CenterPoint.Y = (float)autoMoveResult.Y;
  890. //calibrationInfo.MarkPoint = calibrationInfo.CenterPoint.Clone();
  891. calibrationInfo.RobotCameraRotationMatrix = robotCameraRotationMatrix.RotationMatrix.ToArray();
  892. }
  893. double IntervalX = calibrationInfo.Width / 2;
  894. double IntervalY = calibrationInfo.Height / 2;
  895. Vector<double> P1 = Vector<double>.Build.Dense(new double[] { -IntervalX, -IntervalY });
  896. Vector<double> P2 = Vector<double>.Build.Dense(new double[] { 0, -IntervalY });
  897. Vector<double> P3 = Vector<double>.Build.Dense(new double[] { IntervalX, -IntervalY });
  898. Vector<double> P4 = Vector<double>.Build.Dense(new double[] { IntervalX, 0 });
  899. Vector<double> P5 = Vector<double>.Build.Dense(new double[] { 0, 0 });
  900. Vector<double> P6 = Vector<double>.Build.Dense(new double[] { -IntervalX, 0 });
  901. Vector<double> P7 = Vector<double>.Build.Dense(new double[] { -IntervalX, IntervalY });
  902. Vector<double> P8 = Vector<double>.Build.Dense(new double[] { 0, IntervalY });
  903. Vector<double> P9 = Vector<double>.Build.Dense(new double[] { IntervalX, IntervalY });
  904. /// <summary>
  905. /// 机器人与相机之间的旋转矩阵
  906. /// </summary>
  907. Matrix<double> RobotCameraRotationMatrix = Matrix<double>.Build.DenseOfArray(calibrationInfo.RobotCameraRotationMatrix);
  908. //1
  909. P1 = RobotCameraRotationMatrix.Inverse() * P1 + P0;
  910. //2
  911. P2 = RobotCameraRotationMatrix.Inverse() * P2 + P0;
  912. //3
  913. P3 = RobotCameraRotationMatrix.Inverse() * P3 + P0;
  914. //4
  915. P4 = RobotCameraRotationMatrix.Inverse() * P4 + P0;
  916. //5
  917. P5 = RobotCameraRotationMatrix.Inverse() * P5 + P0;
  918. //6
  919. P6 = RobotCameraRotationMatrix.Inverse() * P6 + P0;
  920. //7
  921. P7 = RobotCameraRotationMatrix.Inverse() * P7 + P0;
  922. //8
  923. P8 = RobotCameraRotationMatrix.Inverse() * P8 + P0;
  924. //9
  925. P9 = RobotCameraRotationMatrix.Inverse() * P9 + P0;
  926. List<RPoint> rPoints = new List<RPoint>();
  927. rPoints.Add(new RPoint()
  928. {
  929. Number = 1,
  930. X = (float)P1[0],
  931. Y = (float)P1[1],
  932. Z = calibrationInfo.CenterPoint.Z,
  933. U = calibrationInfo.CenterPoint.U,
  934. V = calibrationInfo.CenterPoint.V,
  935. W = calibrationInfo.CenterPoint.W,
  936. Hand = calibrationInfo.CenterPoint.Hand,
  937. Local = calibrationInfo.CenterPoint.Local,
  938. Tool = calibrationInfo.CenterPoint.Tool
  939. });
  940. rPoints.Add(new RPoint()
  941. {
  942. Number = 2,
  943. X = (float)P2[0],
  944. Y = (float)P2[1],
  945. Z = calibrationInfo.CenterPoint.Z,
  946. U = calibrationInfo.CenterPoint.U,
  947. V = calibrationInfo.CenterPoint.V,
  948. W = calibrationInfo.CenterPoint.W,
  949. Hand = calibrationInfo.CenterPoint.Hand,
  950. Local = calibrationInfo.CenterPoint.Local,
  951. Tool = calibrationInfo.CenterPoint.Tool
  952. });
  953. rPoints.Add(new RPoint()
  954. {
  955. Number = 3,
  956. X = (float)P3[0],
  957. Y = (float)P3[1],
  958. Z = calibrationInfo.CenterPoint.Z,
  959. U = calibrationInfo.CenterPoint.U,
  960. V = calibrationInfo.CenterPoint.V,
  961. W = calibrationInfo.CenterPoint.W,
  962. Hand = calibrationInfo.CenterPoint.Hand,
  963. Local = calibrationInfo.CenterPoint.Local,
  964. Tool = calibrationInfo.CenterPoint.Tool
  965. });
  966. rPoints.Add(new RPoint()
  967. {
  968. Number = 4,
  969. X = (float)P4[0],
  970. Y = (float)P4[1],
  971. Z = calibrationInfo.CenterPoint.Z,
  972. U = calibrationInfo.CenterPoint.U,
  973. V = calibrationInfo.CenterPoint.V,
  974. W = calibrationInfo.CenterPoint.W,
  975. Hand = calibrationInfo.CenterPoint.Hand,
  976. Local = calibrationInfo.CenterPoint.Local,
  977. Tool = calibrationInfo.CenterPoint.Tool
  978. });
  979. rPoints.Add(new RPoint()
  980. {
  981. Number = 5,
  982. X = (float)P5[0],
  983. Y = (float)P5[1],
  984. Z = calibrationInfo.CenterPoint.Z,
  985. U = calibrationInfo.CenterPoint.U,
  986. V = calibrationInfo.CenterPoint.V,
  987. W = calibrationInfo.CenterPoint.W,
  988. Hand = calibrationInfo.CenterPoint.Hand,
  989. Local = calibrationInfo.CenterPoint.Local,
  990. Tool = calibrationInfo.CenterPoint.Tool
  991. });
  992. rPoints.Add(new RPoint()
  993. {
  994. Number = 6,
  995. X = (float)P6[0],
  996. Y = (float)P6[1],
  997. Z = calibrationInfo.CenterPoint.Z,
  998. U = calibrationInfo.CenterPoint.U,
  999. V = calibrationInfo.CenterPoint.V,
  1000. W = calibrationInfo.CenterPoint.W,
  1001. Hand = calibrationInfo.CenterPoint.Hand,
  1002. Local = calibrationInfo.CenterPoint.Local,
  1003. Tool = calibrationInfo.CenterPoint.Tool
  1004. });
  1005. rPoints.Add(new RPoint()
  1006. {
  1007. Number = 7,
  1008. X = (float)P7[0],
  1009. Y = (float)P7[1],
  1010. Z = calibrationInfo.CenterPoint.Z,
  1011. U = calibrationInfo.CenterPoint.U,
  1012. V = calibrationInfo.CenterPoint.V,
  1013. W = calibrationInfo.CenterPoint.W,
  1014. Hand = calibrationInfo.CenterPoint.Hand,
  1015. Local = calibrationInfo.CenterPoint.Local,
  1016. Tool = calibrationInfo.CenterPoint.Tool
  1017. });
  1018. rPoints.Add(new RPoint()
  1019. {
  1020. Number = 8,
  1021. X = (float)P8[0],
  1022. Y = (float)P8[1],
  1023. Z = calibrationInfo.CenterPoint.Z,
  1024. U = calibrationInfo.CenterPoint.U,
  1025. V = calibrationInfo.CenterPoint.V,
  1026. W = calibrationInfo.CenterPoint.W,
  1027. Hand = calibrationInfo.CenterPoint.Hand,
  1028. Local = calibrationInfo.CenterPoint.Local,
  1029. Tool = calibrationInfo.CenterPoint.Tool
  1030. });
  1031. rPoints.Add(new RPoint()
  1032. {
  1033. Number = 9,
  1034. X = (float)P9[0],
  1035. Y = (float)P9[1],
  1036. Z = calibrationInfo.CenterPoint.Z,
  1037. U = calibrationInfo.CenterPoint.U,
  1038. V = calibrationInfo.CenterPoint.V,
  1039. W = calibrationInfo.CenterPoint.W,
  1040. Hand = calibrationInfo.CenterPoint.Hand,
  1041. Local = calibrationInfo.CenterPoint.Local,
  1042. Tool = calibrationInfo.CenterPoint.Tool
  1043. });
  1044. List<PointF> PixelPoslist = new List<PointF>();
  1045. if (calibrationInfo.CalibPoints == null)
  1046. {
  1047. calibrationInfo.CalibPoints = new System.Collections.ObjectModel.ObservableCollection<RobotPixelPoint>();
  1048. }
  1049. else
  1050. {
  1051. calibrationInfo.CalibPoints.Clear();
  1052. }
  1053. //移动相机标定时,需要先记住特征点的绝对坐标(相对于机器人坐标系) P0
  1054. if (calibrationInfo.CameraMount == Enums.CameraMount.MobileJ4)
  1055. {
  1056. //移动机器人至中心位置
  1057. isSuccess = await robot.CalibMotionAsync(calibrationInfo.CenterPoint, 0);
  1058. if (!isSuccess)
  1059. {
  1060. return (false, null, null, null);
  1061. }
  1062. if (cancellationToken.IsCancellationRequested)
  1063. {
  1064. return (false, null, null, null);
  1065. }
  1066. //将当前机器人Tool 0下的坐标转换成工具坐标下的坐标,并记录下来
  1067. ToolCoord toolCoord = new ToolCoord();
  1068. toolCoord.SetTool(calibrationInfo.Tool.X, calibrationInfo.Tool.Y);
  1069. Vector<double> p = Vector<double>.Build.Dense(new double[] { calibrationInfo.CenterPoint.X, calibrationInfo.CenterPoint.Y }); ;
  1070. var pn = toolCoord.GetToolnCoord(p, calibrationInfo.CenterPoint.U);
  1071. if (calibrationInfo.MarkPoint == null)
  1072. {
  1073. calibrationInfo.MarkPoint = new RPoint();
  1074. }
  1075. calibrationInfo.MarkPoint = calibrationInfo.CenterPoint.Clone();
  1076. calibrationInfo.MarkPoint.X = (float)pn[0];
  1077. calibrationInfo.MarkPoint.Y = (float)pn[1];
  1078. }
  1079. for (int i = 0; i < rPoints.Count; i++)
  1080. {
  1081. //移动机器人至i
  1082. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown)
  1083. {
  1084. isSuccess = await robot.CalibMotionAsync(rPoints[i], 0);
  1085. if (!isSuccess)
  1086. {
  1087. return (false, null, null, null);
  1088. }
  1089. if (cancellationToken.IsCancellationRequested)
  1090. {
  1091. return (false, null, null, null);
  1092. }
  1093. //关闭吸气
  1094. await robot.CalibOutIOAsync(false);
  1095. //移动机器人至待机位置
  1096. isSuccess = await robot.CalibMotionAsync(calibrationInfo.HomePoint, 0);
  1097. if (!isSuccess)
  1098. {
  1099. return (false, null, null, null);
  1100. }
  1101. }
  1102. else
  1103. {
  1104. isSuccess = await robot.CalibMotionAsync(rPoints[i], rPoints[i].Z);
  1105. if (!isSuccess)
  1106. {
  1107. return (false, null, null, null);
  1108. }
  1109. }
  1110. if (cancellationToken.IsCancellationRequested)
  1111. {
  1112. return (false, null, null, null);
  1113. }
  1114. //相机拍照并处理图像
  1115. var photoResult = await CaptureAndProcessCallback();
  1116. if (!photoResult.IsSuccess)
  1117. {
  1118. return (false, null, null, null);
  1119. }
  1120. PointF curPixel = new PointF((float)(photoResult.X), (float)(photoResult.Y));
  1121. PixelPoslist.Add(curPixel);
  1122. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown)
  1123. {
  1124. //移动机器人至i
  1125. isSuccess = await robot.CalibMotionAsync(rPoints[i], 0);
  1126. if (!isSuccess)
  1127. {
  1128. return (false, null, null, null);
  1129. }
  1130. if (cancellationToken.IsCancellationRequested)
  1131. {
  1132. return (false, null, null, null);
  1133. }
  1134. //打开吸气
  1135. await robot.CalibOutIOAsync(true);
  1136. }
  1137. calibrationInfo.CalibPoints.Add(new RobotPixelPoint()
  1138. {
  1139. Number = i + 1,
  1140. Robot = new System.Drawing.PointF(rPoints[i].X, rPoints[i].Y),
  1141. Pixel = new System.Drawing.PointF(PixelPoslist[i].X, PixelPoslist[i].Y)
  1142. });
  1143. if (cancellationToken.IsCancellationRequested)
  1144. {
  1145. return (false, null, null, null);
  1146. }
  1147. }
  1148. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown)
  1149. {
  1150. //移动机器人至待机位置
  1151. isSuccess = await robot.CalibMotionAsync(calibrationInfo.HomePoint, 0);
  1152. if (!isSuccess)
  1153. {
  1154. return (false, null, null, null);
  1155. }
  1156. }
  1157. if (cancellationToken.IsCancellationRequested)
  1158. {
  1159. return (false, null, null, null);
  1160. }
  1161. Mat matPixel = new Mat(9, 2, MatType.CV_64F);
  1162. Mat matRobot = new Mat(9, 2, MatType.CV_64F);
  1163. for (int i = 0; i < 9; i++)
  1164. {
  1165. matPixel.Set<double>(i, 0, PixelPoslist[i].X);
  1166. matPixel.Set<double>(i, 1, PixelPoslist[i].Y);
  1167. //如果是移动相机拍照需要将机器人的9点进行一个变换,为了达到Mark点动,相机不动的目的
  1168. if (calibrationInfo.CameraMount == Enums.CameraMount.MobileJ4)
  1169. {
  1170. //计算每个点相对于第一个点移动的偏移量,将这个偏移量
  1171. double ox = rPoints[i].X - rPoints[0].X;
  1172. double oy = rPoints[i].Y - rPoints[0].Y;
  1173. matRobot.Set<double>(i, 0, calibrationInfo.MarkPoint.X - ox);
  1174. matRobot.Set<double>(i, 1, calibrationInfo.MarkPoint.Y - oy);
  1175. }
  1176. else if (calibrationInfo.CameraMount == Enums.CameraMount.MobileDown_XYPlatform)
  1177. {
  1178. //计算每个点相对于第一个点移动的偏移量,将这个偏移量
  1179. double ox = rPoints[i].X - rPoints[0].X;
  1180. double oy = rPoints[i].Y - rPoints[0].Y;
  1181. matRobot.Set<double>(i, 0, calibrationInfo.MarkPoint.X - ox);
  1182. matRobot.Set<double>(i, 1, calibrationInfo.MarkPoint.Y - oy);
  1183. }
  1184. else
  1185. {
  1186. //将tool 0下的坐标转换成Tool n下的坐标
  1187. if (calibrationInfo.Tool != null)
  1188. {
  1189. ToolCoord toolCoord = new ToolCoord();
  1190. toolCoord.SetTool(calibrationInfo.Tool.X, calibrationInfo.Tool.Y);
  1191. double u1 = rPoints[i].U;
  1192. if (robot.Brand == Enums.RobotBrand.Schneider)
  1193. {
  1194. u1 *= -1;
  1195. }
  1196. var trans = toolCoord.GetToolnCoord(Vector<double>.Build.Dense(new double[] { rPoints[i].X, rPoints[i].Y }), u1);
  1197. matRobot.Set<double>(i, 0, trans[0]);
  1198. matRobot.Set<double>(i, 1, trans[1]);
  1199. }
  1200. else
  1201. {
  1202. matRobot.Set<double>(i, 0, rPoints[i].X);
  1203. matRobot.Set<double>(i, 1, rPoints[i].Y);
  1204. }
  1205. }
  1206. }
  1207. Mat mat2d = Cv2.EstimateAffine2D(matPixel, matRobot);
  1208. 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) } };
  1209. Matrix<double> matrix = Matrix<double>.Build.DenseOfArray(vv);
  1210. calibrationInfo.AffineTransformationMaterial = matrix.ToArray();
  1211. //mat2d.Get<double>(0, 0):表示x方向上的缩放。大于 1,则表示进行了放大操作;小于 1,则表示进行了缩小操作;等于 1,则表示没有进行缩放操作。
  1212. //mat2d.Get<double>(0, 1):表示垂直错切参数,与M[1,0]一起用于计算旋转角度
  1213. //mat2d.Get<double>(0, 2):表示x方向上的平移。
  1214. //mat2d.Get<double>(1, 0):表示水平错切参数,与M[0,1]一起用于计算旋转角度。
  1215. //mat2d.Get<double>(1, 1):表示y方向上的缩放。大于 1,则表示进行了放大操作;小于 1,则表示进行了缩小操作;等于 1,则表示没有进行缩放操作。
  1216. //mat2d.Get<double>(1, 2):表示y方向上的平移。
  1217. //double RotationAngle= Math.Atan2(mat2d.Get<double>(1, 0), mat2d.Get<double>(0, 1))*180/Math.PI; //旋转角度
  1218. //RMS(均方根值) 标定偏差
  1219. double sumX = 0, sumY = 0;
  1220. List<double> differenceX = new List<double>();
  1221. List<double> differenceY = new List<double>();
  1222. for (int i = 0; i < 9; i++)
  1223. {
  1224. var pixel = Vector<double>.Build.Dense(new double[] { PixelPoslist[i].X, PixelPoslist[i].Y, 1 });
  1225. if (calibrationInfo.CameraMount == Enums.CameraMount.MobileJ4)
  1226. {
  1227. //计算每个点相对于第一个点移动的偏移量,Mark点减去这个偏移量及是图像中对应的实际坐标
  1228. double ox = rPoints[i].X - rPoints[0].X;
  1229. double oy = rPoints[i].Y - rPoints[0].Y;
  1230. differenceX.Add((calibrationInfo.MarkPoint.X - ox) - (matrix * pixel)[0]);
  1231. differenceY.Add((calibrationInfo.MarkPoint.Y - oy) - (matrix * pixel)[1]);
  1232. }
  1233. else if (calibrationInfo.CameraMount == Enums.CameraMount.MobileDown_XYPlatform)
  1234. {
  1235. double ox = rPoints[i].X - rPoints[0].X;
  1236. double oy = rPoints[i].Y - rPoints[0].Y;
  1237. differenceX.Add((calibrationInfo.MarkPoint.X - ox) - (matrix * pixel)[0]);
  1238. differenceY.Add((calibrationInfo.MarkPoint.Y - oy) - (matrix * pixel)[1]);
  1239. }
  1240. else
  1241. {
  1242. //将tool 0下的坐标转换成Tool n下的坐标
  1243. if (calibrationInfo.Tool != null)
  1244. {
  1245. ToolCoord toolCoord = new ToolCoord();
  1246. toolCoord.SetTool(calibrationInfo.Tool.X, calibrationInfo.Tool.Y);
  1247. double u1 = rPoints[i].U;
  1248. if (robot.Brand == Enums.RobotBrand.Schneider)
  1249. {
  1250. u1 *= -1;
  1251. }
  1252. var trans = toolCoord.GetToolnCoord(Vector<double>.Build.Dense(new double[] { rPoints[i].X, rPoints[i].Y }), u1);
  1253. differenceX.Add(trans[0] - (matrix * pixel)[0]);
  1254. differenceY.Add(trans[1] - (matrix * pixel)[1]);
  1255. }
  1256. else
  1257. {
  1258. differenceX.Add(rPoints[i].X - (matrix * pixel)[0]);
  1259. differenceY.Add(rPoints[i].Y - (matrix * pixel)[1]);
  1260. }
  1261. }
  1262. sumX += Math.Pow(differenceX[differenceX.Count - 1], 2);
  1263. sumY += Math.Pow(differenceY[differenceY.Count - 1], 2);
  1264. }
  1265. double rmsX, rmsY;
  1266. rmsX = Math.Sqrt(sumX / 9);
  1267. rmsY = Math.Sqrt(sumY / 9);
  1268. CalibrationResult calibrationResult = new CalibrationResult();
  1269. calibrationResult.ScaleX = mat2d.Get<double>(0, 0);
  1270. calibrationResult.ScaleY = mat2d.Get<double>(1, 1);
  1271. calibrationResult.TranslationX = mat2d.Get<double>(0, 2);
  1272. calibrationResult.TranslationY = mat2d.Get<double>(1, 2);
  1273. calibrationResult.Rotate = Math.Atan2(mat2d.Get<double>(1, 0), mat2d.Get<double>(0, 1)) * 180 / Math.PI;
  1274. calibrationResult.RMSX = rmsX;
  1275. calibrationResult.RMSY = rmsY;
  1276. calibrationResult.MaxErrorValueX = differenceX.Max();
  1277. calibrationResult.MaxErrorValueY = differenceY.Max();
  1278. calibrationInfo.CalibrationResult = calibrationResult;
  1279. return (true, matrix, calibrationInfo.CalibPoints.ToArray(), calibrationResult);
  1280. }
  1281. /// <summary>
  1282. /// 执行校准验证。
  1283. /// 使用当前标定参数在一组验证点上计算误差并返回测试结果,用于评估标定质量。
  1284. /// </summary>
  1285. /// <param name="calibrationInfo">用于验证的点集与验收标准配置。</param>
  1286. /// <param name="robot">机器人接口,用于在验证过程中获取或移动到参考位姿。</param>
  1287. /// <param name="cancellationToken">用于取消操作的标记。</param>
  1288. /// <returns>元组字段:IsSuccess 表示验证是否通过;Result 包含详细的验证统计与误差分析。</returns>
  1289. public async Task<(bool IsSuccess, CalibrationTestResult Result)> ExecuteCalibrationValidation(CalibrationInfo calibrationInfo, IRobot robot, CancellationToken cancellationToken)
  1290. {
  1291. if (calibrationInfo == null) return (false, null);
  1292. if (robot == null || !robot.IsConnected) return (false, null);
  1293. double IntervalX = calibrationInfo.Width * 0.90 / 2;
  1294. double IntervalY = calibrationInfo.Height * 0.90 / 2;
  1295. Vector<double> P1 = Vector<double>.Build.Dense(new double[] { -IntervalX, -IntervalY });
  1296. Vector<double> P2 = Vector<double>.Build.Dense(new double[] { IntervalX, -IntervalY });
  1297. Vector<double> P3 = Vector<double>.Build.Dense(new double[] { 0, 0 });
  1298. Vector<double> P4 = Vector<double>.Build.Dense(new double[] { -IntervalX, IntervalY });
  1299. Vector<double> P5 = Vector<double>.Build.Dense(new double[] { IntervalX, IntervalY });
  1300. var P0 = Vector<double>.Build.Dense(new double[] { calibrationInfo.CenterPoint.X, calibrationInfo.CenterPoint.Y });
  1301. /// <summary>
  1302. /// 机器人与相机之间的旋转矩阵
  1303. /// </summary>
  1304. Matrix<double> RobotCameraRotationMatrix = Matrix<double>.Build.DenseOfArray(calibrationInfo.RobotCameraRotationMatrix);
  1305. //1
  1306. P1 = RobotCameraRotationMatrix.Inverse() * P1 + P0;
  1307. //2
  1308. P2 = RobotCameraRotationMatrix.Inverse() * P2 + P0;
  1309. //3
  1310. P3 = RobotCameraRotationMatrix.Inverse() * P3 + P0;
  1311. //4
  1312. P4 = RobotCameraRotationMatrix.Inverse() * P4 + P0;
  1313. //5
  1314. P5 = RobotCameraRotationMatrix.Inverse() * P5 + P0;
  1315. List<RPoint> rPoints = new List<RPoint>();
  1316. rPoints.Add(new RPoint()
  1317. {
  1318. Number = 1,
  1319. X = (float)P1[0],
  1320. Y = (float)P1[1],
  1321. Z = calibrationInfo.CenterPoint.Z,
  1322. U = calibrationInfo.CenterPoint.U,
  1323. V = calibrationInfo.CenterPoint.V,
  1324. W = calibrationInfo.CenterPoint.W,
  1325. Hand = calibrationInfo.CenterPoint.Hand,
  1326. Local = calibrationInfo.CenterPoint.Local,
  1327. Tool = calibrationInfo.CenterPoint.Tool
  1328. });
  1329. rPoints.Add(new RPoint()
  1330. {
  1331. Number = 2,
  1332. X = (float)P2[0],
  1333. Y = (float)P2[1],
  1334. Z = calibrationInfo.CenterPoint.Z,
  1335. U = calibrationInfo.CenterPoint.U,
  1336. V = calibrationInfo.CenterPoint.V,
  1337. W = calibrationInfo.CenterPoint.W,
  1338. Hand = calibrationInfo.CenterPoint.Hand,
  1339. Local = calibrationInfo.CenterPoint.Local,
  1340. Tool = calibrationInfo.CenterPoint.Tool
  1341. });
  1342. rPoints.Add(new RPoint()
  1343. {
  1344. Number = 3,
  1345. X = (float)P3[0],
  1346. Y = (float)P3[1],
  1347. Z = calibrationInfo.CenterPoint.Z,
  1348. U = calibrationInfo.CenterPoint.U,
  1349. V = calibrationInfo.CenterPoint.V,
  1350. W = calibrationInfo.CenterPoint.W,
  1351. Hand = calibrationInfo.CenterPoint.Hand,
  1352. Local = calibrationInfo.CenterPoint.Local,
  1353. Tool = calibrationInfo.CenterPoint.Tool
  1354. });
  1355. rPoints.Add(new RPoint()
  1356. {
  1357. Number = 4,
  1358. X = (float)P4[0],
  1359. Y = (float)P4[1],
  1360. Z = calibrationInfo.CenterPoint.Z,
  1361. U = calibrationInfo.CenterPoint.U,
  1362. V = calibrationInfo.CenterPoint.V,
  1363. W = calibrationInfo.CenterPoint.W,
  1364. Hand = calibrationInfo.CenterPoint.Hand,
  1365. Local = calibrationInfo.CenterPoint.Local,
  1366. Tool = calibrationInfo.CenterPoint.Tool
  1367. });
  1368. rPoints.Add(new RPoint()
  1369. {
  1370. Number = 5,
  1371. X = (float)P5[0],
  1372. Y = (float)P5[1],
  1373. Z = calibrationInfo.CenterPoint.Z,
  1374. U = calibrationInfo.CenterPoint.U,
  1375. V = calibrationInfo.CenterPoint.V,
  1376. W = calibrationInfo.CenterPoint.W,
  1377. Hand = calibrationInfo.CenterPoint.Hand,
  1378. Local = calibrationInfo.CenterPoint.Local,
  1379. Tool = calibrationInfo.CenterPoint.Tool
  1380. });
  1381. bool isSuccess = false;
  1382. List<Vector<double>> TestPos = new List<Vector<double>>();
  1383. for (int i = 0; i < rPoints.Count; i++)
  1384. {
  1385. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown)
  1386. {
  1387. //移动机器人至i
  1388. isSuccess = await robot.CalibMotionAsync(rPoints[i], 0);
  1389. if (!isSuccess)
  1390. {
  1391. return (false, null);
  1392. }
  1393. if (cancellationToken.IsCancellationRequested)
  1394. {
  1395. return (false, null);
  1396. }
  1397. //关闭吸气
  1398. await robot.CalibOutIOAsync(false);
  1399. //移动机器人至待机位置
  1400. isSuccess = await robot.CalibMotionAsync(calibrationInfo.HomePoint, 0);
  1401. if (!isSuccess)
  1402. {
  1403. return (false, null);
  1404. }
  1405. }
  1406. else
  1407. {
  1408. //移动机器人至i
  1409. isSuccess = await robot.CalibMotionAsync(rPoints[i], rPoints[i].Z);
  1410. if (!isSuccess)
  1411. {
  1412. return (false, null);
  1413. }
  1414. }
  1415. if (cancellationToken.IsCancellationRequested)
  1416. {
  1417. return (false, null);
  1418. }
  1419. //相机拍照并处理图像
  1420. var photoResult = await CaptureAndProcessCallback();
  1421. if (!photoResult.IsSuccess)
  1422. {
  1423. return (false, null);
  1424. }
  1425. PointF pixelpos = new PointF((float)(photoResult.X), (float)(photoResult.Y));
  1426. Matrix<double> matrix = Matrix<double>.Build.DenseOfArray(calibrationInfo.AffineTransformationMaterial);
  1427. var rpos = matrix * Vector<double>.Build.Dense(new double[] { pixelpos.X, pixelpos.Y, 1 });
  1428. TestPos.Add(rpos);
  1429. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown)
  1430. {
  1431. //移动机器人至i
  1432. isSuccess = await robot.CalibMotionAsync(rPoints[i], 0);
  1433. if (!isSuccess)
  1434. {
  1435. return (false, null);
  1436. }
  1437. //打开吸气
  1438. await robot.CalibOutIOAsync(true);
  1439. }
  1440. if (cancellationToken.IsCancellationRequested)
  1441. {
  1442. return (false, null);
  1443. }
  1444. }
  1445. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown)
  1446. {
  1447. //移动机器人至待机位置
  1448. isSuccess = await robot.CalibMotionAsync(calibrationInfo.HomePoint, 0);
  1449. if (!isSuccess)
  1450. {
  1451. return (false, null);
  1452. }
  1453. }
  1454. //RMS(均方根值) 标定偏差
  1455. double sumX = 0, sumY = 0;
  1456. List<double> differenceX = new List<double>();
  1457. List<double> differenceY = new List<double>();
  1458. for (int i = 0; i < 5; i++)
  1459. {
  1460. if (calibrationInfo.CameraMount == Enums.CameraMount.MobileJ4)
  1461. {
  1462. //像素直接转换成mm时的点位
  1463. double Robot_X, Robot_Y;
  1464. Robot_X = TestPos[i][0];
  1465. Robot_Y = TestPos[i][1];
  1466. //机器人当前TOOL 0下的坐标
  1467. double curpos_x = 0, curpos_y = 0, curpos_u = 0;
  1468. curpos_x = rPoints[i].X;
  1469. curpos_y = rPoints[i].Y;
  1470. curpos_u = rPoints[i].U;
  1471. //得到旋转矩阵
  1472. double angle = Math.PI * (curpos_u - calibrationInfo.MarkPoint.U) / 180;
  1473. // 创建T矩阵
  1474. Matrix<double> rotationMatrix = Matrix<double>.Build.DenseOfArray(new double[,]
  1475. {
  1476. { Math.Cos(angle), -Math.Sin(angle) },
  1477. { Math.Sin(angle), Math.Cos(angle) }
  1478. });
  1479. Vector<double> p3 = Vector<double>.Build.Dense(new double[] { Robot_X - calibrationInfo.CalibPoints[0].Robot.X, Robot_Y - calibrationInfo.CalibPoints[0].Robot.Y });
  1480. Vector<double> phere = Vector<double>.Build.Dense(new double[] { curpos_x, curpos_y });
  1481. var cc = rotationMatrix * p3 + phere;
  1482. differenceX.Add(cc[0] - calibrationInfo.MarkPoint.X);
  1483. differenceY.Add(cc[1] - calibrationInfo.MarkPoint.Y);
  1484. }
  1485. else if (calibrationInfo.CameraMount == Enums.CameraMount.MobileDown_XYPlatform)
  1486. {
  1487. //像素直接转换成mm时的点位
  1488. double Robot_X, Robot_Y;
  1489. Robot_X = TestPos[i][0];
  1490. Robot_Y = TestPos[i][1];
  1491. //机器人当前TOOL 0下的坐标
  1492. double curpos_x = 0, curpos_y = 0, curpos_u = 0;
  1493. curpos_x = rPoints[i].X;
  1494. curpos_y = rPoints[i].Y;
  1495. curpos_u = rPoints[i].U;
  1496. //得到旋转矩阵
  1497. double angle = Math.PI * (curpos_u - calibrationInfo.MarkPoint.U) / 180;
  1498. // 创建T矩阵
  1499. Matrix<double> rotationMatrix = Matrix<double>.Build.DenseOfArray(new double[,]
  1500. {
  1501. { Math.Cos(angle), -Math.Sin(angle) },
  1502. { Math.Sin(angle), Math.Cos(angle) }
  1503. });
  1504. Vector<double> p3 = Vector<double>.Build.Dense(new double[] { Robot_X - calibrationInfo.CalibPoints[0].Robot.X, Robot_Y - calibrationInfo.CalibPoints[0].Robot.Y });
  1505. Vector<double> phere = Vector<double>.Build.Dense(new double[] { curpos_x, curpos_y });
  1506. var cc = rotationMatrix * p3 + phere;
  1507. differenceX.Add(cc[0] - calibrationInfo.MarkPoint.X);
  1508. differenceY.Add(cc[1] - calibrationInfo.MarkPoint.Y);
  1509. }
  1510. else
  1511. {
  1512. //将tool 0下的坐标转换成Tool n下的坐标
  1513. if (calibrationInfo.Tool != null)
  1514. {
  1515. ToolCoord toolCoord = new ToolCoord();
  1516. toolCoord.SetTool(calibrationInfo.Tool.X, calibrationInfo.Tool.Y);
  1517. double u1 = rPoints[i].U;
  1518. if (robot.Brand == Enums.RobotBrand.Schneider)
  1519. {
  1520. u1 *= -1;
  1521. }
  1522. var trans = toolCoord.GetToolnCoord(Vector<double>.Build.Dense(new double[] { rPoints[i].X, rPoints[i].Y }), u1);
  1523. differenceX.Add(trans[0] - TestPos[i][0]);
  1524. differenceY.Add(trans[1] - TestPos[i][1]);
  1525. }
  1526. else
  1527. {
  1528. differenceX.Add(rPoints[i].X - TestPos[i][0]);
  1529. differenceY.Add(rPoints[i].Y - TestPos[i][1]);
  1530. }
  1531. }
  1532. sumX += Math.Pow(differenceX[differenceX.Count - 1], 2);
  1533. sumY += Math.Pow(differenceY[differenceY.Count - 1], 2);
  1534. }
  1535. double rmsX, rmsY;
  1536. rmsX = Math.Sqrt(sumX / 5);
  1537. rmsY = Math.Sqrt(sumY / 5);
  1538. CalibrationTestResult calibrationTestResult = new CalibrationTestResult();
  1539. calibrationTestResult.MaxErrorValueX = differenceX.Max();
  1540. calibrationTestResult.MinErrorValueX = differenceX.Min();
  1541. calibrationTestResult.MaxErrorValueY = differenceY.Max();
  1542. calibrationTestResult.MinErrorValueY = differenceY.Min();
  1543. calibrationTestResult.RMSEX = rmsX;
  1544. calibrationTestResult.RMSEY = rmsY;
  1545. return (true, calibrationTestResult);
  1546. }
  1547. public void Dispose()
  1548. {
  1549. GC.SuppressFinalize(this);
  1550. }
  1551. }
  1552. }