CalibrationViewModel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. RefreshCalibrationList();
  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. /// <summary>已保存的标定列表(来自 CalibrationStore),供界面选择加载。</summary>
  84. public ObservableCollection<CalibrationInfo> Calibrations { get; } = new ObservableCollection<CalibrationInfo>();
  85. private CalibrationInfo _SelectedCalibration;
  86. /// <summary>选中的已有标定:切换即把参数带回向导(Calib)。</summary>
  87. public CalibrationInfo SelectedCalibration
  88. {
  89. get { return _SelectedCalibration; }
  90. set
  91. {
  92. if (!SetProperty(ref _SelectedCalibration, value) || value == null) return;
  93. Calib = value;
  94. StepIndex = 0;
  95. RefreshDevices();
  96. // 切换标定时重新加载其视觉工具引用,避免沿用上一条的工具。
  97. var provider = Provider;
  98. if (provider != null && !string.IsNullOrEmpty(value.VisionToolRef))
  99. {
  100. try { provider.LoadTool(value.VisionToolRef); } catch { }
  101. }
  102. SetStatus($"已加载标定:{value.Name}");
  103. }
  104. }
  105. private RobotInfo _SelectedRobot;
  106. public RobotInfo SelectedRobot
  107. {
  108. get { return _SelectedRobot; }
  109. set { SetProperty(ref _SelectedRobot, value); if (value != null) Calib.RobotId = value.Id; }
  110. }
  111. private CameraInfo _SelectedCamera;
  112. public CameraInfo SelectedCamera
  113. {
  114. get { return _SelectedCamera; }
  115. set
  116. {
  117. SetProperty(ref _SelectedCamera, value);
  118. if (value != null) { Calib.CameraID = value.Id; Calib.CameraName = value.CameraName; }
  119. else { Calib.CameraID = Guid.Empty; Calib.CameraName = ""; }
  120. }
  121. }
  122. public IEnumerable<CameraMount> CameraMounts => Enum.GetValues(typeof(CameraMount)).Cast<CameraMount>();
  123. public IEnumerable<PickPlaceModel> PickModels => Enum.GetValues(typeof(PickPlaceModel)).Cast<PickPlaceModel>();
  124. public IEnumerable<CalibTechP0Mode> P0Modes => Enum.GetValues(typeof(CalibTechP0Mode)).Cast<CalibTechP0Mode>();
  125. public bool CanBack => StepIndex > 0 && !IsBusy;
  126. public bool CanNext => StepIndex < StepCount - 1 && !IsBusy;
  127. private IRobot CurrentRobot => SelectedRobot != null ? _robotManager.GetRobot(SelectedRobot.Id) : null;
  128. private ICamera CurrentCamera => SelectedCamera != null ? _cameraManager.GetCamera(SelectedCamera.Id) : null;
  129. private ICalibrationVisionProvider Provider => VisualEngineManager.Instance.Current as ICalibrationVisionProvider;
  130. #endregion
  131. #region 命令
  132. private DelegateCommand _NextCommand;
  133. public DelegateCommand NextCommand => _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNext, () => CanNext).ObservesProperty(() => IsBusy).ObservesProperty(() => StepIndex));
  134. private DelegateCommand _BackCommand;
  135. public DelegateCommand BackCommand => _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBack, () => CanBack).ObservesProperty(() => IsBusy).ObservesProperty(() => StepIndex));
  136. private DelegateCommand _NewCommand;
  137. public DelegateCommand NewCommand => _NewCommand ?? (_NewCommand = new DelegateCommand(() => { _SelectedCalibration = null; RaisePropertyChanged(nameof(SelectedCalibration)); Calib = NewCalibration(); StepIndex = 0; RefreshDevices(); StatusText = "已新建标定"; }));
  138. private DelegateCommand _TestCaptureCommand;
  139. public DelegateCommand TestCaptureCommand => _TestCaptureCommand ?? (_TestCaptureCommand = new DelegateCommand(async () => await RunGuarded(TestCaptureAsync)));
  140. private DelegateCommand _TeachCenterCommand;
  141. public DelegateCommand TeachCenterCommand => _TeachCenterCommand ?? (_TeachCenterCommand = new DelegateCommand(async () => await TeachPointAsync(p => Calib.CenterPoint = p, "中心点")));
  142. private DelegateCommand _TeachHomeCommand;
  143. public DelegateCommand TeachHomeCommand => _TeachHomeCommand ?? (_TeachHomeCommand = new DelegateCommand(async () => await TeachPointAsync(p => Calib.HomePoint = p, "待机点")));
  144. private DelegateCommand _TeachMarkCommand;
  145. public DelegateCommand TeachMarkCommand => _TeachMarkCommand ?? (_TeachMarkCommand = new DelegateCommand(async () => await TeachPointAsync(p => Calib.MarkPoint = p, "Mark点")));
  146. private DelegateCommand<bool?> _gripperCommand;
  147. public DelegateCommand<bool?> GripperCommand => _gripperCommand ?? (_gripperCommand = new DelegateCommand<bool?>(async v => await GripperAsync(v == true)));
  148. private DelegateCommand _RunTcpCommand;
  149. public DelegateCommand RunTcpCommand => _RunTcpCommand ?? (_RunTcpCommand = new DelegateCommand(async () => await RunGuarded(RunTcpAsync)));
  150. private DelegateCommand _RunNinePointCommand;
  151. public DelegateCommand RunNinePointCommand => _RunNinePointCommand ?? (_RunNinePointCommand = new DelegateCommand(async () => await RunGuarded(RunNinePointAsync)));
  152. private DelegateCommand _RunVerifyCommand;
  153. public DelegateCommand RunVerifyCommand => _RunVerifyCommand ?? (_RunVerifyCommand = new DelegateCommand(async () => await RunGuarded(RunVerifyAsync)));
  154. private DelegateCommand _SaveCommand;
  155. public DelegateCommand SaveCommand => _SaveCommand ?? (_SaveCommand = new DelegateCommand(Save));
  156. private DelegateCommand _LoadVisionToolCommand;
  157. public DelegateCommand LoadVisionToolCommand => _LoadVisionToolCommand ?? (_LoadVisionToolCommand = new DelegateCommand(LoadVisionTool));
  158. #endregion
  159. #region 初始化
  160. private CalibrationInfo NewCalibration()
  161. {
  162. return new CalibrationInfo
  163. {
  164. Id = Guid.NewGuid(),
  165. Name = "Calib_" + DateTime.Now.ToString("yyyyMMdd_HHmmss"),
  166. DateTime = DateTime.Now,
  167. CenterPoint = new RPoint(),
  168. HomePoint = new RPoint(),
  169. MarkPoint = new RPoint(),
  170. Pick = new PickInfo(),
  171. Tool = new RobotTool(),
  172. Arm = new RobotArm(),
  173. CalibPoints = new ObservableCollection<RobotPixelPoint>(),
  174. Speed = 20,
  175. Accel = 20,
  176. Power = true,
  177. Width = 100,
  178. Height = 80,
  179. Angle = 180,
  180. WaitBlow = 200,
  181. WaitPhoto = 200,
  182. WaitSuction = 200,
  183. ExposureTime = 5000,
  184. Gain = 0,
  185. IsCreate = true,
  186. };
  187. }
  188. /// <summary>从 Store 载入已保存标定到下拉列表;有则默认选中最近一条把参数带回界面,无则新建空白标定。</summary>
  189. private void RefreshCalibrationList()
  190. {
  191. Calibrations.Clear();
  192. foreach (var c in _store.GetAllCalibrations()) Calibrations.Add(c);
  193. if (Calibrations.Count > 0)
  194. {
  195. SelectedCalibration = Calibrations.OrderByDescending(c => c.DateTime).First();
  196. }
  197. else
  198. {
  199. Calib = NewCalibration();
  200. }
  201. }
  202. private void RefreshDevices()
  203. {
  204. Robots.Clear();
  205. foreach (var r in _robotManager.GetAllRobotInfos()) Robots.Add(r);
  206. Cameras.Clear();
  207. foreach (var c in _cameraManager.GetAllCameraInfos()) Cameras.Add(c);
  208. if (Calib.RobotId != Guid.Empty) SelectedRobot = Robots.FirstOrDefault(x => x.Id == Calib.RobotId);
  209. if (Calib.CameraID != Guid.Empty) SelectedCamera = Cameras.FirstOrDefault(x => x.Id == Calib.CameraID);
  210. }
  211. private void WireCallbacks()
  212. {
  213. _calibService.CaptureAndProcessCallback = CaptureAndProcessAsync;
  214. _calibService.PromptPlaceCalibrationBlockCallback = PromptPlaceBlockAsync;
  215. }
  216. /// <summary>拍照 → 视觉引擎取特征点(引擎可换)。</summary>
  217. private async Task<(bool IsSuccess, double X, double Y, double U, int ImageWidth, int ImageHeight)> CaptureAndProcessAsync()
  218. {
  219. var cam = CurrentCamera;
  220. var provider = Provider;
  221. if (cam == null || provider == null)
  222. {
  223. SetStatus("未选择相机或当前视觉引擎不支持自动取点");
  224. return (false, 0, 0, 0, 0, 0);
  225. }
  226. // 惰性加载视觉标定工具(若已配置 .vpp 引用但尚未加载)。
  227. if (provider.CurrentTool == null && !string.IsNullOrEmpty(Calib.VisionToolRef))
  228. {
  229. provider.LoadTool(Calib.VisionToolRef);
  230. }
  231. try
  232. {
  233. cam.SetExposureTime(Calib.ExposureTime);
  234. cam.SetGain(Calib.Gain);
  235. }
  236. catch { }
  237. var img = cam.Grab();
  238. if (img == null) return (false, 0, 0, 0, 0, 0);
  239. if (Calib.IsDistortionCorrection) img = await provider.Undistort(img);
  240. var r = await provider.FindCalibPoint(img);
  241. int w = img.Width, h = img.Height;
  242. return (r.found, r.px, r.py, r.angle, w, h);
  243. }
  244. private Task<bool> PromptPlaceBlockAsync()
  245. {
  246. bool ok = false;
  247. var app = Application.Current;
  248. if (app != null && app.Dispatcher != null && !app.Dispatcher.CheckAccess())
  249. {
  250. app.Dispatcher.Invoke(new Action(() =>
  251. ok = MessageBox.Show("请将标定块摆放到中心/吸嘴下,摆好后点击“确定”继续。", "标定提示",
  252. MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK));
  253. }
  254. else
  255. {
  256. ok = MessageBox.Show("请将标定块摆放到中心/吸嘴下,摆好后点击“确定”继续。", "标定提示",
  257. MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK;
  258. }
  259. return Task.FromResult(ok);
  260. }
  261. #endregion
  262. #region 步骤动作
  263. private async Task TestCaptureAsync()
  264. {
  265. var r = await CaptureAndProcessAsync();
  266. SetStatus(r.IsSuccess
  267. ? $"取点成功:像素=({r.X:F2}, {r.Y:F2}) 角度={r.U:F2} 图像={r.ImageWidth}x{r.ImageHeight}"
  268. : "取点失败:未找到特征点或相机/引擎不可用");
  269. }
  270. private async Task TeachPointAsync(Action<RPoint> assign, string label)
  271. {
  272. var robot = CurrentRobot;
  273. if (robot == null || !robot.IsConnected) { SetStatus("机器人未连接,无法示教" + label); return; }
  274. var pos = await robot.GetRobotPosAsync();
  275. assign(pos);
  276. SetStatus($"{label}已示教:({pos.X:F3}, {pos.Y:F3}, {pos.Z:F3}, U={pos.U:F3})");
  277. }
  278. private async Task GripperAsync(bool on)
  279. {
  280. var robot = CurrentRobot;
  281. if (robot == null || !robot.IsConnected) { SetStatus("机器人未连接"); return; }
  282. await robot.CalibOutIOAsync(on);
  283. SetStatus(on ? "已吸气/夹紧" : "已破真空/张开");
  284. }
  285. private async Task RunTcpAsync()
  286. {
  287. var robot = CurrentRobot;
  288. if (!EnsureReady(robot)) return;
  289. _cts = new CancellationTokenSource();
  290. var r = await _calibService.CalibrateUncalibratedCameraTool(Calib, robot, _cts.Token);
  291. SetStatus(r.IsSuccess
  292. ? $"工具坐标标定完成:Tool=({r.ToolX:F3}, {r.ToolY:F3}) 像素比例=({r.PixelScaleX:F5}, {r.PixelScaleY:F5}) mm/px"
  293. : "工具坐标标定失败");
  294. }
  295. private async Task RunNinePointAsync()
  296. {
  297. var robot = CurrentRobot;
  298. if (!EnsureReady(robot)) return;
  299. _cts = new CancellationTokenSource();
  300. var r = await _calibService.AutoNinePointCalibration(Calib, robot, _cts.Token);
  301. if (!r.IsSuccess) { SetStatus("九点标定失败"); return; }
  302. Calib.AffineTransformationMaterial = r.AffineTransformationMaterial.ToArray();
  303. Calib.CalibPoints = new ObservableCollection<RobotPixelPoint>(r.NinePoint);
  304. Calib.CalibrationResult = r.Result;
  305. SetStatus($"九点标定完成:RMS={r.Result.RMS:F4} mm 最大误差={r.Result.MaxErrorValue:F4} mm 旋转={r.Result.Rotate:F3}°");
  306. }
  307. private async Task RunVerifyAsync()
  308. {
  309. var robot = CurrentRobot;
  310. if (!EnsureReady(robot)) return;
  311. if (Calib.AffineTransformationMaterial == null) { SetStatus("请先完成九点标定"); return; }
  312. _cts = new CancellationTokenSource();
  313. var r = await _calibService.ExecuteCalibrationValidation(Calib, robot, _cts.Token);
  314. if (!r.IsSuccess) { SetStatus("验证失败"); return; }
  315. Calib.CalibrationTestResult = r.Result;
  316. SetStatus($"验证完成:RMSE=({r.Result.RMSEX:F4}, {r.Result.RMSEY:F4}) mm");
  317. }
  318. private void Save()
  319. {
  320. try
  321. {
  322. if (string.IsNullOrWhiteSpace(Calib.Name)) { SetStatus("名称为空,无法保存"); return; }
  323. // 保存视觉工具引用(引擎自定义,如 .vpp 路径)。
  324. var provider = Provider;
  325. if (provider != null)
  326. {
  327. string dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Calibration", Calib.Name);
  328. Calib.VisionToolRef = provider.SaveTool(dir);
  329. }
  330. _store.AddOrUpdateCalibration(Calib);
  331. Calib.IsCreate = false;
  332. // 让已保存标定进入下拉列表并保持选中(不触发重新加载)。
  333. if (!Calibrations.Any(x => ReferenceEquals(x, Calib))) Calibrations.Add(Calib);
  334. _SelectedCalibration = Calib;
  335. RaisePropertyChanged(nameof(SelectedCalibration));
  336. SetStatus($"已保存标定:{Calib.Name}");
  337. }
  338. catch (Exception ex)
  339. {
  340. SetStatus("保存失败:" + ex.Message);
  341. }
  342. }
  343. private void LoadVisionTool()
  344. {
  345. var provider = Provider;
  346. if (provider == null) { SetStatus("当前视觉引擎不支持标定工具"); return; }
  347. var dlg = new Microsoft.Win32.OpenFileDialog { Filter = "VisionPro 工具块|*.vpp|所有文件|*.*" };
  348. if (dlg.ShowDialog() != true) return;
  349. Calib.VisionToolRef = dlg.FileName;
  350. provider.LoadTool(dlg.FileName);
  351. SetStatus("已加载视觉标定工具:" + Path.GetFileName(dlg.FileName));
  352. }
  353. private bool EnsureReady(IRobot robot)
  354. {
  355. if (robot == null || !robot.IsConnected) { SetStatus("机器人未连接"); return false; }
  356. if (Provider == null) { SetStatus("当前视觉引擎不支持自动标定取点"); return false; }
  357. if (Calib.Pick == null) Calib.Pick = new PickInfo();
  358. return true;
  359. }
  360. #endregion
  361. #region 导航
  362. private void ExecuteNext()
  363. {
  364. int next = StepIndex + 1;
  365. // XY 模组无需单独的工具坐标(TCP)步骤,跳过。
  366. if (next == 3 && Calib.CameraMount == CameraMount.MobileDown_XYPlatform) next = 4;
  367. if (next < StepCount) StepIndex = next;
  368. }
  369. private void ExecuteBack()
  370. {
  371. int prev = StepIndex - 1;
  372. if (prev == 3 && Calib.CameraMount == CameraMount.MobileDown_XYPlatform) prev = 2;
  373. if (prev >= 0) StepIndex = prev;
  374. }
  375. #endregion
  376. private void SetStatus(string msg)
  377. {
  378. var app = Application.Current;
  379. if (app != null && app.Dispatcher != null && !app.Dispatcher.CheckAccess())
  380. app.Dispatcher.Invoke(new Action(() => StatusText = msg));
  381. else
  382. StatusText = msg;
  383. }
  384. /// <summary>统一守护:防重入 + 异常兜底 + 状态提示。</summary>
  385. private async Task RunGuarded(Func<Task> action)
  386. {
  387. if (IsBusy) return;
  388. IsBusy = true;
  389. try
  390. {
  391. await action();
  392. }
  393. catch (Exception ex)
  394. {
  395. SetStatus("执行异常:" + ex.Message);
  396. }
  397. finally
  398. {
  399. try { if (_cts != null && !_cts.IsCancellationRequested) _cts.Cancel(); _cts?.Dispose(); }
  400. catch { }
  401. _cts = null;
  402. IsBusy = false;
  403. }
  404. }
  405. }
  406. }