跳转至

Hand-Eye Calibration Repository

controller.infrastructure.persistence.hand_eye_calibration_repository

手眼标定配置仓储 - Infrastructure层 负责从文件系统读取配置

HandEyeCalibrationRepository

HandEyeCalibrationRepository(config_path: str = None)

手眼标定配置仓储 - Infrastructure层。

负责从文件系统读取配置。

职责: - 从 JSON 文件加载配置 - 转换为 Domain 层的值对象 - 处理文件不存在等异常

属性:

名称 类型 描述
config_path Path

配置文件路径。

初始化配置仓储。

参数:

名称 类型 描述 默认
config_path str

配置文件路径. Defaults to None. 如果为 None,则按以下优先级查找: 1. ROS2 share 目录 (controller/config/hand_eye_calibration.json) 2. 开发环境相对路径 (../../config/hand_eye_calibration.json)

None
源代码位于: src/controller/controller/infrastructure/persistence/hand_eye_calibration_repository.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def __init__(self, config_path: str = None):
    """初始化配置仓储。

    Args:
        config_path (str, optional): 配置文件路径. Defaults to None.
            如果为 None,则按以下优先级查找:
            1. ROS2 share 目录 (controller/config/hand_eye_calibration.json)
            2. 开发环境相对路径 (../../config/hand_eye_calibration.json)
    """
    if config_path is None:
        # 使用 ROS2 包资源管理方式查找配置文件
        try:
            # ROS2 方式:从 share 目录加载
            package_share_dir = get_package_share_directory('controller')
            config_path = Path(package_share_dir) / 'config' / 'hand_eye_calibration.json'
        except Exception:
            # 如果包未安装,使用相对路径(开发模式)
            current_file = Path(__file__)
            controller_root = current_file.parent.parent.parent
            config_path = controller_root / "config" / "hand_eye_calibration.json"

    self.config_path = Path(config_path)

load

load() -> HandEyeCalibrationConfig

加载手眼标定配置。

返回:

名称 类型 描述
HandEyeCalibrationConfig HandEyeCalibrationConfig

配置值对象。

引发:

类型 描述
FileNotFoundError

配置文件不存在。

ValueError

配置格式错误。

源代码位于: src/controller/controller/infrastructure/persistence/hand_eye_calibration_repository.py
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
def load(self) -> HandEyeCalibrationConfig:
    """加载手眼标定配置。

    Returns:
        HandEyeCalibrationConfig: 配置值对象。

    Raises:
        FileNotFoundError: 配置文件不存在。
        ValueError: 配置格式错误。
    """
    if not self.config_path.exists():
        raise FileNotFoundError(
            f"配置文件不存在: {self.config_path}\n"
            f"请在 controller/config/ 目录下创建 hand_eye_calibration.json"
        )

    try:
        with open(self.config_path, 'r', encoding='utf-8') as f:
            data = json.load(f)
    except json.JSONDecodeError as e:
        raise ValueError(f"配置文件格式错误: {e}")

    # 解析手眼标定矩阵
    hand_eye_matrix = np.array(data['hand_eye_matrix'], dtype=float)

    # 解析相机内参
    intrinsics_data = data['camera_intrinsics']
    camera_intrinsics = CameraIntrinsics(
        fx=intrinsics_data['fx'],
        fy=intrinsics_data['fy'],
        cx=intrinsics_data['cx'],
        cy=intrinsics_data['cy']
    )

    # 解析目标偏移量
    offset_data = data['target_offset']
    target_offset = TargetOffset(
        x=offset_data['x'],
        y=offset_data['y'],
        z=offset_data['z']
    )

    # 解析末端姿态调整(角度转弧度)
    adjustment_data = data['end_effector_adjustment']
    end_effector_adjustment = EndEffectorAdjustment(
        z_rotation=radians(adjustment_data['z_rotation_deg']),
        y_rotation=radians(adjustment_data['y_rotation_deg']),
        x_rotation=radians(adjustment_data['x_rotation_deg'])
    )

    return HandEyeCalibrationConfig(
        hand_eye_matrix=hand_eye_matrix,
        camera_intrinsics=camera_intrinsics,
        target_offset=target_offset,
        end_effector_adjustment=end_effector_adjustment
    )

save

save(config: HandEyeCalibrationConfig)

保存配置到文件(可选功能,用于标定工具)。

参数:

名称 类型 描述 默认
config HandEyeCalibrationConfig

手眼标定配置对象。

必需
源代码位于: src/controller/controller/infrastructure/persistence/hand_eye_calibration_repository.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
def save(self, config: HandEyeCalibrationConfig):
    """保存配置到文件(可选功能,用于标定工具)。

    Args:
        config (HandEyeCalibrationConfig): 手眼标定配置对象。
    """
    # 确保目录存在
    self.config_path.parent.mkdir(parents=True, exist_ok=True)

    data = {
        "hand_eye_matrix": config.hand_eye_matrix.tolist(),
        "camera_intrinsics": {
            "fx": config.camera_intrinsics.fx,
            "fy": config.camera_intrinsics.fy,
            "cx": config.camera_intrinsics.cx,
            "cy": config.camera_intrinsics.cy
        },
        "target_offset": {
            "x": config.target_offset.x,
            "y": config.target_offset.y,
            "z": config.target_offset.z,
            "comment": "相对于零件位置的偏移量(米)"
        },
        "end_effector_adjustment": {
            "z_rotation_deg": np.degrees(config.end_effector_adjustment.z_rotation),
            "y_rotation_deg": np.degrees(config.end_effector_adjustment.y_rotation),
            "x_rotation_deg": np.degrees(config.end_effector_adjustment.x_rotation),
            "comment": "末端姿态调整(角度)"
        }
    }

    with open(self.config_path, 'w', encoding='utf-8') as f:
        json.dump(data, f, indent=2, ensure_ascii=False)