Merge pull request #2173 from SiboVG/issue-2137

[#2137] Import mass and CG from RASAero sims
This commit is contained in:
Sibo Van Gool 2023-04-05 01:10:13 +02:00 committed by GitHub
commit 27de9f6692
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 508 additions and 172 deletions

View File

@ -29,7 +29,7 @@
<!-- JAR -->
<target name="jar" depends="jar-core,jar-swing">
<target name="jar" depends="clean,jar-core,jar-swing">
</target>
<target name="jar-core" depends="build-core">

View File

@ -116,7 +116,7 @@ public abstract class AbstractMotorLoader implements MotorLoader {
* @param designation the motor designation.
* @return the designation with a possible delay removed.
*/
protected static String removeDelay(String designation) {
public static String removeDelay(String designation) {
if (designation.matches(".*-([0-9]+|[pP])$")) {
designation = designation.substring(0, designation.lastIndexOf('-'));
}

View File

@ -112,15 +112,29 @@ public class RASAeroCommonConstants {
public static final String SIMULATION_LIST = "SimulationList";
public static final String SIMULATION = "Simulation";
public static final String SUSTAINER_ENGINE = "SustainerEngine";
// TODO: SustainerLaunchWt, SustainerCG?
public static final String SUSTAINER_LAUNCH_WT = "SustainerLaunchWt";
public static final String SUSTAINER_NOZZLE_DIAMETER = "SustainerNozzleDiameter";
public static final String SUSTAINER_CG = "SustainerCG";
public static final String SUSTAINER_IGNITION_DELAY = "SustainerIgnitionDelay";
public static final String BOOSTER1_ENGINE = "Booster1Engine";
public static final String BOOSTER1_SEPARATION_DELAY = "Booster1SeparationDelay"; // Delay after booster burnout to separate
public static final String BOOSTER1_IGNITION_DELAY = "Booster1IgnitionDelay";
public static final String BOOSTER1_LAUNCH_WT = "Booster1LaunchWt";
public static final String BOOSTER1_NOZZLE_DIAMETER = "Booster1NozzleDiameter";
public static final String BOOSTER1_CG = "Booster1CG";
public static final String INCLUDE_BOOSTER1 = "IncludeBooster1";
public static final String BOOSTER2_ENGINE = "Booster2Engine";
public static final String BOOSTER2_SEPARATION_DELAY = "Booster2Delay"; // Delay after booster burnout to separate
public static final String BOOSTER2_LAUNCH_WT = "Booster2LaunchWt";
public static final String BOOSTER2_NOZZLE_DIAMETER = "Booster2NozzleDiameter";
public static final String BOOSTER2_CG = "Booster2CG";
public static final String INCLUDE_BOOSTER2 = "IncludeBooster2";
public static final String FLIGHT_TIME = "FlightTime";
public static final String TIME_TO_APOGEE = "TimetoApogee";
public static final String MAX_ALTITUDE = "MaxAltitude";
public static final String MAX_VELOCITY = "MaxVelocity";
public static final String OPTIMUM_WT = "OptimumWt";
public static final String OPTIMUM_MAX_ALT = "OptimumMaxAlt";
/**
@ -143,6 +157,10 @@ public class RASAeroCommonConstants {
* Angle conversion. RASAero is in degrees, OpenRocket in rad.
*/
public static final double RASAERO_TO_OPENROCKET_ANGLE = 180 / Math.PI;
/**
* Weight conversion from OpenRocket units to RASAero units. RASAero is in pounds (lb), OpenRocket in kilograms (kg).
*/
public static final double OPENROCKET_TO_RASAERO_WEIGHT = 2.20462262;
/**
* Temperature conversion. RASAero is in Fahrenheit, OpenRocket in Kelvin.
*/

View File

@ -2,6 +2,7 @@ package net.sf.openrocket.file.rasaero.importt;
import net.sf.openrocket.aerodynamics.WarningSet;
import net.sf.openrocket.database.motor.ThrustCurveMotorSet;
import net.sf.openrocket.file.motor.AbstractMotorLoader;
import net.sf.openrocket.motor.ThrustCurveMotor;
import net.sf.openrocket.startup.Application;
@ -31,7 +32,7 @@ public abstract class RASAeroMotorsLoader {
if (split.length != 2) {
return null;
}
String motorName = split[0];
String motorName = AbstractMotorLoader.removeDelay(split[0]);
String manufacturer = split[1].replaceAll("^\\(|\\)$", ""); // Remove beginning and ending parenthesis
for (ThrustCurveMotor motor : allMotors) {
if (motorName.equals(motor.getDesignation()) && motor.getManufacturer().matches(manufacturer)) {

View File

@ -0,0 +1,485 @@
package net.sf.openrocket.file.rasaero.importt;
import net.sf.openrocket.aerodynamics.WarningSet;
import net.sf.openrocket.document.Simulation;
import net.sf.openrocket.file.DocumentLoadingContext;
import net.sf.openrocket.file.simplesax.AbstractElementHandler;
import net.sf.openrocket.file.simplesax.ElementHandler;
import net.sf.openrocket.file.simplesax.PlainTextHandler;
import net.sf.openrocket.motor.IgnitionEvent;
import net.sf.openrocket.motor.Motor;
import net.sf.openrocket.motor.MotorConfiguration;
import net.sf.openrocket.motor.ThrustCurveMotor;
import net.sf.openrocket.rocketcomponent.AxialStage;
import net.sf.openrocket.rocketcomponent.FlightConfigurationId;
import net.sf.openrocket.rocketcomponent.MotorMount;
import net.sf.openrocket.rocketcomponent.Rocket;
import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.rocketcomponent.StageSeparationConfiguration;
import net.sf.openrocket.simulation.SimulationOptions;
import net.sf.openrocket.util.Coordinate;
import org.xml.sax.SAXException;
import java.util.HashMap;
/**
* Handles RASAero simulation elements.
* We will only import the motor information from it.
*/
public class SimulationHandler extends AbstractElementHandler {
private final DocumentLoadingContext context;
private final Rocket rocket;
private final SimulationOptions launchSiteSettings;
private final int simulationNr;
// Motor information
private ThrustCurveMotor sustainerEngine;
private Double sustainerIgnitionDelay;
private Double sustainerLaunchWt;
private Double sustainerCG;
private ThrustCurveMotor booster1Engine;
private Double booster1IgnitionDelay;
private Double booster1SeparationDelay;
private Double booster1LaunchWt;
private Double booster1CG;
private Boolean includeBooster1;
private ThrustCurveMotor booster2Engine;
private Double booster2SeparationDelay;
private Double booster2LaunchWt;
private Double booster2CG;
private Boolean includeBooster2;
public SimulationHandler(DocumentLoadingContext context, Rocket rocket, SimulationOptions launchSiteSettings, int simulationNr) {
this.context = context;
this.rocket = rocket;
this.launchSiteSettings = launchSiteSettings;
this.simulationNr = simulationNr;
}
@Override
public ElementHandler openElement(String element, HashMap<String, String> attributes, WarningSet warnings) throws SAXException {
if (RASAeroCommonConstants.SUSTAINER_ENGINE.equals(element) || RASAeroCommonConstants.SUSTAINER_IGNITION_DELAY.equals(element)
|| RASAeroCommonConstants.SUSTAINER_LAUNCH_WT.equals(element) || RASAeroCommonConstants.SUSTAINER_CG.equals(element)
|| RASAeroCommonConstants.BOOSTER1_ENGINE.equals(element) || RASAeroCommonConstants.BOOSTER1_IGNITION_DELAY.equals(element)
|| RASAeroCommonConstants.BOOSTER1_SEPARATION_DELAY.equals(element) || RASAeroCommonConstants.BOOSTER1_LAUNCH_WT.equals(element)
|| RASAeroCommonConstants.BOOSTER1_CG.equals(element) ||RASAeroCommonConstants.INCLUDE_BOOSTER1.equals(element)
|| RASAeroCommonConstants.BOOSTER2_ENGINE.equals(element) || RASAeroCommonConstants.BOOSTER2_SEPARATION_DELAY.equals(element)
|| RASAeroCommonConstants.BOOSTER2_LAUNCH_WT.equals(element) || RASAeroCommonConstants.BOOSTER2_CG.equals(element)
|| RASAeroCommonConstants.INCLUDE_BOOSTER2.equals(element)) {
return PlainTextHandler.INSTANCE;
}
return null;
}
@Override
public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings) throws SAXException {
if (RASAeroCommonConstants.SUSTAINER_ENGINE.equals(element)) {
sustainerEngine = RASAeroMotorsLoader.getMotorFromRASAero(content, warnings);
} else if (RASAeroCommonConstants.SUSTAINER_IGNITION_DELAY.equals(element)) {
sustainerIgnitionDelay = Double.parseDouble(content);
} else if (RASAeroCommonConstants.SUSTAINER_LAUNCH_WT.equals(element)) {
sustainerLaunchWt = Double.parseDouble(content) / RASAeroCommonConstants.OPENROCKET_TO_RASAERO_WEIGHT;
} else if (RASAeroCommonConstants.SUSTAINER_CG.equals(element)) {
sustainerCG = Double.parseDouble(content) / RASAeroCommonConstants.RASAERO_TO_OPENROCKET_LENGTH;
} else if (RASAeroCommonConstants.BOOSTER1_ENGINE.equals(element)) {
booster1Engine = RASAeroMotorsLoader.getMotorFromRASAero(content, warnings);
} else if (RASAeroCommonConstants.BOOSTER1_IGNITION_DELAY.equals(element)) {
booster1IgnitionDelay = Double.parseDouble(content);
} else if (RASAeroCommonConstants.BOOSTER1_SEPARATION_DELAY.equals(element)) {
booster1SeparationDelay = Double.parseDouble(content);
} else if (RASAeroCommonConstants.BOOSTER1_LAUNCH_WT.equals(element)) {
booster1LaunchWt = Double.parseDouble(content) / RASAeroCommonConstants.OPENROCKET_TO_RASAERO_WEIGHT;
} else if (RASAeroCommonConstants.BOOSTER1_CG.equals(element)) {
booster1CG = Double.parseDouble(content) / RASAeroCommonConstants.RASAERO_TO_OPENROCKET_LENGTH;
} else if (RASAeroCommonConstants.INCLUDE_BOOSTER1.equals(element)) {
includeBooster1 = Boolean.parseBoolean(content);
} else if (RASAeroCommonConstants.BOOSTER2_ENGINE.equals(element)) {
booster2Engine = RASAeroMotorsLoader.getMotorFromRASAero(content, warnings);
} else if (RASAeroCommonConstants.BOOSTER2_SEPARATION_DELAY.equals(element)) {
booster2SeparationDelay = Double.parseDouble(content);
} else if (RASAeroCommonConstants.BOOSTER2_LAUNCH_WT.equals(element)) {
booster2LaunchWt = Double.parseDouble(content) / RASAeroCommonConstants.OPENROCKET_TO_RASAERO_WEIGHT;
} else if (RASAeroCommonConstants.BOOSTER2_CG.equals(element)) {
booster2CG = Double.parseDouble(content) / RASAeroCommonConstants.RASAERO_TO_OPENROCKET_LENGTH;
} else if (RASAeroCommonConstants.INCLUDE_BOOSTER2.equals(element)) {
includeBooster2 = Boolean.parseBoolean(content);
}
}
@Override
public void endHandler(String element, HashMap<String, String> attributes, String content, WarningSet warnings) throws SAXException {
FlightConfigurationId fcid = new FlightConfigurationId();
rocket.createFlightConfiguration(fcid);
// Select if this is the first config
if (rocket.getSelectedConfiguration().getId().equals(FlightConfigurationId.DEFAULT_VALUE_FCID)) {
rocket.setSelectedConfiguration(fcid);
}
// Add motors to the rocket
MotorMount sustainerMount = addMotorToStage(0, sustainerEngine, sustainerIgnitionDelay, fcid, warnings);
MotorMount booster1Mount = null;
if (includeBooster1) {
booster1Mount = addMotorToStage(1, booster1Engine, booster1IgnitionDelay, fcid, warnings);
}
MotorMount booster2Mount = null;
if (includeBooster2) {
booster2Mount = addMotorToStage(2, booster2Engine, 0.0, fcid, warnings);
}
// Set separation settings
setSeparationDelay(0, 0.0, fcid);
if (includeBooster1) {
setSeparationDelay(1, booster1SeparationDelay, fcid);
}
if (includeBooster2) {
setSeparationDelay(2, booster2SeparationDelay, fcid);
}
// Add a new simulation
Simulation sim = new Simulation(rocket);
sim.setFlightConfigurationId(fcid);
sim.setName("Simulation " + simulationNr);
sim.copySimulationOptionsFrom(launchSiteSettings);
context.getOpenRocketDocument().addSimulation(sim);
// Set the weight and CG overrides
applyMassOverrides(warnings);
applyCGOverrides(sustainerMount, booster1Mount, booster2Mount, fcid);
}
/**
* Adds a motor to the specified stage.
* @param stageNr The stage number
* @param motor The motor to add
* @param ignitionDelay The ignition delay
* @param id The flight configuration id
* @param warnings The warning set
* @return The motor mount in which the motor is added, or null if no motor was added
*/
private MotorMount addMotorToStage(final int stageNr, final Motor motor, final Double ignitionDelay,
final FlightConfigurationId id, final WarningSet warnings) {
if (motor == null || rocket.getStage(stageNr) == null) {
return null;
}
MotorMount mount = getMotorMountForStage(stageNr);
if (mount == null) {
warnings.add("No motor mount found for stage " + stageNr + ". Ignoring motor.");
return null;
}
MotorConfiguration motorConfig = new MotorConfiguration(mount, id);
motorConfig.setMotor(motor);
// RASAero requires apogee deployment, so the sustainer motor should always be plugged
if (stageNr == 0) {
motorConfig.setEjectionDelay(Motor.PLUGGED_DELAY);
}
double delay = ignitionDelay != null ? ignitionDelay : 0.0;
motorConfig.setIgnitionDelay(delay);
if (stageNr < rocket.getStageCount() - 1) { // Use burnout non-last if multi-staged rocket
motorConfig.setIgnitionEvent(IgnitionEvent.BURNOUT);
} else {
motorConfig.setIgnitionEvent(IgnitionEvent.AUTOMATIC);
}
mount.setMotorConfig(motorConfig, id);
return mount;
}
private void setSeparationDelay(final int stageNr, Double separationDelay,
final FlightConfigurationId id) {
if (rocket.getStage(stageNr) == null) {
return;
}
StageSeparationConfiguration config = rocket.getStage(stageNr).getSeparationConfigurations().get(id);
if (separationDelay != null) {
config.setSeparationDelay(separationDelay);
}
config.setSeparationEvent(StageSeparationConfiguration.SeparationEvent.BURNOUT);
}
/**
* Returns the furthest back motor mount in the stage.
* @param stage stage number
* @return furthest back motor mount of the stage
*/
private MotorMount getMotorMountForStage(int stage) {
MotorMount mount = null;
for (RocketComponent component : rocket.getStage(stage)) {
if (component instanceof MotorMount) {
mount = (MotorMount) component;
}
}
if (mount != null) {
mount.setMotorMount(true);
}
return mount;
}
private void applyMassOverrides(WarningSet warnings) {
// Don't do anything if the mass has already been overridden by a previous simulation
if (rocket.getStage(0).isMassOverridden()) {
return;
}
applySustainerMassOverride();
applyBooster1MassOverride(warnings);
applyBooster2MassOverride(warnings);
}
/**
* Applies the mass from the RASAero simulation to the sustainer as an override, and returns the final sustainer mass.
* Note: the sustainer motor mass is subtracted from the RASAero mass to get the final mass.
* @return the final sustainer mass
*/
private double applySustainerMassOverride() {
if (sustainerLaunchWt == null || sustainerLaunchWt == 0) {
return 0;
}
// Get the sustainer motor weight
double sustainerMotorWt = 0;
if (sustainerEngine != null) {
sustainerMotorWt = sustainerEngine.getLaunchMass();
}
double sustainerWt = sustainerLaunchWt - sustainerMotorWt;
AxialStage sustainer = rocket.getStage(0);
sustainer.setMassOverridden(true);
sustainer.setSubcomponentsOverriddenMass(true);
sustainer.setOverrideMass(sustainerWt);
return sustainerWt;
}
/**
* Applies the mass from the RASAero simulation to booster1 as an override, and returns the final booster1 mass.
* Note: the sustainer mass and booster1 motor mass is subtracted from the RASAero mass to get the final mass.
* @param warnings list to add import warnings to
* @return the final booster1 mass
*/
private double applyBooster1MassOverride(WarningSet warnings) {
if (!includeBooster1 || booster1LaunchWt == null || booster1LaunchWt == 0 || sustainerLaunchWt == null) {
return 0;
}
// Calculate the final booster mass
final double boosterWt;
if (sustainerLaunchWt > booster1LaunchWt) {
warnings.add("Sustainer wt is greater than total loaded wt of booster 1. Setting mass to 0.");
boosterWt = 0;
} else {
// Get the booster motor weight
double boosterMotorWt = 0;
if (booster1Engine != null) {
boosterMotorWt = booster1Engine.getLaunchMass();
}
boosterWt = booster1LaunchWt - boosterMotorWt - sustainerLaunchWt;
}
AxialStage booster = rocket.getStage(1);
booster.setMassOverridden(true);
booster.setSubcomponentsOverriddenMass(true);
booster.setOverrideMass(boosterWt);
return boosterWt;
}
/**
* Applies the mass from the RASAero simulation to booster2 as an override, and returns the final booster2 mass.
* Note: the sustainer mass, booster1 mass, and booster2 motor mass is subtracted from the RASAero mass to get the final mass.
* @param warnings list to add import warnings to
* @return the final booster2 mass
*/
private double applyBooster2MassOverride(WarningSet warnings) {
if (!includeBooster2 || booster2LaunchWt == null || booster2LaunchWt == 0 || booster1LaunchWt == null) {
return 0;
}
// Calculate the final booster mass
final double boosterWt;
if (booster1LaunchWt > booster2LaunchWt) {
warnings.add("Booster1 wt is greater than total loaded wt of booster 2. Setting mass to 0.");
boosterWt = 0;
} else {
// Get the booster motor weight
double boosterMotorWt = 0;
if (booster2Engine != null) {
boosterMotorWt = booster2Engine.getLaunchMass();
}
boosterWt = booster2LaunchWt - boosterMotorWt - booster1LaunchWt;
}
AxialStage booster = rocket.getStage(2);
booster.setMassOverridden(true);
booster.setSubcomponentsOverriddenMass(true);
booster.setOverrideMass(boosterWt);
return boosterWt;
}
/**
* Applies the CG extracted from the simulation to the sustainer, booster1, and booster2.
* @param sustainerMount the sustainer motor mount
* @param booster1Mount the booster1 motor mount
* @param booster2Mount the booster2 motor mount
* @param fcid the flight configuration ID of this simulation
*/
private void applyCGOverrides(MotorMount sustainerMount, MotorMount booster1Mount, MotorMount booster2Mount,
FlightConfigurationId fcid) {
// Don't do anything if the CG has already been overridden by a previous simulation
if (rocket.getStage(0).isCGOverridden()) {
return;
}
applySustainerCGOverride(sustainerMount, fcid);
applyBooster1CGOverride(booster1Mount, fcid);
applyBooster2CGOverride(booster2Mount, fcid);
}
/**
* Applies the CG from the RASAero simulation to the sustainer as an override.
* This CG value can be applied directly.
* @param sustainerMount the sustainer motor mount
* @param fcid the flight configuration ID of this simulation
* @return the CG of the sustainer
*/
private Double applySustainerCGOverride(MotorMount sustainerMount, FlightConfigurationId fcid) {
if (sustainerCG == null) {
return null;
}
AxialStage sustainer = rocket.getStage(0);
/*
sustainerCG is the combined CG of the sustainer and its motor (if present),
so we need to calculate the CG of the sustainer without the motor CG
*/
Double CG = getStageCGWithoutMotorCG(sustainer, sustainerCG, sustainerMount, sustainerEngine, fcid);
sustainer.setCGOverridden(true);
sustainer.setSubcomponentsOverriddenCG(true);
sustainer.setOverrideCGX(CG);
return CG;
}
/**
* Applies the CG from the RASAero simulation to booster1 as an override.
* The CG value returned by RASAero is the aggregate CG of the sustainer and booster1.
* To calculate the CG of booster1, we use the following formula:
* booster1 CG = [booster1 mass x (distance between CG of sustainer and combined CG)] / (sustainer mass)
* @param booster1Mount the booster1 motor mount
* @param fcid the flight configuration ID of this simulation
* @return the CG of booster1
*/
private Double applyBooster1CGOverride(MotorMount booster1Mount, FlightConfigurationId fcid) {
if (!includeBooster1 || booster1CG == null || sustainerCG == null || booster1LaunchWt == null || sustainerLaunchWt == null) {
return null;
}
AxialStage booster = rocket.getStage(1);
// Do a back-transform of the combined CG of the sustainer and booster1 to get the CG of booster1
Double CG = getCGFromCombinedCG(sustainerLaunchWt, booster1LaunchWt - sustainerLaunchWt, sustainerCG, booster1CG);
/*
the above CG is the combined CG of booster1 and its motor (if present),
so we need to calculate the CG of booster1 without the motor CG
*/
CG = getStageCGWithoutMotorCG(booster, CG, booster1Mount, booster1Engine, fcid);
// ! this CG is relative to the front of the sustainer, we need it referenced to the front of the booster
CG = CG - booster.getPosition().x;
booster.setCGOverridden(true);
booster.setSubcomponentsOverriddenCG(true);
booster.setOverrideCGX(CG);
return CG;
}
/**
* Applies the CG from the RASAero simulation to booster2 as an override.
* The CG value returned by RASAero is the aggregate CG of the sustainer, booster1, and booster2.
* To calculate the CG of booster2, we use the following formula:
* booster2 CG = [booster2 mass x (distance between CG of sustainer and combined CG)] / (sustainer mass + booster1 mass)
* @param booster2Mount the booster1 motor mount
* @param fcid the flight configuration ID of this simulation
* @return the CG of booster2
*/
private Double applyBooster2CGOverride(MotorMount booster2Mount, FlightConfigurationId fcid) {
if (!includeBooster2 || booster2CG == null || booster1CG == null || sustainerCG == null ||
booster2LaunchWt == null || booster1LaunchWt == null || sustainerLaunchWt == null) {
return null;
}
AxialStage booster = rocket.getStage(2);
// Do a back-transform of the combined CG of the sustainer, booster1, and booster2 to get the CG of booster2
Double CG = getCGFromCombinedCG(booster1LaunchWt, booster2LaunchWt - booster1LaunchWt, booster1CG, booster2CG);
/*
the above CG is the combined CG of booster2 and its motor (if present),
so we need to calculate the CG of booster2 without the motor CG
*/
CG = getStageCGWithoutMotorCG(booster, CG, booster2Mount, booster2Engine, fcid);
// ! this CG is relative to the front of the sustainer, we need it referenced to the front of the booster
CG = CG - booster.getPosition().x;
booster.setCGOverridden(true);
booster.setSubcomponentsOverriddenCG(true);
booster.setOverrideCGX(CG);
return CG;
}
/**
* Return the CG of object B from the CG of object A, the combined CG of A and B, and the masses of A and B.
* @param objAMass the mass of object A (known CG)
* @param objBMass the mass of object B (unknown CG)
* @param objACG the CG of object A
* @param combinedCG the combined CG of A and B
* @return the CG of object B
*/
private Double getCGFromCombinedCG(Double objAMass, Double objBMass, Double objACG, Double combinedCG) {
if (combinedCG == null || objACG == null || objAMass == null || objBMass == null) {
return combinedCG;
}
return combinedCG * (1 + objAMass / objBMass) - objACG * (objAMass / objBMass);
}
/**
* Extracts the CG of a stage from a combined CG of the stage and stage motor.
* @param stage the stage
* @param combinedCG the combiend CG of the stage and its motor
* @param mount the motor mount of the stage that holds the motor
* @param motor the motor
* @param fcid the flight configuration ID of this simulation
* @return the CG of the stage without the motor CG
*/
private Double getStageCGWithoutMotorCG(AxialStage stage, Double combinedCG, MotorMount mount, ThrustCurveMotor motor,
FlightConfigurationId fcid) {
if (motor == null || mount == null || stage == null || combinedCG == null) {
return combinedCG;
}
double stageMass = stage.getMass(); // Should be overridden by now, so don't use getSectionMass()
double motorMass = motor.getLaunchMass();
Coordinate[] CGPoints = motor.getCGPoints();
if (CGPoints != null && CGPoints.length > 1) {
double motorPositionXRel = mount.getMotorPosition(fcid).x; // Motor position relative to the mount
double mountLocationX = mount.getLocations()[0].x;
double motorLocationX = mountLocationX + motorPositionXRel; // Front location of the motor
double motorCG = motorLocationX + CGPoints[0].x;
return getCGFromCombinedCG(motorMass, stageMass, motorCG, combinedCG);
}
return combinedCG;
}
}

View File

@ -1,20 +1,10 @@
package net.sf.openrocket.file.rasaero.importt;
import net.sf.openrocket.aerodynamics.WarningSet;
import net.sf.openrocket.document.Simulation;
import net.sf.openrocket.file.DocumentLoadingContext;
import net.sf.openrocket.file.simplesax.AbstractElementHandler;
import net.sf.openrocket.file.simplesax.ElementHandler;
import net.sf.openrocket.file.simplesax.PlainTextHandler;
import net.sf.openrocket.motor.IgnitionEvent;
import net.sf.openrocket.motor.Motor;
import net.sf.openrocket.motor.MotorConfiguration;
import net.sf.openrocket.motor.ThrustCurveMotor;
import net.sf.openrocket.rocketcomponent.FlightConfigurationId;
import net.sf.openrocket.rocketcomponent.MotorMount;
import net.sf.openrocket.rocketcomponent.Rocket;
import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.rocketcomponent.StageSeparationConfiguration;
import net.sf.openrocket.simulation.SimulationOptions;
import org.xml.sax.SAXException;
@ -52,162 +42,4 @@ public class SimulationListHandler extends AbstractElementHandler {
public void endHandler(String element, HashMap<String, String> attributes, String content, WarningSet warnings) throws SAXException {
RASAeroMotorsLoader.clearAllMotors();
}
/**
* Handles RASAero simulation elements.
* We will only import the motor information from it.
*/
private static class SimulationHandler extends AbstractElementHandler {
private final DocumentLoadingContext context;
private final Rocket rocket;
private final SimulationOptions launchSiteSettings;
private final int simulationNr;
// Motor information
private ThrustCurveMotor sustainerEngine;
private Double sustainerIgnitionDelay;
private ThrustCurveMotor booster1Engine;
private Double booster1IgnitionDelay;
private Double booster1SeparationDelay;
private Boolean includeBooster1;
private ThrustCurveMotor booster2Engine;
private Double booster2SeparationDelay;
private Boolean includeBooster2;
public SimulationHandler(DocumentLoadingContext context, Rocket rocket, SimulationOptions launchSiteSettings, int simulationNr) {
this.context = context;
this.rocket = rocket;
this.launchSiteSettings = launchSiteSettings;
this.simulationNr = simulationNr;
}
@Override
public ElementHandler openElement(String element, HashMap<String, String> attributes, WarningSet warnings) throws SAXException {
if (RASAeroCommonConstants.SUSTAINER_ENGINE.equals(element) || RASAeroCommonConstants.SUSTAINER_IGNITION_DELAY.equals(element)
|| RASAeroCommonConstants.BOOSTER1_ENGINE.equals(element) || RASAeroCommonConstants.BOOSTER1_IGNITION_DELAY.equals(element)
|| RASAeroCommonConstants.BOOSTER1_SEPARATION_DELAY.equals(element) || RASAeroCommonConstants.INCLUDE_BOOSTER1.equals(element)
|| RASAeroCommonConstants.BOOSTER2_ENGINE.equals(element) || RASAeroCommonConstants.BOOSTER2_SEPARATION_DELAY.equals(element)
|| RASAeroCommonConstants.INCLUDE_BOOSTER2.equals(element)) {
return PlainTextHandler.INSTANCE;
}
return null;
}
@Override
public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings) throws SAXException {
if (RASAeroCommonConstants.SUSTAINER_ENGINE.equals(element)) {
sustainerEngine = RASAeroMotorsLoader.getMotorFromRASAero(content, warnings);
} else if (RASAeroCommonConstants.SUSTAINER_IGNITION_DELAY.equals(element)) {
sustainerIgnitionDelay = Double.parseDouble(content);
} else if (RASAeroCommonConstants.BOOSTER1_ENGINE.equals(element)) {
booster1Engine = RASAeroMotorsLoader.getMotorFromRASAero(content, warnings);
} else if (RASAeroCommonConstants.BOOSTER1_IGNITION_DELAY.equals(element)) {
booster1IgnitionDelay = Double.parseDouble(content);
} else if (RASAeroCommonConstants.BOOSTER1_SEPARATION_DELAY.equals(element)) {
booster1SeparationDelay = Double.parseDouble(content);
} else if (RASAeroCommonConstants.INCLUDE_BOOSTER1.equals(element)) {
includeBooster1 = Boolean.parseBoolean(content);
} else if (RASAeroCommonConstants.BOOSTER2_ENGINE.equals(element)) {
booster2Engine = RASAeroMotorsLoader.getMotorFromRASAero(content, warnings);
} else if (RASAeroCommonConstants.BOOSTER2_SEPARATION_DELAY.equals(element)) {
booster2SeparationDelay = Double.parseDouble(content);
} else if (RASAeroCommonConstants.INCLUDE_BOOSTER2.equals(element)) {
includeBooster2 = Boolean.parseBoolean(content);
}
}
@Override
public void endHandler(String element, HashMap<String, String> attributes, String content, WarningSet warnings) throws SAXException {
FlightConfigurationId id = new FlightConfigurationId();
rocket.createFlightConfiguration(id);
// Select if this is the first config
if (rocket.getSelectedConfiguration().getId().equals(FlightConfigurationId.DEFAULT_VALUE_FCID)) {
rocket.setSelectedConfiguration(id);
}
// Add motors to the rocket
addMotorToStage(0, sustainerEngine, sustainerIgnitionDelay, id, warnings);
if (includeBooster1) {
addMotorToStage(1, booster1Engine, booster1IgnitionDelay, id, warnings);
}
if (includeBooster2) {
addMotorToStage(2, booster2Engine, 0.0, id, warnings);
}
// Set separation settings
setSeparationDelay(0, 0.0, id);
if (includeBooster1) {
setSeparationDelay(1, booster1SeparationDelay, id);
}
if (includeBooster2) {
setSeparationDelay(2, booster2SeparationDelay, id);
}
// Add a new simulation
Simulation sim = new Simulation(rocket);
sim.setFlightConfigurationId(id);
sim.setName("Simulation " + simulationNr);
sim.copySimulationOptionsFrom(launchSiteSettings);
context.getOpenRocketDocument().addSimulation(sim);
}
private void addMotorToStage(final int stageNr, final Motor motor, final Double ignitionDelay,
final FlightConfigurationId id, final WarningSet warnings) {
if (motor == null || rocket.getStage(stageNr) == null) {
return;
}
MotorMount mount = getMotorMountForStage(stageNr);
if (mount == null) {
warnings.add("No motor mount found for stage " + stageNr + ". Ignoring motor.");
return;
}
MotorConfiguration motorConfig = new MotorConfiguration(mount, id);
motorConfig.setMotor(motor);
// RASAero requires apogee deployment, so the sustainer motor should always be plugged
if (stageNr == 0) {
motorConfig.setEjectionDelay(Motor.PLUGGED_DELAY);
}
double delay = ignitionDelay != null ? ignitionDelay : 0.0;
motorConfig.setIgnitionDelay(delay);
if (stageNr < rocket.getStageCount() - 1) { // Use burnout non-last if multi-staged rocket
motorConfig.setIgnitionEvent(IgnitionEvent.BURNOUT);
} else {
motorConfig.setIgnitionEvent(IgnitionEvent.AUTOMATIC);
}
mount.setMotorConfig(motorConfig, id);
}
private void setSeparationDelay(final int stageNr, Double separationDelay,
final FlightConfigurationId id) {
if (rocket.getStage(stageNr) == null) {
return;
}
StageSeparationConfiguration config = rocket.getStage(stageNr).getSeparationConfigurations().get(id);
if (separationDelay != null) {
config.setSeparationDelay(separationDelay);
}
config.setSeparationEvent(StageSeparationConfiguration.SeparationEvent.BURNOUT);
}
/**
* Returns the furthest back motor mount in the stage.
* @param stage stage number
* @return furthest back motor mount of the stage
*/
private MotorMount getMotorMountForStage(int stage) {
MotorMount mount = null;
for (RocketComponent component : rocket.getStage(stage)) {
if (component instanceof MotorMount) {
mount = (MotorMount) component;
}
}
if (mount != null) {
mount.setMotorMount(true);
}
return mount;
}
}
}