00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 #include "HokieSatSimulation.h"
00013 
00014 
00015 
00016 
00017 
00018 
00019 int main()
00020 {
00021     Orbit* pHokiesatOrbit = SetupOrbit();
00022     Attitude* pHokiesatAttitude = SetupAttitude();
00023     
00024     
00025         NumericPropagator* pHokiesatPropagator = SetupPropagator();
00026         pHokiesatOrbit->SetPropagator(pHokiesatPropagator);
00027         pHokiesatAttitude->SetPropagator(pHokiesatPropagator);
00028 
00029     
00030         Environment* pEarthEnv = SetupEnvironment(pHokiesatAttitude);
00031         pHokiesatOrbit->SetEnvironment(pEarthEnv);
00032         pHokiesatAttitude->SetEnvironment(pEarthEnv);
00033 
00034     
00035         double propTime = 20; 
00036             cout << "Propagation time (mins): " << flush;
00037             cin >> propTime;
00038         double propStep = 60; 
00039             cout << "Propagation step (secs): " << flush;
00040             cin >> propStep;
00041         vector<ssfTime> integrationTimes;
00042         ssfTime begin(0);
00043         ssfTime end(begin + propStep);
00044         integrationTimes.push_back(begin);
00045         integrationTimes.push_back(end);
00046         
00047     
00048         cout << "PropTime = " << begin.GetSeconds() << " s -> " << end.GetSeconds() << " s" << endl;
00049         cout << "Orbit State: " << ~pHokiesatOrbit->GetStateObject().GetStateRepresentation()->GetPositionVelocity();
00050         cout << "Attitude State: " << ~pHokiesatAttitude->GetStateObject().GetState() << endl;
00051 
00052     
00053         tick();
00054         pHokiesatPropagator->Propagate(integrationTimes, pHokiesatOrbit->GetStateObject().GetStateRepresentation()->GetPositionVelocity(), pHokiesatAttitude->GetStateObject().GetState());                            
00055 
00056         for (int ii = 0; ii < propTime*60/propStep ; ++ii)
00057         {
00058             
00059             integrationTimes[0] = integrationTimes[1];
00060             integrationTimes[1] = integrationTimes[0] + propStep;
00061             
00062             pHokiesatPropagator->Propagate(integrationTimes, pHokiesatOrbit->GetStateObject().GetStateRepresentation()->GetPositionVelocity(), pHokiesatAttitude->GetStateObject().GetState());
00063             if((ii % 100) < 5)
00064                 cout << integrationTimes[0].GetSeconds() << ", ";
00065         }
00066         cout << endl;
00067         ssfSeconds calcTime = tock();
00068         cout << "finished propagating " << propTime*60 << " sim-seconds in " << calcTime << " real-seconds." << endl;     
00069     
00070         Matrix orbitHistory = pHokiesatOrbit->GetHistoryObject().GetHistory();
00071         Matrix orbitPlotting = orbitHistory(_,_(MatrixIndexBase+1,MatrixIndexBase+3));
00072         Matrix attitudeHistory = pHokiesatAttitude->GetHistoryObject().GetHistory();
00073         Matrix attitudePlotting = attitudeHistory(_,_(MatrixIndexBase,MatrixIndexBase+4));
00074 
00075         Plot3D(orbitPlotting);
00076         Plot2D(attitudePlotting);
00077    
00078     
00079         ofstream ofile;
00080         ofile.open("OrbitHistory.dat");
00081         ofile << pHokiesatOrbit->GetHistoryObject().GetHistory();
00082         ofile.close();
00083     
00084         ofile.open("AttitudeHistory.dat");
00085         ofile << pHokiesatAttitude->GetHistoryObject().GetHistory();
00086         ofile.close();
00087 
00088     return 0;
00089                             
00090 }
00091 
00092 
00093 
00094 
00095 Environment* SetupEnvironment(Attitude* pSatAttitude)
00096 {
00097     
00098     Environment* pEarthEnv = new Environment;
00099     EarthCentralBody *pCBEarth = new EarthCentralBody;
00100     pEarthEnv->SetCentralBody(pCBEarth);
00101     
00102     
00103         EnvFunction TwoBodyGravity(&GravityForceFunction);
00104         double *mu = new double(pCBEarth->GetGravitationalParameter());
00105         TwoBodyGravity.AddParameter(reinterpret_cast<void*>(mu), 1);
00106         pEarthEnv->AddForceFunction(TwoBodyGravity);
00107     
00108     cout << "Add Drag? (y or n): " << flush;
00109     char answer;
00110     cin >> answer;
00111     if(answer == 'y')
00112     {
00113         
00114         EnvFunction DragForce(&SimpleAerodynamicDragForce);
00115         double *BC = new double(2);
00116         DragForce.AddParameter(reinterpret_cast<void*>(BC), 1);
00117         double *rho = new double(1.13 * pow(static_cast<double>(10), static_cast<double>(-12))); 
00118         DragForce.AddParameter(reinterpret_cast<void*>(rho), 2);
00119         pEarthEnv->AddForceFunction(DragForce);
00120     }
00121     
00122     
00123         EnvFunction GGTorque(&GravityGradientTorque);
00124         Matrix *MOI = new Matrix(pSatAttitude->GetParameters()(_(1,3),_));
00125         GGTorque.AddParameter(reinterpret_cast<void*>(MOI), 1);
00126         GGTorque.AddParameter(reinterpret_cast<void*>(mu), 2);
00127         pEarthEnv->AddTorqueFunction(GGTorque);
00128     
00129     
00130         pCBEarth->SetMagneticModel(new TiltedDipoleMagneticModel);
00131     return pEarthEnv;
00132 }
00133 
00134 
00135 
00136 
00137 NumericPropagator* SetupPropagator()
00138 {
00139     CombinedNumericPropagator* pSatProp = new CombinedNumericPropagator;
00140     
00141     
00142         
00143     RungeKuttaFehlbergIntegrator* orbitIntegrator = new RungeKuttaFehlbergIntegrator; 
00144     RungeKuttaFehlbergIntegrator* attitudeIntegrator = new RungeKuttaFehlbergIntegrator; 
00145 
00146     orbitIntegrator->SetTolerance(pow(10.,-7.));
00147     orbitIntegrator->SetStepSizes(0.01, 300);
00148     pSatProp->SetOrbitIntegrator(orbitIntegrator);
00149     attitudeIntegrator->SetTolerance(pow(10.,-7.));
00150     attitudeIntegrator->SetStepSizes(0.01, 5);
00151     pSatProp->SetAttitudeIntegrator(attitudeIntegrator);
00152     
00153     return pSatProp;
00154 }
00155 
00156 
00157 
00158 
00159 Orbit* SetupOrbit()
00160 {
00161     Orbit* pSatOrbit = new Orbit;
00162     
00163     
00164     OrbitState SatOrbitState;
00165     SatOrbitState.SetStateRepresentation(new PositionVelocity);
00166     SatOrbitState.SetOrbitFrame(new OrbitFrameIJK);
00167     Vector initPV(6);
00168         
00169         initPV(VectorIndexBase+0) = -5776.6; 
00170         initPV(VectorIndexBase+1) = -157; 
00171         initPV(VectorIndexBase+2) = 3496.9; 
00172         initPV(VectorIndexBase+3) = -2.595;  
00173         initPV(VectorIndexBase+4) = -5.651;  
00174         initPV(VectorIndexBase+5) = -4.513; 
00175     SatOrbitState.SetState(initPV);
00176     pSatOrbit->SetStateObject(SatOrbitState);
00177     
00178     pSatOrbit->SetDynamicsEq(&TwoBodyDynamics);
00179     pSatOrbit->SetStateConversion(&PositionVelocityConvFunc);
00180 
00181     return pSatOrbit;
00182 }
00183 
00184 
00185 
00186 
00187 
00188 Attitude* SetupAttitude()
00189 {
00190     Attitude* pSatAttitude = new Attitude;
00191     
00192     AttitudeState SatAttState;
00193     SatAttState.SetRotation(Rotation(Quaternion(0,0,0,1)));
00194     Vector initAngVelVector(3);
00195         initAngVelVector(1) = 0;
00196     SatAttState.SetAngularVelocity(initAngVelVector);
00197 
00198     pSatAttitude->SetStateObject(SatAttState);
00199     pSatAttitude->SetDynamicsEq(&AttituteDynamics_QuaternionAngVel);
00200     pSatAttitude->SetStateConversion(&QuaternionAngVelConvFunc);
00201 
00202     
00203     Matrix MOI(3,3);
00204         MOI(1,1) =  0.4084; MOI(1,2) =  0.0046; MOI(1,3) = 0.0;
00205         MOI(2,1) =  0.0046; MOI(2,2) =  0.3802; MOI(2,3) = 0.0;
00206         MOI(3,1) =  0.0;    MOI(3,2) =  0.0;    MOI(3,3) = 0.4530;
00207 
00208     MOI = eye(3);
00209         MOI(1,1) = 2; MOI(2,2) = 2; MOI(3,3) = 10;
00210     Matrix params(6,3);
00211         params(_(1,3),_) = MOI;
00212         params(_(4,6),_) = MOI.inverse();
00213     
00214     pSatAttitude->SetParameters(params);
00215     
00216     return pSatAttitude;
00217     
00218 }
00219 
00220 Vector ControlTorques(const ssfTime &_currentTime, const OrbitState  &_currentOrbitState, const AttitudeState &_currentAttitudeState, const EnvFuncParamaterType &_parameterList) 
00221 {
00222   
00223 
00224 
00225    
00226 
00227 
00228 
00229 
00230 
00231 
00232 
00233 
00234 
00235 
00236 
00237 
00238 
00239 
00240 
00241 
00242 
00243 
00244 
00245 
00246 
00247 
00248 
00249 
00250 
00251 
00252 
00253 
00254 
00255 
00256 
00257 
00258 
00259 
00260 
00261 
00262 
00263 
00264 
00265 
00266 
00267 
00268 
00269 
00270 
00271 
00272 
00273 
00274 
00275 
00276 
00277 
00278 
00279 
00280 
00281 
00282 
00283 
00284 
00285 
00286 
00287 
00288 
00289 
00290 
00291 
00292 
00293 
00294 
00295 
00296 
00297 
00298 
00299 
00300 
00301 
00302 
00303 
00304     return Vector(3);
00305 }
00306 
00307 
00308 
00309 
00310 
00311 
00312 
00313 
00314 
00315 
00316 
00317 
00318 
00319 
00320 
00321 
00322 
00323 
00324 
00325 
00326 
00327 
00328 
00329 
00330 
00331 
00332 
00333 
00334 
00335 
00336 
00337 
00338 
00339 
00340 
00341 
00342