Files
rc_yacht/scripts/MiniMaxSimulation.FCMacro
T

172 lines
9.3 KiB
Plaintext

import FreeCAD as App
import Part
import Mesh
import math
import json
import os
from FreeCAD import Vector
doc = App.ActiveDocument or App.getDocument("MiniMaxHull")
def bounds(name):
obj = doc.getObject(name)
if obj is None:
return {"exists": False, "status": "FAIL"}
b = obj.Shape.BoundBox
valid = obj.Shape.isValid() and obj.Shape.Volume > 0
return {"exists": True, "valid": valid, "status": "PASS" if valid else "FAIL", "volume_mm3": obj.Shape.Volume, "bounds_mm": [b.XMin, b.XMax, b.YMin, b.YMax, b.ZMin, b.ZMax]}
def rod(a, b, radius=0.8):
v = b.sub(a)
return Part.makeCylinder(radius, v.Length, a, v)
def collision(path_shape, target):
return path_shape.common(target).Volume
def center(shape):
if shape.Solids:
return shape.Solids[0].CenterOfMass
return Vector(0, 0, 0)
def submerged_at(shape, waterline):
cutter = Part.makeBox(400, 200, waterline + 100, Vector(-100, -100, -100))
return shape.common(cutter)
report = {
"document": doc.Name,
"suite": "MiniMax virtual qualification suite",
"assumptions": {
"target_mass_g": 145.0,
"estimated_kg_z_mm": -3.66,
"estimated_bm_mm": 22.46,
"servo_sweep_deg": [-60, 60],
"wind_model": "quasi-static drag estimate",
"status": "engineering approximation; physical measurements remain required"
},
"checks": {},
"objects": {},
"hydrostatics": {},
"kinematics": {},
"printability": {},
"limitations": ["not CFD", "not FEM", "not wave dynamics", "not servo torque validation", "not watertightness proof", "not a substitute for tank testing"]
}
object_names = ["Hull", "Keel", "KeelBallast", "Rudder", "RudderStock", "Mast", "Boom", "MainSail", "JibSail", "SG90_Sail", "SG90_Rudder", "ESP32_Tray", "CockpitHatch", "HatchSealLip", "HatchScrewBosses"]
for name in object_names:
report["objects"][name] = bounds(name)
required = [name for name in object_names if name not in ["KeelBallast", "RudderStock", "MainSail", "JibSail", "Boom"]]
missing = [name for name in required if not report["objects"][name]["exists"]]
invalid = [name for name, result in report["objects"].items() if result.get("exists") and result.get("status") != "PASS"]
report["checks"]["required_objects"] = {"status": "PASS" if not missing else "FAIL", "missing": missing}
report["checks"]["shape_validity"] = {"status": "PASS" if not invalid else "FAIL", "invalid": invalid}
hull = doc.getObject("Hull").Shape
hull_outer = doc.getObject("HullOuter").Shape
hull_box = hull_outer.BoundBox
report["checks"]["hull_envelope"] = {
"status": "PASS" if 160 <= hull_box.XLength <= 175 and hull_box.YLength <= 70 else "FAIL",
"length_mm": hull_box.XLength,
"beam_mm": hull_box.YLength,
"height_mm": hull_box.ZLength
}
waterlines = [19.0, 21.0, 23.0, 25.0, 27.0]
for waterline in waterlines:
submerged = submerged_at(hull_outer, waterline)
report["hydrostatics"][str(waterline)] = {
"waterline_mm": waterline,
"submerged_volume_cm3": submerged.Volume / 1000.0,
"displacement_g_freshwater": submerged.Volume / 1000.0,
"center_of_buoyancy_mm": [center(submerged).x, center(submerged).y, center(submerged).z]
}
design = report["hydrostatics"]["23.0"]
report["hydrostatics"]["design_point"] = design
report["checks"]["design_displacement"] = {"status": "PASS_WITHIN_MARGIN" if design["displacement_g_freshwater"] >= 140.0 else "FAIL", "required_g": 145.0, "minimum_acceptance_g": 140.0, "available_g": design["displacement_g_freshwater"]}
tray = doc.getObject("ESP32_Tray").Shape
rudder_servo = Vector(102.7, 0, 7.7)
rudder_target = Vector(185.5, 0, 13.9)
rudder_worst = 0.0
rudder_results = []
for deg in range(-60, 61, 5):
angle = math.radians(deg)
start = rudder_servo + Vector(10 * math.cos(angle), 10 * math.sin(angle), 3)
path = rod(start, Vector(121.6, 15.4, 10.8)).fuse(rod(Vector(121.6, 15.4, 10.8), Vector(164, 15.4, 12.3))).fuse(rod(Vector(164, 15.4, 12.3), rudder_target))
volume = collision(path, tray)
rudder_worst = max(rudder_worst, volume)
rudder_results.append({"deg": deg, "tray_collision_mm3": volume})
report["kinematics"]["rudder"] = {"sweep_deg": [-60, 60], "step_deg": 5, "route": "side route via y=20 mm", "pushrod_length_mm": (rudder_target - rudder_servo).Length, "worst_tray_collision_mm3": rudder_worst, "samples": rudder_results, "status": "PASS" if rudder_worst == 0 else "FAIL"}
sheet_servo = Vector(79.2, 0, 9.2)
sheet_target = Vector(113.9, 0, 21.6)
sheet_worst_tray = 0.0
sheet_worst_servo = 0.0
for deg in range(-60, 61, 5):
angle = math.radians(deg)
start = sheet_servo + Vector(10 * math.cos(angle), 10 * math.sin(angle), 3)
path = rod(start, Vector(start.x, 15.4, 10.8)).fuse(rod(Vector(start.x, 15.4, 10.8), Vector(113.9, 15.4, 14.0))).fuse(rod(Vector(113.9, 15.4, 14.0), sheet_target))
sheet_worst_tray = max(sheet_worst_tray, collision(path, tray))
sheet_worst_servo = max(sheet_worst_servo, collision(path, doc.getObject("SG90_Rudder").Shape))
report["kinematics"]["sheet"] = {"sweep_deg": [-60, 60], "step_deg": 5, "pushrod_length_mm": (sheet_target - sheet_servo).Length, "worst_tray_collision_mm3": sheet_worst_tray, "worst_opposite_servo_collision_mm3": sheet_worst_servo, "status": "PASS" if sheet_worst_tray == 0 and sheet_worst_servo == 0 else "FAIL"}
report["checks"]["kinematics"] = {"status": "PASS" if report["kinematics"]["rudder"]["status"] == "PASS" and report["kinematics"]["sheet"]["status"] == "PASS" else "FAIL"}
hatch = doc.getObject("CockpitHatch").Shape
coaming = doc.getObject("DeckCoaming").Shape if doc.getObject("DeckCoaming") else None
hatch_clearance = hatch.BoundBox.ZMin - (coaming.BoundBox.ZMax if coaming else hatch.BoundBox.ZMin)
production = ["Hull", "Keel", "Rudder", "Mast", "Boom", "MainSail", "JibSail", "CockpitHatch", "HatchSealLip", "HatchScrewBosses"]
print_failures = [name for name in production if not report["objects"].get(name, {}).get("valid", False)]
report["printability"] = {"production_parts": production, "invalid_parts": print_failures, "hatch_coaming_vertical_clearance_mm": hatch_clearance, "hatch_geometry_status": "PASS" if hatch_clearance >= 0 else "REVIEW", "status": "PASS" if not print_failures else "FAIL", "watertightness": "REVIEW: requires gasket and physical leak test"}
report["checks"]["printability_geometry"] = {"status": report["printability"]["status"]}
stl_files = ["MiniMaxHull_Hull.stl", "MiniMaxHull_Keel.stl", "MiniMaxHull_Rudder.stl", "MiniMaxHull_MastBoom.stl", "MiniMaxHull_Sails.stl", "MiniMaxHull_Hatch.stl"]
stl_validation = {}
for filename in stl_files:
path = os.path.join("/tmp", filename)
if not os.path.exists(path):
stl_validation[filename] = {"status": "REVIEW", "reason": "STL not present on FreeCAD host"}
continue
mesh = Mesh.Mesh(path)
b = mesh.BoundBox
solid = bool(mesh.isSolid())
stl_validation[filename] = {"status": "PASS" if solid else "FAIL", "is_solid": solid, "facets": len(mesh.Facets), "volume_mm3": mesh.Volume, "bounds_mm": [b.XLength, b.YLength, b.ZLength]}
report["stl_validation"] = stl_validation
report["checks"]["stl_validation"] = {"status": "PASS" if all(item.get("status") == "PASS" for item in stl_validation.values()) else "REVIEW"}
kg_z = report["assumptions"]["estimated_kg_z_mm"]
bm = report["assumptions"]["estimated_bm_mm"]
for key, data in report["hydrostatics"].items():
if key == "design_point":
continue
data["estimated_gm_mm"] = data["center_of_buoyancy_mm"][2] + bm - kg_z
data["stability_status"] = "PASS_PRELIMINARY" if data["estimated_gm_mm"] > 0 else "FAIL"
report["checks"]["preliminary_stability"] = {"status": "PASS_PRELIMINARY" if report["hydrostatics"]["23.0"]["estimated_gm_mm"] > 0 else "FAIL", "design_gm_mm": report["hydrostatics"]["23.0"]["estimated_gm_mm"]}
mass_cases = []
kb_design = report["hydrostatics"]["23.0"]["center_of_buoyancy_mm"][2]
for mass_g in [140, 145, 150, 155]:
for kg_z in [-10, -5, 0, 5]:
gm = kb_design + bm - kg_z
mass_cases.append({"mass_g": mass_g, "kg_z_mm": kg_z, "gm_mm": gm, "status": "PASS_PRELIMINARY" if gm > 0 else "FAIL"})
report["mass_cg_sweep"] = {"mass_g_cases": [140, 145, 150, 155], "kg_z_cases_mm": [-10, -5, 0, 5], "cases": mass_cases, "worst_gm_mm": min(item["gm_mm"] for item in mass_cases), "status": "PASS_PRELIMINARY" if all(item["status"] != "FAIL" for item in mass_cases) else "FAIL", "note": "Mass does not change the simplified GM calculation; physical CG measurement is required"}
report["checks"]["mass_cg_sweep"] = {"status": report["mass_cg_sweep"]["status"], "worst_gm_mm": report["mass_cg_sweep"]["worst_gm_mm"]}
wind = {}
for speed in [3, 5, 7, 10]:
q = 0.5 * 1.225 * speed * speed
force = q * 1.0 * 0.002442
moment = force * 0.08
heel = math.degrees(math.atan(moment / (1.915 * 0.03733)))
wind[str(speed)] = {"wind_m_s": speed, "force_N_est": force, "heel_deg_est": heel}
report["checks"]["wind_load"] = {"status": "REVIEW", "samples": wind, "reason": "simplified quasi-static estimate"}
statuses = [value["status"] for value in report["checks"].values()]
report["overall"] = "PASS_WITH_REVIEW" if "FAIL" not in statuses else "FAIL"
path = doc.FileName
root = os.path.dirname(path) if path else os.getcwd()
out = os.path.join(root, "reports", "virtual_simulation_report.json")
os.makedirs(os.path.dirname(out), exist_ok=True)
with open(out, "w") as f:
json.dump(report, f, indent=2)
print(json.dumps(report, indent=2))