88import net .sf .openrocket .aerodynamics .FlightConditions ;
99
1010public class ORBrakeSimulationListener extends AbstractSimulationListener {
11+ /**
12+ * The simulation listener connects to and influences simulations that is
13+ * attached to.
14+ */
1115
12- // double velocity;
13- // double altitude;
14- // double thrust;
15- double setpoint ; //desired altitude in feet
16-
16+
1717 // Input parameters for PID controller
18- double Kp ; //proportional gain constant
19- double Ki ; //integral gain constant
20- double Kd ; //derivative gain constant
21- double tau ; //low pass filter time constant
22- // double min_inte = 1; //integral min limit
23- // double max_inte = 5; //integral max limit
24- double T ; //sample time in sec
18+ double setpoint ; // Target altitude in feet
19+ double Kp ; // Proportional gain constant
20+ double Ki ; // Integral gain constant
21+ double Kd ; // Derivative gain constant
22+ double tau ; // Low pass filter time constant
23+ double T = .05 ; // Sample time in sec
24+
25+ // Input parameters for apogee estimator
26+ double Cd ;
27+ double mass ;
2528
2629 // Memory variables for PID controller
27- double inte = 0 ; //integral term
28- double prev_err = 0 ; //previous error
29- double diff = 0 ; //differential term
30- double prev_measure = 0 ; //previous measurement
30+ double inte = 0 ; // Integral term
31+ double prev_err = 0 ; // Previous error
32+ double diff = 0 ; // Differential term
33+ double prev_measure = 0 ; // Previous measurement
3134
3235 private static final double surfConst [][] = { // Surface constants for presimulated airbrake extensions.
3336 {-0.000000000 , 0.000000000 , -0.000000000 , 0.0000000000 , 0.000000000 }, // 0 %
@@ -38,30 +41,28 @@ public class ORBrakeSimulationListener extends AbstractSimulationListener {
3841 {-1.161195104 , -0.001690272 , -0.003398721 , 0.0000376809 , 0.002936851 } // 100%
3942 };
4043
41- public ORBrakeSimulationListener (double setpoint , double Kp , double Ki , double Kd , double tau , double T ) {
44+ public ORBrakeSimulationListener (double setpoint , double Kp , double Ki , double Kd , double tau , double Cd , double mass ) {
4245 super ();
4346 this .setpoint = setpoint ;
4447 this .Kp = Kp ;
4548 this .Ki = Ki ;
4649 this .Kd = Kd ;
4750 this .tau = tau ;
48- this .T = T ;
51+ this .Cd = Cd ;
52+ this .mass = mass ;
4953 }
5054
5155 @ Override
52- public FlightConditions postFlightConditions (SimulationStatus status , FlightConditions conditions )
53- /**
54- * Called immediately after the flight conditions of the current time
55- * step so that relevant ones can be extracted.
56- *
57- * @param status Object that contains simulation status details.
58- * @param conditions Object that contains flight condition details.
59- * @return null.
60- */
61- {
62- // velocity = conditions.getVelocity();
63- return null ;
64- }
56+ public void startSimulation (SimulationStatus status )
57+ /**
58+ * Gets the time step at the start of the simulation.
59+ *
60+ * @param status The status object at the start of the sim.
61+ * @return void
62+ */
63+ {
64+ T = status .getSimulationConditions ().getTimeStep ();
65+ }
6566
6667 @ Override
6768 public double postSimpleThrustCalculation (SimulationStatus status , double thrust ) // throws SimulationException
@@ -74,16 +75,13 @@ public double postSimpleThrustCalculation(SimulationStatus status, double thrust
7475 * @return The modified thrust to be actually applied.
7576 */
7677 {
77- // if (status.getSimulationTime() )
78- // status.getRocketVelocity().normalize();
79- // this.thrust = thrust;
80- // this.altitude = status.getRocketPosition().z;
81- return thrust + airbrakeForce (status , thrust );
78+ double drag = airbrakeForce (status , thrust );
79+ return thrust + drag ;
8280 }
8381
8482 double airbrakeForce (SimulationStatus status , double thrust )
8583 {
86- double requiredDrag = requiredDrag (setpoint , status , thrust );
84+ double requiredDrag = requiredDrag (status , thrust );
8785 double surf = dragSurface (5 , status .getRocketPosition ().z , status .getRocketVelocity ().length ());
8886 if (requiredDrag > surf ) {
8987 requiredDrag = surf ;
@@ -93,33 +91,45 @@ public double postSimpleThrustCalculation(SimulationStatus status, double thrust
9391 return -requiredDrag ;
9492 }
9593
96- double requiredDrag (double SP , SimulationStatus status , double thrust ) //PID controller to get updated drag coefficient
94+ double requiredDrag (SimulationStatus status , double thrust ) //PID controller to get updated drag coefficient
9795 /**
98- * SP = desired altitude setpoint
99- * measure = actual altitude
96+ * Computes required drag using a PID controller.
97+ *
98+ * @param status The current simulation status object.
99+ * @param thrust The current thrust of the vehicle.
100100 */
101101 {
102102 // Initial conditions
103103 double out = 0 ;
104- double measure = status .getRocketPosition ().z ;
104+ double alt = status .getRocketPosition ().z ;
105105 double velocity = status .getRocketVelocity ().length ();
106+ double vertVelocity = status .getRocketVelocity ().z ;
106107
108+ // double mass = status.getSimulationConditions().getRocket().getMass();
109+ // double Cd = status.getSimulationConditions().getAerodynamicCalculator().getAerodynamicForces().getCD();
110+ double gravity = status .getSimulationConditions ().getGravityModel ().getGravity (status .getRocketWorldPosition ());
111+ double refArea = status .getConfiguration ().getReferenceArea ();
112+
113+ double termVelocity = Math .sqrt ((2 *mass *gravity )/( Cd * refArea *1.225 ));
114+ double predApogee = alt +(((Math .pow (termVelocity ,2 ))/(2 *gravity ))* Math .log ((Math .pow (vertVelocity ,2 )+Math .pow (termVelocity ,2 ))/(Math .pow (termVelocity ,2 ))));
115+
116+ // PID Controller
107117 if (thrust == 0 )
108118 {
109119 // Error function
110- double err = SP - measure ;
120+ double err = setpoint - predApogee ;
111121
112122 // Proportional term
113- double prop = Kp *err ;
123+ double prop = - Kp *err ;
114124
115125 // Integral term
116126 inte += 0.5 *Ki *T *(err +prev_err );
117127
118128 // Anti-wind up (dynamic integral clamping)
119129 double min_inte ; //integral min limit
120130 double max_inte ; //integral max limit
121- if (dragSurface (5 , measure , velocity ) > prop ) {
122- max_inte = dragSurface (5 , measure , velocity ) - prop ;
131+ if (dragSurface (5 , predApogee , velocity ) > prop ) {
132+ max_inte = dragSurface (5 , predApogee , velocity ) - prop ;
123133 } else {
124134 max_inte = 0 ;
125135 }
@@ -135,17 +145,17 @@ public double postSimpleThrustCalculation(SimulationStatus status, double thrust
135145 }
136146
137147 // Differential term
138- diff = ( -2 *Kd *(measure - prev_measure ) + (2 *tau -T )*diff ) / (2 *tau +T );
148+ diff = ( -2 *Kd *(predApogee - prev_measure ) + (2 *tau -T )*diff ) / (2 *tau +T );
139149
140150 // Output
141151 out = prop + inte + diff ;
142152
143153 // Update memory
144154 prev_err = err ;
145- prev_measure = measure ;
155+ prev_measure = predApogee ;
146156 }
147157
148- return out ; //required drag
158+ return out ;
149159 }
150160
151161// double extensionFromDrag(double requiredDrag)
0 commit comments