-
Notifications
You must be signed in to change notification settings - Fork 11
/
attprop.cpp
1746 lines (1356 loc) · 80.1 KB
/
attprop.cpp
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//==========================================================================
/*
* Copyright 2020 Sergio De Florio
* All rigths reserved
*
* This file is part of SpOCK
*
* SpOCK is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 3
*
* SpOCK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SpOCK. If not, see <https://www.gnu.org/licenses/>.
*
*/
//==========================================================================
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <math.h>
#include <typeinfo>
#include <chrono>
#include <thread>
#include <Attitude.h>
#include <AttitudeQ.h>
#include <IO_utils.h>
#include <VarTypes.h>
#include <Constants.h>
#include <Transformations.h>
#include <HIL_interface.h>
#include <EarthSunSensor.h>
#include <Magneto.h>
#include <WheelMR.h>
#include <SolarPanel.h>
#include <Solarsys.h>
// External libraries: Eigen
#include <Eigen/Core>
#include "boost/multi_array.hpp"
#ifdef USE_SPICE
extern "C"
{
#include "extlib/cspice/include/SpiceUsr.h"
}
#endif
using namespace std;
using namespace SC;
using namespace constants;
using namespace mathconst;
using namespace math;
using namespace earthsun;
using namespace magneto;
using namespace mrwheel;
using namespace solarpan;
using namespace boost;
int main(int argc, char *argv[])
{
chrono::time_point<chrono::high_resolution_clock> clockstart, clockend;
clockstart = chrono::high_resolution_clock::now();
/////////////// Simmulation parameters XML file ///////////////
if(argc < 2)
{
// Tell the user how to run the program
cerr << "Usage: " << argv[0] << " path_to/simulation/parameters/file.xml\nEx: ./bin/AttitudePropagator /home/username/path1/path2/input/simparam.xml" << endl;
return 1;
}
string XML_simparam_file(argv[1]);
/////////////////////////////////////////////////////////////
//////////// CREATE ATTITUDE PROPAGATOR CLASS ///////////////
/////////////////////////////////////////////////////////////
#ifdef USE_QUATERNION
using namespace attitudeq;
ATTQ SC_Attitude;
//PROP* SC_Attitude_ptr = &SC_Attitude;
#else
using namespace attitude;
ATT SC_Attitude;
#endif
///////////////////////////////////////////////////////////////
//////////// SIMULATION PARAMETERS VARIABLES //////////////////
///////////////////////////////////////////////////////////////
///////////////////////// Files paths /////////////////////////
// Input files path
string Orbit_ephemeris, Attitude_ephemeris, TLE_file, Data_path, planetephemeris, eop, pck_data, leapsecond, magneticfield, gravityfield, atmosphere, sunmoon;
// Output files path
string orbfile_name, attfile_name, sensors_filename, csv_torques_name, csv_accelerations_name;
///////////////////////// Simulation parameters /////////////////////////
// Simulation step
int SIM_STEP;
// Simulation duration
int SIM_DURATION;
// Initial orbit UTC date and time
Vector6d init_orbtime = Vector6d::Zero();
// Initial orbit state
Vector6d init_orbstate = Vector6d::Zero();
// Initial attitude state
double phi, theta, psi, om_x, om_y, om_z;
// Initial state in RTN frame on/off
bool initstate_in_RTN;
// Real time simulation on/off
bool realtime;
// Step execution waiting time for hardware-in-the-loop simulations
double realtime_wait;
// Gravity gradient torque on/off
bool ggrad_on;
// Magnetic torque on/off
bool mag_on;
// Atmospheric drag torque on/off
bool drag_on;
// Solar radiation pressure torque on/off
bool srp_on;
// Maximum order and degree of gravitational field model used for the orbit propagation
int nMAX;
// Third body perturbation on/off
bool sunmoon_on;
// Atmospheric drag model used
string Drag_Model;
// Solar radiation pressure model used
string SRP_Model;
// Attitude type during orbit propagation
string AttitudeType;
// Attitude control on/off
bool attctrl_on;
// Attitude control type
string AttCtrlType;
// Orbit control on/off
bool orbctrl_on;
// Orbit control type
string OrbCtrlType;
///////////////////////// Spacecraft properties /////////////////////////
// Spacecraft mass
double SC_mass;
// Spacecraft center of mass position vector in body-fixed coordinates
static Vec3d CoG;
// Moments of inertia matrix. Moment of inertia taken at the center of mass and aligned with the body-fixed frame [kg*m^2]
static Mat3x3d MoI = Mat3x3d::Zero();
// Drag coefficient
static double SC_Cd;
// SRP coefficient
static double SC_Cr;
// Drag area to be used with atmospheric drag simple model
static double SC_Area_D;
// Radiation area to be used with solar radiation pressure simple model
static double SC_Area_R;
// Spacecraft magnetic dipole moment vector in body-fixed coordinates
static Vec3d Mdip;
// Spacecraft surfaces;
Face F_Xplus, F_Xminus, F_Yplus, F_Yminus, F_Zplus, F_Zminus;
F_Xplus.n = spacecraft::Normals.at("+X");
F_Xminus.n = spacecraft::Normals.at("-X");
F_Yplus.n = spacecraft::Normals.at("+Y");
F_Yminus.n = spacecraft::Normals.at("-Y");
F_Zplus.n = spacecraft::Normals.at("+Z");
F_Zminus.n = spacecraft::Normals.at("-Z");
///////////////// ADCS sensors and actuators /////////////////
// Sensors and actuators classes
SYS_params Sensor_prm_SUN, Sensor_prm_EARTH, Sensor_prm_CSS1, Sensor_prm_CSS2, Sensor_prm_CSS3, Sensor_prm_CSS4, Sensor_prm_CSS5, Sensor_prm_CSS6, Sensor_prm_MAG, Sensor_prm_MAGstowed, Sensor_prm_RS, Sensor_prm_MAGTRQ, Sensor_prm_WHEEL1, Sensor_prm_WHEEL2, Sensor_prm_WHEEL3, Solarpan1_prm, Solarpan2_prm, Solarpan3_prm, OrbitPropulsion1_prm, OrbitPropulsion2_prm;
///////////////// Commanded attitude maneuvers /////////////////
vector<maneuver> all_maneuvers; // Struct maneuver defined in VarTypes.h
//////////////////////////////////////////////////////////////
////////// PARSING OF XML SIMULATION PARAMETERS FILE /////////
//////////////////////////////////////////////////////////////
XML_parser(XML_simparam_file, Orbit_ephemeris, Attitude_ephemeris, TLE_file, Data_path, planetephemeris, eop, pck_data, leapsecond, magneticfield, gravityfield, atmosphere, sunmoon, orbfile_name, attfile_name, sensors_filename, csv_torques_name, csv_accelerations_name, SIM_STEP, SIM_DURATION, init_orbtime, init_orbstate, phi, theta, psi, om_x, om_y, om_z, initstate_in_RTN, realtime, realtime_wait, ggrad_on, mag_on, drag_on, srp_on, nMAX, sunmoon_on, Drag_Model, SRP_Model, AttitudeType, attctrl_on, AttCtrlType, orbctrl_on, OrbCtrlType, SC_mass, MoI, CoG, SC_Cd, SC_Cr, SC_Area_D, SC_Area_R, Mdip, F_Xplus, F_Xminus, F_Yplus, F_Yminus, F_Zplus, F_Zminus, Sensor_prm_SUN, Sensor_prm_EARTH, Sensor_prm_CSS1, Sensor_prm_CSS2, Sensor_prm_CSS3, Sensor_prm_CSS4, Sensor_prm_CSS5, Sensor_prm_CSS6, Sensor_prm_MAG, Sensor_prm_MAGstowed, Sensor_prm_RS, Sensor_prm_MAGTRQ, Sensor_prm_WHEEL1, Sensor_prm_WHEEL2, Sensor_prm_WHEEL3, Solarpan1_prm, Solarpan2_prm, Solarpan3_prm, OrbitPropulsion1_prm, OrbitPropulsion2_prm, all_maneuvers);
//cout << "Sono qui" << endl;
size_t lastslash = XML_simparam_file.find_last_of("/");
string ReadXML_TXT_file_name = XML_simparam_file.substr(lastslash+1);
size_t lastspoint = ReadXML_TXT_file_name.find_last_of(".");
ReadXML_TXT_file_name = ReadXML_TXT_file_name.substr(0,lastspoint);
const string ReadXML_TXT_file = XML_simparam_file.substr(0,lastslash) + "/Read_" + ReadXML_TXT_file_name + ".txt";
//const string ReadXML_TXT_file = "input/readXML.txt";
// Put read XML in a text file (for check purposes)
ReadXMLtoTXT(ReadXML_TXT_file, Orbit_ephemeris, Attitude_ephemeris, TLE_file, Data_path, planetephemeris, eop, pck_data, leapsecond, magneticfield, gravityfield, atmosphere, sunmoon, orbfile_name, attfile_name, sensors_filename, csv_torques_name, csv_accelerations_name, SIM_STEP, SIM_DURATION, init_orbtime, init_orbstate, phi, theta, psi, om_x, om_y, om_z, initstate_in_RTN, realtime, realtime_wait, ggrad_on, mag_on, drag_on, srp_on, nMAX, sunmoon_on, Drag_Model, SRP_Model, AttitudeType, attctrl_on, AttCtrlType, orbctrl_on, OrbCtrlType, SC_mass, MoI, CoG, SC_Cd, SC_Cr, SC_Area_D, SC_Area_R, Mdip, F_Xplus, F_Xminus, F_Yplus, F_Yminus, F_Zplus, F_Zminus, Sensor_prm_SUN, Sensor_prm_EARTH, Sensor_prm_CSS1, Sensor_prm_CSS2, Sensor_prm_CSS3, Sensor_prm_CSS4, Sensor_prm_CSS5, Sensor_prm_CSS6, Sensor_prm_MAG, Sensor_prm_MAGstowed, Sensor_prm_RS, Sensor_prm_MAGTRQ, Sensor_prm_WHEEL1, Sensor_prm_WHEEL2, Sensor_prm_WHEEL3, Solarpan1_prm, Solarpan2_prm, Solarpan3_prm, OrbitPropulsion1_prm, OrbitPropulsion2_prm, all_maneuvers);
// Load orbit ephemerides
Eigen::MatrixXd loaded_ephem;
try{ loaded_ephem = read_csvfile(Orbit_ephemeris.c_str(),15); }
catch(const string errmsg)
{
cerr << "Orbit ephemerides: " + errmsg << endl;
exit(EXIT_FAILURE);
}
int matrows = loaded_ephem.rows();
double ephem_duration = loaded_ephem(matrows-1,1) - loaded_ephem(0,1);
if(SIM_DURATION > ephem_duration)
{
cerr << "\nThe duration of the simulation (<simduration>) cannot be longer than the duration of the input orbit ephemeris" << endl;
exit(EXIT_FAILURE);
}
//////////////////////////////////////////////////////////////
/////////////// PROCESSING OF PARSED VARIABLES ///////////////
//////////////////////////////////////////////////////////////
//////////////////// Orbit and environmental models /////////////////////
// Load SPICE Kernels
#ifdef USE_SPICE
planetephemeris = Data_path + "/cspice/" + planetephemeris;
eop = Data_path + "/cspice/" + eop;
leapsecond = Data_path + "/cspice/" + leapsecond;
pck_data = Data_path + "/cspice/" + pck_data;
furnsh_c(planetephemeris.c_str( ));
furnsh_c(eop.c_str( ));
furnsh_c(leapsecond.c_str( ));
furnsh_c(pck_data.c_str( ));
#endif
EnvModels envmodels_paths;
envmodels_paths.datapath = Data_path;
envmodels_paths.sunmoon = sunmoon;
envmodels_paths.magneticfield = magneticfield;
envmodels_paths.gravityfield = gravityfield;
envmodels_paths.atmosphere = atmosphere;
////////////////////////////// Outputs //////////////////////////////////
// Files
//string mat_attfile_name = "output/" + attfile_name + ".mat";
//string csv_attfile_name = "output/" + attfile_name + ".csv";
///////////////// ADCS sensors and actuators objects /////////////////
EARTHSUNSENS SunCamera(Sensor_prm_SUN);
EARTHSUNSENS EarthCamera(Sensor_prm_EARTH);
EARTHSUNSENS CSS1(Sensor_prm_CSS1);
CSS1.Init();
EARTHSUNSENS CSS2(Sensor_prm_CSS2);
CSS2.Init();
EARTHSUNSENS CSS3(Sensor_prm_CSS3);
CSS3.Init();
EARTHSUNSENS CSS4(Sensor_prm_CSS4);
CSS4.Init();
EARTHSUNSENS CSS5(Sensor_prm_CSS5);
CSS5.Init();
EARTHSUNSENS CSS6(Sensor_prm_CSS6);
CSS6.Init();
Sensor_prm_MAG.SpaceEnv.magneticfield = envmodels_paths.magneticfield;
MAGNETO Magnetometer1(Sensor_prm_MAG);
Magnetometer1.Init();
Sensor_prm_MAGstowed.SpaceEnv.magneticfield = envmodels_paths.magneticfield;
MAGNETO Magnetometer2(Sensor_prm_MAGstowed);
Magnetometer2.Init();
Mat3x3d Mag_SC2SYS;
if(Magnetometer1.On) Mag_SC2SYS = Sensor_prm_MAG.SC2SYS;
if(Magnetometer2.On) Mag_SC2SYS = Sensor_prm_MAGstowed.SC2SYS;
MRWHEEL RateSensor(Sensor_prm_RS);
Sensor_prm_MAGTRQ.SpaceEnv.magneticfield = envmodels_paths.magneticfield;
MAGNETO Magnetorquer(Sensor_prm_MAGTRQ);
Magnetorquer.Init();
MRWHEEL Wheel1(Sensor_prm_WHEEL1);
Wheel1.Init();
MRWHEEL Wheel2(Sensor_prm_WHEEL2);
Wheel2.Init();
MRWHEEL Wheel3(Sensor_prm_WHEEL3);
Wheel3.Init();
///////////////// Solar panels objects /////////////////
SOLARPAN SolarPanel1(Solarpan1_prm);
SolarPanel1.Init();
SOLARPAN SolarPanel2(Solarpan2_prm);
SolarPanel2.Init();
SOLARPAN SolarPanel3(Solarpan3_prm);
SolarPanel3.Init();
///////////////// Spacecraft parameters /////////////////
SC_params SC_prms;
SC_prms.SC_mass = SC_mass;
SC_prms.MoI = MoI;
SC_prms.Segment["+X"] = F_Xplus;
SC_prms.Segment["-X"] = F_Xminus;
SC_prms.Segment["+Y"] = F_Yplus;
SC_prms.Segment["-Y"] = F_Yminus;
SC_prms.Segment["+Z"] = F_Zplus;
SC_prms.Segment["-Z"] = F_Zminus;
SC_prms.Mdip = Mdip;
SC_prms.CD = SC_Cd;
SC_prms.C_SRP = SC_Cr;
///////////////////////// Initial state parameters //////////////////////
// Orbit initial state vector
double ini_GPStime, GPStime, omega_orb;
Vec3d posECI = Vec3d::Zero();
Vec3d velECI = Vec3d::Zero();
Vec3d posECEF = Vec3d::Zero();
Vector6d current_orbstate = Vector6d::Zero();
Vector6d current_orbstateECEF = Vector6d::Zero();
VectorNd<15> ephem_row = VectorNd<15>::Zero();
ephem_row = loaded_ephem.row(0);
ini_GPStime = ephem_row(1);
current_orbstate = ephem_row.segment(2,6);
posECI = current_orbstate.segment(0,3);
velECI = current_orbstate.segment(3,3);
current_orbstateECEF = ephem_row.segment(8,6);
posECEF = current_orbstateECEF.segment(0,3);
omega_orb = velECI.norm()/posECI.norm(); //sqrt( astro::GM_EARTH/(orbrho*orbrho*orbrho) );
omega_orb = omega_orb*RAD2DEG;
// Attitude initial state vector
Vector6d init_attstateRTN;
VectorXd init_attstate;
// Conversion to radians and normalization to 2*pi
phi = mod(phi*DEG2RAD,PI2);
theta = mod(theta*DEG2RAD,PI2);
psi = mod(psi*DEG2RAD,PI2);
om_x = om_x*DEG2RAD;
om_y = om_y*DEG2RAD;
om_z = om_z*DEG2RAD;
Mat3x3d ECItoBody, T_ECI2RTN, T_RTN2ECI, T_RTN2Body;
// Quaternion state + angular velocity vector to be used by the sensor and actuator objects
VectorNd<7> stateQ = VectorNd<7>::Zero();
#ifdef USE_QUATERNION
VectorNd<7> init_attstateQ = VectorNd<7>::Zero();
if(initstate_in_RTN)
{
init_attstateRTN(0) = phi;
init_attstateRTN(1) = theta;
init_attstateRTN(2) = psi;
init_attstateRTN(3) = om_x;
init_attstateRTN(4) = om_y;
init_attstateRTN(5) = om_z;
cout << "\nInitial state (RTN): phi = " << phi*RAD2DEG << " theta = " << theta*RAD2DEG << " psi = " << psi*RAD2DEG << " deg," << " om_x = " << om_x*RAD2DEG << " om_y = " << om_y*RAD2DEG << " om_z = " << om_z*RAD2DEG << " deg/s\n" << endl;
T_ECI2RTN = ECI2RTN_Matrix(current_orbstate);
T_RTN2ECI = T_ECI2RTN.transpose();
T_RTN2Body = RotationMatrix321(init_attstateRTN(0), init_attstateRTN(1), init_attstateRTN(2));
ECItoBody = T_RTN2Body*T_ECI2RTN;
init_attstateQ.segment(0,4) = RotationMatrix2Quaternion(ECItoBody);
init_attstateQ.segment(4,3) = init_attstateRTN.segment(3,3);
//om_z = om_z + omega_orb;
//init_attstateQ.segment(4,3) = T_RTN2Body*init_attstateRTN.segment(3,3); // Angular velocity vector in body frame
//// Euler angles with respect to ECI frame
//init_attstate.segment(0,3) = EulerAng;
//for(int i = 0; i < 3; i++) init_attstate(i) = mod(init_attstate(i),PI2);
//init_attstate.segment(3,3) = T_RTN2ECI*init_attstateRTN.segment(3,3);
}
else
{
ECItoBody = RotationMatrix321(phi, theta, psi);
init_attstateQ.segment(0,4) = RotationMatrix2Quaternion(ECItoBody);
init_attstateQ(4) = om_x;
init_attstateQ(5) = om_y;
init_attstateQ(6) = om_z;
}
init_attstate = init_attstateQ;
stateQ = init_attstateQ;
//cout << "Initial state: " << init_attstate(0) << " " << init_attstate(1) << " " << init_attstate(2) << " " << init_attstate(3) << " " << init_attstate(4)*RAD2DEG << " " << init_attstate(5)*RAD2DEG << " " << init_attstate(6)*RAD2DEG << endl;
#else
Vector6d init_attstate_ECI;
if(initstate_in_RTN)
{
init_attstateRTN(0) = phi;
init_attstateRTN(1) = theta;
init_attstateRTN(2) = psi;
init_attstateRTN(3) = om_x;
init_attstateRTN(4) = om_y;
init_attstateRTN(5) = om_z;
cout << "\nInitial attitude state (RTN): phi = " << phi*RAD2DEG << " theta = " << theta*RAD2DEG << " psi = " << psi*RAD2DEG << " deg," << " om_x = " << om_x*RAD2DEG << " om_y = " << om_y*RAD2DEG << " om_z = " << om_z*RAD2DEG << " deg/s\n" << endl;
//for(int i = 0; i < 3; i++) init_attstateRTN(i) = mod(init_attstateRTN(i)*DEG2RAD,PI2);
//for(int i = 3; i < 6; i++) init_attstateRTN(i) = init_attstateRTN(i)*DEG2RAD;
T_ECI2RTN = ECI2RTN_Matrix(current_orbstate);
T_RTN2ECI = T_ECI2RTN.transpose();
T_RTN2Body = RotationMatrix321(init_attstateRTN(0), init_attstateRTN(1), init_attstateRTN(2));
ECItoBody = T_RTN2Body*T_ECI2RTN;
Vec3d EulerAng = EulerAngles321(ECItoBody);
phi = EulerAng(0);
theta = EulerAng(1);
psi = EulerAng(2);
init_attstate_ECI(0) = phi;
init_attstate_ECI(1) = theta;
init_attstate_ECI(2) = psi;
init_attstate_ECI(3) = om_x;
init_attstate_ECI(4) = om_y;
init_attstate_ECI(5) = om_z;
}
else
{
ECItoBody = RotationMatrix321(phi, theta, psi);
init_attstate_ECI(0) = phi;
init_attstate_ECI(1) = theta;
init_attstate_ECI(2) = psi;
init_attstate_ECI(3) = om_x;
init_attstate_ECI(4) = om_y;
init_attstate_ECI(5) = om_z;
}
init_attstate = init_attstate_ECI;
stateQ.segment(0,4) = RotationMatrix2Quaternion(ECItoBody);
stateQ(4) = om_x;
stateQ(5) = om_y;
stateQ(6) = om_z;
#endif
//////////////////// Environment models //////////////////////////
//bool T_model[4] = {ggrad_on, mag_on, drag_on, srp_on};
bool T_model[5] = {ggrad_on, mag_on, drag_on, srp_on, sunmoon_on};
Drag_Model = "Panels";
SRP_Model = "Panels";
//////////////////// Run-start display message //////////////////////////
//string sim_duration_string = to_string(SIM_DURATION/86400.0) + " days";
//if(SIM_DURATION/86400.0 < 1.0) sim_duration_string = to_string(SIM_DURATION/3600.0) + " hours";
//if(SIM_DURATION/3600.0 < 1.0) sim_duration_string = to_string(SIM_DURATION/60.0) + " minutes";
//
//string pert_txt = "Simulation duration: " + sim_duration_string + "\n\nPERTURBATIONS\n\n";
////if(HarrisPriester_on) pert_txt = pert_txt + "Atmospheric drag: " + hr_atm_name + "\n";
////if(Jacchia_on) pert_txt = pert_txt + "Atmospheric drag: " + j_atm_name + "\n";
////if(DTM2000_on) pert_txt = pert_txt + "Atmospheric drag: " + dtm_atm_name + "\n";
//if(ggrad_on) pert_txt = pert_txt + "Gravity gradient\n";
//if(mag_on) pert_txt = pert_txt + "Earth's magnetic field: " + magneticfield + " model\n";
//if(drag_on) pert_txt = pert_txt + "Panels atmospheric drag model, Atmosphere: " + atmosphere + " model\n";
//if(srp_on) pert_txt = pert_txt + "Panels solar radiation pressure model\n";
//if( !(ggrad_on || mag_on || srp_on || drag_on) ) pert_txt = "NO PERTURBATIONS\n\n";
//
//cout << pert_txt << "\n" << endl;
RunStartMessage(init_orbtime, init_orbstate, SIM_DURATION, T_model, nMAX, Drag_Model, SRP_Model, magneticfield, gravityfield, atmosphere, sunmoon,"ATT");
///////////////// Commanded attitude maneuvers /////////////////
// Sort maneuvers by type and put them in vectors
vector<maneuver> wheels_maneuvers; // Maneuvers executed by reaction/momentum wheels
vector<maneuver> magneto_maneuvers; // Maneuvers executed by magnetotorquers
vector<maneuver> thrusters_maneuvers; // Maneuvers executed by thrusters
//vector<maneuver>::iterator man_ind;
unsigned int man_ind;
vector<maneuver>::size_type man_size;
string man_name;
Vec3d ontime = Vec3d::Zero();
Vec3d unit_ontime = Vec3d::Zero();
double m_init_time; // Re-used in propagation loop
maneuver unit_maneuver;
for(man_ind = 0 ; man_ind < all_maneuvers.size(); man_ind++)
{
man_name = all_maneuvers[man_ind].name;
if( man_name.compare("WheelsManeuver") == 0 ) wheels_maneuvers.push_back(all_maneuvers[man_ind]);
else if( man_name.compare("MagnetoManeuver") == 0 ) magneto_maneuvers.push_back(all_maneuvers[man_ind]);
else if( man_name.compare("ThrustersManeuver") == 0 ) thrusters_maneuvers.push_back(all_maneuvers[man_ind]);
}
man_size = magneto_maneuvers.size();
// Process ontimes in magneto_maneuvers and thrusters_maneuvers. If an ontime is larger than 1 s it is splitted in slots of 1 s (e.g. if ontime = 3.25 s it is splitted in ontimes 1 + 1 + 1 + 0.25)
if( !magneto_maneuvers.empty() )
{
for( man_ind = 0; man_ind < man_size; man_ind++ )
{
m_init_time = magneto_maneuvers[man_ind].init_time;
ontime = magneto_maneuvers[man_ind].ManVec;
ontime = ontime/1000.0; // Conversion of the ontime in seconds
if( (ontime(0) > 1.0) || (ontime(1) > 1.0) || (ontime(2) > 1.0) )
{
unit_maneuver = magneto_maneuvers[man_ind];
for(int i = 0 ; i < 3; i++)
{
if( ontime(i) > 1.0 )
{
ontime(i) = ontime(i) - 1.0;
m_init_time = m_init_time + 1.0;
unit_ontime(i) = 1.0;
}
}
// Make place for the insertion of a new element
man_size = man_size + 1;
magneto_maneuvers.resize(man_size);
// Translate up the element over position man_ind
for( unsigned int i = man_size-1; i > man_ind; i-- ) magneto_maneuvers[i] = magneto_maneuvers[i-1];
// Change the element that was in position man_ind
magneto_maneuvers[man_ind+1].init_time = m_init_time;
magneto_maneuvers[man_ind+1].ManVec = ontime*1000.0; // Re-conversion in milliseconds
// Insert new element in position man_ind
unit_maneuver.ManVec = unit_ontime*1000.0;
magneto_maneuvers[man_ind] = unit_maneuver;
}
}
}
vector<maneuver>::iterator W_man_ind, M_man_ind, T_man_ind; // To be used in propagation loop
W_man_ind = wheels_maneuvers.begin();
M_man_ind = magneto_maneuvers.begin();
T_man_ind = thrusters_maneuvers.begin();
man_size = magneto_maneuvers.size();
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////// ATTITUDE INITIALIZATION /////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VectorNd<12> orbstateECI_ECEF;
orbstateECI_ECEF << current_orbstate, current_orbstateECEF;
SC_Attitude.ggrad_on = T_model[0];
SC_Attitude.mag_on = T_model[1];
SC_Attitude.drag_on = T_model[2];
SC_Attitude.srp_on = T_model[3];
SC_Attitude.simdur = SIM_DURATION;
SC_Attitude.magnetometer_on = Magnetometer1.On || Magnetometer1.On; // True if at least one of the magnetometers is switched-on
SC_Attitude.Setup(SC_prms,envmodels_paths);
SC_Attitude.Init(ini_GPStime, init_attstate, orbstateECI_ECEF);
SC_Attitude.ForceModelsSetup();
double eps_abs = 1E-8;
double eps_rel = 0.0;
double factor_x = 0.0;
double factor_dxdt = 0.0;
SC_Attitude.StepperSetup(eps_abs, eps_rel, factor_x, factor_dxdt);
////////////////////////////////////////////////////////////////////
///////////////// ADCS SENSORS INTERFACE-VARIABLES /////////////////
////////////////////////////////////////////////////////////////////
// Time
unsigned int UnixTime = GPS2Unix(ini_GPStime);
// CSS
VectorNd<6> CssRaw_d;
VectorNui<6> CssRaw;
VectorNd<1> Css_out;
Css_out = CSS1.Output(ini_GPStime, stateQ.segment(0,4), posECI);
CssRaw_d(0) = Css_out(0);
Css_out = CSS2.Output(ini_GPStime, stateQ.segment(0,4), posECI);
CssRaw_d(1) = Css_out(0);
Css_out = CSS3.Output(ini_GPStime, stateQ.segment(0,4), posECI);
CssRaw_d(2) = Css_out(0);
Css_out = CSS4.Output(ini_GPStime, stateQ.segment(0,4), posECI);
CssRaw_d(3) = Css_out(0);
Css_out = CSS5.Output(ini_GPStime, stateQ.segment(0,4), posECI);
CssRaw_d(4) = Css_out(0);
Css_out = CSS6.Output(ini_GPStime, stateQ.segment(0,4), posECI);
CssRaw_d(5) = Css_out(0);
for(int i = 0 ; i < 6; i++) CssRaw_d(i) = round(CssRaw_d(i)); // Before casting to unsigned integer we have to round because the casting to integer rounds always down
CssRaw = CssRaw_d.cast<unsigned int>(); // Cast type of Eigen library variable
// Sun camera
Vec4d camera_out;
VectorNd<2> SunRaw_d;
VectorNi<2> SunRaw;
camera_out = SunCamera.Output(ini_GPStime, stateQ.segment(0,4), posECI);
for(int i = 0 ; i < 2; i++) SunRaw_d(i) = round(camera_out(i));
SunRaw = SunRaw_d.cast<int>();
//camera_out = SunCamera.Output(ini_GPStime, stateQ.segment(0,4), posECI);
//SunRaw = ( camera_out.segment(0,2) ).cast<int>();
unsigned int SunBusy = (unsigned int)camera_out(2);
unsigned int SunResult = (unsigned int)camera_out(3);
// Earth camera
VectorNd<2> NadirRaw_d;
VectorNi<2> NadirRaw;
camera_out = EarthCamera.Output(ini_GPStime, stateQ.segment(0,4), posECI);
for(int i = 0 ; i < 2; i++) NadirRaw_d(i) = round(camera_out(i));
NadirRaw = NadirRaw_d.cast<int>();
//camera_out = EarthCamera.Output(ini_GPStime, stateQ.segment(0,4), posECI);
//NadirRaw = ( camera_out.segment(0,2) ).cast<int>();
unsigned int NadirBusy = (unsigned int)camera_out(2);
unsigned int NadirResult = (unsigned int)camera_out(3);
// Magnetometer
VectorNd<3> MagRaw_d = VectorNd<3>::Zero();
VectorNi<3> MagRaw = VectorNi<3>::Zero(); // Magnetic field vector in magnetometer frame nanotesla/2.5
VectorNd<3> MagRaw_RTN_d = VectorNd<3>::Zero();
VectorNi<3> MagRaw_RTN = VectorNi<3>::Zero();
if(Magnetometer1.On && Magnetometer2.On) cerr << "WARNING: both magnetometers are on. Only the second magnetometer listed in the XML input file will be considered active" << endl;
if(Magnetometer1.On) MagRaw_d = Magnetometer1.Output(ini_GPStime, stateQ.segment(0,4), posECEF);
if(Magnetometer2.On) MagRaw_d = Magnetometer2.Output(ini_GPStime, stateQ.segment(0,4), posECEF);
for(int i = 0 ; i < 3; i++) MagRaw_d(i) = round(MagRaw_d(i)*1E-3); // Before casting to unsigned integer we have to round because the casting to integer rounds always down. 1E-3: conversion from nanotesla to microtesla
MagRaw = MagRaw_d.cast<int>(); // Cast type of Eigen library variable
// MagRaw = ( Magnetometer1.Output(ini_GPStime, stateQ.segment(0,4), posECEF) ).cast<int>();
// Rate sensor
VectorNd<3> RateRaw_d;
VectorNi<3> RateRaw;
RateRaw_d = RateSensor.Output(ini_GPStime, stateQ, current_orbstate);
for(int i = 0 ; i < 3; i++) RateRaw_d(i) = round(RateRaw_d(i)*1E3); // 1E3: conversion from deg to millideg
RateRaw = RateRaw_d.cast<int>();
// RateRaw = ( RateSensor.Output(ini_GPStime, stateQ, current_orbstate) ).cast<int>();
// Star sensors
VectorNi<3> Star1Camera = VectorNi<3>::Zero();
VectorNi<3> Star1Inertial = VectorNi<3>::Zero();
VectorNi<3> Star2Camera = VectorNi<3>::Zero();
VectorNi<3> Star2Inertial = VectorNi<3>::Zero();
VectorNi<3> Star3Camera = VectorNi<3>::Zero();
VectorNi<3> Star3Inertial = VectorNi<3>::Zero();
//////////////////////////// Vector for csv file /////////////////////////
VectorNi<27> sensors_output;
sensors_output(0) = ini_GPStime;
sensors_output.segment(1,6) = CssRaw.cast<int>();
sensors_output.segment(7,2) = SunRaw;
sensors_output(9) = SunBusy;
sensors_output(10) = SunResult;
sensors_output.segment(11,2) = NadirRaw;
sensors_output(13) = NadirBusy;
sensors_output(14) = NadirResult;
sensors_output.segment(15,3) = MagRaw;
//sensors_output.segment(18,3) = VectorNi<3>::Zero()
sensors_output.segment(21,3) = RateRaw;
// WheelRaw: see below
//sensors_output.segment(28,3) = Star1Camera;
//sensors_output.segment(31,3) = Star1Inertial;
//
//sensors_output.segment(34,3) = Star2Camera;
//sensors_output.segment(37,3) = Star2Inertial;
//
//sensors_output.segment(40,3) = Star3Camera;
//sensors_output.segment(43,3) = Star3Inertial;
////////////////////////////////////////////////////////////////////
///////////////// ADCS ACTUATORS INTERFACE-VARIABLES ///////////////
////////////////////////////////////////////////////////////////////
// Magnetotorquer ontimes commanded by ADCS controller
Vec3d m_ontimes = Vec3d::Zero();
// Magnetotorquer ontimes commanded manually
Vec3d m_ontimesCMD = Vec3d::Zero();
// Vector containing the speeds commanded manually of the 3 reaction/momentum wheels
Vec3d wheelspeedCMD;
// Vector containing the speeds commanded by ADCS controller of the 3 reaction/momentum wheels
Vec3d wheelspeedCTRL;
// Magnetotorquer torque
Vec3d Tm = Vec3d::Zero();
// Reaction/momentum wheels torque
Vec3d Tw = Vec3d::Zero();
// Total torque given by actuators
Vec3d T_act = Vec3d::Zero();
Vec3d T_CTRL = Vec3d::Zero();
// Vector of reaction wheels angular momentums in spacecraft frame
Vec3d hw_SC = Vec3d::Zero();
// Environment perturbation torques
VectorNd<15> TenvRTN = VectorNd<15>::Zero();
VectorNd<15> Torque_env = VectorNd<15>::Zero();
Wheel1.wheelspeedCMD = Wheel1.wheelspeed_SYS;
Wheel2.wheelspeedCMD = Wheel2.wheelspeed_SYS;
Wheel3.wheelspeedCMD = Wheel3.wheelspeed_SYS;
Vec3d WheelRaw_d; // Velocities of momentum wheels [RPM]
VectorNi<3> WheelRaw; // Velocities of momentum wheels [RPM]
Vec3d Wheel_out;
Wheel_out = Wheel1.Output(ini_GPStime, stateQ, current_orbstate);
WheelRaw_d(0) = Wheel_out(2);
Wheel_out = Wheel2.Output(ini_GPStime, stateQ, current_orbstate);
WheelRaw_d(1) = Wheel_out(2);
Wheel_out = Wheel3.Output(ini_GPStime, stateQ, current_orbstate);
WheelRaw_d(2) = Wheel_out(2);
//cout << "Sono qui\n" << endl;
for(int i = 0 ; i < 3; i++) WheelRaw_d(i) = round(WheelRaw_d(i));
WheelRaw = WheelRaw_d.cast<int>();
sensors_output.segment(24,3) = WheelRaw;
// Struct for all sensors and actuators (see VarTypes.h)
sensorTCs SensorReadings;
sensorTCs* sensorTCs_ptr = &SensorReadings;
//////////////////////////////////////////////////////
///////////////// SOLAR PANELS OUTPUT ///////////////
/////////////////////////////////////////////////////
Vec3d panel1_out, panel2_out, panel3_out;
panel1_out = SolarPanel1.Output(ini_GPStime, stateQ.segment(0,4), posECI);
panel2_out = SolarPanel2.Output(ini_GPStime, stateQ.segment(0,4), posECI);
panel3_out = SolarPanel3.Output(ini_GPStime, stateQ.segment(0,4), posECI);
VectorNd<8> panels_output;
panels_output(0) = ini_GPStime;
panels_output.segment(1,2) = panel1_out.segment(0,2);
panels_output.segment(3,2) = panel2_out.segment(0,2);
panels_output.segment(5,2) = panel3_out.segment(0,2);
panels_output(7) = panel1_out(2);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////// ATTITUDE INITIALIZATION /////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//VectorNd<12> orbstateECI_ECEF;
//orbstateECI_ECEF << current_orbstate, current_orbstateECEF;
//
//SC_Attitude.ggrad_on = T_model[0];
//SC_Attitude.mag_on = T_model[1];
//SC_Attitude.drag_on = T_model[2];
//SC_Attitude.srp_on = T_model[3];
//
//SC_Attitude.Setup(SC_prms,envmodels_paths);
//SC_Attitude.Init(ini_GPStime, init_attstate, orbstateECI_ECEF);
//SC_Attitude.ForceModelsSetup();
//
//double eps_abs = 1E-8;
//double eps_rel = 0.0;
//double factor_x = 0.0;
//double factor_dxdt = 0.0;
//
//SC_Attitude.StepperSetup(eps_abs, eps_rel, factor_x, factor_dxdt);
cout << "Start\n" << endl;
//chrono::time_point<chrono::high_resolution_clock> forstart, forend;
//////////////////////////// CSV files /////////////////////////
VectorNd<7> attitudeRTN_state_vec;
Vector6d attstateRTN;
// Attitude state RTN
ofstream state_file;
state_file.open(attfile_name);
T_ECI2RTN = ECI2RTN_Matrix(current_orbstate);
T_RTN2ECI = T_ECI2RTN.transpose();
T_RTN2Body = ECItoBody*T_RTN2ECI;
Vec3d euler_ang = EulerAngles321(T_RTN2Body);
attstateRTN << mod(euler_ang(0),PI2)*RAD2DEG, mod(euler_ang(1),PI2)*RAD2DEG, mod(euler_ang(2),PI2)*RAD2DEG, om_x*RAD2DEG, om_y*RAD2DEG, om_z*RAD2DEG;
attitudeRTN_state_vec(0) = ini_GPStime;
attitudeRTN_state_vec.segment(1,6) = attstateRTN;
state_file << fixed << attitudeRTN_state_vec(0) << "," << attitudeRTN_state_vec(1) << "," << attitudeRTN_state_vec(2) << "," << attitudeRTN_state_vec(3) << "," << attitudeRTN_state_vec(4) << "," << attitudeRTN_state_vec(5) << "," << attitudeRTN_state_vec(6) << endl;
// Attitude state quaternions
string attfile_name_quat = attfile_name.replace(attfile_name.end() - 4,attfile_name.end(),"_Quaternions.csv");
ofstream state_file_quat;
state_file_quat.open(attfile_name_quat);
state_file_quat << fixed << ini_GPStime << "," <<stateQ(0) << "," << stateQ(1) << "," << stateQ(2) << "," << stateQ(3) << "," << stateQ(4) << "," << stateQ(5) << "," << stateQ(6) << endl;
// Sensors output
ofstream sensors_file;
sensors_file.open(sensors_filename);
sensors_file << "GPS Time [s],CSS1,CSS2,CSS3,CSS4,CSS5,CSS6,SunCam Az,SunCam El,SunCapture,SunDetection,EarthCam Az,EarthCam El,EarthCapture,EarthDetection,MagX_magframe,MagY_magframe,MagZ_magframe,MagR_RTN,MagT_RTN,MagN_RTN,RateX_sensframe,RateY_sensframe,RateZ_sensframe,WheelSpeedX,WheelSpeedY,WheelSpeedZ,SolarPanels1 Pow,SolarPanels1 Curr,SolarPanels2 Pow,SolarPanels2 Curr,SolarPanels3 Pow,SolarPanels3 Curr,Eclipse" << endl;
Mat3x3d Mag_SYS2SC;
Mag_SYS2SC = Mag_SC2SYS.transpose();
MagRaw_RTN_d = (T_RTN2Body.transpose())*Mag_SYS2SC*MagRaw_d;
MagRaw_RTN = MagRaw_RTN_d.cast<int>();
sensors_output.segment(18,3) = MagRaw_RTN;
sensors_file << fixed << sensors_output(0) << "," << sensors_output(1) << "," << sensors_output(2) << "," << sensors_output(3) << "," << sensors_output(4) << "," << sensors_output(5) << "," << sensors_output(6) << "," << sensors_output(7) << "," << sensors_output(8) << "," << sensors_output(9) << "," << sensors_output(10) << "," << sensors_output(11) << "," << sensors_output(12) << "," << sensors_output(13) << "," << sensors_output(14) << "," << sensors_output(15) << "," << sensors_output(16) << "," << sensors_output(17) << "," << sensors_output(18) << "," << sensors_output(19) << "," << sensors_output(20) << "," << sensors_output(21) << "," << sensors_output(22) << "," << sensors_output(23) << "," << sensors_output(24) << "," << sensors_output(25) << "," << sensors_output(26) << "," << panels_output(1) << "," << panels_output(2) << "," << panels_output(3) << "," << panels_output(4) << "," << panels_output(5) << "," << panels_output(6) << "," << panels_output(7) << endl;
// Torques (spacecraft body-fixed frame)
ofstream torques_file;
torques_file.open(csv_torques_name);
torques_file << "GPS Time [s],TenvX,TenvY,TenvZ,TenvR,TenvT,TenvN,TmX,TmY,TmZ,TwX,TwY,TwZ,TactX,TactY,TactZ,Mag. ontimes X,Mag. ontimes Y,Mag. ontimes Z,WheelCTRL X,WheelCTRL Y,WheelCTRL Z" << endl;
// Environmental torques in RTN frame
string csv_envtorques_name = csv_torques_name.replace(csv_torques_name.end() - 4,csv_torques_name.end(),"_EnvironmentRTN.csv");
ofstream env_torques_file;
env_torques_file.open(csv_envtorques_name);
env_torques_file << "GPS Time [s],GravR,GravT,GravN,MagR,MagT,MagN,SRP_R,SRP_T,SRP_N,DragR,DragT,DragN,T_R,T_T,T_N" << endl;
//////////////////////////// Matrices for csv files /////////////////////////
const int output_rows = SIM_DURATION/SIM_STEP;
int step = 0;
MatrixXd attstate_to_file = MatrixXd::Zero(output_rows+1,11);
attstate_to_file.row(step).segment(0,7) = attitudeRTN_state_vec;
attstate_to_file.row(step).segment(7,4) = stateQ.segment(0,4);
MatrixXd sensors_to_file = MatrixXd::Zero(output_rows+1,34);
sensors_to_file.row(step).segment(0,27) = sensors_output.segment(0,27).cast<double>();
sensors_to_file.row(step).segment(27,7) = panels_output.segment(1,7);
MatrixXd torques_to_file = MatrixXd::Zero(output_rows+1,22);
torques_to_file(step,0) = ini_GPStime;
torques_to_file.row(step).segment(1,3) = Torque_env.segment(12,3);
torques_to_file.row(step).segment(4,3) = TenvRTN.segment(12,3);
torques_to_file.row(step).segment(7,3) = Tm;
torques_to_file.row(step).segment(10,3) = Tw;
torques_to_file.row(step).segment(13,3) = T_act;
torques_to_file.row(step).segment(16,3) = m_ontimes;
torques_to_file.row(step).segment(19,3) = wheelspeedCMD;
MatrixXd envtorquesRTN_to_file = MatrixXd::Zero(output_rows+1,16);
envtorquesRTN_to_file(step,0) = ini_GPStime;
envtorquesRTN_to_file.row(step).segment(1,15) = TenvRTN;
step++;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////// ATTITUDE PROPAGATION LOOP ///////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VectorXd prop_state;
int barwidth = 100;
for( double t = 0.0 ; t < SIM_DURATION ; t += SIM_STEP )
{
//////////////// Do propagation step /////////////////
m_ontimes = Vec3d::Zero();
//forstart = chrono::high_resolution_clock::now();
ephem_row = loaded_ephem.row(step);
current_orbstate = ephem_row.segment(2,6);
posECI = current_orbstate.segment(0,3);
velECI = current_orbstate.segment(3,3);
//omega_orb = velECI.norm()/posECI.norm();
current_orbstateECEF = ephem_row.segment(8,6);
posECEF = current_orbstateECEF.segment(0,3);
GPStime = ephem_row(1);
//////////////// Do propagation step /////////////////
orbstateECI_ECEF << current_orbstate, current_orbstateECEF;
// Insert orbit state
SC_Attitude.orbstate = orbstateECI_ECEF;
// Insert reaction wheels' angular momentum
SC_Attitude.hw = hw_SC;
// Insert total torque generated by the actuators
SC_Attitude.Maneuver(T_act);
// Integration of the trajectory at time t
SC_Attitude.Integrate(t,SIM_STEP);
// Environmental torques
Torque_env = SC_Attitude.Torque_env;
// Current attitude state
prop_state = SC_Attitude.state;
#ifdef VERBOSE
cout << "Step: " << t << "\n" << endl;
#ifdef USE_QUATERNION
cout << prop_state(0) << " " << prop_state(1) << " " << prop_state(2) << " " << prop_state(3) << " " << prop_state(4)*RAD2DEG << " " << prop_state(5)*RAD2DEG << " " << prop_state(6)*RAD2DEG << endl;
#else
cout << prop_state(0)*RAD2DEG << " " << prop_state(1)*RAD2DEG << " " << prop_state(2)*RAD2DEG << " " << prop_state(3)*RAD2DEG << " " << prop_state(4)*RAD2DEG << " " << prop_state(5)*RAD2DEG << endl;
#endif
#endif
//////////////// Display propagation progress /////////////////
RunStatusBar(t, SIM_DURATION, barwidth);
/////////////// ADCS sensors/actuators inputs/outputs ////////////////
#ifdef USE_QUATERNION
stateQ.segment(0,4) = prop_state.segment(0,4);
stateQ.segment(4,3) = prop_state.segment(4,3);
#else
ECItoBody = RotationMatrix321(prop_state(0), prop_state(1), prop_state(2));
stateQ.segment(0,4) = RotationMatrix2Quaternion(ECItoBody);
stateQ.segment(4,3) = prop_state.segment(3,3);
//cout << "prop_state: " << t << " " << prop_state(0)*RAD2DEG << " " << prop_state(1)*RAD2DEG << " " << prop_state(2)*RAD2DEG << endl;
//cout << "ECItoBody: " << t << " " << ECItoBody << endl;
#endif
// Time
UnixTime = GPS2Unix(GPStime);
// CSS