CameraCalibrationService.cs 63 KB

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