CameraCalibrationService.cs 75 KB

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