CameraCalibrationService.cs 71 KB

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