COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
agent_001.cpp File Reference
#include "support/configCosmos.h"
#include "support/elapsedtime.h"
#include "support/timeutils.h"
#include "agent/agentclass.h"
#include <iostream>
#include <string>
Include dependency graph for agent_001.cpp:

Functions

int32_t sample_agent_request_function (string &request, string &response, Agent *cdata)
 
int main (int argc, char **argv)
 

Variables

static uint64_t request_counter = 10000
 
static Agentagent
 ensure the Agent constructor creates only one instance per process More...
 
string node_name = "sat_001"
 
string agent_name = "agent_001"
 
string node_agent_name = "["+node_name+":"+agent_name+"]"
 

Function Documentation

int32_t sample_agent_request_function ( string &  request,
string &  response,
Agent cdata 
)
231 {
232  // Send response back to agent_002
233  response = "I am the one they call [sat_001:agent_001]";
234  //JIMNOTE: there is a actually a bug with the string request variable supplied... it is not passed properly to the user's request function after multiple different requests have been made. the request is parsed properly, i.e. this function is still called, but the request string contains overflow characters from previous requests beyond the null character. somewhere a string.resize() call is missing. HACKY FIX: mixing char[] within a string only prints correctly if cast as c_str(), so do that for now.
235  cout << "[" << node_name << ":" << agent_name << "]"<<" received <"<<request.c_str()<<"> request!"<<endl;
236  cout << "[" << node_name << ":" << agent_name << "] transmit <"<<request.c_str()<<"> response:\n TX: \"" << response << "\" ("<<response.size()<<" bytes)"<<endl;
237 
238  // Increment counter of how many requests were run
239  request_counter--;
240 
241  return 0;
242 }
static uint64_t request_counter
Definition: agent_001.cpp:41
string node_name
Definition: agent_001.cpp:46
string agent_name
Definition: agent_001.cpp:47
int main ( int  argc,
char **  argv 
)
51 {
52  // construct agent
53  cout << node_agent_name << " starting..."<<endl;
54  agent = new Agent(node_name, agent_name, 1.);
55 
56  // exit with error if unable to start agent
57  if(agent->last_error() < 0) {
58  cout<<"error: unable to start "<<node_agent_name<<" ("<<agent->last_error()<<") "<<cosmos_error_string(agent->last_error())<<endl;
59  exit(1);
60  } else {
61  cout << node_agent_name << " started."<<endl;
62  }
63 
64  // add custom request functions for this agent
65  string request_name = "identify_yourself";
66  agent->add_request(request_name, sample_agent_request_function, "\n\t\trequest to support the reporting of identification");
67 
68  // try to locate a specific agent (agent_002)
69  string agent_target = "agent_002"; // The name of the agent this agent will send requests to
70  beatstruc agent_target_heartbeat = agent->find_agent(node_name, agent_target, 2.);
71  //cout<<"["<<node_name<<":"<<agent_name<<"] looking for ["<<node_name<<":"<<agent_target<<"]..."<<endl;
72 
73  if(agent->debug_level>1) {
74  cout<<"A agent "<<agent_target<<" beatstruc:"<<endl;
75  cout<<agent_target_heartbeat;
76  }
77 
78  string response; // Variable to store agent_002's response
79 
80  // Start executing the agent
81  agent->cinfo->add_name("Short UTC", &agent->cinfo->node.loc.utc, "double");
82  agent->cinfo->add_name("Longest Ever UTC", &agent->cinfo->node.loc.utc, "double");
83  agent->cinfo->set_value<double>("Short UTC", 213.0);
84 
85  // test set_json with double
86  //agent->cinfo->set_json("Short UTC", "{\"Short UTC\": 214.5}");
87  agent->cinfo->set_json("{\"Short UTC\": 214.5}");
88 
89  cout<<"Short UTC = <"<<agent->cinfo->get_value<double>("Short UTC")<<">"<<endl;
90  cout<<"Longest Ever UTC = <"<<agent->cinfo->get_value<double>("Longest Ever UTC")<<">"<<endl;
91 
92  //agent->cinfo->add_name("cosmosdata", &*agent->cinfo, "cosmosstruc");
93  agent->cinfo->add_name("cosmosdata", agent->cinfo, "cosmosstruc");
94  cout<<"cosmosdata = <"<<agent->cinfo->get_json_pretty<cosmosstruc>("cosmosdata")<<">"<<endl;
95 
96  agent->cinfo->add_name("My Favorite Users", &agent->cinfo->user, "vector<userstruc>");
97  cout<<"Old User Data = <"<<agent->cinfo->get_json<vector<userstruc>>("My Favorite Users")<<">"<<endl;
98 
99  string raw = "{\"My Favorite Users\": [{\"cpu\": \"\", \"name\": \"\", \"node\": \"\", \"tool\": \"\"},{\"cpu\": \"cpu2\", \"name\": \"name2\", \"node\": \"node2\", \"tool\": \"tool2\"}]}";
100  agent->cinfo->set_json(raw);
101 
102  cout<<"New User Data = <"<<agent->cinfo->get_json<vector<userstruc>>("My Favorite Users")<<">"<<endl;
103  cout<<"New User Data = <"<<agent->cinfo->get_json<userstruc>("user[1]")<<">"<<endl;
104  //cout<<"names = "<<agent->cinfo->names.size()<<endl;
105  //cout<<"names = "<<agent->cinfo->names.size()<<endl;
106  //agent->cinfo->print_all_names_types_values();
107  //cout<<"names = "<<agent->cinfo->names.size()<<endl;
108 
109 // test orbital dynamics for simulation
110 
111 // km
112  agent->cinfo->P_pos_t = 6285.0;
113  agent->cinfo->Q_pos_t = 3628.6;
114  agent->cinfo->W_pos_t = 0.0;
115 // km/s
116  agent->cinfo->P_vel_t = -2.4913;
117  agent->cinfo->Q_vel_t = 11.290;
118  agent->cinfo->W_vel_t = 0.0;
119 
120 
121  agent->cinfo->O = 40.0 * ( M_PI / 180.0);
122  agent->cinfo->i = 30.0 * ( M_PI / 180.0);
123  agent->cinfo->w = 60.0 * ( M_PI / 180.0);
124 
126  cout<<"("<<agent->cinfo->R_0_0<<", "<<agent->cinfo->R_0_1<<", "<<agent->cinfo->R_0_2<<")"<<endl;
127  cout<<"("<<agent->cinfo->R_1_0<<", "<<agent->cinfo->R_1_1<<", "<<agent->cinfo->R_1_2<<")"<<endl;
128  cout<<"("<<agent->cinfo->R_2_0<<", "<<agent->cinfo->R_2_1<<", "<<agent->cinfo->R_2_2<<")"<<endl;
129 
131 
132  cout<<"("<<agent->cinfo->I_pos_t<<", "<<agent->cinfo->J_pos_t<<", "<<agent->cinfo->K_pos_t<<")"<<endl;
133  cout<<"("<<agent->cinfo->I_vel_t<<", "<<agent->cinfo->J_vel_t<<", "<<agent->cinfo->K_vel_t<<")"<<endl;
134 
135  agent->cinfo->P_pos_t = -1;
136  agent->cinfo->Q_pos_t = -1;
137  agent->cinfo->W_pos_t = -1;
138 
139  agent->cinfo->P_vel_t = -1;
140  agent->cinfo->Q_vel_t = -1;
141  agent->cinfo->W_vel_t = -1;
142 
143  cout<<"("<<agent->cinfo->P_pos_t<<", "<<agent->cinfo->Q_pos_t<<", "<<agent->cinfo->W_pos_t<<")"<<endl;
144  cout<<"("<<agent->cinfo->P_vel_t<<", "<<agent->cinfo->Q_vel_t<<", "<<agent->cinfo->W_vel_t<<")"<<endl;
146  cout<<"("<<agent->cinfo->P_pos_t<<", "<<agent->cinfo->Q_pos_t<<", "<<agent->cinfo->W_pos_t<<")"<<endl;
147  cout<<"("<<agent->cinfo->P_vel_t<<", "<<agent->cinfo->Q_vel_t<<", "<<agent->cinfo->W_vel_t<<")"<<endl;
148 
149 cout<<"Okay, that co-ord transformation works!"<<endl<<endl;
150 
151 cout<<"Now to try to get the orbis of ISS..."<<endl;
152 
153  // shorter name for cosmosdata
154  cosmosstruc* c = agent->cinfo;
155 
156  // here is a TLE
157  // try to figure out the orbit!
158  // 1 25544U 98067A 20358.21750033 .00016717 00000-0 10270-3 0 9095
159  // 2 25544 51.6435 133.1544 0001122 148.6515 211.4702 15.49226224 21314
160  // ******* --> inclination in degrees
161  // ******** --> longitude of the right ascending node in degrees
162  // ******* --> eccentricity (assumed leading decimal)
163  // ******** --> argument of the perigee in degrees
164  // *********** --> mean (angular) motion in rev/day
165  // must convert to rad/s !!!
166 
167  // semi-major axis from mean motion of TLE (mean elements vs osculating elements)
168  double N = 15.49226224; // rev/day
169 
170  // convert to rad/s
171 
172  c->n = (2.0*M_PI*N / 86400); // convert to rad / s
173  c->a = pow(c->mu, 1.0/3.0) / pow(c->n, 2.0/3.0);
174  cout<< "calculated a from TLE = "<<c->a<<endl;
175 
176  c->T = ( 2.0 * M_PI ) / c->n;
177  cout<< "calculated T (in seconds) = "<<c->T<<endl;
178 
179  // eccentricity
180  c->e = 0.0001122;
181  // longitude of the right ascending node
182  c->O = 133.1544 * (M_PI/180.0); // radians
183  // inclination
184  c->i = 51.6435 * (M_PI/180.0); // radians
185  // argument of periapsis
186  c->w = 148.6515 * (M_PI/180.0); // radians
187 
188 
189  while (agent->running()) {
190 
191  cout<<"["<<node_name<<":"<<agent_name<<"] running..."<<endl;
192 /* old tests
193  string target_request_name = "any_body_out_there";
194  agent->cinfo->set_value<double>("Cooler UTC", 99.99);
195  cout<<"\tUTC == "<< agent->cinfo->node.loc.utc <<endl;
196  cout<<"\tCooler UTC == "<< agent->cinfo->get_value<double>("Cooler UTC")<<endl;
197  cout<<"\tORBIT == "<< agent->cinfo->node.loc.pos.orbit <<endl;
198 
199  // this agent can set his own values ... obvs... can another agent?
200  //agent->cinfo->node.loc.utc = 13.456;
201 
202  cout<<node_agent_name<<" transmit <"<<target_request_name<<"> request to ["<<node_name<<":"<<agent_target<<"]..."<<endl;
203  // Initiate request to agent_002
204  agent->send_request(agent_target_heartbeat, "any_body_out_there", response, 2.);
205 
206  // Check for response from agent_002
207  if (response.size() > 1) {
208  // The case if agent_002 is on and successfully receives the request
209  cout << node_agent_name << " received <"<<target_request_name<<"> response from ["<<agent_target_heartbeat.node<<":"<<agent_target_heartbeat.proc<<"]:\n RX: \"" << response << "\" ("<<response.size()<<" bytes)"<<endl;
210 
211  // Clear the response for next request
212  response.clear();
213  } else {
214  // The case if agent_002 is not running
215  //cout<<"["<<node_name<<":"<<agent_name<<"] looking for ["<<node_name<<":"<<agent_target<<"]..."<<endl;
216 
217  agent_target_heartbeat = agent->find_agent(node_name, agent_target, 2.);
218  if(agent->debug_level>1) {
219  cout<<"B agent "<<agent_target<<" beatstruc:"<<endl;
220  cout<<agent_target_heartbeat<<endl;
221  }
222  }
223 */
224  // Sleep for 5 sec
225  COSMOS_SLEEP(5.);
226  }
227  return 0;
228 }
uint16_t debug_level
Flag for level of debugging, keep it public so that it can be controlled from the outside...
Definition: agentclass.h:362
double P_vel_t
Definition: jsondef.h:4377
string get_json(const string &s)
Gets a JSON-formatted string of the data associated with the provided name in Namespace 2...
Definition: jsondef.h:7281
double R_1_1
Definition: jsondef.h:4394
double I_vel_t
Definition: jsondef.h:4420
void add_name(const string &s, void *v, string t)
Adds a name, memory location, and datatype to Namespace 2.0.
Definition: jsondef.h:4611
string get_json_pretty(const string &s)
Definition: jsondef.h:7589
double K_vel_t
Definition: jsondef.h:4422
Definition: jsondef.h:923
double Q_vel_t
Definition: jsondef.h:4378
T get_value(const string &s) const
Gets the value of the data associated with the provided name in Namespace 2.0.
Definition: jsondef.h:6792
double W_pos_t
Definition: jsondef.h:4374
double J_pos_t
Definition: jsondef.h:4418
double R_1_0
Definition: jsondef.h:4390
double R_2_2
Definition: jsondef.h:4399
double utc
Master time for location, in Modified Julian Day.
Definition: convertdef.h:879
double O
Definition: jsondef.h:4336
double K_pos_t
Definition: jsondef.h:4419
Definition: jsondef.h:1199
double W_vel_t
Definition: jsondef.h:4379
string node_name
Definition: agent_001.cpp:46
nodestruc node
Structure for summary information in node.
Definition: jsondef.h:4220
string cosmos_error_string(int32_t cosmos_errno)
Definition: cosmos-errno.cpp:45
uint16_t running()
Check if we&#39;re supposed to be running.
Definition: agentclass.cpp:391
void set_PQW_from_IJK()
Definition: jsondef.h:4464
double R_0_0
Definition: jsondef.h:4389
int32_t sample_agent_request_function(string &request, string &response, Agent *cdata)
Definition: agent_001.cpp:230
double R_1_2
Definition: jsondef.h:4398
int32_t add_request(string token, external_request_function function, string synopsis="", string description="")
Add internal request to Agent request list with description and synopsis.
Definition: agentclass.cpp:312
static Agent * agent
ensure the Agent constructor creates only one instance per process
Definition: agent_001.cpp:45
Definition: agentclass.h:139
double R_0_1
Definition: jsondef.h:4393
double Q_pos_t
Definition: jsondef.h:4373
locstruc loc
Location structure.
Definition: jsondef.h:3596
double J_vel_t
Definition: jsondef.h:4421
vector< userstruc > user
Single entry vector for user information.
Definition: jsondef.h:4256
string node_agent_name
Definition: agent_001.cpp:48
double mu
Definition: jsondef.h:4350
double P_pos_t
Definition: jsondef.h:4372
int32_t last_error()
Definition: agentclass.cpp:414
double T
Definition: jsondef.h:4356
double w
Definition: jsondef.h:4338
beatstruc find_agent(string node, string agent, double waitsec=0.)
Find agent.
Definition: agentclass.cpp:559
string agent_name
Definition: agent_001.cpp:47
double R_2_1
Definition: jsondef.h:4395
void set_json(const string &json)
Sets the data in Namespace 2.0 with a JSON-formatted string.
Definition: jsondef.h:6822
void set_up_rotation_matrix()
Definition: jsondef.h:4401
cosmosstruc * cinfo
Definition: agentclass.h:346
double n
Definition: jsondef.h:4353
double I_pos_t
Definition: jsondef.h:4417
double R_0_2
Definition: jsondef.h:4397
void set_value(const string &s, const T &value) const
Sets the value of the data associated with the provided name in Namespace 2.0.
Definition: jsondef.h:6808
Definition: jsondef.h:4199
double a
Definition: jsondef.h:4320
double R_2_0
Definition: jsondef.h:4391
void set_IJK_from_PQW()
Definition: jsondef.h:4451
double i
Definition: jsondef.h:4334
double e
Definition: jsondef.h:4322

Variable Documentation

uint64_t request_counter = 10000
static
Agent* agent
static

ensure the Agent constructor creates only one instance per process

string node_name = "sat_001"
string agent_name = "agent_001"
string node_agent_name = "["+node_name+":"+agent_name+"]"