d2f22fa7ed4a7153fda57be35d57f274a9d5e9a99e46ed7fa4437bdb75c78a6c.source 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. using Prism.Commands;
  2. using Prism.Mvvm;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using TeamAAS.Camera.Interfaces;
  12. using TeamAAS.Camera.Models;
  13. using TeamAAS.Robot.Enums;
  14. using TeamAAS.Robot.Interfaces;
  15. using TeamAAS.Robot.Models;
  16. using TeamAAS.Robot.Models.Robot;
  17. using TeamAAS.Vision;
  18. using TeamAAS.Vision.Calibration.Enums;
  19. using TeamAAS.Vision.Calibration.Interfaces;
  20. using TeamAAS.Vision.Calibration.Models;
  21. using TeamAAS.Vision.Calibration.Services;
  22. namespace TeamAAS.ViewModels
  23. {
  24. /// <summary>
  25. /// 标定向导 ViewModel:驱动分步标定流程,方便调试人员单步执行。
  26. /// 算法本体在 <see cref="CameraCalibrationService"/>(TeamAAS.Vision),
  27. /// 视觉取点经当前视觉引擎的 <see cref="ICalibrationVisionProvider"/>(可整体替换)。
  28. /// 步骤:0 基础设置 / 1 视觉与相机 / 2 示教点位 / 3 工具坐标TCP / 4 九点标定 / 5 验证 / 6 完成。
  29. /// </summary>
  30. public class CalibrationViewModel : BindableBase
  31. {
  32. private readonly IRobotManager _robotManager;
  33. private readonly ICameraManager _cameraManager;
  34. private readonly CameraCalibrationService _calibService;
  35. private readonly ICalibrationService _store;
  36. private CancellationTokenSource _cts;
  37. public const int StepCount = 7;
  38. public CalibrationViewModel(IRobotManager robotManager, ICameraManager cameraManager)
  39. {
  40. _robotManager = robotManager;
  41. _cameraManager = cameraManager;
  42. _calibService = new CameraCalibrationService();
  43. _store = new CalibrationStore();
  44. _store.LoadAll();
  45. Calib = NewCalibration();
  46. RefreshDevices();
  47. WireCallbacks();
  48. EngineCalibrationView = (VisualEngineManager.Instance.Current ?? VisualEngineManager.Instance.GetOrLoad(App.SystemConfig?.Vision?.EngineName))?.GetCalibrationView();
  49. }
  50. #region 绑定属性
  51. private CalibrationInfo _Calib;
  52. public CalibrationInfo Calib
  53. {
  54. get { return _Calib; }
  55. set { SetProperty(ref _Calib, value); }
  56. }
  57. private int _StepIndex;
  58. public int StepIndex
  59. {
  60. get { return _StepIndex; }
  61. set { SetProperty(ref _StepIndex, value); RaisePropertyChanged(nameof(CanBack)); RaisePropertyChanged(nameof(CanNext)); }
  62. }
  63. private bool _IsBusy;
  64. public bool IsBusy
  65. {
  66. get { return _IsBusy; }
  67. set { SetProperty(ref _IsBusy, value); }
  68. }
  69. private string _StatusText = "就绪";
  70. public string StatusText
  71. {
  72. get { return _StatusText; }
  73. set { SetProperty(ref _StatusText, value); }
  74. }
  75. private FrameworkElement _EngineCalibrationView;
  76. public FrameworkElement EngineCalibrationView
  77. {
  78. get { return _EngineCalibrationView; }
  79. set { SetProperty(ref _EngineCalibrationView, value); }
  80. }
  81. public ObservableCollection<RobotInfo> Robots { get; } = new ObservableCollection<RobotInfo>();
  82. public ObservableCollection<CameraInfo> Cameras { get; } = new ObservableCollection<CameraInfo>();
  83. private RobotInfo _SelectedRobot;
  84. public RobotInfo SelectedRobot
  85. {
  86. get { return _SelectedRobot; }
  87. set { SetProperty(ref _SelectedRobot, value); if (value != null) Calib.RobotId = value.Id; }
  88. }
  89. private CameraInfo _SelectedCamera;
  90. public CameraInfo SelectedCamera
  91. {
  92. get { return _SelectedCamera; }
  93. set
  94. {
  95. SetProperty(ref _SelectedCamera, value);
  96. if (value != null) { Calib.CameraID = value.Id; Calib.CameraName = value.CameraName; }
  97. else { Calib.CameraID = Guid.Empty; Calib.CameraName = ""; }
  98. }
  99. }
  100. public IEnumerable<CameraMount> CameraMounts => Enum.GetValues(typeof(CameraMount)).Cast<CameraMount>();
  101. public IEnumerable<PickPlaceModel> PickModels => Enum.GetValues(typeof(PickPlaceModel)).Cast<PickPlaceModel>();
  102. public IEnumerable<CalibTechP0Mode> P0Modes => Enum.GetValues(typeof(CalibTechP0Mode)).Cast<CalibTechP0Mode>();
  103. public bool CanBack => StepIndex > 0 && !IsBusy;
  104. public bool CanNext => StepIndex < StepCount - 1 && !IsBusy;
  105. private IRobot CurrentRobot => SelectedRobot != null ? _robotManager.GetRobot(SelectedRobot.Id) : null;
  106. private ICamera CurrentCamera => SelectedCamera != null ? _cameraManager.GetCamera(SelectedCamera.Id) : null;
  107. private ICalibrationVisionProvider Provider => VisualEngineManager.Instance.Current as ICalibrationVisionProvider;
  108. #endregion
  109. #region 命令
  110. private DelegateCommand _NextCommand;
  111. public DelegateCommand NextCommand => _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNext, () => CanNext).ObservesProperty(() => IsBusy).ObservesProperty(() => StepIndex));
  112. private DelegateCommand _BackCommand;
  113. public DelegateCommand BackCommand => _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBack, () => CanBack).ObservesProperty(() => IsBusy).ObservesProperty(() => StepIndex));
  114. private DelegateCommand _NewCommand;
  115. public DelegateCommand NewCommand => _NewCommand ?? (_NewCommand = new DelegateCommand(() => { Calib = NewCalibration(); StepIndex = 0; StatusText = "已新建标定"; }));
  116. private DelegateCommand _TestCaptureCommand;
  117. public DelegateCommand TestCaptureCommand => _TestCaptureCommand ?? (_TestCaptureCommand = new DelegateCommand(async () => await RunGuarded(TestCaptureAsync)));
  118. private DelegateCommand _TeachCenterCommand;
  119. public DelegateCommand TeachCenterCommand => _TeachCenterCommand ?? (_TeachCenterCommand = new DelegateCommand(async () => await TeachPointAsync(p => Calib.CenterPoint = p, "中心点")));
  120. private DelegateCommand _TeachHomeCommand;
  121. public DelegateCommand TeachHomeCommand => _TeachHomeCommand ?? (_TeachHomeCommand = new DelegateCommand(async () => await TeachPointAsync(p => Calib.HomePoint = p, "待机点")));
  122. private DelegateCommand _TeachMarkCommand;
  123. public DelegateCommand TeachMarkCommand => _TeachMarkCommand ?? (_TeachMarkCommand = new DelegateCommand(async () => await TeachPointAsync(p => Calib.MarkPoint = p, "Mark点")));
  124. private DelegateCommand<bool?> _gripperCommand;
  125. public DelegateCommand<bool?> GripperCommand => _gripperCommand ?? (_gripperCommand = new DelegateCommand<bool?>(async v => await GripperAsync(v == true)));
  126. private DelegateCommand _RunTcpCommand;
  127. public DelegateCommand RunTcpCommand => _RunTcpCommand ?? (_RunTcpCommand = new DelegateCommand(async () => await RunGuarded(RunTcpAsync)));
  128. private DelegateCommand _RunNinePointCommand;
  129. public DelegateCommand RunNinePointCommand => _RunNinePointCommand ?? (_RunNinePointCommand = new DelegateCommand(async () => await RunGuarded(RunNinePointAsync)));
  130. private DelegateCommand _RunVerifyCommand;
  131. public DelegateCommand RunVerifyCommand => _RunVerifyCommand ?? (_RunVerifyCommand = new DelegateCommand(async () => await RunGuarded(RunVerifyAsync)));
  132. private DelegateCommand _SaveCommand;
  133. public DelegateCommand SaveCommand => _SaveCommand ?? (_SaveCommand = new DelegateCommand(Save));
  134. private DelegateCommand _LoadVisionToolCommand;
  135. public DelegateCommand LoadVisionToolCommand => _LoadVisionToolCommand ?? (_LoadVisionToolCommand = new DelegateCommand(LoadVisionTool));
  136. #endregion
  137. #region 初始化
  138. private CalibrationInfo NewCalibration()
  139. {
  140. return new CalibrationInfo
  141. {
  142. Id = Guid.NewGuid(),
  143. Name = "Calib_" + DateTime.Now.ToString("yyyyMMdd_HHmmss"),
  144. DateTime = DateTime.Now,
  145. CenterPoint = new RPoint(),
  146. HomePoint = new RPoint(),
  147. MarkPoint = new RPoint(),
  148. Pick = new PickInfo(),
  149. Tool = new RobotTool(),
  150. Arm = new RobotArm(),
  151. CalibPoints = new ObservableCollection<RobotPixelPoint>(),
  152. Speed = 20,
  153. Accel = 20,
  154. Power = true,
  155. Width = 100,
  156. Height = 80,
  157. Angle = 180,
  158. WaitBlow = 200,
  159. WaitPhoto = 200,
  160. WaitSuction = 200,
  161. ExposureTime = 5000,
  162. Gain = 0,
  163. IsCreate = true,
  164. };
  165. }
  166. private void RefreshDevices()
  167. {
  168. Robots.Clear();
  169. foreach (var r in _robotManager.GetAllRobotInfos()) Robots.Add(r);
  170. Cameras.Clear();
  171. foreach (var c in _cameraManager.GetAllCameraInfos()) Cameras.Add(c);
  172. if (Calib.RobotId != Guid.Empty) SelectedRobot = Robots.FirstOrDefault(x => x.Id == Calib.RobotId);
  173. if (Calib.CameraID != Guid.Empty) SelectedCamera = Cameras.FirstOrDefault(x => x.Id == Calib.CameraID);
  174. }
  175. private void WireCallbacks()
  176. {
  177. _calibService.CaptureAndProcessCallback = CaptureAndProcessAsync;
  178. _calibService.PromptPlaceCalibrationBlockCallback = PromptPlaceBlockAsync;
  179. }
  180. /// <summary>拍照 → 视觉引擎取特征点(引擎可换)。</summary>
  181. private async Task<(bool IsSuccess, double X, double Y, double U, int ImageWidth, int ImageHeight)> CaptureAndProcessAsync()
  182. {
  183. var cam = CurrentCamera;
  184. var provider = Provider;
  185. if (cam == null || provider == null)
  186. {
  187. SetStatus("未选择相机或当前视觉引擎不支持自动取点");
  188. return (false, 0, 0, 0, 0, 0);
  189. }
  190. // 惰性加载视觉标定工具(若已配置 .vpp 引用但尚未加载)。
  191. if (provider.CurrentTool == null && !string.IsNullOrEmpty(Calib.VisionToolRef))
  192. {
  193. provider.LoadTool(Calib.VisionToolRef);
  194. }
  195. try
  196. {
  197. cam.SetExposureTime(Calib.ExposureTime);
  198. cam.SetGain(Calib.Gain);
  199. }
  200. catch { }
  201. var img = cam.Grab();
  202. if (img == null) return (false, 0, 0, 0, 0, 0);
  203. if (Calib.IsDistortionCorrection) img = await provider.Undistort(img);
  204. var r = await provider.FindCalibPoint(img);
  205. int w = img.Width, h = img.Height;
  206. return (r.found, r.px, r.py, r.angle, w, h);
  207. }
  208. private Task<bool> PromptPlaceBlockAsync()
  209. {
  210. bool ok = false;
  211. var app = Application.Current;
  212. if (app != null && app.Dispatcher != null && !app.Dispatcher.CheckAccess())
  213. {
  214. app.Dispatcher.Invoke(new Action(() =>
  215. ok = MessageBox.Show("请将标定块摆放到中心/吸嘴下,摆好后点击“确定”继续。", "标定提示",
  216. MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK));
  217. }
  218. else
  219. {
  220. ok = MessageBox.Show("请将标定块摆放到中心/吸嘴下,摆好后点击“确定”继续。", "标定提示",
  221. MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK;
  222. }
  223. return Task.FromResult(ok);
  224. }
  225. #endregion
  226. #region 步骤动作
  227. private async Task TestCaptureAsync()
  228. {
  229. var r = await CaptureAndProcessAsync();
  230. SetStatus(r.IsSuccess
  231. ? $"取点成功:像素=({r.X:F2}, {r.Y:F2}) 角度={r.U:F2} 图像={r.ImageWidth}x{r.ImageHeight}"
  232. : "取点失败:未找到特征点或相机/引擎不可用");
  233. }
  234. private async Task TeachPointAsync(Action<RPoint> assign, string label)
  235. {
  236. var robot = CurrentRobot;
  237. if (robot == null || !robot.IsConnected) { SetStatus("机器人未连接,无法示教" + label); return; }
  238. var pos = await robot.GetRobotPosAsync();
  239. assign(pos);
  240. SetStatus($"{label}已示教:({pos.X:F3}, {pos.Y:F3}, {pos.Z:F3}, U={pos.U:F3})");
  241. }
  242. private async Task GripperAsync(bool on)
  243. {
  244. var robot = CurrentRobot;
  245. if (robot == null || !robot.IsConnected) { SetStatus("机器人未连接"); return; }
  246. await robot.CalibOutIOAsync(on);
  247. SetStatus(on ? "已吸气/夹紧" : "已破真空/张开");
  248. }
  249. private async Task RunTcpAsync()
  250. {
  251. var robot = CurrentRobot;
  252. if (!EnsureReady(robot)) return;
  253. _cts = new CancellationTokenSource();
  254. var r = await _calibService.CalibrateUncalibratedCameraTool(Calib, robot, _cts.Token);
  255. SetStatus(r.IsSuccess
  256. ? $"工具坐标标定完成:Tool=({r.ToolX:F3}, {r.ToolY:F3}) 像素比例=({r.PixelScaleX:F5}, {r.PixelScaleY:F5}) mm/px"
  257. : "工具坐标标定失败");
  258. }
  259. private async Task RunNinePointAsync()
  260. {
  261. var robot = CurrentRobot;
  262. if (!EnsureReady(robot)) return;
  263. _cts = new CancellationTokenSource();
  264. var r = await _calibService.AutoNinePointCalibration(Calib, robot, _cts.Token);
  265. if (!r.IsSuccess) { SetStatus("九点标定失败"); return; }
  266. Calib.AffineTransformationMaterial = r.AffineTransformationMaterial.ToArray();
  267. Calib.CalibPoints = new ObservableCollection<RobotPixelPoint>(r.NinePoint);
  268. Calib.CalibrationResult = r.Result;
  269. SetStatus($"九点标定完成:RMS={r.Result.RMS:F4} mm 最大误差={r.Result.MaxErrorValue:F4} mm 旋转={r.Result.Rotate:F3}°");
  270. }
  271. private async Task RunVerifyAsync()
  272. {
  273. var robot = CurrentRobot;
  274. if (!EnsureReady(robot)) return;
  275. if (Calib.AffineTransformationMaterial == null) { SetStatus("请先完成九点标定"); return; }
  276. _cts = new CancellationTokenSource();
  277. var r = await _calibService.ExecuteCalibrationValidation(Calib, robot, _cts.Token);
  278. if (!r.IsSuccess) { SetStatus("验证失败"); return; }
  279. Calib.CalibrationTestResult = r.Result;
  280. SetStatus($"验证完成:RMSE=({r.Result.RMSEX:F4}, {r.Result.RMSEY:F4}) mm");
  281. }
  282. private void Save()
  283. {
  284. try
  285. {
  286. if (string.IsNullOrWhiteSpace(Calib.Name)) { SetStatus("名称为空,无法保存"); return; }
  287. // 保存视觉工具引用(引擎自定义,如 .vpp 路径)。
  288. var provider = Provider;
  289. if (provider != null)
  290. {
  291. string dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Calibration", Calib.Name);
  292. Calib.VisionToolRef = provider.SaveTool(dir);
  293. }
  294. _store.AddOrUpdateCalibration(Calib);
  295. Calib.IsCreate = false;
  296. SetStatus($"已保存标定:{Calib.Name}");
  297. }
  298. catch (Exception ex)
  299. {
  300. SetStatus("保存失败:" + ex.Message);
  301. }
  302. }
  303. private void LoadVisionTool()
  304. {
  305. var provider = Provider;
  306. if (provider == null) { SetStatus("当前视觉引擎不支持标定工具"); return; }
  307. var dlg = new Microsoft.Win32.OpenFileDialog { Filter = "VisionPro 工具块|*.vpp|所有文件|*.*" };
  308. if (dlg.ShowDialog() != true) return;
  309. Calib.VisionToolRef = dlg.FileName;
  310. provider.LoadTool(dlg.FileName);
  311. SetStatus("已加载视觉标定工具:" + Path.GetFileName(dlg.FileName));
  312. }
  313. private bool EnsureReady(IRobot robot)
  314. {
  315. if (robot == null || !robot.IsConnected) { SetStatus("机器人未连接"); return false; }
  316. if (Provider == null) { SetStatus("当前视觉引擎不支持自动标定取点"); return false; }
  317. if (Calib.Pick == null) Calib.Pick = new PickInfo();
  318. return true;
  319. }
  320. #endregion
  321. #region 导航
  322. private void ExecuteNext()
  323. {
  324. int next = StepIndex + 1;
  325. // XY 模组无需单独的工具坐标(TCP)步骤,跳过。
  326. if (next == 3 && Calib.CameraMount == CameraMount.MobileDown_XYPlatform) next = 4;
  327. if (next < StepCount) StepIndex = next;
  328. }
  329. private void ExecuteBack()
  330. {
  331. int prev = StepIndex - 1;
  332. if (prev == 3 && Calib.CameraMount == CameraMount.MobileDown_XYPlatform) prev = 2;
  333. if (prev >= 0) StepIndex = prev;
  334. }
  335. #endregion
  336. private void SetStatus(string msg)
  337. {
  338. var app = Application.Current;
  339. if (app != null && app.Dispatcher != null && !app.Dispatcher.CheckAccess())
  340. app.Dispatcher.Invoke(new Action(() => StatusText = msg));
  341. else
  342. StatusText = msg;
  343. }
  344. /// <summary>统一守护:防重入 + 异常兜底 + 状态提示。</summary>
  345. private async Task RunGuarded(Func<Task> action)
  346. {
  347. if (IsBusy) return;
  348. IsBusy = true;
  349. try
  350. {
  351. await action();
  352. }
  353. catch (Exception ex)
  354. {
  355. SetStatus("执行异常:" + ex.Message);
  356. }
  357. finally
  358. {
  359. try { if (_cts != null && !_cts.IsCancellationRequested) _cts.Cancel(); _cts?.Dispose(); }
  360. catch { }
  361. _cts = null;
  362. IsBusy = false;
  363. }
  364. }
  365. }
  366. }