CameraCalibrationService.cs 66 KB

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