CameraCalibrationService.cs 84 KB

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