CameraCalibrationService.cs 89 KB

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