Domain Layer - Utilities
controller.domain.utils.message_decoder
MessageDecoder
MessageDecoder(config_path='controller/config/message_decoder_config.yaml')
消息解码器。
基于 YAML 配置文件动态解析十六进制消息。
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
config |
dict
|
加载的配置字典。 |
robot_utils |
RobotUtils
|
工具类实例。 |
parsers |
Dict[str, BaseParser]
|
已注册的解析器字典。 |
MessageIn |
type
|
动态创建的消息数据类。 |
初始化消息解码器。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
config_path
|
str
|
配置文件路径. Defaults to "controller/config/message_decoder_config.yaml". |
'controller/config/message_decoder_config.yaml'
|
源代码位于: src/controller/controller/domain/utils/message_decoder.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
decode_message
decode_message(hex_message: str) -> Any
解码十六进制消息。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
hex_message
|
str
|
原始十六进制字符串。 |
必需 |
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
Any |
Any
|
解码后的消息对象 (MessageIn)。 |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
如果消息格式错误或校验失败。 |
源代码位于: src/controller/controller/domain/utils/message_decoder.py
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 | |
decode_message_detailed
decode_message_detailed(hex_message: str) -> Dict[str, Any]
解码消息并返回详细信息(包括原始十六进制值)。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
hex_message
|
str
|
原始十六进制字符串。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: 包含详细信息的字典。 |
源代码位于: src/controller/controller/domain/utils/message_decoder.py
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
controller.domain.utils.message_encoder
MessageEncoder
MessageEncoder(config_path='controller/config/message_encoder_config.yaml')
消息编码器。
基于 YAML 配置文件动态生成十六进制消息。
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
config |
dict
|
加载的配置字典。 |
robot_utils |
RobotUtils
|
工具类实例。 |
formatters |
Dict[str, BaseFormatter]
|
已注册的格式化器字典。 |
Message |
type
|
动态创建的消息数据类。 |
初始化消息编码器。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
config_path
|
str
|
配置文件路径. Defaults to "controller/config/message_encoder_config.yaml". |
'controller/config/message_encoder_config.yaml'
|
源代码位于: src/controller/controller/domain/utils/message_encoder.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
create_message
create_message(**kwargs: Any) -> Any
创建 MessageOut 实例。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
**kwargs
|
Any
|
消息字段值。 |
{}
|
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
Any |
Any
|
消息对象 (MessageOut)。 |
源代码位于: src/controller/controller/domain/utils/message_encoder.py
134 135 136 137 138 139 140 141 142 143 | |
interpret_message
interpret_message(message: Any) -> str
根据配置动态将消息对象编码为字符串。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
message
|
Any
|
MessageOut 对象。 |
必需 |
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
str |
str
|
编码后的十六进制字符串。 |
源代码位于: src/controller/controller/domain/utils/message_encoder.py
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 | |
controller.domain.utils.image_drawing_utils
图像绘制工具类 - Domain层 用于在图像上绘制检测结果
ImageDrawingUtils
图像绘制工具类。
提供静态方法用于在 OpenCV 图像上绘制检测结果和辅助图形。
draw_detection_result
staticmethod
draw_detection_result(image: ndarray, detection: Dict) -> np.ndarray
在图像上绘制检测结果。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
image
|
ndarray
|
原始图像(BGR)。 |
必需 |
detection
|
Dict
|
检测结果字典,包含: - head_center (Tuple[float, float]): 头部中心点 (x, y)。 - central_center (Tuple[float, float]): 中央中心点 (x, y)。 - real_center (Tuple[float, float]): 实际中心点 (x, y)。 - angle (float): 角度(弧度)。 - depth (float): central_center 的深度(米)。 - real_depth (float): real_center 的深度(米)。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
ndarray
|
np.ndarray: 绘制后的图像(新图像,不修改原图)。 |
源代码位于: src/controller/controller/domain/utils/image_drawing_utils.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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 | |
draw_direction_arrow
staticmethod
draw_direction_arrow(image: ndarray, center: Tuple[int, int], angle: float)
绘制方向箭头。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
image
|
ndarray
|
图像(原地修改)。 |
必需 |
center
|
Tuple[int, int]
|
中心点坐标 (x, y)。 |
必需 |
angle
|
float
|
角度(弧度)- 直着向上为 0,顺时针为正。 |
必需 |
源代码位于: src/controller/controller/domain/utils/image_drawing_utils.py
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 | |
controller.domain.utils.kinematic_utils
KinematicUtils
运动学工具类。
提供基本的运动学变换和角度处理函数。
dh2rm
staticmethod
dh2rm(a: float, alpha: float, d: float, theta: float) -> np.ndarray
根据 DH 参数计算变换矩阵。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
a
|
float
|
连杆长度。 |
必需 |
alpha
|
float
|
连杆扭转角。 |
必需 |
d
|
float
|
连杆偏移。 |
必需 |
theta
|
float
|
关节角。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
ndarray
|
np.ndarray: 4x4 齐次变换矩阵。 |
源代码位于: src/controller/controller/domain/utils/kinematic_utils.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
rm2quat
staticmethod
rm2quat(rm: ndarray) -> np.ndarray
旋转矩阵转四元数。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
rm
|
ndarray
|
3x3 旋转矩阵或 4x4 变换矩阵。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
ndarray
|
np.ndarray: 四元数 [x, y, z, w]。 |
源代码位于: src/controller/controller/domain/utils/kinematic_utils.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
quat2rm
staticmethod
quat2rm(quat: ndarray) -> np.ndarray
四元数转旋转矩阵。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
quat
|
ndarray
|
四元数 [x, y, z, w]。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
ndarray
|
np.ndarray: 3x3 旋转矩阵。 |
源代码位于: src/controller/controller/domain/utils/kinematic_utils.py
51 52 53 54 55 56 57 58 59 60 61 | |
quat2euler
staticmethod
quat2euler(quat: ndarray) -> np.ndarray
四元数转欧拉角 (XYZ 顺序)。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
quat
|
ndarray
|
四元数 [x, y, z, w]。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
ndarray
|
np.ndarray: 欧拉角 [roll, pitch, yaw] (弧度)。 |
源代码位于: src/controller/controller/domain/utils/kinematic_utils.py
63 64 65 66 67 68 69 70 71 72 73 | |
euler2quat
staticmethod
euler2quat(euler: ndarray) -> np.ndarray
欧拉角转四元数 (XYZ 顺序)。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
euler
|
ndarray
|
欧拉角 [roll, pitch, yaw] (弧度)。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
ndarray
|
np.ndarray: 四元数 [x, y, z, w]。 |
源代码位于: src/controller/controller/domain/utils/kinematic_utils.py
75 76 77 78 79 80 81 82 83 84 85 | |
euler2rm
staticmethod
euler2rm(euler: ndarray) -> np.ndarray
欧拉角转旋转矩阵 (XYZ 顺序)。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
euler
|
ndarray
|
欧拉角 [roll, pitch, yaw] (弧度)。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
ndarray
|
np.ndarray: 3x3 旋转矩阵。 |
源代码位于: src/controller/controller/domain/utils/kinematic_utils.py
87 88 89 90 91 92 93 94 95 96 97 | |
rm2euler
staticmethod
rm2euler(rm: ndarray) -> np.ndarray
旋转矩阵转欧拉角 (XYZ 顺序)。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
rm
|
ndarray
|
3x3 旋转矩阵。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
ndarray
|
np.ndarray: 欧拉角 [roll, pitch, yaw] (弧度)。 |
源代码位于: src/controller/controller/domain/utils/kinematic_utils.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
normalize_angle
staticmethod
normalize_angle(angle: float) -> float
将角度归一化到 [-pi, pi] 范围。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
angle
|
float
|
输入角度(弧度)。 |
必需 |
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
float |
float
|
归一化后的角度(弧度)。 |
源代码位于: src/controller/controller/domain/utils/kinematic_utils.py
114 115 116 117 118 119 120 121 122 123 124 | |
controller.domain.utils.robot_utils
RobotUtils
RobotUtils()
机器人工具类。
提供机器人相关的数值转换、校验和计算等工具方法。
初始化机器人参数。
源代码位于: src/controller/controller/domain/utils/robot_utils.py
10 11 12 13 14 15 | |
position2radian
position2radian(position: list) -> list
编码器位置值转换为弧度。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
position
|
list
|
编码器位置值列表。 |
必需 |
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
list |
list
|
关节角度列表(弧度)。 |
源代码位于: src/controller/controller/domain/utils/robot_utils.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
radian2position
radian2position(radian: list) -> list
弧度转换为编码器位置值。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
radian
|
list
|
关节角度列表(弧度)。 |
必需 |
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
list |
list
|
编码器位置值列表(32位整数)。 |
源代码位于: src/controller/controller/domain/utils/robot_utils.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
speed2position
speed2position(speed: list) -> list
速度值转换为位置增量值。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
speed
|
list
|
速度列表。 |
必需 |
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
list |
list
|
对应的时间步长内的位置增量值。 |
源代码位于: src/controller/controller/domain/utils/robot_utils.py
48 49 50 51 52 53 54 55 56 57 | |
torque_transfer
staticmethod
torque_transfer(torque: list) -> list
力矩值转换与归一化。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
torque
|
list
|
力矩值列表,长度不超过6。 |
必需 |
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
list |
list
|
转换后的力矩值列表(16位整数)。 |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
如果力矩列表长度超过6。 |
源代码位于: src/controller/controller/domain/utils/robot_utils.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
effector2hex
staticmethod
effector2hex(effector_data: float) -> list
末端执行器数据转换为十六进制字节列表。
将浮点数拆分为整数部分和小数部分,分别转换为 2 字节的大端序十六进制。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
effector_data
|
float
|
末端执行器数据。 |
必需 |
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
list |
list
|
包含两个十六进制字符串的列表 [整数部分hex, 小数部分hex]。 |
源代码位于: src/controller/controller/domain/utils/robot_utils.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
calculate_crc16
staticmethod
calculate_crc16(data) -> int
计算 CRC16 校验和 (CCITT)。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
data
|
Union[bytes, str]
|
字节数组或十六进制字符串。 |
必需 |
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
int |
int
|
16位校验和。如果输入格式错误返回 False。 |
源代码位于: src/controller/controller/domain/utils/robot_utils.py
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 | |