CameraCalibrationService.cs 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759
  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. await robot.CalibMotionAsync(calibrationInfo.HomePoint, null);
  401. if (cancellationToken.IsCancellationRequested)
  402. {
  403. return (false, null, 0, 0, 0, 0);
  404. }
  405. if (calibrationInfo.Pick.PickPlaceModel == Enums.PickPlaceModel.Inhal)
  406. {
  407. //打开吸气
  408. await robot.CalibOutIOAsync(true);
  409. }
  410. else
  411. {
  412. //张开夹爪
  413. //夹爪张开为false,夹爪闭合为True
  414. await robot.CalibOutIOAsync(false);
  415. }
  416. //移动独立模组至待机点
  417. bool isFinished = await ((XYZU_Robot)robot).WaitCameraMoveFinishedAsync(calibrationInfo.CameraMotionModuleIndex, calibrationInfo.IndependentDownCameraMotionModulePoint);
  418. if (!isFinished)
  419. {
  420. return (false, null, 0, 0, 0, 0);
  421. }
  422. var view = new ShowMessage(Lang.提示, Lang.请将校准块放置在吸嘴中心);
  423. var result = await DialogHost.Show(view, "CalibToolDialog", null, null, null);
  424. if (!((bool)result)) return (false, null, 0, 0, 0, 0);
  425. if (calibrationInfo.Pick.PickPlaceModel == Enums.PickPlaceModel.Clamp)
  426. {
  427. //夹紧夹爪
  428. await robot.CalibOutIOAsync(true);
  429. }
  430. //计算相机与机器人坐标系的旋转矩阵
  431. var robotCameraRotationMatrix = await AutoIdentifyRobotCameraRotationMatrix(calibrationInfo, robot, cancellationToken);
  432. if (!robotCameraRotationMatrix.IsSuccess)
  433. {
  434. return (false, null, 0, 0, 0, 0);
  435. }
  436. pixelScaleX = robotCameraRotationMatrix.PixelScaleX;
  437. pixelScaleY = robotCameraRotationMatrix.PixelScaleY;
  438. RobotCameraRotationMatrix = robotCameraRotationMatrix.Item2;
  439. //相机拍照并处理图像
  440. var photoResult = await CaptureAndProcessCallback();
  441. if (!photoResult.IsSuccess)
  442. {
  443. return (false, null, 0, 0, 0, 0);
  444. }
  445. PointF P0 = new PointF((float)(photoResult.X), (float)(photoResult.Y));
  446. RPoint RobotU = calibrationInfo.CenterPoint.Clone();
  447. RobotU.U += (float)calibrationInfo.Angle;
  448. //移动机器人中心点
  449. await robot.CalibMotionAsync(calibrationInfo.CenterPoint, null);
  450. if (cancellationToken.IsCancellationRequested)
  451. {
  452. return (false, null, 0, 0, 0, 0);
  453. }
  454. //机器人U轴旋转
  455. await robot.CalibMotionAsync(RobotU, null);
  456. if (cancellationToken.IsCancellationRequested)
  457. {
  458. return (false, null, 0, 0, 0, 0);
  459. }
  460. //相机拍照并处理图像
  461. photoResult = await CaptureAndProcessCallback();
  462. if (!photoResult.IsSuccess)
  463. {
  464. return (false, null, 0, 0, 0, 0);
  465. }
  466. PointF curPixel = new PointF((float)(photoResult.X), (float)(photoResult.Y));
  467. bool IsFinish = false;
  468. for (int i = 0; i < 10; i++)
  469. {
  470. //计算需要移动的偏移量,目的是逼近中心点
  471. double offsetx = (P0.X - curPixel.X) * pixelScaleX;
  472. double offsety = (P0.Y - curPixel.Y) * pixelScaleY;
  473. var pos = RobotCameraRotationMatrix.Inverse() * Vector<double>.Build.Dense(new double[] { offsetx, offsety });
  474. RobotU.X += (float)pos[0];
  475. RobotU.Y += (float)pos[1];
  476. //机器人移动
  477. await robot.CalibMotionAsync(RobotU, null);
  478. if (cancellationToken.IsCancellationRequested)
  479. {
  480. return (false, null, 0, 0, 0, 0);
  481. }
  482. photoResult = await CaptureAndProcessCallback();
  483. if (!photoResult.IsSuccess)
  484. {
  485. return (false, null, 0, 0, 0, 0);
  486. }
  487. curPixel = new PointF((float)(photoResult.X), (float)(photoResult.Y));
  488. offsetx = P0.X - curPixel.X;
  489. offsety = P0.Y - curPixel.Y;
  490. //await Console.Out.WriteLineAsync($"移动后的差异:{offsetx},{offsety}");
  491. if (Math.Abs(offsetx) < 1 && Math.Abs(offsetx) < 1)
  492. {
  493. //移动机器人至待机位置
  494. await robot.CalibMotionAsync(calibrationInfo.HomePoint, null);
  495. IsFinish = true;
  496. break;
  497. }
  498. if (cancellationToken.IsCancellationRequested)
  499. {
  500. return (false, null, 0, 0, 0, 0);
  501. }
  502. }
  503. if (IsFinish)
  504. {
  505. Vector<double> p0 = Vector<double>.Build.Dense(new double[] { calibrationInfo.CenterPoint.X, calibrationInfo.CenterPoint.Y });
  506. Vector<double> p1 = Vector<double>.Build.Dense(new double[] { RobotU.X, RobotU.Y });
  507. ToolCoord toolCoord = new ToolCoord();
  508. double u1 = calibrationInfo.CenterPoint.U;
  509. double u2 = RobotU.U;
  510. if (robot.Brand == Enums.RobotBrand.Schneider)
  511. {
  512. u1 *= -1;
  513. u2 *= -1;
  514. }
  515. toolCoord.ComputeTool(p0, p1, u1, u2);
  516. if (calibrationInfo.Tool == null)
  517. {
  518. calibrationInfo.Tool = new RobotTool();
  519. }
  520. calibrationInfo.Tool.X = toolCoord.X;
  521. calibrationInfo.Tool.Y = toolCoord.Y;
  522. return (true, RobotCameraRotationMatrix, calibrationInfo.Tool.X, calibrationInfo.Tool.Y, pixelScaleX, pixelScaleY);
  523. }
  524. else
  525. {
  526. return (false, null, 0, 0, 0, 0);
  527. }
  528. }
  529. /// <summary>
  530. /// 通过未标定的相机校准工具坐标。
  531. /// 当相机内参未知或未可靠标定时,通过已知机器人位姿与图像特征的对应关系反推工具坐标与像素尺度。
  532. /// </summary>
  533. /// <param name="calibrationInfo">标定配置信息(可能包含初始猜测或参考点)。</param>
  534. /// <param name="robot">机器人接口。</param>
  535. /// <param name="cancellationToken">用于取消操作的标记。</param>
  536. /// <returns>同 <see cref="CalibrateFixedDownCameraTool"/> 的返回约定。</returns>
  537. public async Task<(bool IsSuccess, Matrix<double> RotationMatrix, double ToolX, double ToolY, double PixelScaleX, double PixelScaleY)> CalibrateUncalibratedCameraTool(CalibrationInfo calibrationInfo, IRobot robot, CancellationToken cancellationToken)
  538. {
  539. if (calibrationInfo == null) return (false, null, 0, 0, 0, 0);
  540. if (robot == null || !robot.IsConnected) return (false, null, 0, 0, 0, 0);
  541. double pixelScaleX = 0;
  542. double pixelScaleY = 0;
  543. Matrix<double> RobotCameraRotationMatrix = null;
  544. //发送校准的参数
  545. await robot.CalibParameAsync(calibrationInfo.Pick, calibrationInfo.Speed, calibrationInfo.Accel, calibrationInfo.Power, calibrationInfo.WaitSuction, calibrationInfo.WaitBlow);
  546. (bool IsSuccess, Matrix<double> RotationMatrix, double ToolX, double ToolY, double PixelScaleX, double PixelScaleY) calibresult;
  547. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown)
  548. {
  549. calibresult = await CalibrateFixedDownCameraTool(calibrationInfo, robot, cancellationToken);
  550. }
  551. else if (calibrationInfo.CameraMount == Enums.CameraMount.FixedUp)
  552. {
  553. calibresult = await CalibrateFixedUpCameraTool(calibrationInfo, robot, cancellationToken);
  554. }
  555. else if (calibrationInfo.CameraMount == Enums.CameraMount.MobileJ4)
  556. {
  557. calibresult = await CalibrateJ4MovingDownCameraTool(calibrationInfo, robot, cancellationToken);
  558. }
  559. else if (calibrationInfo.CameraMount == Enums.CameraMount.MobileDown_IndependentXYPlatform)
  560. {
  561. calibresult = await CalibrateIndependentXYPlatformMovingDownCameraTool(calibrationInfo, robot, cancellationToken);
  562. }
  563. else
  564. {
  565. return (false, null, 0, 0, 0, 0);
  566. }
  567. if (!calibresult.IsSuccess)
  568. {
  569. return (false, null, 0, 0, 0, 0);
  570. }
  571. pixelScaleX = calibresult.PixelScaleX;
  572. pixelScaleY = calibresult.PixelScaleY;
  573. RobotCameraRotationMatrix = calibresult.RotationMatrix;
  574. return (true, RobotCameraRotationMatrix, calibresult.ToolX, calibresult.ToolY, pixelScaleX, pixelScaleY);
  575. }
  576. /// <summary>
  577. /// 自动识别机器人与相机图像坐标系的旋转矩阵。
  578. /// 使用若干已知的机器人位姿与对应图像坐标点,估计二者之间的旋转变换与像素尺度。
  579. /// </summary>
  580. /// <param name="calibrationInfo">包含用于识别的点集与图像处理配置。</param>
  581. /// <param name="robot">机器人接口,用于获取对应位姿或移动机器人到指定位置以采集数据。</param>
  582. /// <param name="cancellationToken">用于取消操作的标记。</param>
  583. /// <returns>
  584. /// 元组字段:
  585. /// IsSuccess - 是否成功识别;
  586. /// Matrix<double> - 旋转(或仿射)矩阵;
  587. /// PixelScaleX - X 方向像素尺度(如 mm/px);
  588. /// PixelScaleY - Y 方向像素尺度(如 mm/px)。
  589. /// </returns>
  590. public async Task<(bool IsSuccess, Matrix<double> RotationMatrix, double PixelScaleX, double PixelScaleY)> AutoIdentifyRobotCameraRotationMatrix(CalibrationInfo calibrationInfo, IRobot robot, CancellationToken cancellationToken)
  591. {
  592. //移动距离,单位mm
  593. float distance = ((float)calibrationInfo.Height) / 3;
  594. //-------------------------------------------P0-----------------------------------
  595. //移动机器人至中心点
  596. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown && !calibrationInfo.FixedDownCameraNoReturnToHome)
  597. {
  598. await robot.CalibMotionAsync(calibrationInfo.CenterPoint, null);
  599. //关闭吸气
  600. await robot.CalibOutIOAsync(false);
  601. }
  602. else
  603. {
  604. await robot.CalibMotionAsync(calibrationInfo.CenterPoint, calibrationInfo.CenterPoint.Z);
  605. }
  606. RPoint RobotP0 = await robot.GetRobotPosAsync();
  607. RPoint RobotP1 = RobotP0.Clone();
  608. RPoint RobotP2 = RobotP0.Clone();
  609. RobotP1.X += distance;
  610. RobotP2.Y += distance;
  611. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown && !calibrationInfo.FixedDownCameraNoReturnToHome)
  612. {
  613. //移动机器人至待机位置
  614. await robot.CalibMotionAsync(calibrationInfo.HomePoint, null);
  615. }
  616. //相机拍照并处理图像
  617. var result = await CaptureAndProcessCallback();
  618. if (!result.IsSuccess)
  619. {
  620. return (false, null, 0, 0);
  621. }
  622. PointF P0 = new PointF((float)result.X, (float)result.Y);
  623. //-------------------------------------------P1-----------------------------------
  624. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown && !calibrationInfo.FixedDownCameraNoReturnToHome)
  625. {
  626. //移动机器人至P0
  627. await robot.CalibMotionAsync(RobotP0, null);
  628. if (cancellationToken.IsCancellationRequested)
  629. {
  630. return (false, null, 0, 0);
  631. }
  632. //打开吸气
  633. await robot.CalibOutIOAsync(true);
  634. //移动机器人至P1
  635. await robot.CalibMotionAsync(RobotP1, null);
  636. if (cancellationToken.IsCancellationRequested)
  637. {
  638. return (false, null, 0, 0);
  639. }
  640. //关闭吸气
  641. await robot.CalibOutIOAsync(false);
  642. //移动机器人至待机位置
  643. await robot.CalibMotionAsync(calibrationInfo.HomePoint, null);
  644. }
  645. else
  646. {
  647. //移动机器人至P1
  648. await robot.CalibMotionAsync(RobotP1, RobotP1.Z);
  649. }
  650. if (cancellationToken.IsCancellationRequested)
  651. {
  652. return (false, null, 0, 0);
  653. }
  654. //相机拍照并处理图像
  655. result = await CaptureAndProcessCallback();
  656. if (!result.IsSuccess)
  657. {
  658. return (false, null, 0, 0);
  659. }
  660. PointF P1 = new PointF((float)result.X, (float)result.Y);
  661. //-------------------------------------------P2-----------------------------------
  662. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown && !calibrationInfo.FixedDownCameraNoReturnToHome)
  663. {
  664. //移动机器人至P1
  665. await robot.CalibMotionAsync(RobotP1, null);
  666. if (cancellationToken.IsCancellationRequested)
  667. {
  668. return (false, null, 0, 0);
  669. }
  670. //打开吸气
  671. await robot.CalibOutIOAsync(true);
  672. //移动机器人至P2
  673. await robot.CalibMotionAsync(RobotP2, null);
  674. if (cancellationToken.IsCancellationRequested)
  675. {
  676. return (false, null, 0, 0);
  677. }
  678. //关闭吸气
  679. await robot.CalibOutIOAsync(false);
  680. //移动机器人至待机位置
  681. await robot.CalibMotionAsync(calibrationInfo.HomePoint, null);
  682. }
  683. else
  684. {
  685. //移动机器人至P2
  686. await robot.CalibMotionAsync(RobotP2, RobotP2.Z);
  687. }
  688. if (cancellationToken.IsCancellationRequested)
  689. {
  690. return (false, null, 0, 0);
  691. }
  692. //相机拍照并处理图像
  693. result = await CaptureAndProcessCallback();
  694. if (!result.IsSuccess)
  695. {
  696. return (false, null, 0, 0);
  697. }
  698. PointF P2 = new PointF((float)result.X, (float)result.Y);
  699. //-------------------------------------------P0--------------------------------------
  700. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown && !calibrationInfo.FixedDownCameraNoReturnToHome)
  701. {
  702. //移动机器人至P2
  703. await robot.CalibMotionAsync(RobotP2, null);
  704. if (cancellationToken.IsCancellationRequested)
  705. {
  706. return (false, null, 0, 0);
  707. }
  708. //打开吸气
  709. await robot.CalibOutIOAsync(true);
  710. //移动机器人至P0
  711. await robot.CalibMotionAsync(RobotP0, null);
  712. if (cancellationToken.IsCancellationRequested)
  713. {
  714. return (false, null, 0, 0);
  715. }
  716. //关闭吸气
  717. await robot.CalibOutIOAsync(false);
  718. //移动机器人至待机位置
  719. await robot.CalibMotionAsync(calibrationInfo.HomePoint, null);
  720. }
  721. else
  722. {
  723. //移动机器人至P0
  724. await robot.CalibMotionAsync(RobotP0, RobotP0.Z);
  725. }
  726. if (cancellationToken.IsCancellationRequested)
  727. {
  728. return (false, null, 0, 0);
  729. }
  730. //-----------------------------------------计算旋转矩阵------------------------------
  731. Matrix<double> M = CalculateImageRotationMatrix(P0, P1, P2);
  732. //Theta = Acos(M11)
  733. //相机与机器人坐标系角度:Theta * 180 / PI
  734. //-----------------------------------------计算像素和毫米比例-----------------------
  735. Vector<double> P11 = Vector<double>.Build.Dense(new double[] { distance, 0 });
  736. Vector<double> P21 = Vector<double>.Build.Dense(new double[] { P1.X, P1.Y }) - Vector<double>.Build.Dense(new double[] { P0.X, P0.Y });
  737. double PixelScaleX = Math.Abs((M * P11)[0] / P21[0]); //mm/pixel
  738. double PixelScaleY = Math.Abs((M * P11)[1] / P21[1]); //mm/pixel
  739. return (true, M, PixelScaleX, PixelScaleY);
  740. }
  741. /// <summary>
  742. /// 根据图像上的三个点计算图像坐标系的旋转矩阵。
  743. /// 通常通过已知三点在像素坐标系中的位置计算出局部旋转/仿射变换,用于后续坐标换算。
  744. /// </summary>
  745. /// <param name="P0">图像中的点 0(像素坐标)。</param>
  746. /// <param name="P1">图像中的点 1(像素坐标)。</param>
  747. /// <param name="P2">图像中的点 2(像素坐标)。</param>
  748. /// <returns>表示图像旋转或仿射变换的 3x3 或 2x2 矩阵(具体尺寸由实现决定)。</returns>
  749. public Matrix<double> CalculateImageRotationMatrix(PointF P0, PointF P1, PointF P2)
  750. {
  751. //1.将点转换为向量表示:
  752. Vector<double> Point0 = Vector<double>.Build.Dense(new double[] { (double)P0.X, (double)P0.Y });
  753. Vector<double> Point1 = Vector<double>.Build.Dense(new double[] { (double)P1.X, (double)P1.Y });
  754. Vector<double> Point2 = Vector<double>.Build.Dense(new double[] { (double)P2.X, (double)P2.Y });
  755. //2.计算坐标系的基向量:
  756. Vector<double> E1 = (Point1 - Point0) / (Point1 - Point0).L2Norm();
  757. Vector<double> E2 = (Point2 - Point0 - E1.DotProduct(Point2 - Point0) * E1) / (Point2 - Point0 - E1.DotProduct(Point2 - Point0) * E1).L2Norm();
  758. //3.计算旋转矩阵:我们可以使用基向量组成的矩阵作为旋转矩阵。具体地,我们可以将基向量 e1 和 e2 分别作为新坐标系下的 $x$ 和 $y$ 轴,然后组成一个矩阵,再对这个矩阵求逆即可得到旋转矩阵。代码实现:
  759. Matrix<double> R = Matrix<double>.Build.DenseOfColumnVectors(E1, E2);
  760. return R;
  761. }
  762. /// <summary>
  763. /// 自动移动机器人至图像中心点位置。
  764. /// 典型用例为将相机视野中心对应的工作点转换为机器人坐标并移动机器人到该点以便校准或抓取。
  765. /// </summary>
  766. /// <param name="robot">机器人接口,用于执行移动命令并返回当前位置。</param>
  767. /// <param name="matrix">图像坐标系到机器人坐标系的转换矩阵(旋转/仿射)。</param>
  768. /// <param name="PixelScaleX">像素到物理距离在 X 方向的缩放因子(如 mm/px)。</param>
  769. /// <param name="PixelScaleY">像素到物理距离在 Y 方向的缩放因子(如 mm/px)。</param>
  770. /// <param name="cancellationToken">用于取消操作的标记。</param>
  771. /// <returns>
  772. /// 元组字段:
  773. /// IsSuccess - 是否成功移动到图像中心;
  774. /// X - 移动后机器人在基坐标系下的 X;
  775. /// Y - 移动后机器人在基坐标系下的 Y;
  776. /// U - 移动后机器人的朝向(工具角度)。
  777. /// </returns>
  778. public async Task<(bool IsSuccess, double X, double Y, double U)> AutoMoveRobotToImageCenter(IRobot robot, Matrix<double> matrix, double PixelScaleX, double PixelScaleY, CancellationToken cancellationToken)
  779. {
  780. bool IsFinish = false;
  781. //相机拍照并处理图像
  782. var result = await CaptureAndProcessCallback();
  783. if (!result.IsSuccess)
  784. {
  785. return (false, 0, 0, 0);
  786. }
  787. PointF curPixel = new PointF((float)result.X, (float)result.Y);
  788. //中心点像素坐标
  789. PointF P0 = new PointF((float)(result.ImageWidth / 2), (float)(result.ImageHeight / 2));
  790. var curpos = robot.GetRobotPos();
  791. for (int i = 0; i < 10; i++)
  792. {
  793. //计算需要移动的偏移量,目的是逼近中心点
  794. double offsetx = (P0.X - curPixel.X) * PixelScaleX;
  795. double offsety = (P0.Y - curPixel.Y) * PixelScaleY;
  796. var pos = matrix.Inverse() * Vector<double>.Build.Dense(new double[] { offsetx, offsety });
  797. curpos.X += (float)pos[0];
  798. curpos.Y += (float)pos[1];
  799. //机器人移动
  800. await robot.CalibMotionAsync(curpos, curpos.Z);
  801. await Task.Delay(200);
  802. result = await CaptureAndProcessCallback();
  803. if (!result.IsSuccess)
  804. {
  805. return (false, 0, 0, 0);
  806. }
  807. curPixel = new PointF((float)result.X, (float)result.Y);
  808. offsetx = P0.X - curPixel.X;
  809. offsety = P0.Y - curPixel.Y;
  810. //await Console.Out.WriteLineAsync($"移动后的差异:{offsetx},{offsety}");
  811. if (Math.Abs(offsetx) < 3 && Math.Abs(offsetx) < 3)
  812. {
  813. IsFinish = true;
  814. break;
  815. }
  816. if (cancellationToken.IsCancellationRequested)
  817. {
  818. return (false, 0, 0, 0);
  819. }
  820. }
  821. if (IsFinish)
  822. {
  823. var finalPos = robot.GetRobotPos();
  824. return (true, finalPos.X, finalPos.Y, finalPos.U);
  825. }
  826. else
  827. {
  828. return (false, 0, 0, 0);
  829. }
  830. }
  831. /// <summary>
  832. /// 自动九点标定。
  833. /// 使用 9 个采样点进行仿射/透视变换求解,以提高标定精度并生成验证用的九点数据结构。
  834. /// </summary>
  835. /// <param name="calibrationInfo">九点标定的配置信息(点列、采集顺序、图像处理参数等)。</param>
  836. /// <param name="robot">机器人接口。</param>
  837. /// <param name="cancellationToken">用于取消操作的标记。</param>
  838. /// <returns>
  839. /// 元组字段:
  840. /// IsSuccess - 标定是否成功;
  841. /// AffineTransformationMaterial - 求解得到的仿射变换矩阵;
  842. /// NinePoint - 九点标定结果封装(像素点与对应的机器人位姿);
  843. /// Result - 详细的标定结果与统计信息(误差、残差等)。
  844. /// </returns>
  845. public async Task<(bool IsSuccess, Matrix<double> AffineTransformationMaterial, RobotPixelPoint[] NinePoint, CalibrationResult Result)> AutoNinePointCalibration(CalibrationInfo calibrationInfo, IRobot robot, CancellationToken cancellationToken)
  846. {
  847. if (calibrationInfo == null) return (false, null, null, null);
  848. if (robot == null || !robot.IsConnected) return (false, null, null, null);
  849. //发送校准的参数
  850. await robot.CalibParameAsync(calibrationInfo.Pick, calibrationInfo.Speed, calibrationInfo.Accel, calibrationInfo.Power, calibrationInfo.WaitSuction, calibrationInfo.WaitBlow);
  851. var P0 = Vector<double>.Build.Dense(new double[] { calibrationInfo.CenterPoint.X, calibrationInfo.CenterPoint.Y });
  852. if (robot.Brand == Enums.RobotBrand.XYZ_Platform)
  853. {
  854. //移动机器人至中心位置
  855. await robot.CalibMotionAsync(calibrationInfo.CenterPoint, null);
  856. if (cancellationToken.IsCancellationRequested)
  857. {
  858. return (false, null, null, null);
  859. }
  860. //计算相机与机器人坐标系的旋转矩阵
  861. var robotCameraRotationMatrix = await AutoIdentifyRobotCameraRotationMatrix(calibrationInfo, robot, cancellationToken);
  862. if (!robotCameraRotationMatrix.IsSuccess)
  863. {
  864. return (false, null, null, null);
  865. }
  866. //自动移动机器人至图像中心点位置
  867. var autoMoveResult = await AutoMoveRobotToImageCenter(robot, robotCameraRotationMatrix.RotationMatrix, robotCameraRotationMatrix.PixelScaleX, robotCameraRotationMatrix.PixelScaleY, cancellationToken);
  868. if (!autoMoveResult.IsSuccess)
  869. {
  870. return (false, null, null, null);
  871. }
  872. calibrationInfo.CenterPoint.X = (float)autoMoveResult.X;
  873. calibrationInfo.CenterPoint.Y = (float)autoMoveResult.Y;
  874. if (calibrationInfo.CameraMount != Enums.CameraMount.MobileDown_XYPlatform)
  875. {
  876. calibrationInfo.MarkPoint = calibrationInfo.CenterPoint.Clone();
  877. }
  878. calibrationInfo.RobotCameraRotationMatrix = robotCameraRotationMatrix.RotationMatrix.ToArray();
  879. }
  880. else if (calibrationInfo.CameraMount == Enums.CameraMount.MobileDown_XYPlatform)
  881. {
  882. //移动机器人至中心位置
  883. await robot.CalibMotionAsync(calibrationInfo.CenterPoint, null);
  884. if (cancellationToken.IsCancellationRequested)
  885. {
  886. return (false, null, null, null);
  887. }
  888. //计算相机与机器人坐标系的旋转矩阵
  889. var robotCameraRotationMatrix = await AutoIdentifyRobotCameraRotationMatrix(calibrationInfo, robot, cancellationToken);
  890. if (!robotCameraRotationMatrix.IsSuccess)
  891. {
  892. return (false, null, null, null);
  893. }
  894. //自动移动机器人至图像中心点位置
  895. var autoMoveResult = await AutoMoveRobotToImageCenter(robot, robotCameraRotationMatrix.RotationMatrix, robotCameraRotationMatrix.PixelScaleX, robotCameraRotationMatrix.PixelScaleY, cancellationToken);
  896. if (!autoMoveResult.IsSuccess)
  897. {
  898. return (false, null, null, null);
  899. }
  900. calibrationInfo.CenterPoint.X = (float)autoMoveResult.X;
  901. calibrationInfo.CenterPoint.Y = (float)autoMoveResult.Y;
  902. //calibrationInfo.MarkPoint = calibrationInfo.CenterPoint.Clone();
  903. calibrationInfo.RobotCameraRotationMatrix = robotCameraRotationMatrix.RotationMatrix.ToArray();
  904. }
  905. else if (calibrationInfo.CameraMount == Enums.CameraMount.MobileDown_IndependentXYPlatform)
  906. {
  907. //移动独立模组至待机点
  908. bool isFinished = await ((XYZU_Robot)robot).WaitCameraMoveFinishedAsync(calibrationInfo.CameraMotionModuleIndex, calibrationInfo.IndependentDownCameraMotionModulePoint);
  909. if (!isFinished)
  910. {
  911. return (false, null, null, null);
  912. }
  913. }
  914. double IntervalX = calibrationInfo.Width / 2;
  915. double IntervalY = calibrationInfo.Height / 2;
  916. Vector<double> P1 = Vector<double>.Build.Dense(new double[] { -IntervalX, -IntervalY });
  917. Vector<double> P2 = Vector<double>.Build.Dense(new double[] { 0, -IntervalY });
  918. Vector<double> P3 = Vector<double>.Build.Dense(new double[] { IntervalX, -IntervalY });
  919. Vector<double> P4 = Vector<double>.Build.Dense(new double[] { IntervalX, 0 });
  920. Vector<double> P5 = Vector<double>.Build.Dense(new double[] { 0, 0 });
  921. Vector<double> P6 = Vector<double>.Build.Dense(new double[] { -IntervalX, 0 });
  922. Vector<double> P7 = Vector<double>.Build.Dense(new double[] { -IntervalX, IntervalY });
  923. Vector<double> P8 = Vector<double>.Build.Dense(new double[] { 0, IntervalY });
  924. Vector<double> P9 = Vector<double>.Build.Dense(new double[] { IntervalX, IntervalY });
  925. /// <summary>
  926. /// 机器人与相机之间的旋转矩阵
  927. /// </summary>
  928. Matrix<double> RobotCameraRotationMatrix = Matrix<double>.Build.DenseOfArray(calibrationInfo.RobotCameraRotationMatrix);
  929. //1
  930. P1 = RobotCameraRotationMatrix.Inverse() * P1 + P0;
  931. //2
  932. P2 = RobotCameraRotationMatrix.Inverse() * P2 + P0;
  933. //3
  934. P3 = RobotCameraRotationMatrix.Inverse() * P3 + P0;
  935. //4
  936. P4 = RobotCameraRotationMatrix.Inverse() * P4 + P0;
  937. //5
  938. P5 = RobotCameraRotationMatrix.Inverse() * P5 + P0;
  939. //6
  940. P6 = RobotCameraRotationMatrix.Inverse() * P6 + P0;
  941. //7
  942. P7 = RobotCameraRotationMatrix.Inverse() * P7 + P0;
  943. //8
  944. P8 = RobotCameraRotationMatrix.Inverse() * P8 + P0;
  945. //9
  946. P9 = RobotCameraRotationMatrix.Inverse() * P9 + P0;
  947. List<RPoint> rPoints = new List<RPoint>();
  948. rPoints.Add(new RPoint()
  949. {
  950. Number = 1,
  951. X = (float)P1[0],
  952. Y = (float)P1[1],
  953. Z = calibrationInfo.CenterPoint.Z,
  954. U = calibrationInfo.CenterPoint.U,
  955. V = calibrationInfo.CenterPoint.V,
  956. W = calibrationInfo.CenterPoint.W,
  957. Hand = calibrationInfo.CenterPoint.Hand,
  958. Local = calibrationInfo.CenterPoint.Local,
  959. Tool = calibrationInfo.CenterPoint.Tool
  960. });
  961. rPoints.Add(new RPoint()
  962. {
  963. Number = 2,
  964. X = (float)P2[0],
  965. Y = (float)P2[1],
  966. Z = calibrationInfo.CenterPoint.Z,
  967. U = calibrationInfo.CenterPoint.U,
  968. V = calibrationInfo.CenterPoint.V,
  969. W = calibrationInfo.CenterPoint.W,
  970. Hand = calibrationInfo.CenterPoint.Hand,
  971. Local = calibrationInfo.CenterPoint.Local,
  972. Tool = calibrationInfo.CenterPoint.Tool
  973. });
  974. rPoints.Add(new RPoint()
  975. {
  976. Number = 3,
  977. X = (float)P3[0],
  978. Y = (float)P3[1],
  979. Z = calibrationInfo.CenterPoint.Z,
  980. U = calibrationInfo.CenterPoint.U,
  981. V = calibrationInfo.CenterPoint.V,
  982. W = calibrationInfo.CenterPoint.W,
  983. Hand = calibrationInfo.CenterPoint.Hand,
  984. Local = calibrationInfo.CenterPoint.Local,
  985. Tool = calibrationInfo.CenterPoint.Tool
  986. });
  987. rPoints.Add(new RPoint()
  988. {
  989. Number = 4,
  990. X = (float)P4[0],
  991. Y = (float)P4[1],
  992. Z = calibrationInfo.CenterPoint.Z,
  993. U = calibrationInfo.CenterPoint.U,
  994. V = calibrationInfo.CenterPoint.V,
  995. W = calibrationInfo.CenterPoint.W,
  996. Hand = calibrationInfo.CenterPoint.Hand,
  997. Local = calibrationInfo.CenterPoint.Local,
  998. Tool = calibrationInfo.CenterPoint.Tool
  999. });
  1000. rPoints.Add(new RPoint()
  1001. {
  1002. Number = 5,
  1003. X = (float)P5[0],
  1004. Y = (float)P5[1],
  1005. Z = calibrationInfo.CenterPoint.Z,
  1006. U = calibrationInfo.CenterPoint.U,
  1007. V = calibrationInfo.CenterPoint.V,
  1008. W = calibrationInfo.CenterPoint.W,
  1009. Hand = calibrationInfo.CenterPoint.Hand,
  1010. Local = calibrationInfo.CenterPoint.Local,
  1011. Tool = calibrationInfo.CenterPoint.Tool
  1012. });
  1013. rPoints.Add(new RPoint()
  1014. {
  1015. Number = 6,
  1016. X = (float)P6[0],
  1017. Y = (float)P6[1],
  1018. Z = calibrationInfo.CenterPoint.Z,
  1019. U = calibrationInfo.CenterPoint.U,
  1020. V = calibrationInfo.CenterPoint.V,
  1021. W = calibrationInfo.CenterPoint.W,
  1022. Hand = calibrationInfo.CenterPoint.Hand,
  1023. Local = calibrationInfo.CenterPoint.Local,
  1024. Tool = calibrationInfo.CenterPoint.Tool
  1025. });
  1026. rPoints.Add(new RPoint()
  1027. {
  1028. Number = 7,
  1029. X = (float)P7[0],
  1030. Y = (float)P7[1],
  1031. Z = calibrationInfo.CenterPoint.Z,
  1032. U = calibrationInfo.CenterPoint.U,
  1033. V = calibrationInfo.CenterPoint.V,
  1034. W = calibrationInfo.CenterPoint.W,
  1035. Hand = calibrationInfo.CenterPoint.Hand,
  1036. Local = calibrationInfo.CenterPoint.Local,
  1037. Tool = calibrationInfo.CenterPoint.Tool
  1038. });
  1039. rPoints.Add(new RPoint()
  1040. {
  1041. Number = 8,
  1042. X = (float)P8[0],
  1043. Y = (float)P8[1],
  1044. Z = calibrationInfo.CenterPoint.Z,
  1045. U = calibrationInfo.CenterPoint.U,
  1046. V = calibrationInfo.CenterPoint.V,
  1047. W = calibrationInfo.CenterPoint.W,
  1048. Hand = calibrationInfo.CenterPoint.Hand,
  1049. Local = calibrationInfo.CenterPoint.Local,
  1050. Tool = calibrationInfo.CenterPoint.Tool
  1051. });
  1052. rPoints.Add(new RPoint()
  1053. {
  1054. Number = 9,
  1055. X = (float)P9[0],
  1056. Y = (float)P9[1],
  1057. Z = calibrationInfo.CenterPoint.Z,
  1058. U = calibrationInfo.CenterPoint.U,
  1059. V = calibrationInfo.CenterPoint.V,
  1060. W = calibrationInfo.CenterPoint.W,
  1061. Hand = calibrationInfo.CenterPoint.Hand,
  1062. Local = calibrationInfo.CenterPoint.Local,
  1063. Tool = calibrationInfo.CenterPoint.Tool
  1064. });
  1065. List<PointF> PixelPoslist = new List<PointF>();
  1066. if (calibrationInfo.CalibPoints == null)
  1067. {
  1068. calibrationInfo.CalibPoints = new System.Collections.ObjectModel.ObservableCollection<RobotPixelPoint>();
  1069. }
  1070. else
  1071. {
  1072. calibrationInfo.CalibPoints.Clear();
  1073. }
  1074. //移动相机标定时,需要先记住特征点的绝对坐标(相对于机器人坐标系) P0
  1075. if (calibrationInfo.CameraMount == Enums.CameraMount.MobileJ4)
  1076. {
  1077. //移动机器人至中心位置
  1078. await robot.CalibMotionAsync(calibrationInfo.CenterPoint, null);
  1079. if (cancellationToken.IsCancellationRequested)
  1080. {
  1081. return (false, null, null, null);
  1082. }
  1083. //将当前机器人Tool 0下的坐标转换成工具坐标下的坐标,并记录下来
  1084. ToolCoord toolCoord = new ToolCoord();
  1085. toolCoord.SetTool(calibrationInfo.Tool.X, calibrationInfo.Tool.Y);
  1086. Vector<double> p = Vector<double>.Build.Dense(new double[] { calibrationInfo.CenterPoint.X, calibrationInfo.CenterPoint.Y }); ;
  1087. var pn = toolCoord.GetToolnCoord(p, calibrationInfo.CenterPoint.U);
  1088. if (calibrationInfo.MarkPoint == null)
  1089. {
  1090. calibrationInfo.MarkPoint = new RPoint();
  1091. }
  1092. calibrationInfo.MarkPoint = calibrationInfo.CenterPoint.Clone();
  1093. calibrationInfo.MarkPoint.X = (float)pn[0];
  1094. calibrationInfo.MarkPoint.Y = (float)pn[1];
  1095. }
  1096. for (int i = 0; i < rPoints.Count; i++)
  1097. {
  1098. //移动机器人至i
  1099. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown)
  1100. {
  1101. await robot.CalibMotionAsync(rPoints[i], null);
  1102. if (cancellationToken.IsCancellationRequested)
  1103. {
  1104. return (false, null, null, null);
  1105. }
  1106. //关闭吸气
  1107. await robot.CalibOutIOAsync(false);
  1108. //移动机器人至待机位置
  1109. await robot.CalibMotionAsync(calibrationInfo.HomePoint, null);
  1110. }
  1111. else
  1112. {
  1113. await robot.CalibMotionAsync(rPoints[i], rPoints[i].Z);
  1114. }
  1115. if (cancellationToken.IsCancellationRequested)
  1116. {
  1117. return (false, null, null, null);
  1118. }
  1119. //相机拍照并处理图像
  1120. var photoResult = await CaptureAndProcessCallback();
  1121. if (!photoResult.IsSuccess)
  1122. {
  1123. return (false, null, null, null);
  1124. }
  1125. PointF curPixel = new PointF((float)(photoResult.X), (float)(photoResult.Y));
  1126. PixelPoslist.Add(curPixel);
  1127. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown)
  1128. {
  1129. //移动机器人至i
  1130. await robot.CalibMotionAsync(rPoints[i], null);
  1131. if (cancellationToken.IsCancellationRequested)
  1132. {
  1133. return (false, null, null, null);
  1134. }
  1135. //打开吸气
  1136. await robot.CalibOutIOAsync(true);
  1137. }
  1138. calibrationInfo.CalibPoints.Add(new RobotPixelPoint()
  1139. {
  1140. Number = i + 1,
  1141. Robot = new System.Drawing.PointF(rPoints[i].X, rPoints[i].Y),
  1142. Pixel = new System.Drawing.PointF(PixelPoslist[i].X, PixelPoslist[i].Y)
  1143. });
  1144. if (cancellationToken.IsCancellationRequested)
  1145. {
  1146. return (false, null, null, null);
  1147. }
  1148. }
  1149. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown)
  1150. {
  1151. //移动机器人至待机位置
  1152. await robot.CalibMotionAsync(calibrationInfo.HomePoint, null);
  1153. }
  1154. if (cancellationToken.IsCancellationRequested)
  1155. {
  1156. return (false, null, null, null);
  1157. }
  1158. Mat matPixel = new Mat(9, 2, MatType.CV_64F);
  1159. Mat matRobot = new Mat(9, 2, MatType.CV_64F);
  1160. for (int i = 0; i < 9; i++)
  1161. {
  1162. matPixel.Set<double>(i, 0, PixelPoslist[i].X);
  1163. matPixel.Set<double>(i, 1, PixelPoslist[i].Y);
  1164. //如果是移动相机拍照需要将机器人的9点进行一个变换,为了达到Mark点动,相机不动的目的
  1165. if (calibrationInfo.CameraMount == Enums.CameraMount.MobileJ4)
  1166. {
  1167. //计算每个点相对于第一个点移动的偏移量,将这个偏移量
  1168. double ox = rPoints[i].X - rPoints[0].X;
  1169. double oy = rPoints[i].Y - rPoints[0].Y;
  1170. matRobot.Set<double>(i, 0, calibrationInfo.MarkPoint.X - ox);
  1171. matRobot.Set<double>(i, 1, calibrationInfo.MarkPoint.Y - oy);
  1172. }
  1173. else if (calibrationInfo.CameraMount == Enums.CameraMount.MobileDown_XYPlatform)
  1174. {
  1175. //计算每个点相对于第一个点移动的偏移量,将这个偏移量
  1176. double ox = rPoints[i].X - rPoints[0].X;
  1177. double oy = rPoints[i].Y - rPoints[0].Y;
  1178. matRobot.Set<double>(i, 0, calibrationInfo.MarkPoint.X - ox);
  1179. matRobot.Set<double>(i, 1, calibrationInfo.MarkPoint.Y - oy);
  1180. }
  1181. else
  1182. {
  1183. //将tool 0下的坐标转换成Tool n下的坐标
  1184. if (calibrationInfo.Tool != null)
  1185. {
  1186. ToolCoord toolCoord = new ToolCoord();
  1187. toolCoord.SetTool(calibrationInfo.Tool.X, calibrationInfo.Tool.Y);
  1188. double u1 = rPoints[i].U;
  1189. if (robot.Brand == Enums.RobotBrand.Schneider)
  1190. {
  1191. u1 *= -1;
  1192. }
  1193. var trans = toolCoord.GetToolnCoord(Vector<double>.Build.Dense(new double[] { rPoints[i].X, rPoints[i].Y }), u1);
  1194. matRobot.Set<double>(i, 0, trans[0]);
  1195. matRobot.Set<double>(i, 1, trans[1]);
  1196. }
  1197. else
  1198. {
  1199. matRobot.Set<double>(i, 0, rPoints[i].X);
  1200. matRobot.Set<double>(i, 1, rPoints[i].Y);
  1201. }
  1202. }
  1203. }
  1204. Mat mat2d = Cv2.EstimateAffine2D(matPixel, matRobot);
  1205. 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) } };
  1206. Matrix<double> matrix = Matrix<double>.Build.DenseOfArray(vv);
  1207. calibrationInfo.AffineTransformationMaterial = matrix.ToArray();
  1208. //mat2d.Get<double>(0, 0):表示x方向上的缩放。大于 1,则表示进行了放大操作;小于 1,则表示进行了缩小操作;等于 1,则表示没有进行缩放操作。
  1209. //mat2d.Get<double>(0, 1):表示垂直错切参数,与M[1,0]一起用于计算旋转角度
  1210. //mat2d.Get<double>(0, 2):表示x方向上的平移。
  1211. //mat2d.Get<double>(1, 0):表示水平错切参数,与M[0,1]一起用于计算旋转角度。
  1212. //mat2d.Get<double>(1, 1):表示y方向上的缩放。大于 1,则表示进行了放大操作;小于 1,则表示进行了缩小操作;等于 1,则表示没有进行缩放操作。
  1213. //mat2d.Get<double>(1, 2):表示y方向上的平移。
  1214. //double RotationAngle= Math.Atan2(mat2d.Get<double>(1, 0), mat2d.Get<double>(0, 1))*180/Math.PI; //旋转角度
  1215. //RMS(均方根值) 标定偏差
  1216. double sumX = 0, sumY = 0;
  1217. List<double> differenceX = new List<double>();
  1218. List<double> differenceY = new List<double>();
  1219. for (int i = 0; i < 9; i++)
  1220. {
  1221. var pixel = Vector<double>.Build.Dense(new double[] { PixelPoslist[i].X, PixelPoslist[i].Y, 1 });
  1222. if (calibrationInfo.CameraMount == Enums.CameraMount.MobileJ4)
  1223. {
  1224. //计算每个点相对于第一个点移动的偏移量,Mark点减去这个偏移量及是图像中对应的实际坐标
  1225. double ox = rPoints[i].X - rPoints[0].X;
  1226. double oy = rPoints[i].Y - rPoints[0].Y;
  1227. differenceX.Add((calibrationInfo.MarkPoint.X - ox) - (matrix * pixel)[0]);
  1228. differenceY.Add((calibrationInfo.MarkPoint.Y - oy) - (matrix * pixel)[1]);
  1229. }
  1230. else if (calibrationInfo.CameraMount == Enums.CameraMount.MobileDown_XYPlatform)
  1231. {
  1232. double ox = rPoints[i].X - rPoints[0].X;
  1233. double oy = rPoints[i].Y - rPoints[0].Y;
  1234. differenceX.Add((calibrationInfo.MarkPoint.X - ox) - (matrix * pixel)[0]);
  1235. differenceY.Add((calibrationInfo.MarkPoint.Y - oy) - (matrix * pixel)[1]);
  1236. }
  1237. else
  1238. {
  1239. //将tool 0下的坐标转换成Tool n下的坐标
  1240. if (calibrationInfo.Tool != null)
  1241. {
  1242. ToolCoord toolCoord = new ToolCoord();
  1243. toolCoord.SetTool(calibrationInfo.Tool.X, calibrationInfo.Tool.Y);
  1244. double u1 = rPoints[i].U;
  1245. if (robot.Brand == Enums.RobotBrand.Schneider)
  1246. {
  1247. u1 *= -1;
  1248. }
  1249. var trans = toolCoord.GetToolnCoord(Vector<double>.Build.Dense(new double[] { rPoints[i].X, rPoints[i].Y }), u1);
  1250. differenceX.Add(trans[0] - (matrix * pixel)[0]);
  1251. differenceY.Add(trans[1] - (matrix * pixel)[1]);
  1252. }
  1253. else
  1254. {
  1255. differenceX.Add(rPoints[i].X - (matrix * pixel)[0]);
  1256. differenceY.Add(rPoints[i].Y - (matrix * pixel)[1]);
  1257. }
  1258. }
  1259. sumX += Math.Pow(differenceX[differenceX.Count - 1], 2);
  1260. sumY += Math.Pow(differenceY[differenceY.Count - 1], 2);
  1261. }
  1262. double rmsX, rmsY;
  1263. rmsX = Math.Sqrt(sumX / 9);
  1264. rmsY = Math.Sqrt(sumY / 9);
  1265. CalibrationResult calibrationResult = new CalibrationResult();
  1266. calibrationResult.ScaleX = mat2d.Get<double>(0, 0);
  1267. calibrationResult.ScaleY = mat2d.Get<double>(1, 1);
  1268. calibrationResult.TranslationX = mat2d.Get<double>(0, 2);
  1269. calibrationResult.TranslationY = mat2d.Get<double>(1, 2);
  1270. calibrationResult.Rotate = Math.Atan2(mat2d.Get<double>(1, 0), mat2d.Get<double>(0, 1)) * 180 / Math.PI;
  1271. calibrationResult.RMSX = rmsX;
  1272. calibrationResult.RMSY = rmsY;
  1273. calibrationResult.MaxErrorValueX = differenceX.Max();
  1274. calibrationResult.MaxErrorValueY = differenceY.Max();
  1275. calibrationInfo.CalibrationResult = calibrationResult;
  1276. return (true, matrix, calibrationInfo.CalibPoints.ToArray(), calibrationResult);
  1277. }
  1278. /// <summary>
  1279. /// 执行校准验证。
  1280. /// 使用当前标定参数在一组验证点上计算误差并返回测试结果,用于评估标定质量。
  1281. /// </summary>
  1282. /// <param name="calibrationInfo">用于验证的点集与验收标准配置。</param>
  1283. /// <param name="robot">机器人接口,用于在验证过程中获取或移动到参考位姿。</param>
  1284. /// <param name="cancellationToken">用于取消操作的标记。</param>
  1285. /// <returns>元组字段:IsSuccess 表示验证是否通过;Result 包含详细的验证统计与误差分析。</returns>
  1286. public async Task<(bool IsSuccess, CalibrationTestResult Result)> ExecuteCalibrationValidation(CalibrationInfo calibrationInfo, IRobot robot, CancellationToken cancellationToken)
  1287. {
  1288. if (calibrationInfo == null) return (false, null);
  1289. if (robot == null || !robot.IsConnected) return (false, null);
  1290. double IntervalX = calibrationInfo.Width * 0.90 / 2;
  1291. double IntervalY = calibrationInfo.Height * 0.90 / 2;
  1292. Vector<double> P1 = Vector<double>.Build.Dense(new double[] { -IntervalX, -IntervalY });
  1293. Vector<double> P2 = Vector<double>.Build.Dense(new double[] { IntervalX, -IntervalY });
  1294. Vector<double> P3 = Vector<double>.Build.Dense(new double[] { 0, 0 });
  1295. Vector<double> P4 = Vector<double>.Build.Dense(new double[] { -IntervalX, IntervalY });
  1296. Vector<double> P5 = Vector<double>.Build.Dense(new double[] { IntervalX, IntervalY });
  1297. var P0 = Vector<double>.Build.Dense(new double[] { calibrationInfo.CenterPoint.X, calibrationInfo.CenterPoint.Y });
  1298. /// <summary>
  1299. /// 机器人与相机之间的旋转矩阵
  1300. /// </summary>
  1301. Matrix<double> RobotCameraRotationMatrix = Matrix<double>.Build.DenseOfArray(calibrationInfo.RobotCameraRotationMatrix);
  1302. //1
  1303. P1 = RobotCameraRotationMatrix.Inverse() * P1 + P0;
  1304. //2
  1305. P2 = RobotCameraRotationMatrix.Inverse() * P2 + P0;
  1306. //3
  1307. P3 = RobotCameraRotationMatrix.Inverse() * P3 + P0;
  1308. //4
  1309. P4 = RobotCameraRotationMatrix.Inverse() * P4 + P0;
  1310. //5
  1311. P5 = RobotCameraRotationMatrix.Inverse() * P5 + P0;
  1312. List<RPoint> rPoints = new List<RPoint>();
  1313. rPoints.Add(new RPoint()
  1314. {
  1315. Number = 1,
  1316. X = (float)P1[0],
  1317. Y = (float)P1[1],
  1318. Z = calibrationInfo.CenterPoint.Z,
  1319. U = calibrationInfo.CenterPoint.U,
  1320. V = calibrationInfo.CenterPoint.V,
  1321. W = calibrationInfo.CenterPoint.W,
  1322. Hand = calibrationInfo.CenterPoint.Hand,
  1323. Local = calibrationInfo.CenterPoint.Local,
  1324. Tool = calibrationInfo.CenterPoint.Tool
  1325. });
  1326. rPoints.Add(new RPoint()
  1327. {
  1328. Number = 2,
  1329. X = (float)P2[0],
  1330. Y = (float)P2[1],
  1331. Z = calibrationInfo.CenterPoint.Z,
  1332. U = calibrationInfo.CenterPoint.U,
  1333. V = calibrationInfo.CenterPoint.V,
  1334. W = calibrationInfo.CenterPoint.W,
  1335. Hand = calibrationInfo.CenterPoint.Hand,
  1336. Local = calibrationInfo.CenterPoint.Local,
  1337. Tool = calibrationInfo.CenterPoint.Tool
  1338. });
  1339. rPoints.Add(new RPoint()
  1340. {
  1341. Number = 3,
  1342. X = (float)P3[0],
  1343. Y = (float)P3[1],
  1344. Z = calibrationInfo.CenterPoint.Z,
  1345. U = calibrationInfo.CenterPoint.U,
  1346. V = calibrationInfo.CenterPoint.V,
  1347. W = calibrationInfo.CenterPoint.W,
  1348. Hand = calibrationInfo.CenterPoint.Hand,
  1349. Local = calibrationInfo.CenterPoint.Local,
  1350. Tool = calibrationInfo.CenterPoint.Tool
  1351. });
  1352. rPoints.Add(new RPoint()
  1353. {
  1354. Number = 4,
  1355. X = (float)P4[0],
  1356. Y = (float)P4[1],
  1357. Z = calibrationInfo.CenterPoint.Z,
  1358. U = calibrationInfo.CenterPoint.U,
  1359. V = calibrationInfo.CenterPoint.V,
  1360. W = calibrationInfo.CenterPoint.W,
  1361. Hand = calibrationInfo.CenterPoint.Hand,
  1362. Local = calibrationInfo.CenterPoint.Local,
  1363. Tool = calibrationInfo.CenterPoint.Tool
  1364. });
  1365. rPoints.Add(new RPoint()
  1366. {
  1367. Number = 5,
  1368. X = (float)P5[0],
  1369. Y = (float)P5[1],
  1370. Z = calibrationInfo.CenterPoint.Z,
  1371. U = calibrationInfo.CenterPoint.U,
  1372. V = calibrationInfo.CenterPoint.V,
  1373. W = calibrationInfo.CenterPoint.W,
  1374. Hand = calibrationInfo.CenterPoint.Hand,
  1375. Local = calibrationInfo.CenterPoint.Local,
  1376. Tool = calibrationInfo.CenterPoint.Tool
  1377. });
  1378. List<Vector<double>> TestPos = new List<Vector<double>>();
  1379. for (int i = 0; i < rPoints.Count; i++)
  1380. {
  1381. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown)
  1382. {
  1383. //移动机器人至i
  1384. await robot.CalibMotionAsync(rPoints[i], null);
  1385. if (cancellationToken.IsCancellationRequested)
  1386. {
  1387. return (false, null);
  1388. }
  1389. //关闭吸气
  1390. await robot.CalibOutIOAsync(false);
  1391. //移动机器人至待机位置
  1392. await robot.CalibMotionAsync(calibrationInfo.HomePoint, null);
  1393. }
  1394. else
  1395. {
  1396. //移动机器人至i
  1397. await robot.CalibMotionAsync(rPoints[i], rPoints[i].Z);
  1398. }
  1399. if (cancellationToken.IsCancellationRequested)
  1400. {
  1401. return (false, null);
  1402. }
  1403. //相机拍照并处理图像
  1404. var photoResult = await CaptureAndProcessCallback();
  1405. if (!photoResult.IsSuccess)
  1406. {
  1407. return (false, null);
  1408. }
  1409. PointF pixelpos = new PointF((float)(photoResult.X), (float)(photoResult.Y));
  1410. Matrix<double> matrix = Matrix<double>.Build.DenseOfArray(calibrationInfo.AffineTransformationMaterial);
  1411. var rpos = matrix * Vector<double>.Build.Dense(new double[] { pixelpos.X, pixelpos.Y, 1 });
  1412. TestPos.Add(rpos);
  1413. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown)
  1414. {
  1415. //移动机器人至i
  1416. await robot.CalibMotionAsync(rPoints[i], null);
  1417. //打开吸气
  1418. await robot.CalibOutIOAsync(true);
  1419. }
  1420. if (cancellationToken.IsCancellationRequested)
  1421. {
  1422. return (false, null);
  1423. }
  1424. }
  1425. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown)
  1426. {
  1427. //移动机器人至待机位置
  1428. await robot.CalibMotionAsync(calibrationInfo.HomePoint, null);
  1429. }
  1430. //RMS(均方根值) 标定偏差
  1431. double sumX = 0, sumY = 0;
  1432. List<double> differenceX = new List<double>();
  1433. List<double> differenceY = new List<double>();
  1434. for (int i = 0; i < 5; i++)
  1435. {
  1436. if (calibrationInfo.CameraMount == Enums.CameraMount.MobileJ4)
  1437. {
  1438. //像素直接转换成mm时的点位
  1439. double Robot_X, Robot_Y;
  1440. Robot_X = TestPos[i][0];
  1441. Robot_Y = TestPos[i][1];
  1442. //机器人当前TOOL 0下的坐标
  1443. double curpos_x = 0, curpos_y = 0, curpos_u = 0;
  1444. curpos_x = rPoints[i].X;
  1445. curpos_y = rPoints[i].Y;
  1446. curpos_u = rPoints[i].U;
  1447. //得到旋转矩阵
  1448. double angle = Math.PI * (curpos_u - calibrationInfo.MarkPoint.U) / 180;
  1449. // 创建T矩阵
  1450. Matrix<double> rotationMatrix = Matrix<double>.Build.DenseOfArray(new double[,]
  1451. {
  1452. { Math.Cos(angle), -Math.Sin(angle) },
  1453. { Math.Sin(angle), Math.Cos(angle) }
  1454. });
  1455. Vector<double> p3 = Vector<double>.Build.Dense(new double[] { Robot_X - calibrationInfo.CalibPoints[0].Robot.X, Robot_Y - calibrationInfo.CalibPoints[0].Robot.Y });
  1456. Vector<double> phere = Vector<double>.Build.Dense(new double[] { curpos_x, curpos_y });
  1457. var cc = rotationMatrix * p3 + phere;
  1458. differenceX.Add(cc[0] - calibrationInfo.MarkPoint.X);
  1459. differenceY.Add(cc[1] - calibrationInfo.MarkPoint.Y);
  1460. }
  1461. else if (calibrationInfo.CameraMount == Enums.CameraMount.MobileDown_XYPlatform)
  1462. {
  1463. //像素直接转换成mm时的点位
  1464. double Robot_X, Robot_Y;
  1465. Robot_X = TestPos[i][0];
  1466. Robot_Y = TestPos[i][1];
  1467. //机器人当前TOOL 0下的坐标
  1468. double curpos_x = 0, curpos_y = 0, curpos_u = 0;
  1469. curpos_x = rPoints[i].X;
  1470. curpos_y = rPoints[i].Y;
  1471. curpos_u = rPoints[i].U;
  1472. //得到旋转矩阵
  1473. double angle = Math.PI * (curpos_u - calibrationInfo.MarkPoint.U) / 180;
  1474. // 创建T矩阵
  1475. Matrix<double> rotationMatrix = Matrix<double>.Build.DenseOfArray(new double[,]
  1476. {
  1477. { Math.Cos(angle), -Math.Sin(angle) },
  1478. { Math.Sin(angle), Math.Cos(angle) }
  1479. });
  1480. Vector<double> p3 = Vector<double>.Build.Dense(new double[] { Robot_X - calibrationInfo.CalibPoints[0].Robot.X, Robot_Y - calibrationInfo.CalibPoints[0].Robot.Y });
  1481. Vector<double> phere = Vector<double>.Build.Dense(new double[] { curpos_x, curpos_y });
  1482. var cc = rotationMatrix * p3 + phere;
  1483. differenceX.Add(cc[0] - calibrationInfo.MarkPoint.X);
  1484. differenceY.Add(cc[1] - calibrationInfo.MarkPoint.Y);
  1485. }
  1486. else
  1487. {
  1488. //将tool 0下的坐标转换成Tool n下的坐标
  1489. if (calibrationInfo.Tool != null)
  1490. {
  1491. ToolCoord toolCoord = new ToolCoord();
  1492. toolCoord.SetTool(calibrationInfo.Tool.X, calibrationInfo.Tool.Y);
  1493. double u1 = rPoints[i].U;
  1494. if (robot.Brand == Enums.RobotBrand.Schneider)
  1495. {
  1496. u1 *= -1;
  1497. }
  1498. var trans = toolCoord.GetToolnCoord(Vector<double>.Build.Dense(new double[] { rPoints[i].X, rPoints[i].Y }), u1);
  1499. differenceX.Add(trans[0] - TestPos[i][0]);
  1500. differenceY.Add(trans[1] - TestPos[i][1]);
  1501. }
  1502. else
  1503. {
  1504. differenceX.Add(rPoints[i].X - TestPos[i][0]);
  1505. differenceY.Add(rPoints[i].Y - TestPos[i][1]);
  1506. }
  1507. }
  1508. sumX += Math.Pow(differenceX[differenceX.Count - 1], 2);
  1509. sumY += Math.Pow(differenceY[differenceY.Count - 1], 2);
  1510. }
  1511. double rmsX, rmsY;
  1512. rmsX = Math.Sqrt(sumX / 5);
  1513. rmsY = Math.Sqrt(sumY / 5);
  1514. CalibrationTestResult calibrationTestResult = new CalibrationTestResult();
  1515. calibrationTestResult.MaxErrorValueX = differenceX.Max();
  1516. calibrationTestResult.MinErrorValueX = differenceX.Min();
  1517. calibrationTestResult.MaxErrorValueY = differenceY.Max();
  1518. calibrationTestResult.MinErrorValueY = differenceY.Min();
  1519. calibrationTestResult.RMSEX = rmsX;
  1520. calibrationTestResult.RMSEY = rmsY;
  1521. return (true, calibrationTestResult);
  1522. }
  1523. /// <summary>
  1524. /// 独立模组相机计算独立模组步进坐标系与绝对坐标系的刚体变换矩阵
  1525. /// </summary>
  1526. /// <param name="calibrationInfo"></param>
  1527. /// <param name="robot"></param>
  1528. /// <param name="cancellationToken"></param>
  1529. /// <returns></returns>
  1530. public async Task<(bool IsSuccess, double[,] TransformationMatrix)> CalculateIndependentCameraRigidTransformationMatrix(CalibrationInfo calibrationInfo, IRobot robot, CancellationToken cancellationToken)
  1531. {
  1532. //模组移动Mark至相机中心点,此时Mark的绝对坐标是:P0,步进坐标:P1
  1533. // 1.移动独立模组至待机点
  1534. bool isFinished = await ((XYZU_Robot)robot).WaitCameraMoveFinishedAsync(calibrationInfo.CameraMotionModuleIndex, calibrationInfo.IndependentDownCameraMotionModulePoint);
  1535. if (!isFinished)
  1536. {
  1537. return (false, null);
  1538. }
  1539. PointF P11 = new PointF(calibrationInfo.IndependentDownCameraMotionModulePoint.X, calibrationInfo.IndependentDownCameraMotionModulePoint.Y);
  1540. double pixelScaleX = 0;
  1541. double pixelScaleY = 0;
  1542. Matrix<double> RobotCameraRotationMatrix = null;
  1543. // 2.移动机器人至中心位置
  1544. await robot.CalibMotionAsync(calibrationInfo.CenterPoint, null);
  1545. // 3.计算相机与机器人坐标系的旋转矩阵
  1546. var robotCameraRotationMatrix = await AutoIdentifyRobotCameraRotationMatrix(calibrationInfo, robot, cancellationToken);
  1547. if (!robotCameraRotationMatrix.IsSuccess)
  1548. {
  1549. return (false, null);
  1550. }
  1551. pixelScaleX = robotCameraRotationMatrix.PixelScaleX;
  1552. pixelScaleY = robotCameraRotationMatrix.PixelScaleY;
  1553. RobotCameraRotationMatrix = robotCameraRotationMatrix.Item2;
  1554. // 4.自动移动机器人至图像中心点位置
  1555. var autoMoveResult = await AutoMoveRobotToImageCenter(robot, RobotCameraRotationMatrix, pixelScaleX, pixelScaleY, cancellationToken);
  1556. if (!autoMoveResult.IsSuccess)
  1557. {
  1558. return (false, null);
  1559. }
  1560. // 5.记录当前机器人Tool 0下的坐标转换成工具坐标下的坐标,并记录下来
  1561. ToolCoord toolCoord = new ToolCoord();
  1562. toolCoord.SetTool(calibrationInfo.Tool.X, calibrationInfo.Tool.Y);
  1563. //获取机器人当前坐标
  1564. var currentRobotPos = await robot.GetRobotPosAsync();
  1565. Vector<double> p = Vector<double>.Build.Dense(new double[] { currentRobotPos.X, currentRobotPos.Y }); ;
  1566. var p1 = toolCoord.GetToolnCoord(p, currentRobotPos.U);
  1567. PointF P1 = new PointF((float)p1[0], (float)p1[1]);
  1568. // 6.移动独立模组至标定点位置
  1569. var movePos = calibrationInfo.IndependentDownCameraMotionModulePoint.Clone();
  1570. movePos.X += (float)calibrationInfo.IndependentDownCameraMotionModuleMoveX;
  1571. isFinished = await ((XYZU_Robot)robot).WaitCameraMoveFinishedAsync(calibrationInfo.CameraMotionModuleIndex, movePos);
  1572. if (!isFinished)
  1573. {
  1574. return (false, null);
  1575. }
  1576. PointF P12 = new PointF(movePos.X, movePos.Y);
  1577. await Task.Delay(300);
  1578. // 7.自动移动机器人至图像中心点位置
  1579. autoMoveResult = await AutoMoveRobotToImageCenter(robot, RobotCameraRotationMatrix, pixelScaleX, pixelScaleY, cancellationToken);
  1580. if (!autoMoveResult.IsSuccess)
  1581. {
  1582. return (false, null);
  1583. }
  1584. // 8.记录当前机器人Tool 0下的坐标转换成工具坐标下的坐标,并记录下来
  1585. ToolCoord toolCoord1 = new ToolCoord();
  1586. toolCoord1.SetTool(calibrationInfo.Tool.X, calibrationInfo.Tool.Y);
  1587. //获取机器人当前坐标
  1588. currentRobotPos = await robot.GetRobotPosAsync();
  1589. Vector<double> pf = Vector<double>.Build.Dense(new double[] { currentRobotPos.X, currentRobotPos.Y }); ;
  1590. var p2 = toolCoord1.GetToolnCoord(pf, currentRobotPos.U);
  1591. PointF P2 = new PointF((float)p2[0], (float)p2[1]);
  1592. CoordinateTransformer local = new CoordinateTransformer(P1, P2, P11, P12);
  1593. // 9.计算刚体变换矩阵
  1594. //得到旋转矩阵
  1595. var rigidMatrix = local.InvR;
  1596. //平移矩阵
  1597. var translation = local.T;
  1598. double[,] TransformationMatrix = new double[3, 3];
  1599. TransformationMatrix[0, 0] = rigidMatrix[0, 0];
  1600. TransformationMatrix[0, 1] = rigidMatrix[0, 1];
  1601. TransformationMatrix[0, 2] = translation[0, 0];
  1602. TransformationMatrix[1, 0] = rigidMatrix[1, 0];
  1603. TransformationMatrix[1, 1] = rigidMatrix[1, 1];
  1604. TransformationMatrix[1, 2] = translation[1, 0];
  1605. TransformationMatrix[2, 0] = 0;
  1606. TransformationMatrix[2, 1] = 0;
  1607. TransformationMatrix[2, 2] = 1;
  1608. return (true, TransformationMatrix);
  1609. }
  1610. public void Dispose()
  1611. {
  1612. GC.SuppressFinalize(this);
  1613. }
  1614. }
  1615. }