CalibrationManager.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.IO;
  3. namespace AutoFocusTool.Calib
  4. {
  5. /// <summary>
  6. /// 标定结果管理器:统一封装"优先读 JSON,降级到 EEPROM"的逻辑,
  7. /// 确保标定结果真正被使用,而不是只存不用。
  8. /// </summary>
  9. public class CalibrationManager
  10. {
  11. private CalibrationFile _cache;
  12. private readonly string _jsonPath;
  13. public CalibrationManager(string jsonPath = @"C:\claudeFile\TL\AutoFocusTool\calibration.json")
  14. {
  15. _jsonPath = jsonPath;
  16. }
  17. /// <summary>
  18. /// 获取指定舱室和well的标定参数。
  19. /// 优先从 JSON 读取;若 JSON 不存在、该well标定失败或质量差,则降级到 EEPROM。
  20. /// </summary>
  21. /// <returns>(水平脉冲, Z焦点, 曝光值); 曝光值=-1 表示未标定,需自行设置</returns>
  22. public (int hPos, int z, int exp) GetWellParams(int house, int well, Serial.HouseMotor motor)
  23. {
  24. // 尝试从 JSON 加载
  25. if (_cache == null && File.Exists(_jsonPath))
  26. {
  27. try { _cache = CalibrationFile.Load(_jsonPath); }
  28. catch { _cache = null; }
  29. }
  30. // 检查 JSON 中的标定结果
  31. var wc = _cache?.Find(house, well);
  32. if (wc != null && wc.CircleFound && wc.PeakRatio > 1.2)
  33. {
  34. // 标定成功且质量合格,使用 JSON 参数
  35. return (wc.HorizontalPulse, wc.FocusZ, wc.Exposure);
  36. }
  37. // 降级到 EEPROM
  38. int hPos = motor.ReadWellHorizontalPos(well);
  39. int z = motor.ReadWellFocusZero(well);
  40. if (hPos < 0) hPos = 0;
  41. if (z < 0) z = 0;
  42. return (hPos, z, -1); // 曝光未标定
  43. }
  44. /// <summary>
  45. /// 检查某个well是否有合格的标定结果。
  46. /// </summary>
  47. public bool HasValidCalibration(int house, int well)
  48. {
  49. if (_cache == null && File.Exists(_jsonPath))
  50. {
  51. try { _cache = CalibrationFile.Load(_jsonPath); }
  52. catch { return false; }
  53. }
  54. var wc = _cache?.Find(house, well);
  55. return wc != null && wc.CircleFound && wc.PeakRatio > 1.2;
  56. }
  57. /// <summary>
  58. /// 刷新缓存(当 calibration.json 被重写后调用)。
  59. /// </summary>
  60. public void RefreshCache()
  61. {
  62. _cache = null;
  63. }
  64. }
  65. }