""" 包括一个点位需要的状态: 1、点位名称 2、速度 3、工具id 4、工件id 5、关节坐标 J1-J6 6、运动类型 7、平滑时间 """ class PointState: VALID_SPEED_RANGE = (0, 100) VALID_TOOL_WORK_ID_RANGE = (0, 14) VALID_JOINT_COUNT = 6 VALID_JOINT_RANGE = (-180, 180) VALID_MOTION_TYPES = ["直线", "曲线中间点", "曲线终点", "自由路径"] VALID_BLEND_TIME_RANGE = (0, 500) def __init__(self, pos_name, speed, tool_id, work_id, joint_values, motion_type, blend_time): # 数据合法性判断 self.pos_name = self._validate_pos_name(pos_name) self.speed = self._validate_speed(speed) self.tool_id = self._validate_tool_work_id(tool_id, "工具ID") self.work_id = self._validate_tool_work_id(work_id, "工件ID") self.joint_values = self._validate_joint_values(joint_values) self.motion_type = self._validate_motion_type(motion_type) self.blend_time = self._validate_blend_time(blend_time) def _validate_pos_name(self, pos_name): """校验点位名称(非空)""" if not isinstance(pos_name, str): raise TypeError("点位名称必须是字符串类型") stripped_name = pos_name.strip() if not stripped_name: raise ValueError("点位名称不能为空或仅包含空格") return stripped_name def _validate_speed(self, speed): """校验速度(0-100范围)""" if not isinstance(speed, (int, float)): raise TypeError(f"速度必须是数字类型,当前类型:{type(speed).__name__}") min_val, max_val = self.VALID_SPEED_RANGE if not (min_val <= speed <= max_val): raise ValueError(f"速度必须在 {min_val}-{max_val} 之间,当前值:{speed}") return speed def _validate_tool_work_id(self, value, field_name): """校验工具ID/工件ID(0-14范围,整数)""" if not isinstance(value, int): raise TypeError(f"{field_name}必须是整数类型,当前类型:{type(value).__name__}") min_val, max_val = self.VALID_TOOL_WORK_ID_RANGE if not (min_val <= value <= max_val): raise ValueError(f"{field_name}必须在 {min_val}-{max_val} 之间,当前值:{value}") return value def _validate_joint_values(self, joint_values): """校验关节值(6个元素,每个在-180~180范围)""" if not isinstance(joint_values, list): raise TypeError(f"关节值必须是列表类型,当前类型:{type(joint_values).__name__}") if len(joint_values) != self.VALID_JOINT_COUNT: raise ValueError( f"关节值必须包含 {self.VALID_JOINT_COUNT} 个元素(J1-J6)," f"当前数量:{len(joint_values)}" ) # 逐个校验关节值 validated_joints = [] for i, val in enumerate(joint_values, 1): if not isinstance(val, (int, float)): raise TypeError(f"J{i}关节值必须是数字类型,当前类型:{type(val).__name__}") min_val, max_val = self.VALID_JOINT_RANGE if not (min_val <= val <= max_val): raise ValueError( f"J{i}关节值必须在 {min_val}~{max_val} 之间,当前值:{val}" ) validated_joints.append(val) return validated_joints def _validate_motion_type(self, motion_type): """校验运动类型(必须是预定义的选项)""" if not isinstance(motion_type, str): raise TypeError(f"运动类型必须是字符串类型,当前类型:{type(motion_type).__name__}") if motion_type not in self.VALID_MOTION_TYPES: raise ValueError( f"运动类型必须是以下之一:{self.VALID_MOTION_TYPES}," f"当前值:{motion_type}" ) return motion_type def _validate_blend_time(self, blend_time): """校验平滑时间(-1表示停止,否则0-500范围)""" if not isinstance(blend_time, (int, float)): raise TypeError(f"平滑时间必须是数字类型,当前类型:{type(blend_time).__name__}") # 停止模式(-1)或正常平滑时间(0-500) if blend_time == -1: return -1 min_val, max_val = self.VALID_BLEND_TIME_RANGE if not (min_val <= blend_time <= max_val): raise ValueError( f"平滑时间必须是-1(停止)或 {min_val}-{max_val} 之间(毫秒)," f"当前值:{blend_time}" ) return blend_time def to_dict(self): """转换为字典,方便序列化或存储""" return { "pos_name": self.pos_name, "speed": self.speed, "tool_id": self.tool_id, "work_id": self.work_id, "joint_values": self.joint_values, "motion_type": self.motion_type, "blend_time": self.blend_time } def __str__(self): """打印调试""" return ( f"点位名:{self.pos_name},速度:{self.speed}%\n" f"工具ID: {self.tool_id},工件ID: {self.work_id}\n" f"关节值:{self.joint_values}\n" f"运动类型:{self.motion_type},平滑时间:{self.blend_time}" )