| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- """故障频率解析与计算。
- 目标:按“风机/测点/转速”解析部件,读取轴承几何或齿轮齿数,输出特征频率,
- 并明确返回缺失参数,避免用猜测参数生成诊断结论。
- """
- from __future__ import annotations
- import math
- import re
- from dataclasses import dataclass, field
- from typing import Any, Dict, List, Optional, Tuple
- from app.report.collector import platform_conn
- BEARING_FIELDS = ("n_rolls", "roller_diameter", "pitch_diameter", "contact_angle")
- @dataclass
- class ComponentRef:
- component_type: str
- position: str = ""
- stage: int = 0
- brand: Optional[str] = None
- model: Optional[str] = None
- teeth: Optional[int] = None
- warnings: List[str] = field(default_factory=list)
- def _safe_engine(value: str) -> str:
- if not re.fullmatch(r"[A-Za-z0-9_]+", value or ""):
- raise ValueError("非法的风机编号")
- return value
- def _one(cur, sql: str, params: Tuple[Any, ...] = ()) -> Optional[Dict[str, Any]]:
- cur.execute(sql, params)
- return cur.fetchone()
- class FaultFrequencyService:
- def __init__(self, engine_code: str, point_name: str, rpm: float,
- mill_type_code: Optional[str] = None):
- self.engine_code = _safe_engine(engine_code)
- self.point_name = (point_name or "").strip()
- self.rpm = float(rpm or 0)
- self.mill_type_code = (mill_type_code or "").strip() or None
- def resolve(self) -> Dict[str, Any]:
- conn = platform_conn()
- try:
- with conn.cursor() as cur:
- mill_type = self._mill_type(cur)
- if not mill_type:
- return self._unsupported([], "风机未关联机型(wind_engine_group.mill_type_code 为空)")
- ratio = self._speed_ratio(cur, mill_type)
- ref = self._component(cur, mill_type)
- if ref is None:
- return self._unsupported([], "该测点未映射到主轴承/齿轮箱/发电机")
- if not ref.brand or not ref.model:
- return self._unsupported(["brand", "model"], "部件品牌或型号缺失")
- spec = self._spec(cur, ref.brand, ref.model)
- if spec is None:
- return self._unsupported(["brand/model"], f"故障参数库无 {ref.brand}/{ref.model}")
- missing = [k for k in BEARING_FIELDS if spec.get(k) is None]
- if missing:
- return self._unsupported(missing, "轴承几何参数不完整,无法计算故障频率")
- shaft_rpm, speed_note = self._shaft_rpm(ref, ratio)
- if shaft_rpm <= 0:
- return self._unsupported(["rpm"], "转速为 0,无法计算")
- fr = shaft_rpm / 60.0
- n = float(spec["n_rolls"])
- d = float(spec["roller_diameter"])
- D = float(spec["pitch_diameter"])
- theta = math.radians(float(spec["contact_angle"]))
- ratio_d = d / D
- bpfi = (n / 2) * (1 + ratio_d * math.cos(theta)) * fr
- bpfo = (n / 2) * (1 - ratio_d * math.cos(theta)) * fr
- bsf = (D / (2 * d)) * (1 - (ratio_d * math.cos(theta)) ** 2) * fr
- ftf = 0.5 * (1 - ratio_d * math.cos(theta)) * fr
- freqs = {
- "BPFI": round(bpfi, 3),
- "BPFO": round(bpfo, 3),
- "BSF": round(bsf, 3),
- "FTF": round(ftf, 3),
- }
- if ref.teeth:
- freqs["GMF"] = round(float(ref.teeth) * fr, 3)
- return {
- "code": 200,
- "supported": True,
- "engineCode": self.engine_code,
- "millTypeCode": mill_type,
- "componentType": ref.component_type,
- "position": ref.position,
- "stage": ref.stage,
- "brand": ref.brand,
- "model": ref.model,
- "nRolls": n,
- "rollerDiameter": d,
- "pitchDiameter": D,
- "contactAngle": float(spec["contact_angle"]),
- "rpm": round(self.rpm, 3),
- "shaftRpm": round(shaft_rpm, 3),
- "speedRatio": round(ratio, 4),
- "frequencies": freqs,
- "source": spec.get("source") or "component_fault_spec",
- "confidence": float(spec.get("confidence") or 0),
- "missing": [],
- "warnings": ref.warnings + ([speed_note] if speed_note else []),
- }
- finally:
- conn.close()
- def _mill_type(self, cur) -> Optional[str]:
- if self.mill_type_code:
- return self.mill_type_code
- row = _one(cur, "SELECT mill_type_code FROM wind_engine_group WHERE engine_code=%s", (self.engine_code,))
- return row["mill_type_code"] if row else None
- def _speed_ratio(self, cur, mill_type: str) -> float:
- row = _one(cur, "SELECT rotational_speed_ratio FROM wind_engine_mill WHERE mill_type_code=%s", (mill_type,))
- try:
- value = float(row["rotational_speed_ratio"]) if row and row.get("rotational_speed_ratio") else 1.0
- except (TypeError, ValueError):
- value = 1.0
- return value if value > 0 else 1.0
- def _component(self, cur, mill_type: str) -> Optional[ComponentRef]:
- map_row = _one(cur, "SELECT component_type, position, stage FROM measurement_point_component WHERE point_name=%s",
- (self.point_name,))
- if map_row:
- ctype, pos, stage = map_row["component_type"], map_row["position"] or "", int(map_row["stage"] or 0)
- else:
- ctype, pos, stage = self._infer(self.point_name)
- if not ctype:
- return None
- if ctype == "main_bearing":
- row = _one(cur, "SELECT * FROM unit_bearings WHERE mill_type_code=%s LIMIT 1", (mill_type,))
- if not row:
- return None
- if pos == "front":
- brand, model = row.get("front_bearing_brand"), row.get("front_bearing_model")
- elif pos == "rear":
- brand, model = row.get("rear_bearing_brand"), row.get("rear_bearing_model")
- else:
- brand, model = row.get("front_bearing_brand"), row.get("front_bearing_model")
- pos = "front"
- return ComponentRef(ctype, pos, 0, brand, model)
- if ctype == "generator":
- row = _one(cur, "SELECT * FROM unit_dynamo WHERE mill_type_code=%s LIMIT 1", (mill_type,))
- if not row:
- return None
- if pos == "non_drive_end":
- brand, model = row.get("non_drive_end_bearing_brand"), row.get("non_drive_end_bearing_model")
- else:
- brand, model = row.get("drive_end_bearing_brand"), row.get("drive_end_bearing_model")
- pos = pos or "drive_end"
- return ComponentRef(ctype, pos, 0, brand, model)
- gear = _one(cur, "SELECT code FROM unit_gearbox WHERE mill_type_code=%s LIMIT 1", (mill_type,))
- if not gear:
- return None
- code = gear["code"]
- if ctype == "gearbox_gear":
- row = _one(cur, """SELECT * FROM unit_gearbox_structure
- WHERE gearbox_code=%s AND gearbox_structure=1 AND planetary_gear_grade=%s LIMIT 1""",
- (code, stage or 1))
- if not row:
- return None
- return ComponentRef(ctype, pos or "planet", stage or 1,
- row.get("bearing_brand"), row.get("bearing_model"),
- row.get("gear_ring_teeth_count"))
- if ctype == "gearbox_bearing":
- grade = 3 if pos == "high_speed" else 2 if pos in ("intermediate", "low_speed_intermediate") else 1
- row = _one(cur, """SELECT * FROM unit_gearbox_bearings
- WHERE gearbox_code=%s AND parallel_wheel_grade=%s LIMIT 1""", (code, grade))
- if not row:
- return None
- brand = row.get("bearing_rs_brand") or row.get("bearing_brand")
- model = row.get("bearing_rs_model") or row.get("bearing_model")
- return ComponentRef(ctype, pos, 0, brand, model)
- return None
- @staticmethod
- def _infer(point: str) -> Tuple[Optional[str], str, int]:
- p = (point or "").lower()
- stage = 1 if "first" in p else 2 if "second" in p else 3 if "third" in p else 0
- if "main_bearing" in p:
- pos = "front" if "front" in p else "rear" if "rear" in p else "main"
- return "main_bearing", pos, 0
- if "generator" in p or "stator" in p:
- pos = "non_drive_end" if "non_drive" in p else "drive_end"
- return "generator", pos, 0
- if "gearbox" in p:
- if "planet" in p or "sun" in p:
- return "gearbox_gear", "planet", stage or 1
- return "gearbox_bearing", "high_speed" if "high_speed" in p else "low_speed", 0
- return None, "", 0
- def _spec(self, cur, brand: str, model: str) -> Optional[Dict[str, Any]]:
- row = _one(cur, """SELECT * FROM component_fault_spec
- WHERE component_type IN ('bearing','main_bearing','generator','gearbox_bearing')
- AND brand=%s AND model=%s LIMIT 1""", (brand, model))
- if row:
- return row
- row = _one(cur, """SELECT * FROM unit_dict_brand_model
- WHERE unit_type=1 AND manufacture=%s AND model_number=%s LIMIT 1""", (brand, model))
- if row:
- return {
- "n_rolls": row.get("rolls_number"),
- "roller_diameter": row.get("rolls_diameter"),
- "pitch_diameter": row.get("circle_diameter"),
- "contact_angle": row.get("theta_deg"),
- "source": "unit_dict_brand_model",
- "confidence": 0.9,
- }
- return None
- def _shaft_rpm(self, ref: ComponentRef, ratio: float) -> Tuple[float, str]:
- if self.rpm <= 0:
- return 0.0, ""
- if ref.component_type in ("main_bearing", "gearbox_gear", "gearbox_bearing") and ratio > 1:
- if ref.component_type == "gearbox_bearing" and ref.position == "high_speed":
- return self.rpm, ""
- note = "按机型传动比折算到低速轴" if ref.component_type == "main_bearing" else "按机型传动比折算,级间转速为近似值"
- return self.rpm / ratio, note
- return self.rpm, ""
- def _unsupported(self, missing: List[str], message: str) -> Dict[str, Any]:
- return {
- "code": 200,
- "supported": False,
- "engineCode": self.engine_code,
- "millTypeCode": self.mill_type_code,
- "componentType": None,
- "frequencies": {},
- "missing": missing,
- "message": message,
- "warnings": [],
- }
|