CameraCalibrationService.cs 65 KB

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