Skip to content
This repository was archived by the owner on Dec 2, 2024. It is now read-only.

Commit 928fdaf

Browse files
authored
Merge pull request #2 from WPI-HPRC/Development
Development
2 parents e8365f3 + 177c81c commit 928fdaf

5 files changed

Lines changed: 107 additions & 64 deletions

File tree

src/net/sf/openrocket/ORBrake/ORBrake.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
public class ORBrake extends AbstractSimulationExtension
88
{
9+
/**
10+
* GUI back end and listener instantiation.
11+
*/
912
@Override
1013
public String getName()
1114
{
@@ -21,7 +24,7 @@ public String getDescription()
2124
@Override
2225
public void initialize(SimulationConditions conditions) throws SimulationException
2326
{
24-
conditions.getSimulationListenerList().add(new ORBrakeSimulationListener(getSetpoint(), getKp(), getKi(), getKd(), getTau(), getT()));
27+
conditions.getSimulationListenerList().add(new ORBrakeSimulationListener(getSetpoint(), getKp(), getKi(), getKd(), getTau(), getCd(), getMass()));
2528
}
2629

2730
public double getSetpoint()
@@ -74,13 +77,24 @@ public void setTau(double tau)
7477
fireChangeEvent();
7578
}
7679

77-
public double getT()
80+
public double getCd()
7881
{
79-
return config.getDouble("T", 1.0);
82+
return config.getDouble("Cd", .49);
8083
}
81-
public void setT(double T)
84+
public void setCd(double Cd)
8285
{
83-
config.put("T", T);
86+
config.put("Cd", Cd);
87+
fireChangeEvent();
88+
}
89+
90+
91+
public double getMass()
92+
{
93+
return config.getDouble("Mass", 20.02);
94+
}
95+
public void setMass(double mass)
96+
{
97+
config.put("Mass", mass);
8498
fireChangeEvent();
8599
}
86100

src/net/sf/openrocket/ORBrake/ORBrakeConfigurator.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
@Plugin
1818
public class ORBrakeConfigurator extends AbstractSwingSimulationExtensionConfigurator<ORBrake> {
19+
/**
20+
* The configurator is responsible for creating the config GUI when the extension is loaded.
21+
*/
1922

2023
public ORBrakeConfigurator() {
2124
super(ORBrake.class);
@@ -94,20 +97,33 @@ protected JComponent getConfigurationComponent(ORBrake extension, Simulation sim
9497
panel.add(sliderTau, "w 75lp, wrap");
9598

9699

97-
panel.add(new JLabel("Sample Time:"));
98-
DoubleModel T = new DoubleModel(extension, "T", UnitGroup.UNITS_TIME_STEP, 0);
100+
panel.add(new JLabel("Drag Coeficient:"));
101+
DoubleModel Cd = new DoubleModel(extension, "Cd", UnitGroup.UNITS_COEFFICIENT, 0);
99102

100-
JSpinner spinT = new JSpinner(T.getSpinnerModel());
101-
spinT.setEditor(new SpinnerEditor(spinT));
102-
panel.add(spinT, "w 65lp!");
103+
JSpinner spinCd = new JSpinner(Cd.getSpinnerModel());
104+
spinCd.setEditor(new SpinnerEditor(spinCd));
105+
panel.add(spinCd, "w 65lp!");
103106

104-
UnitSelector unitT = new UnitSelector(T);
105-
panel.add(unitT, "w 25");
107+
UnitSelector unitCd = new UnitSelector(Cd);
108+
panel.add(unitCd, "w 25");
106109

107-
BasicSlider sliderT = new BasicSlider(T.getSliderModel(0, 3));
108-
panel.add(sliderT, "w 75lp, wrap");
110+
BasicSlider sliderCd = new BasicSlider(Cd.getSliderModel(0, 2));
111+
panel.add(sliderCd, "w 75lp, wrap");
109112

110113

114+
panel.add(new JLabel("Estimate Mass:"));
115+
DoubleModel Mass = new DoubleModel(extension, "Mass", UnitGroup.UNITS_COEFFICIENT, 0);
116+
117+
JSpinner spinMass = new JSpinner(Mass.getSpinnerModel());
118+
spinMass.setEditor(new SpinnerEditor(spinMass));
119+
panel.add(spinMass, "w 65lp!");
120+
121+
UnitSelector unitMass = new UnitSelector(Mass);
122+
panel.add(unitMass, "w 25");
123+
124+
BasicSlider sliderMass = new BasicSlider(Mass.getSliderModel(0, 40));
125+
panel.add(sliderMass, "w 75lp, wrap");
126+
111127
return panel;
112128
}
113129

src/net/sf/openrocket/ORBrake/ORBrakeProvider.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77

88
@Plugin
99
public class ORBrakeProvider extends AbstractSimulationExtensionProvider {
10+
/**
11+
* Lists the plugin in the simulation options.
12+
*/
1013

1114
public ORBrakeProvider() {
12-
super(ORBrake.class, "Flight", "OR Brake");
15+
super(ORBrake.class, "WPI", "OR Brake");
1316
}
1417

1518
}

src/net/sf/openrocket/ORBrake/ORBrakeSimulationListener.java

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,29 @@
88
import net.sf.openrocket.aerodynamics.FlightConditions;
99

1010
public 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)

src/net/sf/openrocket/ORBrake/ORBrakeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ORBrakeTest {
1414

1515
@BeforeAll
1616
static void setUpBeforeClass() throws Exception {
17-
listener = new ORBrakeSimulationListener(2000.0, 10.0, 0.0, 0.0, 1.0, .5);
17+
listener = new ORBrakeSimulationListener(2000.0, 10.0, 0.0, 0.0, 1.0, 10, .4);
1818
}
1919

2020
// @AfterAll

0 commit comments

Comments
 (0)