Algorithm Domain Services
controller.domain.services.algorithm.kinematic_domain_service
KinematicDomainService
KinematicDomainService()
运动学领域服务。
提供机械臂正逆运动学解算功能,包括DH参数管理、坐标变换矩阵计算等。
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
dh_param |
DHParam
|
机械臂 DH 参数对象。 |
kinematic_dh |
ndarray
|
运动学 DH 参数矩阵。 |
kinematic_utils |
KinematicUtils
|
运动学工具类实例。 |
gripper2base |
ndarray
|
当前末端到基座的变换矩阵。 |
初始化运动学服务。
源代码位于: src/controller/controller/domain/services/algorithm/kinematic_domain_service.py
21 22 23 24 25 26 | |
get_kinematic_dh
get_kinematic_dh() -> np.ndarray
获取运动学 DH 参数矩阵。
返回:
| 类型 | 描述 |
|---|---|
ndarray
|
np.ndarray: DH 参数矩阵。 |
源代码位于: src/controller/controller/domain/services/algorithm/kinematic_domain_service.py
28 29 30 31 32 33 34 | |
get_gripper2base
get_gripper2base(theta_list: list[float] | ndarray = None) -> tuple[np.ndarray, np.ndarray]
计算末端执行器相对于基座的位姿(四元数 + 位置)。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
theta_list
|
list[float] | ndarray
|
关节角度列表(弧度)。如果不传则使用当前内部状态。 |
None
|
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
tuple |
tuple[ndarray, ndarray]
|
(quat, pos) - quat (np.ndarray): 姿态四元数 [x, y, z, w]。 - pos (np.ndarray): 位置坐标 [x, y, z]。 |
源代码位于: src/controller/controller/domain/services/algorithm/kinematic_domain_service.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
get_gripper2base_rm
get_gripper2base_rm(theta_list: list[float]) -> np.ndarray
获取末端执行器相对于基座的变换矩阵。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
theta_list
|
list[float]
|
关节角度列表(弧度)。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
ndarray
|
np.ndarray: 4x4 齐次变换矩阵。 |
源代码位于: src/controller/controller/domain/services/algorithm/kinematic_domain_service.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
inverse_kinematic
inverse_kinematic(rm: ndarray, pos: ndarray, initial_theta: list[float] | None = None) -> list[float]
机械臂逆运动学求解。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
rm
|
ndarray
|
目标姿态旋转矩阵 (3x3)。 |
必需 |
pos
|
ndarray
|
目标位置坐标 [x, y, z]。 |
必需 |
initial_theta
|
list[float] | None
|
初始关节角度猜测值,用于选择最优解。 |
None
|
返回:
| 类型 | 描述 |
|---|---|
list[float]
|
list[float]: 最优关节角度解(弧度)。 |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
如果未找到有效的逆运动学解。 |
源代码位于: src/controller/controller/domain/services/algorithm/kinematic_domain_service.py
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 111 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
verify_solution
verify_solution(theta_list: list[float], target_pos: ndarray) -> bool
验证逆运动学解的准确性。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
theta_list
|
list[float]
|
待验证的关节角度解。 |
必需 |
target_pos
|
ndarray
|
目标位置坐标。 |
必需 |
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
bool |
bool
|
如果正解位置与目标位置误差小于 1e-5 则返回 True。 |
源代码位于: src/controller/controller/domain/services/algorithm/kinematic_domain_service.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
controller.domain.services.algorithm.trajectory_domain_service
SCurve
SCurve(v_max=[pi / 4] * 6, acc_max=[pi / 8] * 6, t_j=0.5)
S曲线速度规划算法。
实现基于加加速度(Jerk)限制的S型速度曲线规划。
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
v_max |
ndarray
|
最大速度限制。 |
acc_max |
ndarray
|
最大加速度限制。 |
t_j |
float
|
加加减速段时间。 |
初始化 S 曲线规划器。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
v_max
|
list[float]
|
最大速度. Defaults to [pi/4]*6. |
[pi / 4] * 6
|
acc_max
|
list[float]
|
最大加速度. Defaults to [pi/8]*6. |
[pi / 8] * 6
|
t_j
|
float
|
Jerk 时间参数. Defaults to 0.5. |
0.5
|
源代码位于: src/controller/controller/domain/services/algorithm/trajectory_domain_service.py
16 17 18 19 20 21 22 23 24 25 26 | |
solve_cubic_numeric
staticmethod
solve_cubic_numeric(a, b, c, d, tol=1e-08)
求解三次方程实根。
源代码位于: src/controller/controller/domain/services/algorithm/trajectory_domain_service.py
28 29 30 31 32 33 34 35 | |
solve_quadratic
staticmethod
solve_quadratic(a, b, c, tol=1e-08)
求解二次方程实根。
源代码位于: src/controller/controller/domain/services/algorithm/trajectory_domain_service.py
37 38 39 40 41 42 43 44 45 | |
base_method
staticmethod
base_method(t: float, jerk: float, a_start: float, v_start: float, s_start: float) -> tuple[float, float, float]
基础运动学方程计算。
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
tuple |
tuple[float, float, float]
|
(a, v, s) 当前时刻的加速度、速度、位移。 |
源代码位于: src/controller/controller/domain/services/algorithm/trajectory_domain_service.py
47 48 49 50 51 52 53 54 55 56 57 | |
planning
planning(start_angles: list[float], target_angles: list[float], v_start: list[float] = [0] * 6, dt: float = 0.01) -> tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]
规划从起点到终点的S曲线轨迹。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
start_angles
|
list[float]
|
起点角度。 |
必需 |
target_angles
|
list[float]
|
终点角度。 |
必需 |
v_start
|
list[float]
|
起始速度. Defaults to [0]*6. |
[0] * 6
|
dt
|
float
|
时间步长. Defaults to 0.01. |
0.01
|
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
tuple |
tuple[ndarray, ndarray, ndarray, ndarray]
|
(times, accelerations, velocities, positions) 规划结果。 |
源代码位于: src/controller/controller/domain/services/algorithm/trajectory_domain_service.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | |
controller.domain.services.algorithm.curve_motion
CurveMotionDomainService
CurveMotionDomainService(kinematic_solver: KinematicDomainService, v_max=[pi / 4] * 6, a_max=[pi / 8] * 6, j_max=[pi / 16] * 6, dt=0.01)
曲线运动规划服务。
提供基于弧长参数化的空间曲线规划、姿态平滑插值以及基于 TOPPRA 的时间参数化功能。
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
v_max |
ndarray
|
各关节最大速度 (rad/s)。 |
a_max |
ndarray
|
各关节最大加速度 (rad/s²)。 |
j_max |
ndarray
|
各关节最大加加速度 (rad/s³)。 |
dt |
float
|
时间步长 (s)。 |
kinematic_solver |
KinematicDomainService
|
运动学求解器实例。 |
初始化曲线运动服务。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
kinematic_solver
|
KinematicDomainService
|
运动学求解器服务实例。 |
必需 |
v_max
|
list[float]
|
最大速度列表. Defaults to [pi/4]*6. |
[pi / 4] * 6
|
a_max
|
list[float]
|
最大加速度列表. Defaults to [pi/8]*6. |
[pi / 8] * 6
|
j_max
|
list[float]
|
最大加加速度列表. Defaults to [pi/16]*6. |
[pi / 16] * 6
|
dt
|
float
|
采样时间步长. Defaults to 0.01. |
0.01
|
源代码位于: src/controller/controller/domain/services/algorithm/curve_motion.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | |
clamp
clamp(x, low, high)
将值 x 限制在 [low, high] 区间内。
源代码位于: src/controller/controller/domain/services/algorithm/curve_motion.py
50 51 52 | |
curve_motion
curve_motion(pos_fun, u0: float, u1: float, start_position: list[float], end_position: list[float] | None, ds: float = 0.002, include_end: bool = True, orientation_mode: str = 'slerp', tool_axis: str = 'z', up_hint: ndarray = np.array([0, 0, 1.0])) -> tuple[list[float], list[list[float]], list[list[float]], list[list[float]]]
执行曲线运动规划。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
pos_fun
|
callable
|
位置函数,接受参数 u 返回 (3,) 坐标。 |
必需 |
u0
|
float
|
参数起始值。 |
必需 |
u1
|
float
|
参数终止值。 |
必需 |
start_position
|
list[float]
|
起点关节角度。 |
必需 |
end_position
|
list[float] | None
|
终点关节角度。 |
必需 |
ds
|
float
|
采样弧长步长. Defaults to 0.002. |
0.002
|
include_end
|
bool
|
是否包含终点. Defaults to True. |
True
|
orientation_mode
|
str
|
姿态插值模式 ('fixed'|'slerp'|'tangent'). Defaults to "slerp". |
'slerp'
|
tool_axis
|
str
|
工具轴方向 ('x'|'y'|'z'). Defaults to "z". |
'z'
|
up_hint
|
ndarray
|
向上向量提示,用于切向跟随模式. Defaults to [0, 0, 1.0]. |
array([0, 0, 1.0])
|
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
tuple |
tuple[list[float], list[list[float]], list[list[float]], list[list[float]]]
|
(t_list, positions, qd, qdd) - t_list: 时间戳列表 - positions: 关节角度轨迹列表 - qd: 关节速度轨迹列表 - qdd: 关节加速度轨迹列表 |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
如果 orientation_mode 无效或 slerp 模式下未提供 end_position。 |
源代码位于: src/controller/controller/domain/services/algorithm/curve_motion.py
54 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 111 112 113 114 115 116 | |
make_pos_fun_spline
make_pos_fun_spline(start_position: list[float], end_position: list[float], mid_points: list, bc_type: str = 'natural') -> tuple[callable, np.ndarray]
构造基于三次样条的位置函数。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
start_position
|
list[float]
|
起点关节角度。 |
必需 |
end_position
|
list[float]
|
终点关节角度。 |
必需 |
mid_points
|
list
|
中间点坐标列表 (N, 3)。 |
必需 |
bc_type
|
str
|
边界条件类型. Defaults to "natural". |
'natural'
|
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
tuple |
tuple[callable, ndarray]
|
(pos_fun, s) - pos_fun: 位置函数 callable - s: 累积弦长数组 |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
如果点集形状不正确。 |
源代码位于: src/controller/controller/domain/services/algorithm/curve_motion.py
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
sample_parametric_equal_arclen
sample_parametric_equal_arclen(pos_fun, u0: float, u1: float, ds: float = 0.002, include_end: bool = True, dense: int = 4000) -> tuple[np.ndarray, np.ndarray, int]
对参数曲线 r(u) 做等弧长采样。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
pos_fun
|
callable
|
参数曲线函数 r(u)。 |
必需 |
u0
|
float
|
参数起始值。 |
必需 |
u1
|
float
|
参数终止值。 |
必需 |
ds
|
float
|
目标弧长采样间隔. Defaults to 0.002. |
0.002
|
include_end
|
bool
|
结果是否必须包含终点. Defaults to True. |
True
|
dense
|
int
|
初始密采样的点数. Defaults to 4000. |
4000
|
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
tuple |
tuple[ndarray, ndarray, int]
|
(pos_list, u_eq, n_seg) - pos_list: 等弧长采样后的坐标列表 (M, 3) - u_eq: 对应的参数值列表 (M,) - n_seg: 分段数 |
源代码位于: src/controller/controller/domain/services/algorithm/curve_motion.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |
wrap_to_pi
wrap_to_pi(q: ndarray) -> np.ndarray
将角度包裹到 (-pi, pi],避免 2π 跳变影响计算。
源代码位于: src/controller/controller/domain/services/algorithm/curve_motion.py
303 304 305 | |
ensure_2d_array
ensure_2d_array(arr: ndarray) -> np.ndarray
将输入转换为二维数组 (N, dof) 并做基本校验。
Raises
ValueError 当路标数少于 2(缺少起点或终点)时抛出。
源代码位于: src/controller/controller/domain/services/algorithm/curve_motion.py
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | |
toppra_time_parameterize
toppra_time_parameterize(waypoints: ndarray, v_max: ndarray | float, a_max: ndarray | float, dt: float = 0.01, grid_n: int = 400) -> tuple[list[float], list[list[float]], list[list[float]], list[list[float]]]
使用 TOPPRA 对路径进行时间参数化。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
waypoints
|
ndarray
|
路径点数组 (N, dof)。 |
必需 |
v_max
|
ndarray | float
|
最大速度约束。 |
必需 |
a_max
|
ndarray | float
|
最大加速度约束。 |
必需 |
dt
|
float
|
输出轨迹的时间步长. Defaults to 0.01. |
0.01
|
grid_n
|
int
|
TOPPRA 离散化网格点数. Defaults to 400. |
400
|
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
tuple |
tuple[list[float], list[list[float]], list[list[float]], list[list[float]]]
|
(t, q, qd, qdd) - t: 时间戳列表 - q: 关节位置列表 - qd: 关节速度列表 - qdd: 关节加速度列表 |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
如果 waypoints 维度不正确或约束格式错误。 |
RuntimeError
|
如果 TOPPRA 求解失败。 |
源代码位于: src/controller/controller/domain/services/algorithm/curve_motion.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | |
controller.domain.services.algorithm.hand_eye_transform_domain_service
手眼标定变换服务 - Domain层
HandEyeTransformDomainService
HandEyeTransformDomainService(config: HandEyeCalibrationConfig, kinematic_service: KinematicDomainService)
手眼标定变换服务。
职责: 1. 像素坐标 → 相机坐标系 2. 相机坐标系 → 基坐标系 3. 计算目标位姿(包含偏移和姿态调整) 4. 提供逆运动学求解
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
config |
HandEyeCalibrationConfig
|
手眼标定配置。 |
kinematic_service |
KinematicDomainService
|
运动学服务实例。 |
初始化手眼标定变换服务。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
config
|
HandEyeCalibrationConfig
|
手眼标定配置(通过DI注入)。 |
必需 |
kinematic_service
|
KinematicDomainService
|
运动学服务。 |
必需 |
源代码位于: src/controller/controller/domain/services/algorithm/hand_eye_transform_domain_service.py
25 26 27 28 29 30 31 32 33 34 35 36 37 | |
calculate_target_joint_angles
calculate_target_joint_angles(central_center: List[float], depth: float, real_center: List[float], real_depth: float, angle: float, current_joint_angles: List[float]) -> Optional[List[float]]
计算运动到零件位置所需的目标关节角度。
完整流程: 1. 像素坐标 → 相机坐标系 2. 正运动学:当前关节角度 → 末端位姿 3. 相机坐标系 → 基坐标系 4. 计算零件方向 + 偏移量 5. 逆运动学:目标位姿 → 目标关节角度
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
central_center
|
List[float]
|
中心点像素坐标 [u, v]。 |
必需 |
depth
|
float
|
中心点深度 (mm)。 |
必需 |
real_center
|
List[float]
|
实际中心点像素坐标 [u, v]。 |
必需 |
real_depth
|
float
|
实际中心点深度 (mm)。 |
必需 |
angle
|
float
|
零件角度(弧度)。 |
必需 |
current_joint_angles
|
List[float]
|
当前关节角度列表(弧度)。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
Optional[List[float]]
|
Optional[List[float]]: 目标关节角度列表(弧度),如果无解则返回 None。 |
源代码位于: src/controller/controller/domain/services/algorithm/hand_eye_transform_domain_service.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
get_z_rotation_matrix
get_z_rotation_matrix(angle: float) -> np.ndarray
获取绕 Z 轴旋转的齐次变换矩阵。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
angle
|
float
|
旋转角度(弧度)。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
ndarray
|
np.ndarray: 4x4 齐次变换矩阵。 |
源代码位于: src/controller/controller/domain/services/algorithm/hand_eye_transform_domain_service.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
get_y_rotation_matrix
get_y_rotation_matrix(angle: float) -> np.ndarray
获取绕 Y 轴旋转的齐次变换矩阵。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
angle
|
float
|
旋转角度(弧度)。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
ndarray
|
np.ndarray: 4x4 齐次变换矩阵。 |
源代码位于: src/controller/controller/domain/services/algorithm/hand_eye_transform_domain_service.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
get_x_rotation_matrix
get_x_rotation_matrix(angle: float) -> np.ndarray
获取绕 X 轴旋转的齐次变换矩阵。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
angle
|
float
|
旋转角度(弧度)。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
ndarray
|
np.ndarray: 4x4 齐次变换矩阵。 |
源代码位于: src/controller/controller/domain/services/algorithm/hand_eye_transform_domain_service.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |