CameraCalibrationService.cs 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  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. await Task.Delay(200);
  651. result = await CaptureAndProcessCallback();
  652. if (!result.IsSuccess)
  653. {
  654. return (false, 0, 0, 0);
  655. }
  656. curPixel = new PointF((float)result.X, (float)result.Y);
  657. offsetx = P0.X - curPixel.X;
  658. offsety = P0.Y - curPixel.Y;
  659. //await Console.Out.WriteLineAsync($"移动后的差异:{offsetx},{offsety}");
  660. if (Math.Abs(offsetx) < 3 && Math.Abs(offsetx) < 3)
  661. {
  662. IsFinish = true;
  663. break;
  664. }
  665. if (cancellationToken.IsCancellationRequested)
  666. {
  667. return (false, 0, 0, 0);
  668. }
  669. }
  670. if (IsFinish)
  671. {
  672. var finalPos = robot.GetRobotPos();
  673. return (true, finalPos.X, finalPos.Y, finalPos.U);
  674. }
  675. else
  676. {
  677. return (false, 0, 0, 0);
  678. }
  679. }
  680. /// <summary>
  681. /// 自动九点标定。
  682. /// 使用 9 个采样点进行仿射/透视变换求解,以提高标定精度并生成验证用的九点数据结构。
  683. /// </summary>
  684. /// <param name="calibrationInfo">九点标定的配置信息(点列、采集顺序、图像处理参数等)。</param>
  685. /// <param name="robot">机器人接口。</param>
  686. /// <param name="cancellationToken">用于取消操作的标记。</param>
  687. /// <returns>
  688. /// 元组字段:
  689. /// IsSuccess - 标定是否成功;
  690. /// AffineTransformationMaterial - 求解得到的仿射变换矩阵;
  691. /// NinePoint - 九点标定结果封装(像素点与对应的机器人位姿);
  692. /// Result - 详细的标定结果与统计信息(误差、残差等)。
  693. /// </returns>
  694. public async Task<(bool IsSuccess, Matrix<double> AffineTransformationMaterial, RobotPixelPoint[] NinePoint, CalibrationResult Result)> AutoNinePointCalibration(CalibrationInfo calibrationInfo, IRobot robot, CancellationToken cancellationToken)
  695. {
  696. if (calibrationInfo == null) return (false, null, null, null);
  697. if (robot == null || !robot.IsConnected) return (false, null, null, null);
  698. double IntervalX = calibrationInfo.Width / 2;
  699. double IntervalY = calibrationInfo.Height / 2;
  700. Vector<double> P1 = Vector<double>.Build.Dense(new double[] { -IntervalX, -IntervalY });
  701. Vector<double> P2 = Vector<double>.Build.Dense(new double[] { 0, -IntervalY });
  702. Vector<double> P3 = Vector<double>.Build.Dense(new double[] { IntervalX, -IntervalY });
  703. Vector<double> P4 = Vector<double>.Build.Dense(new double[] { IntervalX, 0 });
  704. Vector<double> P5 = Vector<double>.Build.Dense(new double[] { 0, 0 });
  705. Vector<double> P6 = Vector<double>.Build.Dense(new double[] { -IntervalX, 0 });
  706. Vector<double> P7 = Vector<double>.Build.Dense(new double[] { -IntervalX, IntervalY });
  707. Vector<double> P8 = Vector<double>.Build.Dense(new double[] { 0, IntervalY });
  708. Vector<double> P9 = Vector<double>.Build.Dense(new double[] { IntervalX, IntervalY });
  709. //发送校准的参数
  710. await robot.CalibParameAsync(calibrationInfo.Pick, calibrationInfo.Speed, calibrationInfo.Accel, calibrationInfo.Power, calibrationInfo.WaitSuction, calibrationInfo.WaitBlow);
  711. var P0 = Vector<double>.Build.Dense(new double[] { calibrationInfo.CenterPoint.X, calibrationInfo.CenterPoint.Y });
  712. if (robot.Brand == Enums.RobotBrand.XYZ_Platform)
  713. {
  714. //移动机器人至中心位置
  715. await robot.CalibMotionAsync(calibrationInfo.CenterPoint, 0);
  716. if (cancellationToken.IsCancellationRequested)
  717. {
  718. return (false, null, null, null);
  719. }
  720. //计算相机与机器人坐标系的旋转矩阵
  721. var robotCameraRotationMatrix = await AutoIdentifyRobotCameraRotationMatrix(calibrationInfo, robot, cancellationToken);
  722. if (!robotCameraRotationMatrix.IsSuccess)
  723. {
  724. return (false, null, null, null);
  725. }
  726. //自动移动机器人至图像中心点位置
  727. var autoMoveResult = await AutoMoveRobotToImageCenter(robot, robotCameraRotationMatrix.RotationMatrix, robotCameraRotationMatrix.PixelScaleX, robotCameraRotationMatrix.PixelScaleY, cancellationToken);
  728. if (!autoMoveResult.IsSuccess)
  729. {
  730. return (false, null, null, null);
  731. }
  732. calibrationInfo.CenterPoint.X = (float)autoMoveResult.X;
  733. calibrationInfo.CenterPoint.Y = (float)autoMoveResult.Y;
  734. if (calibrationInfo.CameraMount != Enums.CameraMount.MobileDown_XYPlatform)
  735. {
  736. calibrationInfo.MarkPoint = calibrationInfo.CenterPoint.Clone();
  737. }
  738. calibrationInfo.RobotCameraRotationMatrix = robotCameraRotationMatrix.RotationMatrix.ToArray();
  739. }
  740. /// <summary>
  741. /// 机器人与相机之间的旋转矩阵
  742. /// </summary>
  743. Matrix<double> RobotCameraRotationMatrix = Matrix<double>.Build.DenseOfArray(calibrationInfo.RobotCameraRotationMatrix);
  744. //1
  745. P1 = RobotCameraRotationMatrix.Inverse() * P1 + P0;
  746. //2
  747. P2 = RobotCameraRotationMatrix.Inverse() * P2 + P0;
  748. //3
  749. P3 = RobotCameraRotationMatrix.Inverse() * P3 + P0;
  750. //4
  751. P4 = RobotCameraRotationMatrix.Inverse() * P4 + P0;
  752. //5
  753. P5 = RobotCameraRotationMatrix.Inverse() * P5 + P0;
  754. //6
  755. P6 = RobotCameraRotationMatrix.Inverse() * P6 + P0;
  756. //7
  757. P7 = RobotCameraRotationMatrix.Inverse() * P7 + P0;
  758. //8
  759. P8 = RobotCameraRotationMatrix.Inverse() * P8 + P0;
  760. //9
  761. P9 = RobotCameraRotationMatrix.Inverse() * P9 + P0;
  762. List<RPoint> rPoints = new List<RPoint>();
  763. rPoints.Add(new RPoint()
  764. {
  765. Number = 1,
  766. X = (float)P1[0],
  767. Y = (float)P1[1],
  768. Z = calibrationInfo.CenterPoint.Z,
  769. U = calibrationInfo.CenterPoint.U,
  770. V = calibrationInfo.CenterPoint.V,
  771. W = calibrationInfo.CenterPoint.W,
  772. Hand = calibrationInfo.CenterPoint.Hand,
  773. Local = calibrationInfo.CenterPoint.Local,
  774. Tool = calibrationInfo.CenterPoint.Tool
  775. });
  776. rPoints.Add(new RPoint()
  777. {
  778. Number = 2,
  779. X = (float)P2[0],
  780. Y = (float)P2[1],
  781. Z = calibrationInfo.CenterPoint.Z,
  782. U = calibrationInfo.CenterPoint.U,
  783. V = calibrationInfo.CenterPoint.V,
  784. W = calibrationInfo.CenterPoint.W,
  785. Hand = calibrationInfo.CenterPoint.Hand,
  786. Local = calibrationInfo.CenterPoint.Local,
  787. Tool = calibrationInfo.CenterPoint.Tool
  788. });
  789. rPoints.Add(new RPoint()
  790. {
  791. Number = 3,
  792. X = (float)P3[0],
  793. Y = (float)P3[1],
  794. Z = calibrationInfo.CenterPoint.Z,
  795. U = calibrationInfo.CenterPoint.U,
  796. V = calibrationInfo.CenterPoint.V,
  797. W = calibrationInfo.CenterPoint.W,
  798. Hand = calibrationInfo.CenterPoint.Hand,
  799. Local = calibrationInfo.CenterPoint.Local,
  800. Tool = calibrationInfo.CenterPoint.Tool
  801. });
  802. rPoints.Add(new RPoint()
  803. {
  804. Number = 4,
  805. X = (float)P4[0],
  806. Y = (float)P4[1],
  807. Z = calibrationInfo.CenterPoint.Z,
  808. U = calibrationInfo.CenterPoint.U,
  809. V = calibrationInfo.CenterPoint.V,
  810. W = calibrationInfo.CenterPoint.W,
  811. Hand = calibrationInfo.CenterPoint.Hand,
  812. Local = calibrationInfo.CenterPoint.Local,
  813. Tool = calibrationInfo.CenterPoint.Tool
  814. });
  815. rPoints.Add(new RPoint()
  816. {
  817. Number = 5,
  818. X = (float)P5[0],
  819. Y = (float)P5[1],
  820. Z = calibrationInfo.CenterPoint.Z,
  821. U = calibrationInfo.CenterPoint.U,
  822. V = calibrationInfo.CenterPoint.V,
  823. W = calibrationInfo.CenterPoint.W,
  824. Hand = calibrationInfo.CenterPoint.Hand,
  825. Local = calibrationInfo.CenterPoint.Local,
  826. Tool = calibrationInfo.CenterPoint.Tool
  827. });
  828. rPoints.Add(new RPoint()
  829. {
  830. Number = 6,
  831. X = (float)P6[0],
  832. Y = (float)P6[1],
  833. Z = calibrationInfo.CenterPoint.Z,
  834. U = calibrationInfo.CenterPoint.U,
  835. V = calibrationInfo.CenterPoint.V,
  836. W = calibrationInfo.CenterPoint.W,
  837. Hand = calibrationInfo.CenterPoint.Hand,
  838. Local = calibrationInfo.CenterPoint.Local,
  839. Tool = calibrationInfo.CenterPoint.Tool
  840. });
  841. rPoints.Add(new RPoint()
  842. {
  843. Number = 7,
  844. X = (float)P7[0],
  845. Y = (float)P7[1],
  846. Z = calibrationInfo.CenterPoint.Z,
  847. U = calibrationInfo.CenterPoint.U,
  848. V = calibrationInfo.CenterPoint.V,
  849. W = calibrationInfo.CenterPoint.W,
  850. Hand = calibrationInfo.CenterPoint.Hand,
  851. Local = calibrationInfo.CenterPoint.Local,
  852. Tool = calibrationInfo.CenterPoint.Tool
  853. });
  854. rPoints.Add(new RPoint()
  855. {
  856. Number = 8,
  857. X = (float)P8[0],
  858. Y = (float)P8[1],
  859. Z = calibrationInfo.CenterPoint.Z,
  860. U = calibrationInfo.CenterPoint.U,
  861. V = calibrationInfo.CenterPoint.V,
  862. W = calibrationInfo.CenterPoint.W,
  863. Hand = calibrationInfo.CenterPoint.Hand,
  864. Local = calibrationInfo.CenterPoint.Local,
  865. Tool = calibrationInfo.CenterPoint.Tool
  866. });
  867. rPoints.Add(new RPoint()
  868. {
  869. Number = 9,
  870. X = (float)P9[0],
  871. Y = (float)P9[1],
  872. Z = calibrationInfo.CenterPoint.Z,
  873. U = calibrationInfo.CenterPoint.U,
  874. V = calibrationInfo.CenterPoint.V,
  875. W = calibrationInfo.CenterPoint.W,
  876. Hand = calibrationInfo.CenterPoint.Hand,
  877. Local = calibrationInfo.CenterPoint.Local,
  878. Tool = calibrationInfo.CenterPoint.Tool
  879. });
  880. List<PointF> PixelPoslist = new List<PointF>();
  881. if (calibrationInfo.CalibPoints == null)
  882. {
  883. calibrationInfo.CalibPoints = new System.Collections.ObjectModel.ObservableCollection<RobotPixelPoint>();
  884. }
  885. else
  886. {
  887. calibrationInfo.CalibPoints.Clear();
  888. }
  889. //移动相机标定时,需要先记住特征点的绝对坐标(相对于机器人坐标系) P0
  890. if (calibrationInfo.CameraMount == Enums.CameraMount.MobileJ4)
  891. {
  892. //移动机器人至中心位置
  893. await robot.CalibMotionAsync(calibrationInfo.CenterPoint, 0);
  894. if (cancellationToken.IsCancellationRequested)
  895. {
  896. return (false, null, null, null);
  897. }
  898. //将当前机器人Tool 0下的坐标转换成工具坐标下的坐标,并记录下来
  899. ToolCoord toolCoord = new ToolCoord();
  900. toolCoord.SetTool(calibrationInfo.Tool.X, calibrationInfo.Tool.Y);
  901. Vector<double> p = Vector<double>.Build.Dense(new double[] { calibrationInfo.CenterPoint.X, calibrationInfo.CenterPoint.Y }); ;
  902. var pn = toolCoord.GetToolnCoord(p, calibrationInfo.CenterPoint.U);
  903. if (calibrationInfo.MarkPoint == null)
  904. {
  905. calibrationInfo.MarkPoint = new RPoint();
  906. }
  907. calibrationInfo.MarkPoint = calibrationInfo.CenterPoint.Clone();
  908. calibrationInfo.MarkPoint.X = (float)pn[0];
  909. calibrationInfo.MarkPoint.Y = (float)pn[1];
  910. }
  911. for (int i = 0; i < rPoints.Count; i++)
  912. {
  913. //移动机器人至i
  914. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown)
  915. {
  916. await robot.CalibMotionAsync(rPoints[i], 0);
  917. if (cancellationToken.IsCancellationRequested)
  918. {
  919. return (false, null, null, null);
  920. }
  921. //关闭吸气
  922. await robot.CalibOutIOAsync(false);
  923. //移动机器人至待机位置
  924. await robot.CalibMotionAsync(calibrationInfo.HomePoint, 0);
  925. }
  926. else
  927. {
  928. await robot.CalibMotionAsync(rPoints[i], rPoints[i].Z);
  929. }
  930. if (cancellationToken.IsCancellationRequested)
  931. {
  932. return (false, null, null, null);
  933. }
  934. //相机拍照并处理图像
  935. var photoResult = await CaptureAndProcessCallback();
  936. if (!photoResult.IsSuccess)
  937. {
  938. return (false, null, null, null);
  939. }
  940. PointF curPixel = new PointF((float)(photoResult.X), (float)(photoResult.Y));
  941. PixelPoslist.Add(curPixel);
  942. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown)
  943. {
  944. //移动机器人至i
  945. await robot.CalibMotionAsync(rPoints[i], 0);
  946. if (cancellationToken.IsCancellationRequested)
  947. {
  948. return (false, null, null, null);
  949. }
  950. //打开吸气
  951. await robot.CalibOutIOAsync(true);
  952. }
  953. calibrationInfo.CalibPoints.Add(new RobotPixelPoint()
  954. {
  955. Number = i + 1,
  956. Robot = new System.Drawing.PointF(rPoints[i].X, rPoints[i].Y),
  957. Pixel = new System.Drawing.PointF(PixelPoslist[i].X, PixelPoslist[i].Y)
  958. });
  959. if (cancellationToken.IsCancellationRequested)
  960. {
  961. return (false, null, null, null);
  962. }
  963. }
  964. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown)
  965. {
  966. //移动机器人至待机位置
  967. await robot.CalibMotionAsync(calibrationInfo.HomePoint, 0);
  968. }
  969. if (cancellationToken.IsCancellationRequested)
  970. {
  971. return (false, null, null, null);
  972. }
  973. Mat matPixel = new Mat(9, 2, MatType.CV_64F);
  974. Mat matRobot = new Mat(9, 2, MatType.CV_64F);
  975. for (int i = 0; i < 9; i++)
  976. {
  977. matPixel.Set<double>(i, 0, PixelPoslist[i].X);
  978. matPixel.Set<double>(i, 1, PixelPoslist[i].Y);
  979. //如果是移动相机拍照需要将机器人的9点进行一个变换,为了达到Mark点动,相机不动的目的
  980. if (calibrationInfo.CameraMount == Enums.CameraMount.MobileJ4)
  981. {
  982. //计算每个点相对于第一个点移动的偏移量,将这个偏移量
  983. double ox = rPoints[i].X - rPoints[0].X;
  984. double oy = rPoints[i].Y - rPoints[0].Y;
  985. matRobot.Set<double>(i, 0, calibrationInfo.MarkPoint.X - ox);
  986. matRobot.Set<double>(i, 1, calibrationInfo.MarkPoint.Y - oy);
  987. }
  988. else if (calibrationInfo.CameraMount == Enums.CameraMount.MobileDown_XYPlatform)
  989. {
  990. //计算每个点相对于第一个点移动的偏移量,将这个偏移量
  991. double ox = rPoints[i].X - rPoints[0].X;
  992. double oy = rPoints[i].Y - rPoints[0].Y;
  993. matRobot.Set<double>(i, 0, calibrationInfo.MarkPoint.X - ox);
  994. matRobot.Set<double>(i, 1, calibrationInfo.MarkPoint.Y - oy);
  995. }
  996. else
  997. {
  998. //将tool 0下的坐标转换成Tool n下的坐标
  999. if (calibrationInfo.Tool != null)
  1000. {
  1001. ToolCoord toolCoord = new ToolCoord();
  1002. toolCoord.SetTool(calibrationInfo.Tool.X, calibrationInfo.Tool.Y);
  1003. double u1 = rPoints[i].U;
  1004. if (robot.Brand == Enums.RobotBrand.Schneider)
  1005. {
  1006. u1 *= -1;
  1007. }
  1008. var trans = toolCoord.GetToolnCoord(Vector<double>.Build.Dense(new double[] { rPoints[i].X, rPoints[i].Y }), u1);
  1009. matRobot.Set<double>(i, 0, trans[0]);
  1010. matRobot.Set<double>(i, 1, trans[1]);
  1011. }
  1012. else
  1013. {
  1014. matRobot.Set<double>(i, 0, rPoints[i].X);
  1015. matRobot.Set<double>(i, 1, rPoints[i].Y);
  1016. }
  1017. }
  1018. }
  1019. Mat mat2d = Cv2.EstimateAffine2D(matPixel, matRobot);
  1020. 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) } };
  1021. Matrix<double> matrix = Matrix<double>.Build.DenseOfArray(vv);
  1022. calibrationInfo.AffineTransformationMaterial = matrix.ToArray();
  1023. //mat2d.Get<double>(0, 0):表示x方向上的缩放。大于 1,则表示进行了放大操作;小于 1,则表示进行了缩小操作;等于 1,则表示没有进行缩放操作。
  1024. //mat2d.Get<double>(0, 1):表示垂直错切参数,与M[1,0]一起用于计算旋转角度
  1025. //mat2d.Get<double>(0, 2):表示x方向上的平移。
  1026. //mat2d.Get<double>(1, 0):表示水平错切参数,与M[0,1]一起用于计算旋转角度。
  1027. //mat2d.Get<double>(1, 1):表示y方向上的缩放。大于 1,则表示进行了放大操作;小于 1,则表示进行了缩小操作;等于 1,则表示没有进行缩放操作。
  1028. //mat2d.Get<double>(1, 2):表示y方向上的平移。
  1029. //double RotationAngle= Math.Atan2(mat2d.Get<double>(1, 0), mat2d.Get<double>(0, 1))*180/Math.PI; //旋转角度
  1030. //RMS(均方根值) 标定偏差
  1031. double sumX = 0, sumY = 0;
  1032. List<double> differenceX = new List<double>();
  1033. List<double> differenceY = new List<double>();
  1034. for (int i = 0; i < 9; i++)
  1035. {
  1036. var pixel = Vector<double>.Build.Dense(new double[] { PixelPoslist[i].X, PixelPoslist[i].Y, 1 });
  1037. if (calibrationInfo.CameraMount == Enums.CameraMount.MobileJ4)
  1038. {
  1039. //计算每个点相对于第一个点移动的偏移量,Mark点减去这个偏移量及是图像中对应的实际坐标
  1040. double ox = rPoints[i].X - rPoints[0].X;
  1041. double oy = rPoints[i].Y - rPoints[0].Y;
  1042. differenceX.Add((calibrationInfo.MarkPoint.X - ox) - (matrix * pixel)[0]);
  1043. differenceY.Add((calibrationInfo.MarkPoint.Y - oy) - (matrix * pixel)[1]);
  1044. }
  1045. else if (calibrationInfo.CameraMount == Enums.CameraMount.MobileDown_XYPlatform)
  1046. {
  1047. double ox = rPoints[i].X - rPoints[0].X;
  1048. double oy = rPoints[i].Y - rPoints[0].Y;
  1049. differenceX.Add((calibrationInfo.MarkPoint.X - ox) - (matrix * pixel)[0]);
  1050. differenceY.Add((calibrationInfo.MarkPoint.Y - oy) - (matrix * pixel)[1]);
  1051. }
  1052. else
  1053. {
  1054. //将tool 0下的坐标转换成Tool n下的坐标
  1055. if (calibrationInfo.Tool != null)
  1056. {
  1057. ToolCoord toolCoord = new ToolCoord();
  1058. toolCoord.SetTool(calibrationInfo.Tool.X, calibrationInfo.Tool.Y);
  1059. double u1 = rPoints[i].U;
  1060. if (robot.Brand == Enums.RobotBrand.Schneider)
  1061. {
  1062. u1 *= -1;
  1063. }
  1064. var trans = toolCoord.GetToolnCoord(Vector<double>.Build.Dense(new double[] { rPoints[i].X, rPoints[i].Y }), u1);
  1065. differenceX.Add(trans[0] - (matrix * pixel)[0]);
  1066. differenceY.Add(trans[1] - (matrix * pixel)[1]);
  1067. }
  1068. else
  1069. {
  1070. differenceX.Add(rPoints[i].X - (matrix * pixel)[0]);
  1071. differenceY.Add(rPoints[i].Y - (matrix * pixel)[1]);
  1072. }
  1073. }
  1074. sumX += Math.Pow(differenceX[differenceX.Count - 1], 2);
  1075. sumY += Math.Pow(differenceY[differenceY.Count - 1], 2);
  1076. }
  1077. double rmsX, rmsY;
  1078. rmsX = Math.Sqrt(sumX / 9);
  1079. rmsY = Math.Sqrt(sumY / 9);
  1080. CalibrationResult calibrationResult = new CalibrationResult();
  1081. calibrationResult.ScaleX = mat2d.Get<double>(0, 0);
  1082. calibrationResult.ScaleY = mat2d.Get<double>(1, 1);
  1083. calibrationResult.TranslationX = mat2d.Get<double>(0, 2);
  1084. calibrationResult.TranslationY = mat2d.Get<double>(1, 2);
  1085. calibrationResult.Rotate = Math.Atan2(mat2d.Get<double>(1, 0), mat2d.Get<double>(0, 1)) * 180 / Math.PI;
  1086. calibrationResult.RMSX = rmsX;
  1087. calibrationResult.RMSY = rmsY;
  1088. calibrationResult.MaxErrorValueX = differenceX.Max();
  1089. calibrationResult.MaxErrorValueY = differenceY.Max();
  1090. calibrationInfo.CalibrationResult = calibrationResult;
  1091. return (true, matrix, calibrationInfo.CalibPoints.ToArray(), calibrationResult);
  1092. }
  1093. /// <summary>
  1094. /// 执行校准验证。
  1095. /// 使用当前标定参数在一组验证点上计算误差并返回测试结果,用于评估标定质量。
  1096. /// </summary>
  1097. /// <param name="calibrationInfo">用于验证的点集与验收标准配置。</param>
  1098. /// <param name="robot">机器人接口,用于在验证过程中获取或移动到参考位姿。</param>
  1099. /// <param name="cancellationToken">用于取消操作的标记。</param>
  1100. /// <returns>元组字段:IsSuccess 表示验证是否通过;Result 包含详细的验证统计与误差分析。</returns>
  1101. public async Task<(bool IsSuccess, CalibrationTestResult Result)> ExecuteCalibrationValidation(CalibrationInfo calibrationInfo, IRobot robot, CancellationToken cancellationToken)
  1102. {
  1103. if (calibrationInfo == null) return (false, null);
  1104. if (robot == null || !robot.IsConnected) return (false, null);
  1105. double IntervalX = calibrationInfo.Width * 0.90 / 2;
  1106. double IntervalY = calibrationInfo.Height * 0.90 / 2;
  1107. Vector<double> P1 = Vector<double>.Build.Dense(new double[] { -IntervalX, -IntervalY });
  1108. Vector<double> P2 = Vector<double>.Build.Dense(new double[] { IntervalX, -IntervalY });
  1109. Vector<double> P3 = Vector<double>.Build.Dense(new double[] { 0, 0 });
  1110. Vector<double> P4 = Vector<double>.Build.Dense(new double[] { -IntervalX, IntervalY });
  1111. Vector<double> P5 = Vector<double>.Build.Dense(new double[] { IntervalX, IntervalY });
  1112. var P0 = Vector<double>.Build.Dense(new double[] { calibrationInfo.CenterPoint.X, calibrationInfo.CenterPoint.Y });
  1113. /// <summary>
  1114. /// 机器人与相机之间的旋转矩阵
  1115. /// </summary>
  1116. Matrix<double> RobotCameraRotationMatrix = Matrix<double>.Build.DenseOfArray(calibrationInfo.RobotCameraRotationMatrix);
  1117. //1
  1118. P1 = RobotCameraRotationMatrix.Inverse() * P1 + P0;
  1119. //2
  1120. P2 = RobotCameraRotationMatrix.Inverse() * P2 + P0;
  1121. //3
  1122. P3 = RobotCameraRotationMatrix.Inverse() * P3 + P0;
  1123. //4
  1124. P4 = RobotCameraRotationMatrix.Inverse() * P4 + P0;
  1125. //5
  1126. P5 = RobotCameraRotationMatrix.Inverse() * P5 + P0;
  1127. List<RPoint> rPoints = new List<RPoint>();
  1128. rPoints.Add(new RPoint()
  1129. {
  1130. Number = 1,
  1131. X = (float)P1[0],
  1132. Y = (float)P1[1],
  1133. Z = calibrationInfo.CenterPoint.Z,
  1134. U = calibrationInfo.CenterPoint.U,
  1135. V = calibrationInfo.CenterPoint.V,
  1136. W = calibrationInfo.CenterPoint.W,
  1137. Hand = calibrationInfo.CenterPoint.Hand,
  1138. Local = calibrationInfo.CenterPoint.Local,
  1139. Tool = calibrationInfo.CenterPoint.Tool
  1140. });
  1141. rPoints.Add(new RPoint()
  1142. {
  1143. Number = 2,
  1144. X = (float)P2[0],
  1145. Y = (float)P2[1],
  1146. Z = calibrationInfo.CenterPoint.Z,
  1147. U = calibrationInfo.CenterPoint.U,
  1148. V = calibrationInfo.CenterPoint.V,
  1149. W = calibrationInfo.CenterPoint.W,
  1150. Hand = calibrationInfo.CenterPoint.Hand,
  1151. Local = calibrationInfo.CenterPoint.Local,
  1152. Tool = calibrationInfo.CenterPoint.Tool
  1153. });
  1154. rPoints.Add(new RPoint()
  1155. {
  1156. Number = 3,
  1157. X = (float)P3[0],
  1158. Y = (float)P3[1],
  1159. Z = calibrationInfo.CenterPoint.Z,
  1160. U = calibrationInfo.CenterPoint.U,
  1161. V = calibrationInfo.CenterPoint.V,
  1162. W = calibrationInfo.CenterPoint.W,
  1163. Hand = calibrationInfo.CenterPoint.Hand,
  1164. Local = calibrationInfo.CenterPoint.Local,
  1165. Tool = calibrationInfo.CenterPoint.Tool
  1166. });
  1167. rPoints.Add(new RPoint()
  1168. {
  1169. Number = 4,
  1170. X = (float)P4[0],
  1171. Y = (float)P4[1],
  1172. Z = calibrationInfo.CenterPoint.Z,
  1173. U = calibrationInfo.CenterPoint.U,
  1174. V = calibrationInfo.CenterPoint.V,
  1175. W = calibrationInfo.CenterPoint.W,
  1176. Hand = calibrationInfo.CenterPoint.Hand,
  1177. Local = calibrationInfo.CenterPoint.Local,
  1178. Tool = calibrationInfo.CenterPoint.Tool
  1179. });
  1180. rPoints.Add(new RPoint()
  1181. {
  1182. Number = 5,
  1183. X = (float)P5[0],
  1184. Y = (float)P5[1],
  1185. Z = calibrationInfo.CenterPoint.Z,
  1186. U = calibrationInfo.CenterPoint.U,
  1187. V = calibrationInfo.CenterPoint.V,
  1188. W = calibrationInfo.CenterPoint.W,
  1189. Hand = calibrationInfo.CenterPoint.Hand,
  1190. Local = calibrationInfo.CenterPoint.Local,
  1191. Tool = calibrationInfo.CenterPoint.Tool
  1192. });
  1193. List<Vector<double>> TestPos = new List<Vector<double>>();
  1194. for (int i = 0; i < rPoints.Count; i++)
  1195. {
  1196. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown)
  1197. {
  1198. //移动机器人至i
  1199. await robot.CalibMotionAsync(rPoints[i], 0);
  1200. if (cancellationToken.IsCancellationRequested)
  1201. {
  1202. return (false, null);
  1203. }
  1204. //关闭吸气
  1205. await robot.CalibOutIOAsync(false);
  1206. //移动机器人至待机位置
  1207. await robot.CalibMotionAsync(calibrationInfo.HomePoint, 0);
  1208. }
  1209. else
  1210. {
  1211. //移动机器人至i
  1212. await robot.CalibMotionAsync(rPoints[i], rPoints[i].Z);
  1213. }
  1214. if (cancellationToken.IsCancellationRequested)
  1215. {
  1216. return (false, null);
  1217. }
  1218. //相机拍照并处理图像
  1219. var photoResult = await CaptureAndProcessCallback();
  1220. if (!photoResult.IsSuccess)
  1221. {
  1222. return (false, null);
  1223. }
  1224. PointF pixelpos = new PointF((float)(photoResult.X), (float)(photoResult.Y));
  1225. Matrix<double> matrix = Matrix<double>.Build.DenseOfArray(calibrationInfo.AffineTransformationMaterial);
  1226. var rpos = matrix * Vector<double>.Build.Dense(new double[] { pixelpos.X, pixelpos.Y, 1 });
  1227. TestPos.Add(rpos);
  1228. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown)
  1229. {
  1230. //移动机器人至i
  1231. await robot.CalibMotionAsync(rPoints[i], 0);
  1232. //打开吸气
  1233. await robot.CalibOutIOAsync(true);
  1234. }
  1235. if (cancellationToken.IsCancellationRequested)
  1236. {
  1237. return (false, null);
  1238. }
  1239. }
  1240. if (calibrationInfo.CameraMount == Enums.CameraMount.FixedDown)
  1241. {
  1242. //移动机器人至待机位置
  1243. await robot.CalibMotionAsync(calibrationInfo.HomePoint, 0);
  1244. }
  1245. //RMS(均方根值) 标定偏差
  1246. double sumX = 0, sumY = 0;
  1247. List<double> differenceX = new List<double>();
  1248. List<double> differenceY = new List<double>();
  1249. for (int i = 0; i < 5; i++)
  1250. {
  1251. if (calibrationInfo.CameraMount == Enums.CameraMount.MobileJ4)
  1252. {
  1253. //像素直接转换成mm时的点位
  1254. double Robot_X, Robot_Y;
  1255. Robot_X = TestPos[i][0];
  1256. Robot_Y = TestPos[i][1];
  1257. //机器人当前TOOL 0下的坐标
  1258. double curpos_x = 0, curpos_y = 0, curpos_u = 0;
  1259. curpos_x = rPoints[i].X;
  1260. curpos_y = rPoints[i].Y;
  1261. curpos_u = rPoints[i].U;
  1262. //得到旋转矩阵
  1263. double angle = Math.PI * (curpos_u - calibrationInfo.MarkPoint.U) / 180;
  1264. // 创建T矩阵
  1265. Matrix<double> rotationMatrix = Matrix<double>.Build.DenseOfArray(new double[,]
  1266. {
  1267. { Math.Cos(angle), -Math.Sin(angle) },
  1268. { Math.Sin(angle), Math.Cos(angle) }
  1269. });
  1270. Vector<double> p3 = Vector<double>.Build.Dense(new double[] { Robot_X - calibrationInfo.CalibPoints[0].Robot.X, Robot_Y - calibrationInfo.CalibPoints[0].Robot.Y });
  1271. Vector<double> phere = Vector<double>.Build.Dense(new double[] { curpos_x, curpos_y });
  1272. var cc = rotationMatrix * p3 + phere;
  1273. differenceX.Add(cc[0] - calibrationInfo.MarkPoint.X);
  1274. differenceY.Add(cc[1] - calibrationInfo.MarkPoint.Y);
  1275. }
  1276. else if (calibrationInfo.CameraMount == Enums.CameraMount.MobileDown_XYPlatform)
  1277. {
  1278. //像素直接转换成mm时的点位
  1279. double Robot_X, Robot_Y;
  1280. Robot_X = TestPos[i][0];
  1281. Robot_Y = TestPos[i][1];
  1282. //机器人当前TOOL 0下的坐标
  1283. double curpos_x = 0, curpos_y = 0, curpos_u = 0;
  1284. curpos_x = rPoints[i].X;
  1285. curpos_y = rPoints[i].Y;
  1286. curpos_u = rPoints[i].U;
  1287. //得到旋转矩阵
  1288. double angle = Math.PI * (curpos_u - calibrationInfo.MarkPoint.U) / 180;
  1289. // 创建T矩阵
  1290. Matrix<double> rotationMatrix = Matrix<double>.Build.DenseOfArray(new double[,]
  1291. {
  1292. { Math.Cos(angle), -Math.Sin(angle) },
  1293. { Math.Sin(angle), Math.Cos(angle) }
  1294. });
  1295. Vector<double> p3 = Vector<double>.Build.Dense(new double[] { Robot_X - calibrationInfo.CalibPoints[0].Robot.X, Robot_Y - calibrationInfo.CalibPoints[0].Robot.Y });
  1296. Vector<double> phere = Vector<double>.Build.Dense(new double[] { curpos_x, curpos_y });
  1297. var cc = rotationMatrix * p3 + phere;
  1298. differenceX.Add(cc[0] - calibrationInfo.MarkPoint.X);
  1299. differenceY.Add(cc[1] - calibrationInfo.MarkPoint.Y);
  1300. }
  1301. else
  1302. {
  1303. //将tool 0下的坐标转换成Tool n下的坐标
  1304. if (calibrationInfo.Tool != null)
  1305. {
  1306. ToolCoord toolCoord = new ToolCoord();
  1307. toolCoord.SetTool(calibrationInfo.Tool.X, calibrationInfo.Tool.Y);
  1308. double u1 = rPoints[i].U;
  1309. if (robot.Brand == Enums.RobotBrand.Schneider)
  1310. {
  1311. u1 *= -1;
  1312. }
  1313. var trans = toolCoord.GetToolnCoord(Vector<double>.Build.Dense(new double[] { rPoints[i].X, rPoints[i].Y }), u1);
  1314. differenceX.Add(trans[0] - TestPos[i][0]);
  1315. differenceY.Add(trans[1] - TestPos[i][1]);
  1316. }
  1317. else
  1318. {
  1319. differenceX.Add(rPoints[i].X - TestPos[i][0]);
  1320. differenceY.Add(rPoints[i].Y - TestPos[i][1]);
  1321. }
  1322. }
  1323. sumX += Math.Pow(differenceX[differenceX.Count - 1], 2);
  1324. sumY += Math.Pow(differenceY[differenceY.Count - 1], 2);
  1325. }
  1326. double rmsX, rmsY;
  1327. rmsX = Math.Sqrt(sumX / 5);
  1328. rmsY = Math.Sqrt(sumY / 5);
  1329. CalibrationTestResult calibrationTestResult = new CalibrationTestResult();
  1330. calibrationTestResult.MaxErrorValueX = differenceX.Max();
  1331. calibrationTestResult.MinErrorValueX = differenceX.Min();
  1332. calibrationTestResult.MaxErrorValueY = differenceY.Max();
  1333. calibrationTestResult.MinErrorValueY = differenceY.Min();
  1334. calibrationTestResult.RMSEX = rmsX;
  1335. calibrationTestResult.RMSEY = rmsY;
  1336. return (true, calibrationTestResult);
  1337. }
  1338. public void Dispose()
  1339. {
  1340. GC.SuppressFinalize(this);
  1341. }
  1342. }
  1343. }