diff --git a/core/src/net/sf/openrocket/simulation/listeners/example/RollControlListener.java b/core/src/net/sf/openrocket/simulation/listeners/example/RollControlListener.java index 55a557d70..e4736d85b 100644 --- a/core/src/net/sf/openrocket/simulation/listeners/example/RollControlListener.java +++ b/core/src/net/sf/openrocket/simulation/listeners/example/RollControlListener.java @@ -35,8 +35,7 @@ public class RollControlListener extends AbstractSimulationListener { // Maximum control fin angle (rad) private static final double MAX_ANGLE = 15 * Math.PI / 180; - - + /* * PID parameters * @@ -47,29 +46,22 @@ public class RollControlListener extends AbstractSimulationListener { private static final double KP = 0.007; private static final double KI = 0.2; - - - - private double rollrate; + private double rollRate; private double prevTime = 0; private double intState = 0; private double finPosition = 0; - - - + @Override public FlightConditions postFlightConditions(SimulationStatus status, FlightConditions flightConditions) { // Store the current roll rate for later use - rollrate = flightConditions.getRollRate(); + rollRate = flightConditions.getRollRate(); return null; } - - + @Override public void postStep(SimulationStatus status) throws SimulationException { - // Activate PID controller only after a specific time if (status.getSimulationTime() < START_TIME) { prevTime = status.getSimulationTime(); @@ -78,7 +70,7 @@ public class RollControlListener extends AbstractSimulationListener { // Find the fin set named CONTROL FinSet finset = null; - for (RocketComponent c : status.getConfiguration().getActiveComponents()) { + for (RocketComponent c : status.getConfiguration().getAllComponents()) { if ((c instanceof FinSet) && (c.getName().equals(CONTROL_FIN_NAME))) { finset = (FinSet) c; break; @@ -87,23 +79,20 @@ public class RollControlListener extends AbstractSimulationListener { if (finset == null) { throw new SimulationException("A fin set with name '" + CONTROL_FIN_NAME + "' was not found"); } - - + // Determine time step double deltaT = status.getSimulationTime() - prevTime; prevTime = status.getSimulationTime(); - - + // PID controller - double error = SETPOINT - rollrate; + double error = SETPOINT - rollRate; double p = KP * error; intState += error * deltaT; double i = KI * intState; double value = p + i; - - + // Clamp the fin angle between -MAX_ANGLE and MAX_ANGLE if (Math.abs(value) > MAX_ANGLE) { System.err.printf("Attempting to set angle %.1f at t=%.3f, clamping.\n", @@ -111,7 +100,6 @@ public class RollControlListener extends AbstractSimulationListener { value = MathUtil.clamp(value, -MAX_ANGLE, MAX_ANGLE); } - // Limit the fin turn rate if (finPosition < value) { finPosition = Math.min(finPosition + TURNRATE * deltaT, value); @@ -122,6 +110,5 @@ public class RollControlListener extends AbstractSimulationListener { // Set the control fin cant and store the data finset.setCantAngle(finPosition); status.getFlightData().setValue(FIN_CANT_TYPE, finPosition); - } }