CameraCalibrationService.cs 67 KB

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