Files
rc_yacht/scripts/RudderMotionSweep.FCMacro
T

101 lines
3.4 KiB
Plaintext

import FreeCAD as App
import FreeCADGui as Gui
import Part
import math
import time
from FreeCAD import Vector
doc = App.ActiveDocument
if doc is None:
raise RuntimeError("Open MiniMaxHull.FCStd first")
required = [
"RudderMechanism_Rudder",
"RudderMechanism_Stock",
"RudderMechanism_Horn",
"RudderMechanism_Tiller",
"RudderMechanism_Pushrod",
"SG90_Rudder",
"ServoTray_Rudder",
"ESP32_Tray",
"ESP32_BoardEnvelope",
"CockpitHatch",
"DeckCoaming",
]
missing = [name for name in required if doc.getObject(name) is None]
if missing:
raise RuntimeError("Missing rudder mechanism objects: " + ", ".join(missing))
rudder_source = doc.getObject("Rudder")
stock_source = doc.getObject("RudderStock")
horn_source = doc.getObject("ServoHorn_Rudder")
rudder_axis = Vector(187.0, 0.0, 0.0)
servo_axis = Vector(102.3069, 0.0, 13.86)
static_names = [
"SG90_Rudder",
"ServoTray_Rudder",
"ESP32_Tray",
"ESP32_BoardEnvelope",
"CockpitHatch",
"DeckCoaming",
]
moving_names = [
"RudderMechanism_Rudder",
"RudderMechanism_Stock",
"RudderMechanism_Horn",
"RudderMechanism_Tiller",
"RudderMechanism_Pushrod",
]
static_shapes = [doc.getObject(name).Shape for name in static_names]
def endpoint(axis, radius, angle, z):
radians = math.radians(angle)
return Vector(axis.x + radius * math.cos(radians), axis.y + radius * math.sin(radians), z)
def set_frame(angle):
rudder = doc.getObject("RudderMechanism_Rudder")
stock = doc.getObject("RudderMechanism_Stock")
horn = doc.getObject("RudderMechanism_Horn")
tiller = doc.getObject("RudderMechanism_Tiller")
pushrod = doc.getObject("RudderMechanism_Pushrod")
rudder.Shape = rudder_source.Shape.copy()
stock.Shape = stock_source.Shape.copy()
horn.Shape = horn_source.Shape.copy()
rotation = App.Rotation(Vector(0, 0, 1), angle)
rudder.Placement = App.Placement(rudder_axis, rotation) * App.Placement(-rudder_axis, App.Rotation()) * rudder_source.Placement
stock.Placement = App.Placement(rudder_axis, rotation) * App.Placement(-rudder_axis, App.Rotation()) * stock_source.Placement
horn.Placement = App.Placement(servo_axis, rotation) * App.Placement(-servo_axis, App.Rotation()) * horn_source.Placement
p0 = endpoint(servo_axis, 3.8, angle, 13.86)
p1 = endpoint(rudder_axis, 3.5, angle, 13.86)
tiller.Shape = Part.makeCylinder(0.7, 3.5, rudder_axis, Vector(p1.x - rudder_axis.x, p1.y - rudder_axis.y, 0))
pushrod.Shape = Part.makeCylinder(0.65, (p1 - p0).Length, p0, p1 - p0)
for name in moving_names:
doc.getObject(name).MotionAngle = angle
doc.recompute()
def forbidden_intersection():
moving = [doc.getObject(name).Shape for name in moving_names[2:]]
return max((shape.common(static).Volume for shape in moving for static in static_shapes), default=0.0)
angles = list(range(-60, 61, 5)) + list(range(55, -61, -5))
maximum = 0.0
for angle in angles:
set_frame(angle)
maximum = max(maximum, forbidden_intersection())
Gui.updateGui()
time.sleep(0.05)
set_frame(0)
root = doc.getObject("RudderMechanism")
root.SimulationStatus = "PASS_WITH_REVIEW"
root.MaxForbiddenIntersection = "%.6f mm^3" % maximum
doc.recompute()
Gui.activeDocument().activeView().fitAll()
print("Rudder motion sweep complete")
print("Range: -60 deg to +60 deg and return")
print("Positions: %d" % len(angles))
print("Maximum forbidden intersection: %.6f mm^3" % maximum)