00001
00002
00008
00010
00011 #include "Matrix.h"
00012 #include "Rotation.h"
00013 #include "Attitude.h"
00014 #include "Orbit.h"
00015 #include "CombinedNumericPropagator.h"
00016 #include "RungeKuttaFehlbergIntegrator.h"
00017 #include "orbitmodels/TwoBodyDynamics.h"
00018 #include "CentralBody/EarthCentralBody.h"
00019 #include "OrbitState.h"
00020 #include "AttitudeState.h"
00021 #include "orbitstaterep/PositionVelocity.h"
00022 #include "orbitframes/OrbitFrameIJK.h"
00023 #include "Plot.h"
00024 #include <string>
00025 #include <stdlib.h>
00026 #include <sstream>
00027 using namespace O_SESSAME;
00028
00029
00030 static Orbit* myOrbit = NULL;
00031 static Attitude* myAttitude = NULL;
00032 static Environment* myEnvironment = NULL;
00033 static CombinedNumericPropagator* myPropagator = NULL;
00034 static RungeKuttaFehlbergIntegrator* orbitIntegrator = NULL;
00035 static RungeKuttaFehlbergIntegrator* attitudeIntegrator = NULL;
00036 static vector<ssfTime> propTimes;
00037
00038 void SetupPropagator();
00039 void SetupEnvironment();
00040 void SetupOrbit();
00041 void SetupAttitude();
00042
00043 void ChangePropagator();
00044 void ChangeEnvironment();
00045 void ChangeOrbit();
00046 void ChangeOrbitIntegrator();
00047 void ChangeAttitude();
00048 void ChangeAttitudeIntegrator();
00049
00050 void Propagate();
00051 void Plot();
00052
00053 void myOrbitStateConvFunc(const Matrix &_meshPoint, OrbitState &_convertedOrbitState);
00054 void myAttitudeStateConvFunc(const Matrix &_meshPoint, AttitudeState &_convertedAttitudeState);
00055
00056 void DisplayOrbit()
00057 {
00058 if(myOrbit->IsIntegrateable())
00059 cout << "Orbit State (km, km/s): " << ~myOrbit->GetStateObject().GetStateRepresentation()->GetPositionVelocity() << endl;
00060 else
00061 cout << "Orbit State: [ uninitialized ]" << endl;
00062 return;
00063 }
00064
00065 void DisplayAttitude()
00066 {
00067 if(myAttitude->IsIntegrateable())
00068 cout << "Attitude State: (-, rad/s)" << ~myAttitude->GetStateObject().GetState() << endl;
00069 else
00070 cout << "Attitude State: [ uninitialized ]" << endl;
00071 return;
00072 }
00073 void DisplayPropagator()
00074 {
00075 if(myPropagator)
00076 cout << "Propagate times: [" << propTimes[0] << " -> " << propTimes[1] << "] (s)" << endl;
00077 else
00078 cout << "Propagate State: [ uninitialized ]" << endl;
00079 return;
00080 }
00081
00082 int main()
00083 {
00084 SetupOrbit();
00085 SetupAttitude();
00086 SetupPropagator();
00087 myOrbit->SetPropagator(myPropagator);
00088 myAttitude->SetPropagator(myPropagator);
00089
00090 SetupEnvironment();
00091 myOrbit->SetEnvironment(myEnvironment);
00092 myAttitude->SetEnvironment(myEnvironment);
00093
00094 ChangeOrbit();
00095 ChangeEnvironment();
00096 ChangePropagator();
00097 Propagate();
00098
00099 ofstream ofile;
00100
00101 if(myOrbit)
00102 if (myOrbit->IsIntegrateable())
00103 {
00104 ofile.open("OrbitHistory.dat");
00105 ofile << setprecision(15) << myOrbit->GetHistoryObject().GetHistory();
00106 ofile.close();
00107 }
00108
00109 if(myAttitude)
00110 if (myAttitude->IsIntegrateable())
00111 {
00112 ofile.open("AttitudeHistory.dat");
00113 ofile << setprecision(15) << myAttitude->GetHistoryObject().GetHistory();
00114 ofile.close();
00115 }
00116 return 0;
00117
00118 }
00119
00120 void Plot()
00121 {
00122 if(myOrbit)
00123 if (myOrbit->IsIntegrateable())
00124 {
00125 Matrix orbitHistory = myOrbit->GetHistoryObject().GetHistory();
00126 Matrix orbitPlotting = orbitHistory(_,_(MatrixIndexBase+1,MatrixIndexBase+3));
00127 Plot3D(orbitPlotting);
00128 }
00129 if(myAttitude)
00130 if (myAttitude->IsIntegrateable())
00131 {
00132 Matrix attitudeHistory = myAttitude->GetHistoryObject().GetHistory();
00133 Matrix attitudePlotting = attitudeHistory(_,_(MatrixIndexBase,MatrixIndexBase+4));
00134 Plot2D(attitudePlotting);
00135 }
00136
00137 }
00138
00139 void Propagate()
00140 {
00141
00142 cout << "PropTime = " << propTimes[0].GetSeconds() << " s -> " << propTimes[1].GetSeconds() << " s" << endl;
00143 Vector orbInitState(6);
00144 Vector attInitState(7);
00145 if(myOrbit->IsIntegrateable())
00146 {
00147 orbInitState = myOrbit->GetStateObject().GetState();
00148 cout << "Orbit State: " << ~orbInitState << endl;
00149 }
00150 if(myAttitude->IsIntegrateable())
00151 {
00152 attInitState = myAttitude->GetStateObject().GetState();
00153 cout << "Attitude State: " << ~attInitState << endl;
00154 }
00155
00156 tick();
00157 myPropagator->Propagate(propTimes, orbInitState, attInitState);
00158 ssfSeconds calcTime = tock();
00159 cout << "finished propagating in " << calcTime << " seconds." << endl;
00160 }
00161
00162
00163
00164
00165 Vector GravityForceFunction(const ssfTime &_currentTime, const OrbitState &_currentOrbitState, const AttitudeState &_currentAttitudeState, const EnvFuncParamaterType &_parameterList)
00166 {
00167
00168 static Vector Forces(3);
00169
00170 static Vector Position(3); Position(_) = _currentOrbitState.GetState()(_(VectorIndexBase,VectorIndexBase+2));
00171 Forces = -398600.441 / pow(norm2(Position),3) * Position;
00172 return Forces;
00173 }
00174
00175 Vector DragForceFunction(const ssfTime &_currentTime, const OrbitState &_currentOrbitState, const AttitudeState &_currentAttitudeState, const EnvFuncParamaterType &_parameterList)
00176 {
00177 static Vector Forces(3);
00178 double BC = *(reinterpret_cast<double*>(_parameterList[0]));
00179 static Vector Vrel(3); Vrel(_) = _currentOrbitState.GetState()(_(VectorIndexBase+3,VectorIndexBase+5));
00180 double Vrel_mag = norm2(Vrel);
00181 double rho = *(reinterpret_cast<double*>(_parameterList[1]));
00182 Forces = -1/2 * rho / BC * pow(Vrel_mag,2) * Vrel / Vrel_mag;
00183 return Forces;
00184 }
00185
00186 void SetupEnvironment()
00187 {
00188
00189 myEnvironment = new Environment;
00190 EarthCentralBody *pCBEarth = new EarthCentralBody;
00191 myEnvironment->SetCentralBody(pCBEarth);
00192
00193 return;
00194 }
00195 void ChangeEnvironment()
00196 {
00197
00198 cout << "Filling Parameters" << endl;
00199 EnvFunction TwoBodyGravity(&GravityForceFunction);
00200 myEnvironment->AddForceFunction(TwoBodyGravity);
00201
00202
00203 return;
00204 }
00205
00206
00207
00208
00209 void SetupPropagator()
00210 {
00211 myPropagator = new CombinedNumericPropagator;
00212 orbitIntegrator = new RungeKuttaFehlbergIntegrator;
00213 attitudeIntegrator = new RungeKuttaFehlbergIntegrator;
00214
00215 myPropagator->SetOrbitIntegrator(orbitIntegrator);
00216 myPropagator->SetAttitudeIntegrator(attitudeIntegrator);
00217
00218
00219 double intTime = 1;
00220 ssfTime begin(0);
00221 ssfTime end(begin + intTime);
00222 propTimes.push_back(begin);
00223 propTimes.push_back(end);
00224 return;
00225 }
00226
00227 void ChangePropagator()
00228 {
00229 DisplayPropagator();
00230
00231
00232
00233 int stepSize = 1;
00234
00235
00236 double intTime = 86400;
00237
00238 double tolerance;
00239 orbitIntegrator->SetStepSizes(1, 200);
00240 tolerance = 0.000001;
00241 orbitIntegrator->SetTolerance(tolerance);
00242 attitudeIntegrator->SetStepSizes(0.01, 5);
00243 attitudeIntegrator->SetTolerance(tolerance);
00244 ssfTime begin(0);
00245 ssfTime end(begin + intTime);
00246 propTimes[0] = begin;
00247 propTimes[1] = end;
00248 }
00249
00250
00251
00252 void myOrbitStateConvFunc(const Matrix &_meshPoint, OrbitState &_convertedOrbitState)
00253 {
00254 static Vector tempVector(_meshPoint[MatrixColsIndex].getIndexBound() - 1);
00255 tempVector(_) = ~_meshPoint(_,_(MatrixIndexBase+1, _meshPoint[MatrixColsIndex].getIndexBound()));
00256 _convertedOrbitState.SetState(tempVector);
00257 return;
00258 }
00259
00260 void ChangeOrbit()
00261 {
00262 DisplayOrbit();
00263
00264
00265 OrbitState myOrbitState;
00266 myOrbitState.SetStateRepresentation(new PositionVelocity);
00267 myOrbitState.SetOrbitFrame(new OrbitFrameIJK);
00268 Vector initPV(6);
00269
00270 initPV(VectorIndexBase+0) = -2216.054359;
00271 initPV(VectorIndexBase+1) = -2837.094126;
00272 initPV(VectorIndexBase+2) = -6235.382907;
00273 initPV(VectorIndexBase+3) = 6.938058;
00274 initPV(VectorIndexBase+4) = -5.419317;
00275 initPV(VectorIndexBase+5) = 0;
00276
00277 myOrbitState.SetState(initPV);
00278 myOrbit->SetStateObject(myOrbitState);
00279
00280
00281 myOrbit->SetDynamicsEq(&TwoBodyDynamics);
00282 myOrbit->SetStateConversion(&myOrbitStateConvFunc);
00283
00284
00285 return;
00286 }
00287 void SetupOrbit()
00288 {
00289 myOrbit = new Orbit;
00290 return;
00291 }
00292
00293
00294
00295
00296
00297 void myAttitudeStateConvFunc(const Matrix &_meshPoint, AttitudeState &_convertedAttitudeState)
00298 {
00299 static Vector tempQ(4); tempQ(_) = ~_meshPoint(_,_(2, 5));
00300 static Vector tempVector(3); tempVector(_) = ~_meshPoint(1, _(6, 8));
00301 _convertedAttitudeState.SetState(Rotation(Quaternion(tempQ)), tempVector);
00302 return;
00303 }
00304 static Vector AttituteDynamics(const ssfTime &_time, const Vector& _integratingState, Orbit *_Orbit, Attitude *_Attitude, const Matrix &_parameters, const Functor &_forceFunctorPtr)
00305 {
00306 static Vector stateDot(7);
00307 static Matrix bMOI(3,3);
00308 static Matrix qtemp(4,3);
00309 static Matrix Tmoment(3,1);
00310 static Vector qIn(4);
00311 static Vector wIn(3);
00312 qIn = _integratingState(_(VectorIndexBase,VectorIndexBase + 3));
00313 wIn = _integratingState(_(VectorIndexBase + 4,VectorIndexBase + 6));
00314 qIn /= norm2(qIn);
00315
00316
00317 qtemp(_(VectorIndexBase+0,VectorIndexBase+2),_(VectorIndexBase+0,VectorIndexBase+2)) = skew(qIn(_(VectorIndexBase+0,VectorIndexBase+2))) + qIn(VectorIndexBase+3) * eye(3);
00318 qtemp(VectorIndexBase+3, _(VectorIndexBase+0,VectorIndexBase+2)) = -(~qIn(_(VectorIndexBase+0,VectorIndexBase+2)));
00319 qtemp(_,MatrixIndexBase) = 0.5 * qtemp * wIn;
00320
00321 bMOI = _parameters(_(MatrixIndexBase+0,MatrixIndexBase+2),_(MatrixIndexBase+0,MatrixIndexBase+2));
00322 Tmoment(_,_) = (_forceFunctorPtr.Call(_time, _Orbit->GetStateObject(), _Attitude->GetStateObject()))(_);
00323 Tmoment = (bMOI.inverse() * (Tmoment - skew(wIn) * (bMOI * wIn)));
00324
00325 stateDot(_(VectorIndexBase,VectorIndexBase + 3)) = qtemp(_,MatrixIndexBase);
00326 stateDot(_(VectorIndexBase + 4,VectorIndexBase + 6)) = Tmoment(_,MatrixIndexBase);
00327
00328 return stateDot;
00329 }
00330
00331 void ChangeAttitude()
00332 {
00333 DisplayAttitude();
00334 double q1,q2,q3,q4;
00335 cout << "Enter Attitude quaternion: " << flush;
00336 cin >> q1 >> q2 >> q3 >> q4;
00337
00338 AttitudeState myAttitudeState;
00339 myAttitudeState.SetRotation(Rotation(Quaternion(q1,q2,q3,q4)));
00340
00341 cout << "Enter Attitude angular velocity (rad/s): " << flush;
00342 cin >> q1 >> q2 >> q3;
00343 Vector initAngVelVector(3);
00344 initAngVelVector(1) = q1;
00345 initAngVelVector(2) = q2;
00346 initAngVelVector(3) = q3;
00347
00348 myAttitudeState.SetAngularVelocity(initAngVelVector);
00349
00350 myAttitude->SetStateObject(myAttitudeState);
00351 myAttitude->SetDynamicsEq(&AttituteDynamics);
00352 myAttitude->SetStateConversion(&myAttitudeStateConvFunc);
00353 myAttitude->SetParameters(eye(3));
00354
00355 return;
00356 }
00357 void SetupAttitude()
00358 {
00359 myAttitude = new Attitude;
00360
00361 return;
00362
00363 }
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380