00001
00002
00008
00009
00011
00012 #include "Matrix.h"
00013 #include "RungeKuttaIntegrator.h"
00014 #include "LinearInterpolator.h"
00015 #include "Plot.h"
00016 #include "Attitude.h"
00017 #include "AttitudeState.h"
00018 #include <vector.h>
00019 using namespace std;
00020
00021
00022
00023 Vector NullFunctor(const ssfTime& _pSSFTime, const OrbitState& _pOrbitState, const AttitudeState& _pAttitudeState)
00024 {
00025 return Vector(3);
00026 }
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00056 static Vector AttituteDynamics(const ssfTime &_time, const Vector& _integratingState, Orbit *_pOrbit, Attitude *_pAttitude, const Matrix &_parameters, const Functor &_forceFunctorPtr)
00057 {
00058
00059
00060
00061 static Vector stateDot(7);
00062 static Matrix bMOI(3,3);
00063 static Matrix qtemp(4,3);
00064 static Matrix Tmoment(3,1);
00065 static Vector qIn(4);
00066 static Vector wIn(3);
00067
00068
00069 qIn = _integratingState(_(VectorIndexBase,VectorIndexBase + 3));
00070 wIn = _integratingState(_(VectorIndexBase + 4,VectorIndexBase + 6));
00071 qIn /= norm2(qIn);
00072
00073
00074 qtemp(_(VectorIndexBase+0,VectorIndexBase+2),_(VectorIndexBase+0,VectorIndexBase+2)) = skew(qIn(_(VectorIndexBase+0,VectorIndexBase+2))) + qIn(VectorIndexBase+3) * eye(3);
00075 qtemp(VectorIndexBase+3, _(VectorIndexBase+0,VectorIndexBase+2)) = -(~qIn(_(VectorIndexBase+0,VectorIndexBase+2)));
00076 qtemp(_,MatrixIndexBase) = 0.5 * qtemp * wIn;
00077
00078
00079 bMOI = _parameters(_(MatrixIndexBase+0,MatrixIndexBase+2),_(MatrixIndexBase+0,MatrixIndexBase+2));
00080 Tmoment = (bMOI.inverse() * (Tmoment - skew(wIn) * (bMOI * wIn)));
00081
00082
00083 stateDot(_(VectorIndexBase,VectorIndexBase + 3)) = qtemp(_,MatrixIndexBase);
00084 stateDot(_(VectorIndexBase + 4,VectorIndexBase + 6)) = Tmoment(_,MatrixIndexBase);
00085
00086 cout << 0.5 * ~wIn * (bMOI * wIn);
00087 return stateDot;
00088 }
00089
00090 int main()
00091 {
00093
00094 RungeKuttaIntegrator myIntegrator;
00095 myIntegrator.SetNumSteps(1000);
00096
00097 vector<ssfTime> integrationTimes;
00098 ssfTime begin(0);
00099 ssfTime end(begin + 20);
00100 integrationTimes.push_back(begin);
00101 integrationTimes.push_back(end);
00102
00103
00104 AttitudeState myAttitudeState;
00105 myAttitudeState.SetRotation(Rotation(Quaternion(0,0,0,1)));
00106 Vector initAngVelVector(3);
00107 initAngVelVector(1) = 0.1;
00108 myAttitudeState.SetAngularVelocity(initAngVelVector);
00109 SpecificFunctor AttitudeForcesFunctor(&NullFunctor);
00110
00111
00112 Matrix Parameters = eye(3);
00113
00114 Matrix I(3,3);
00115 I(1,1) = 100;
00116 I(2,2) = 200;
00117 I(3,3) = 150;
00118 Vector h(3);
00119 h(1) = 10;
00120 h(2) = 20;
00121 h(3) = 30;
00122
00123 Vector w = I.inverse() * h;
00124 Matrix Tmat = 0.5 * (~w * (I * w));
00125 Vector initEugeneState(4);
00126 initEugeneState(_(1,3)) = h(_(1,3));
00127 initEugeneState(4) = Tmat(1,1);
00128
00129
00130 cout << "PropTime = " << begin.GetSeconds() << " s -> " << end.GetSeconds() << " s" << endl;
00131 cout << "Attitude State: " << ~myAttitudeState.GetState() << endl;
00133
00134 tick();
00135 Matrix history = myIntegrator.Integrate(
00136 integrationTimes,
00137 &AttituteDynamics,
00138 myAttitudeState.GetState(),
00139 NULL,
00140 NULL,
00141 Parameters,
00142 AttitudeForcesFunctor
00143 );
00144
00145 cout << "finished propagating in " << tock() << " seconds." << endl;
00146
00147
00148 Matrix plotting = history(_,_(MatrixIndexBase,MatrixIndexBase+4));
00149
00150
00151
00153
00154 AttitudeHistory myAttHistory;
00155 myAttHistory.SetInterpolator(new LinearInterpolator);
00156 Vector AngVel(3);
00157 Vector Quat(4);
00158 Rotation myRot;
00159 for(int jj = 1;jj <= history[MatrixRowsIndex].getIndexBound(); ++jj)
00160 {
00161 AngVel = ~(history(jj, _(6,8)));
00162 Quat = ~(history(jj,_(2,5)));
00163 myRot.Set(Quaternion(Quat));
00164 myAttHistory.AppendHistory(history(jj,1), myRot, AngVel);
00165 }
00166
00168
00169 double requestTime;
00170 cout << "Output attitude state at time (s): " << flush;
00171 cin >> requestTime;
00172 ssfTime myTime(requestTime);
00173 myAttHistory.GetState(myTime, myRot, AngVel);
00174 cout << myTime.GetSeconds() << " : " << ~myRot.GetQuaternion() << endl;
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00189
00190 ofstream ofile;
00191 ofile.open("AttitudeHistory.dat");
00192 ofile << history;
00193 ofile.close();
00194
00195 return 0;
00196
00197 }
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219