COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
propagator_simple.cpp File Reference
#include "physics/physicslib.h"
#include "math/mathlib.h"
#include "agent/agentclass.h"
#include "support/jsonlib.h"
#include "support/datalib.h"
#include "support/print_utils.h"
#include <sys/stat.h>
#include <typeinfo>
Include dependency graph for propagator_simple.cpp:

Functions

void printMjdAndDateTime (double mjd)
 
int main (int argc, char *argv[])
 

Variables

static string node_name = "cubesat1"
 
static string agent_name = "propagator_simple"
 
static char buffer [255] = ""
 
static Agentagent
 
static vector< eventstruceventdict
 
static vector< eventstrucevents
 
static string mainjstring ={0,0,0}
 

Function Documentation

void printMjdAndDateTime ( double  mjd)
67  {
68  cout << mjdToGregorianFormat(mjd) << setprecision(10) << " (MJD=" << mjd << ")"; // << endl;
69 }
string mjdToGregorianFormat(double mjd)
Definition: timelib.cpp:1417
double mjd
Definition: udp_send.cpp:41
int main ( int  argc,
char *  argv[] 
)
71  {
72 
73  // for propagator
74  uint32_t order = 6; // integration order
75  int32_t mode = 1; // attitude mode (0 - propagate?, 1-LVLH, ...)
76  double dt = 1; // >> check with Eric .1 or 1?
77 
78  double triger_time = 0;
79  double iteration_rate = 1; //in sec
80  double trigger_offset_ms = 10; // ms before sending the command
81  uint32_t precision = static_cast <uint32_t>(1/iteration_rate);
82  double sleep_time = 0.7*iteration_rate;
83  double set_time = 0;
84  double elapsed_seconds = 0;
85  double utc_now;
86  int32_t iretn;
87 
88  // for time profiling
89  ElapsedTime ep;
90  ep.print = false;
91 
92  PrintUtils print;
93 
94  locstruc state; // Set state container
95  locstruc initState; // Set initial state
96 
97  // break down state vector
98  // position
99  double x = state.pos.eci.s.col[0];
100  double y = state.pos.eci.s.col[1];
101  double z = state.pos.eci.s.col[2];
102 
103  // velocity
104  double vx = state.pos.eci.v.col[0];
105  double vy = state.pos.eci.v.col[1];
106  double vz = state.pos.eci.v.col[2];
107 
108  // attitude quaternion
109  double q1 = state.att.icrf.s.d.x;
110  double q2 = state.att.icrf.s.d.y;
111  double q3 = state.att.icrf.s.d.z;
112  double q4 = state.att.icrf.s.w;
113 
114  // --------------------------------------------------------------
115 
116  switch (argc)
117  {
118  case 1:
119  // use default node 'cubesat1'
120  break;
121  case 2:
122  // use node given by user input
123  node_name = argv[1];
124  break;
125  case 3:
126  // use time given by user input
127  // mjdstart = atof(argv[2]);
128  break;
129  default:
130  cout << "Usage: propogator_simple nodename [mjd|0]" << endl;
131  exit (-1);
132  }
133 
134  // cout << "-----------------------------------------------" << endl;
135  // cout << "| COSMOS Propagator Example |" << endl;
136  // cout << "-----------------------------------------------" << endl;
137 
138  // Establish the command channel and heartbeat
140 
141  if (agent->last_error()<0)
142  {
143  cout<<"agent_setup_server failed (returned <"<<AGENT_ERROR_JSON_CREATE<<">)"<<endl;
145  }
146 
147  //agent->cinfo->node.phys.mode = mode;
148  //json_clone(cinfo);
149 
150  //load_dictionary(eventdict, cinfo, (char *)"events.dict");
151 
152 
153  // start timer
154  ep.tic();
155 
156  // ------------------------------------
157  // load state.ini
158 
159  cout << "Loading state.ini info from " << node_name << " node (node must exist in cosmos/nodes) " << endl;
160 
161  struct stat fstat;
162  FILE* fdes;
163  string fname = get_nodedir((node_name.c_str()));
164  fname += "/state.ini";
165 
166  pos_clear(initState);
167 
168  if ((iretn=stat(fname.c_str(), &fstat)) == 0 && (fdes=fopen(fname.c_str(),"r")) != nullptr)
169  {
170  char* ibuf = static_cast <char *>(calloc(1,static_cast <size_t>(fstat.st_size)+1));
171 
172  fread(ibuf, 1, static_cast <size_t>(fstat.st_size), fdes);
173  // fgets(ibuf,fstat.st_size,fdes);
174  json_parse(ibuf, agent->cinfo);
175 
176  free(ibuf);
177 
178  initState.pos.eci = agent->cinfo->node.loc.pos.eci;
179  //initState.att.icrf = agent->cinfo->node.loc.att.icrf;
180  initState.utc = agent->cinfo->node.loc.pos.eci.utc;
181 
182  cout << "Sucessfully found state.ini" << endl;
183 
184  cout << "UTC from state.ini : ";
185  printMjdAndDateTime(initState.utc);
186  print.endline();
187  print.vector("Initial ECI Position : ", initState.pos.eci.s, " m", 3);print.endline();
188  print.vector("Initial ECI Velocity : ", initState.pos.eci.v, " m/s",3);print.endline();
189  //print.end();
190  cout << "-----------------------------------------------" << endl;
191 
192  }
193  else
194  {
195  printf("Unable to open state.ini\n");
196  exit (-1);
197  }
198 
199  ep.toc("load state.ini");
200 
201  // propagate the changes to all frames
202  initState.pos.eci.pass++;
203  pos_eci(&initState);
204 
205  // initialize propagator
206  //CT 2017-06-26: couldn't find a gj_handle data type for this function to use
207  gj_handle gjh;
209  order,
210  mode,
211  dt,
212  currentmjd(),// use curretn time instead of initState.utc for this demo, otherwise it will take a long time to update
213  initState.pos.eci,
214  initState.att.icrf,
215  agent->cinfo->node.phys,
216  agent->cinfo->node.loc);
217 
218  // propagate state to current time so we get an updated state vector
219  // to initialize the GPS sim
220  //CT 2017-06-26: couldn't find a gj_handle data type for this function to use
222 
223  //get initial sim tim
224  double mjd_start_sim = currentmjd();
225 
226 
227  // set SOH
228  std::string soh = "{\"node_loc_utc\","
229  "\"node_loc_pos_eci\","
230  "\"node_loc_att_icrf\"}" ;
231 
232  agent->set_sohstring(soh);
233 
234  // --------------------------------------------------------------
235  while (agent->running()){ //for agent use
236  // get the elapsed seconds from the sim start
237  utc_now = currentmjd(0);
238  elapsed_seconds = (utc_now - mjd_start_sim)*86400; //+ 50./1000.
239 
240  set_time = floor(elapsed_seconds*precision)/precision + iteration_rate;
241  triger_time = set_time - trigger_offset_ms/1000.;
242 
243  if (elapsed_seconds > triger_time){ // send the command 100 ms before the set time
244 
245  // propagate
247  state = agent->cinfo->node.loc;
248 
249  // break down state vector for this demo
250  x = state.pos.eci.s.col[0];
251  y = state.pos.eci.s.col[1];
252  z = state.pos.eci.s.col[2];
253 
254  vx = state.pos.eci.v.col[0];
255  vy = state.pos.eci.v.col[1];
256  vz = state.pos.eci.v.col[2];
257 
258  //
259  q1 = state.att.geoc.s.d.x;
260  q2 = state.att.geoc.s.d.y;
261  q3 = state.att.geoc.s.d.z;
262  q4 = state.att.geoc.s.w;
263 
264  sprintf(buffer,"%s,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f",
265  mjdToGregorianDDMmmYYYY(utc_now).c_str(),
266  x,y,z,
267  vx,vy,vz,
268  q1,q2,q3,q4);
269 
270  //cout << "------------------------------------------------" << endl;
271  //print time
272  cout << mjdToGregorianFormat(currentmjd());
273  cout << " | ";
274  cout << seconds2DDHHMMSS(elapsed_seconds) << " | ";
275 
276  // print state
277  print.vector("pos:",state.pos.geoc.s,1e-3," km | ",0,6);
278  print.vector("vel:", state.pos.geoc.v,1e-3, " km/s | ", 2, 3);
279 
280  // TODO: print attitude
281 
282  // print magnetic field in micro-Tesla
283  print.vector("mag field:", agent->cinfo->node.loc.pos.bearth, 1e6, " uT", 1,3);
284 
285  // TODO: print solar vector
286 
287  // TODO: print moon vector
288 
289  // TODO: print nadir vector
290 
291  print.endline();
292 
293  agent->post(Agent::AgentMessage::SOH, json_of_table(mainjstring, agent->sohtable, agent->cinfo));
294 
295 
296  COSMOS_SLEEP(sleep_time); // sleep for 70% of the iteration time
297 
298  }
299 
300  } // end while
301 
302  return iretn;
303 }
rvector bearth
Earth magnetic vector in ITRS for this time and location.
Definition: convertdef.h:754
double y
Y value.
Definition: vector.h:114
string mjdToGregorianFormat(double mjd)
Definition: timelib.cpp:1417
Definition: eci2kep_test.cpp:33
qatt geoc
Definition: convertdef.h:828
Gauss-Jackson integration handle.
Definition: physicsdef.h:98
Definition: print_utils.h:60
y
Definition: inputfile.py:6
ElapsedTime dt
Definition: agent_file3.cpp:183
double utc
UTC of Position.
Definition: convertdef.h:161
int32_t json_parse(string jstring, cosmosstruc *cinfo)
Parse JSON using Name Space.
Definition: jsonlib.cpp:4799
char ibuf[1000]
Definition: razor_imu.cpp:43
string seconds2DDHHMMSS(double elapsed_seconds)
Convert Elapsed Time in Seconds to Human Readable Format (used for GPS simulator) ...
Definition: timelib.cpp:1514
cvector d
Orientation.
Definition: vector.h:405
const char * json_of_table(string &jstring, vector< jsonentry * > table, cosmosstruc *cinfo)
Create JSON stream from entries.
Definition: jsonlib.cpp:8733
uint32_t pass
pass indicator: allows synchronization with other attitude and position values.
Definition: convertdef.h:170
int iretn
Definition: rw_test.cpp:37
void gauss_jackson_init_eci(gj_handle &gjh, uint32_t order, int32_t mode, double dt, double utc, cartpos ipos, qatt iatt, physicsstruc &physics, locstruc &loc)
Initialize Gauss-Jackson orbit using ECI state vector.
Definition: physicslib.cpp:2479
cartpos geoc
Definition: convertdef.h:739
double utc
Master time for location, in Modified Julian Day.
Definition: convertdef.h:879
double x
X value.
Definition: vector.h:112
int32_t set_sohstring(string list)
Set Limited SOH string.
Definition: agentclass.cpp:641
void vector(string prefix, rvector v, double scale, string suffix, int precision, int fieldwidth)
Definition: print_utils.cpp:85
nodestruc node
Structure for summary information in node.
Definition: jsondef.h:4220
uint16_t running()
Check if we&#39;re supposed to be running.
Definition: agentclass.cpp:391
int32_t pos_clear(locstruc *loc)
Initialize posstruc.
Definition: convertlib.cpp:77
rvector s
Location.
Definition: convertdef.h:163
int32_t post(messstruc mess)
Post a Cosmos::Agent::messstruc.
Definition: agentclass.cpp:2074
bool print
Definition: elapsedtime.h:97
attstruc att
attstruc for this time.
Definition: convertdef.h:883
void tic()
ElapsedTime::tic, equivalent to matlab to start a stopwatch timer.
Definition: elapsedtime.cpp:166
static char buffer[255]
Definition: propagator_simple.cpp:60
void endline()
Definition: print_utils.cpp:606
static char fname[100]
Definition: geomag.cpp:89
Definition: agentclass.h:139
qatt icrf
Definition: convertdef.h:830
locstruc loc
Location structure.
Definition: jsondef.h:3596
int32_t pos_eci(locstruc *loc)
Set ECI position.
Definition: convertlib.cpp:258
vector< locstruc > gauss_jackson_propagate(gj_handle &gjh, physicsstruc &physics, locstruc &loc, double tomjd)
Definition: physicslib.cpp:2871
x
Definition: inputfile.py:6
gj_handle gjh
Definition: agent_node.cpp:80
void printMjdAndDateTime(double mjd)
Definition: propagator_simple.cpp:67
double w
Rotation.
Definition: vector.h:407
static Agent * agent
Definition: propagator_simple.cpp:62
Definition: elapsedtime.h:62
double currentmjd(double offset)
Current UTC in Modified Julian Days.
Definition: timelib.cpp:65
string get_nodedir(string node, bool create_flag)
Get Current Node Directory.
Definition: datalib.cpp:1572
posstruc pos
posstruc for this time.
Definition: convertdef.h:881
static string node_name
Definition: propagator_simple.cpp:50
double z
Z value.
Definition: vector.h:116
int32_t last_error()
Definition: agentclass.cpp:414
quaternion s
0th derivative: Quaternion
Definition: convertdef.h:479
double col[3]
Definition: vector.h:55
static string agent_name
Definition: propagator_simple.cpp:51
cosmosstruc * cinfo
Definition: agentclass.h:346
cartpos eci
Definition: convertdef.h:737
string mjdToGregorianDDMmmYYYY(double mjd)
Definition: timelib.cpp:1479
double toc()
ElapsedTime::toc, equivalent to matlab to stop a stopwatch timer.
Definition: elapsedtime.cpp:174
#define AGENT_ERROR_JSON_CREATE
Definition: cosmos-errno.h:105
vector< jsonentry * > sohtable
State of Health element vector.
Definition: agentclass.h:156
static string mainjstring
Definition: propagator_simple.cpp:65
Definition: convertdef.h:876
physicsstruc phys
Definition: jsondef.h:3597
rvector v
Velocity.
Definition: convertdef.h:165

Variable Documentation

string node_name = "cubesat1"
static
string agent_name = "propagator_simple"
static
char buffer[255] = ""
static
Agent* agent
static
vector<eventstruc> eventdict
static
vector<eventstruc> events
static
string mainjstring ={0,0,0}
static