-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspacecraft.cpp
More file actions
299 lines (231 loc) · 8.32 KB
/
spacecraft.cpp
File metadata and controls
299 lines (231 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/********************************************************************************
* File Name : spacecraft.cpp
* Title :
* Programmer: Motki Kimura
* Belonging :
* Date : 2014.8.3
* Language : C++
*********************************************************************************
* class to calculate the position & velocity of the spacecraft in the sun-centerd
* inertial coordinate
*
********************************************************************************/
#include "spacecraft.h"
#define SC_POSITION block (0, 0, 3, 1)
#define SC_VELOCITY block (3, 0, 3, 1)
SpacecraftCalculator:: SpacecraftCalculator (void): epochMjd_ (0.0), secondsFromEpoch_ (0.0),
ballisticCoeff_ (100.0),
earth_ ("earth", epochMjd_), spacecraftState_ (VectorXd:: Zero (6))
{
for (int i = 0; i < 3; i++) {
initialPosEci_[i] = 0.0;
initialVelEci_[i] = 0.0;
}
for (int i = 0; i < 3; i++) {
observerGeoCoord_[i] = 0.0;
}
}
SpacecraftCalculator:: ~SpacecraftCalculator (void)
{
}
void SpacecraftCalculator:: setSpacecraftOrbitInfo (double epochMjd, const double (&posEci)[3], const double (&velEci)[3])
{
epochMjd_ = epochMjd;
earth_.setEpoch (epochMjd_);
for (int i = 0; i < 3; i++) {
initialPosEci_[i] = posEci[i];
initialVelEci_[i] = velEci[i];
}
Vector3d posSci, velSci;
calcInitialScState (&posSci, &velSci);
setSpacecraftState (0.0, posSci, velSci);
}
void SpacecraftCalculator:: getSpacecraftOrbitInfo (double* epochMjd, double* posEci, double* velEci) const
{
*epochMjd = epochMjd_;
for (int i = 0; i < 3; i++) {
posEci[i] = initialPosEci_[i];
velEci[i] = initialVelEci_[i];
}
}
void SpacecraftCalculator:: setSpacecraftParams (double ballisticCoeff)
{
ballisticCoeff_ = ballisticCoeff;
}
void SpacecraftCalculator:: getSpacecraftParams (double* ballisticCoeff) const
{
*ballisticCoeff = ballisticCoeff_;
}
void SpacecraftCalculator:: setSpacecraftState (double secondsFromEpoch, const double (&posSci)[3], const double (&velSci)[3])
{
secondsFromEpoch_ = secondsFromEpoch;
earth_.setTargetTime (secondsFromEpoch_);
Vector3d posSciVec (posSci);
Vector3d velSciVec (velSci);
spacecraftState_.SC_POSITION = posSciVec;
spacecraftState_.SC_VELOCITY = velSciVec;
}
void SpacecraftCalculator:: setSpacecraftState (double secondsFromEpoch, Vector3d const& posSci, Vector3d const& velSci)
{
secondsFromEpoch_ = secondsFromEpoch;
earth_.setTargetTime (secondsFromEpoch_);
spacecraftState_.SC_POSITION = posSci;
spacecraftState_.SC_VELOCITY = velSci;
}
double SpacecraftCalculator:: getSpacecraftState (double* posSci, double* velSci) const
{
for (int i = 0; i < 3; i++) {
posSci[i] = (spacecraftState_.SC_POSITION) [i];
velSci[i] = (spacecraftState_.SC_VELOCITY) [i];
}
return secondsFromEpoch_;
}
double SpacecraftCalculator:: getSpacecraftState (Vector3d* posSci, Vector3d* velSci) const
{
*posSci = spacecraftState_.SC_POSITION;
*velSci = spacecraftState_.SC_VELOCITY;
return secondsFromEpoch_;
}
void SpacecraftCalculator:: getEpochTime (double* epochMjd) const
{
*epochMjd = epochMjd_;
}
void SpacecraftCalculator:: getSecondsFromEpoch (double* secondsFromEpoch) const
{
*secondsFromEpoch = secondsFromEpoch_;
}
void SpacecraftCalculator:: resetSpacecraftState (void)
{
Vector3d posSci, velSci;
calcInitialScState (&posSci, &velSci);
setSpacecraftState (0.0, posSci, velSci);
}
double SpacecraftCalculator:: integrateSpacecraftState (double dt, double srpErrorRatio)
{
//Rrunge-Kutta
VectorXd k1 (6), k2 (6), k3 (6), k4 (6);
calcSpacecraftStateDerivative (&k1, spacecraftState_, srpErrorRatio);
calcSpacecraftStateDerivative (&k2, spacecraftState_ + k1 * 0.5 * dt, srpErrorRatio);
calcSpacecraftStateDerivative (&k3, spacecraftState_ + k2 * 0.5 * dt, srpErrorRatio);
calcSpacecraftStateDerivative (&k4, spacecraftState_ + k3 * dt, srpErrorRatio);
spacecraftState_ = spacecraftState_ + (k1 + 2.0 * k2 + 2.0 * k3 + k4) / 6.0 * dt;
secondsFromEpoch_ += dt;
earth_.setTargetTime (secondsFromEpoch_);
return secondsFromEpoch_;
}
void SpacecraftCalculator:: getSpacecraftPosEci (double* scPosEci) const
{
Vector3d pos;
calcSpacecraftPosEci (&pos);
for (int i = 0; i < 3; i++) {
scPosEci[i] = pos[i];
}
}
void SpacecraftCalculator:: getSpacecraftVelEci (double* scVelEci) const
{
Vector3d vel;
calcSpacecraftVelEci (&vel);
for (int i = 0; i < 3; i++) {
scVelEci[i] = vel[i];
}
}
void SpacecraftCalculator:: getEarthPosSci (double* earthPosSci) const
{
earth_.getPosition (earthPosSci);
}
void SpacecraftCalculator:: getEarthVelSci (double* earthVelSci) const
{
earth_.getVelocity (earthVelSci);
}
void SpacecraftCalculator:: test1 (int periodDay)
{
// set orbit information
const double EpochMjd = 56992.264444;
const double PosEci[3] = {10718921.0, 747344.0, -1050332.0};
const double VelEci[3] = {5616.853 , 6764.471, -4193.746};
setSpacecraftOrbitInfo (EpochMjd, PosEci, VelEci);
// set parameters
const double BallisticCoeff = 150.0;
setSpacecraftParams (BallisticCoeff);
// variables
Vector3d scPos, scVel;
double declination, ra, doppler_down, doppler_up, distance, speed;
const double Dt = 60.0;
const double SecondsDay = 3600.0 * 24.0;
const int NDay = static_cast<int> (SecondsDay / Dt);
double t = 0.0;
int n = 0;
const double Frequency = 437.325e6f;
cout << "day, x, y, z, u, v, w, declination, RA, doppler, distance, speed" << endl;
do {
getSpacecraftState (&scPos, &scVel);
getGeometryEarthCentered (&declination, &ra);
getDopplerRatioEarthCentered (&doppler_down, &doppler_up);
getDistanceEarthCentered (&distance);
getSpacecraftRelativeSpeed (&speed);
if (n % NDay == 0) {
cout << n / NDay + 1 << ",";
cout << scPos[0] << "," << scPos[1] << "," << scPos[2] << ",";
cout << scVel[0] << "," << scVel[1] << "," << scVel[2] << ",";
cout << declination << "," << ra << ",";
cout << (doppler_down - 1.0) * Frequency << ",";
cout << distance << ",";
cout << speed;
cout << endl;
}
t = integrateSpacecraftState (Dt);
n++;
} while (t < SecondsDay * static_cast<double> (periodDay));
}
void SpacecraftCalculator:: calcInitialScState (Vector3d* posSci, Vector3d* velSci) const
{
const double Pi = M_PI;
const double Obliquity = - 23.44 * Pi / 180.0;
Planet earth ("earth", epochMjd_);
earth.setTargetTime (0.0);
Vector3d earthPos, earthVel;
earth.getPosition (&earthPos);
earth.getVelocity (&earthVel);
Vector3d posEciVec (initialPosEci_);
Vector3d velEciVec (initialVelEci_);
Matrix3d C;
tf:: calcDcm (&C, 0, -1.0 * Obliquity);
*posSci = earthPos + C * posEciVec; // convert from ECI to SCI
*velSci = earthVel + C * velEciVec; // convert from ECI to SCI
}
void SpacecraftCalculator:: calcSpacecraftStateDerivative (VectorXd* scStateDerivative, VectorXd const& scState, double srpErrorRatio) const
{
scStateDerivative->SC_POSITION = scState.SC_VELOCITY;
Vector3d solarGravityAcc, srpAcc, earthGravityAcc;
calcSolarGravityAcc (&solarGravityAcc, scState);
calcSolarRadiationPressureAcc (&srpAcc, scState, srpErrorRatio);
calcEarthGravityAcc (&earthGravityAcc, scState);
scStateDerivative->SC_VELOCITY = solarGravityAcc + srpAcc + earthGravityAcc;
}
void SpacecraftCalculator:: calcSolarGravityAcc (Vector3d* acceleration, VectorXd const& scState) const
{
const double MueSun = 1.32712442099e20;
double distance = (scState.SC_POSITION).norm ();
double solarGravity = MueSun / (distance * distance);
*acceleration = -1.0 * solarGravity * (scState.SC_POSITION / distance);
}
void SpacecraftCalculator:: calcSolarRadiationPressureAcc (Vector3d* acceleration, VectorXd const& scState, double srpErrorRatio) const
{
const double SrpG1 = 1.0e14;
const double SrpCoeff = 1.0;
double distance = (scState.SC_POSITION).norm ();
// cannon-ball SRP model
double srpForce = SrpCoeff * SrpG1 / ballisticCoeff_ / (distance * distance);
srpForce = srpForce * (1.0 + srpErrorRatio);
*acceleration = -1.0 * srpForce * (scState.SC_POSITION / distance);
}
void SpacecraftCalculator:: calcEarthGravityAcc (Vector3d* acceleration, VectorXd const& scState) const
{
const double MueEarth = 3.986004418e14;
Vector3d earthPos;
earth_.getPosition (&earthPos);
Vector3d relativePos = scState.SC_POSITION - earthPos;
double distance = relativePos.norm ();
double earthGravity = MueEarth / (distance * distance);
*acceleration = -1.0 * earthGravity * (relativePos / distance);
}