CameraCalibrationService.cs 85 KB

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