CameraCalibrationService.cs 90 KB

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