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

Macros

#define POLLBUFSIZE   20000
 

Functions

void endline ()
 
int main (int argc, char *argv[])
 

Variables

Agentagent
 
gj_handle gjh
 
std::vector< eventstruceventdict
 
std::vector< eventstrucevents
 
std::string mainjstring
 

Macro Definition Documentation

#define POLLBUFSIZE   20000

Function Documentation

void endline ( )
47  {
48  std::cout << std::endl;
49 }
int main ( int  argc,
char *  argv[] 
)
52 {
53  std::string node;
54  int32_t order = 6;
55  int32_t mode = 1; // attitude mode (0 - propagate?, 1-LVLH, ...)
56  double mjdnow;
57  double mjdstart = -1.;
58  double dt = .1;
59  int32_t iretn;
60 // bool master_timer = false;
61 
62  switch (argc)
63  {
64  case 3:
65  mjdstart = atof(argv[2]);
66  case 2:
67  node = argv[1];
68  break;
69  default:
70  std::cout << "Usage: propogator nodename [mjd|0]" << std::endl;
71  exit (-1);
72  break;
73  }
74 
75  if (!(agent = new Agent(node, "physics", .1, AGENTMAXBUFFER)))
76  {
77  printf("Failed to setup server for node %s: %d\n", node.c_str(), AGENT_ERROR_JSON_CREATE);
79  }
80 
81  agent->cinfo->node.phys.mode = mode;
82 
83  load_dictionary(eventdict, agent->cinfo, (char *)"events.dict");
84 
85  // Set initial state
86  locstruc iloc;
87 
88  pos_clear(iloc);
89 
90  struct stat fstat;
91  FILE* fdes;
92  std::string fname = get_nodedir(node);
93  fname += "/state.ini";
94  if ((iretn=stat(fname.c_str(), &fstat)) == 0 && (fdes=fopen(fname.c_str(),"r")) != NULL)
95  {
96  char* ibuf = (char *)calloc(1,fstat.st_size+1);
97  size_t nbytes = fread(ibuf, 1, fstat.st_size, fdes);
98 // fgets(ibuf,fstat.st_size,fdes);
99  if (nbytes)
100  {
101  json_parse(ibuf, agent->cinfo);
102  }
103  free(ibuf);
105  iloc = agent->cinfo->node.loc;
106 // iloc.pos.eci = agent->cinfo->node.loc.pos.eci;
107 // iloc.att.icrf = agent->cinfo->node.loc.att.icrf;
108 // iloc.utc = agent->cinfo->node.loc.utc;
109 
110 // print_vector("Initial State Vector Position: ", iloc.pos.eci.s.col[0], iloc.pos.eci.s.col[1], iloc.pos.eci.s.col[2], "km");
111  //std::cout << "Initial State Vector Pos: [" << iloc.pos.eci.s.col[0] << ", " << iloc.pos.eci.s.col[1] << ", " << iloc.pos.eci.s.col[2] << "] km " << std::endl;
112  std::cout << "Initial State Vector Vel: [" << iloc.pos.eci.v.col[0] << ", " << iloc.pos.eci.v.col[1] << ", " << iloc.pos.eci.v.col[2] << "] km" << std::endl;
113  std::cout << "Initial MJD: " << std::setprecision(10) << iloc.utc << std::endl;
114  }
115  else
116  {
117  printf("Unable to open state.ini\n");
118  exit (-1);
119  }
120 
121 #define POLLBUFSIZE 20000
122  Agent::messstruc mess;
123 
124  iretn = agent->readring(mess, Agent::AgentMessage::ALL,1);
125  switch ((Agent::AgentMessage)iretn)
126  {
127  case Agent::AgentMessage::SOH:
128  case Agent::AgentMessage::BEAT:
129  {
130  std::string tbuf = json_convert_string(json_extract_namedmember(mess.jdata, "agent_name"));
131  if (!tbuf.empty() && tbuf == "physics")
132  {
133  tbuf = json_convert_string(json_extract_namedmember(mess.jdata, "node_utcoffset"));
134  if (!tbuf.empty())
135  {
136  agent->cinfo->node.utcoffset = atof(tbuf.c_str());
137  printf("slave utcoffset: %f\n", agent->cinfo->node.utcoffset);
138  }
139  }
140  else
141  {
142  if (mjdstart == -1.)
143  {
145  }
146  else if (mjdstart == 0.)
147  {
148  agent->cinfo->node.utcoffset = 0.;
149  }
150  else
151  {
152  agent->cinfo->node.utcoffset = mjdstart - currentmjd(0.);
153  }
154  //printf("master utcoffset: %f\n", agent->cinfo->node.utcoffset);
155  std::cout << "master utcoffset: " << std::setprecision(5) << agent->cinfo->node.utcoffset << std::endl;
156 // master_timer = true;
157  }
158  break;
159  }
160  default:
161  if (mjdstart == -1.)
162  {
164  }
165  else if (mjdstart == 0.)
166  {
167  agent->cinfo->node.utcoffset = 0.;
168  }
169  else
170  {
171  agent->cinfo->node.utcoffset = mjdstart - currentmjd(0.);
172  }
173  //printf("master utcoffset: %f\n", agent->cinfo->node.utcoffset);
174  std::cout << "master utcoffset: " << agent->cinfo->node.utcoffset << std::endl;
175 // master_timer = true;
176  break;
177  }
178 
179  mjdnow = currentmjd(agent->cinfo->node.utcoffset);
180  double sohtimer = mjdnow;
181 
182  if (mjdnow < iloc.utc)
183  {
184  hardware_init_eci(agent->cinfo, iloc);
185  gauss_jackson_init_eci(gjh, order ,mode, -dt, iloc.utc,iloc.pos.eci, iloc.att.icrf, agent->cinfo->node.phys, agent->cinfo->node.loc);
186 
187  //printf("Initialize backwards %f days\n", (agent->cinfo->node.loc.utc-mjdnow));
188  std::cout << "Initialize backwards " << agent->cinfo->node.loc.utc-mjdnow << "days" << std::endl;
189 
193  iloc.utc = agent->cinfo->node.loc.utc;
194  iloc.pos.eci = agent->cinfo->node.loc.pos.eci;
195  iloc.att.icrf = agent->cinfo->node.loc.att.icrf;
196  }
197 
198  double step = 8.64 * (mjdnow-iloc.utc);
199  if (step > 60.)
200  {
201  step = 60.;
202  }
203  if (step < .1)
204  {
205  step = .1;
206  }
207 
208  //printf("Initialize forwards %f days, steps of %f\n", (mjdnow-iloc.utc), step);
209  std::cout << "Initialize forwards " << (mjdnow-iloc.utc) << " days, steps of " << step << std::endl;
210 
211  std::cout << "Initialize Hardware Simulation " << std::endl;
212  hardware_init_eci(agent->cinfo, iloc);
213 
214  std::cout << "Initialize Gauss Jackson Propagator " << std::endl;
215  gauss_jackson_init_eci(gjh, order, mode, step, iloc.utc ,iloc.pos.eci, iloc.att.icrf, agent->cinfo->node.phys, agent->cinfo->node.loc);
216 
217  std::cout << "Start Hardware Simulation " << std::endl;
219 
220  std::cout << "Start Orbital Propagator " << std::endl;
222 
223  std::cout << "Start Hardware Simulation 2" << std::endl;
225 
226  pos_clear(iloc);
227  iloc.pos.eci = agent->cinfo->node.loc.pos.eci;
228  iloc.att.icrf = agent->cinfo->node.loc.att.icrf;
229  iloc.utc = agent->cinfo->node.loc.pos.eci.utc;
230 
231  std::cout << "Start Hardware Simulation 3" << std::endl;
232  hardware_init_eci(agent->cinfo, iloc);
233 
234  std::cout << "Initialize Gauss Jackson Propagator" << std::endl;
235  gauss_jackson_init_eci(gjh, order, mode, dt, iloc.utc ,iloc.pos.eci, iloc.att.icrf, agent->cinfo->node.phys, agent->cinfo->node.loc);
236 
237  mjdnow = currentmjd(agent->cinfo->node.utcoffset);
238 
239  std::string sohstring = json_list_of_soh(agent->cinfo);
240  agent->set_sohstring(sohstring.c_str());
241 
242  std::cout << "Start Agent" << std::endl;
243  while (agent->running())
244  {
245  sohtimer += 1./86400.;
246  mjdnow = currentmjd(agent->cinfo->node.utcoffset);
248 
250 
253  agent->post(Agent::AgentMessage::SOH, json_of_table(mainjstring, agent->sohtable, agent->cinfo));
254  double dsleep = 1000000. * 86400.*(sohtimer - mjdnow);
255 
256  if (dsleep > 0.)
257  {
258  COSMOS_USLEEP(dsleep);
259  }
260 
261  COSMOS_SLEEP(0.01);
262  }
263  agent->shutdown();
264 }
std::vector< eventstruc > events
Definition: propagator.cpp:44
string json_list_of_soh(cosmosstruc *cinfo)
Definition: jsonlib.cpp:9252
gj_handle gjh
Definition: propagator.cpp:42
AgentMessage
Type of Agent Message. Types > 127 are binary.
Definition: agentclass.h:216
size_t calc_events(vector< eventstruc > &dictionary, cosmosstruc *cinfo, vector< eventstruc > &events)
Calculate current Events.
Definition: jsonlib.cpp:11356
std::vector< eventstruc > eventdict
Definition: propagator.cpp:43
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
const char * json_of_table(string &jstring, vector< jsonentry * > table, cosmosstruc *cinfo)
Create JSON stream from entries.
Definition: jsonlib.cpp:8733
void simulate_hardware(cosmosstruc *cinfo, vector< locstruc > &locvec)
Simulate Hardware data - multiple.
Definition: physicslib.cpp:937
int32_t update_target(cosmosstruc *cinfo)
Update Track list.
Definition: jsonlib.cpp:11262
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
Agent * agent
Definition: propagator.cpp:41
double utc
Master time for location, in Modified Julian Day.
Definition: convertdef.h:879
string json_extract_namedmember(string json, string token)
Extract JSON value matching name.
Definition: jsonlib.cpp:4417
int32_t set_sohstring(string list)
Set Limited SOH string.
Definition: agentclass.cpp:641
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
static string sohstring
Definition: agent_cpu-2-0.cpp:72
int32_t pos_clear(locstruc *loc)
Initialize posstruc.
Definition: convertlib.cpp:77
int32_t post(messstruc mess)
Post a Cosmos::Agent::messstruc.
Definition: agentclass.cpp:2074
attstruc att
attstruc for this time.
Definition: convertdef.h:883
double utcoffset
MJD Offset between system UT and simulated UT.
Definition: jsondef.h:3590
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
size_t load_dictionary(vector< eventstruc > &dict, cosmosstruc *cinfo, const char *file)
Load Event Dictionary.
Definition: jsonlib.cpp:11313
int32_t shutdown()
Shutdown agent gracefully.
Definition: agentclass.cpp:366
vector< locstruc > gauss_jackson_propagate(gj_handle &gjh, physicsstruc &physics, locstruc &loc, double tomjd)
Definition: physicslib.cpp:2871
std::string mainjstring
Definition: propagator.cpp:45
Storage for messages.
Definition: agentclass.h:272
int32_t loc_update(locstruc *loc)
Synchronize all frames in location structure.
Definition: convertlib.cpp:2294
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
string jdata
Definition: agentclass.h:277
void hardware_init_eci(cosmosstruc *cinfo, locstruc &loc)
Initialize Hardware.
Definition: physicslib.cpp:877
string json_convert_string(string object)
Convert JSON to string.
Definition: jsonlib.cpp:4557
double col[3]
Definition: vector.h:55
cosmosstruc * cinfo
Definition: agentclass.h:346
cartpos eci
Definition: convertdef.h:737
#define AGENTMAXBUFFER
Maximum AGENT transfer buffer size.
Definition: jsondef.h:438
#define AGENT_ERROR_JSON_CREATE
Definition: cosmos-errno.h:105
vector< jsonentry * > sohtable
State of Health element vector.
Definition: agentclass.h:156
int32_t mode
Definition: jsondef.h:3436
static string node
Definition: agent_monitor.cpp:126
Definition: convertdef.h:876
physicsstruc phys
Definition: jsondef.h:3597
int32_t readring(messstruc &message, AgentMessage type=Agent::AgentMessage::ALL, float waitsec=1., Where where=Where::TAIL, string proc="", string node="")
Check Ring for message.
Definition: agentclass.cpp:2395
rvector v
Velocity.
Definition: convertdef.h:165

Variable Documentation

Agent* agent
gj_handle gjh
std::vector<eventstruc> eventdict
std::vector<eventstruc> events
std::string mainjstring