refactor to use non-deprecated component iterator

This commit is contained in:
thzero 2022-07-03 08:30:51 -05:00
parent a49117f909
commit 26019d48d3

View File

@ -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);
}
}