COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
agent_antenna.cpp File Reference

Agent Antenna for Satellite Ground Station. More...

#include "support/configCosmos.h"
#include "agent/agentclass.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <semaphore.h>
#include "support/jsonlib.h"
#include "support/stringlib.h"
#include "support/timelib.h"
#include "device/general/gs232b_lib.h"
#include "device/general/prkx2su_lib.h"
#include "support/convertlib.h"
#include "support/elapsedtime.h"
Include dependency graph for agent_antenna.cpp:

Classes

struct  azelstruc
 

Functions

int32_t request_debug (string &req, string &response, Agent *)
 
int32_t request_get_offset (string &req, string &response, Agent *)
 
int32_t request_get_state (string &req, string &response, Agent *)
 
int32_t request_set_azel (string &req, string &response, Agent *)
 
int32_t request_track_azel (string &req, string &response, Agent *)
 
int32_t request_get_azel (string &req, string &response, Agent *)
 
int32_t request_jog (string &req, string &response, Agent *)
 
int32_t request_get_horizon (string &req, string &response, Agent *)
 
int32_t request_enable (string &req, string &response, Agent *)
 
int32_t request_disable (string &req, string &response, Agent *)
 
int32_t request_stop (string &req, string &response, Agent *)
 
int32_t request_pause (string &req, string &response, Agent *)
 
int32_t request_set_offset (string &req, string &response, Agent *)
 
int32_t request_set_rotctl (string &req, string &response, Agent *)
 
int32_t request_get_rotctl (string &req, string &response, Agent *)
 
int32_t connect_antenna ()
 
int32_t stop_antenna ()
 
void rotctl_loop ()
 
int load_tle_info (char *file)
 
int main (int argc, char *argv[])
 

Variables

static float gsmin = RADOF(10.)
 
static string antbase = ""
 
static std::string nodename = ""
 
static std::string agentname
 
static std::string antdevice
 
static uint16_t devindex = -1
 
static uint16_t antindex = -1
 
static antstruc target
 
static antstruc current
 
static bool antconnected = false
 
static bool antenabled = false
 
static bool debug
 
static uint16_t targetrotctlport = 0
 
static uint16_t rotctlport = 0
 
static socket_channel rotctlchannel
 
static thread rthread
 
static std::vector< tlestructle
 
static Agentagent
 
static azelstruc antennaoffset = {0., 0.}
 
static LsFit trackaz (5, 2)
 
static LsFit trackel (5, 2)
 
static bool trackflag = false
 

Detailed Description

Agent Antenna for Satellite Ground Station.

This agent controls the ground station antenna (set elevatiom, azimuth, etc.). To read more about how this agent operates go to Agent Antenna

Function Documentation

int32_t request_debug ( string &  req,
string &  response,
Agent  
)
620 {
621  if (debug)
622  {
623  debug = false;
624  }
625  else
626  {
627  debug = true;
628  }
629 
630  return 0;
631 }
static bool debug
Definition: agent_antenna.cpp:164
int32_t request_get_offset ( string &  req,
string &  response,
Agent  
)
480 {
481  float az = antennaoffset.az;
482  float el = antennaoffset.el;
483  response = to_angle(az, 'D') + ' ' + to_angle(el, 'D');
484  return (0);
485 }
static azelstruc antennaoffset
Definition: agent_antenna.cpp:185
float az
Definition: agent_antenna.cpp:182
string to_angle(double value, char units, uint8_t precision)
Definition: stringlib.cpp:313
float el
Definition: agent_antenna.cpp:183
int32_t request_get_state ( string &  req,
string &  response,
Agent  
)
425 {
426  response = "[";
427  response += ' ' + to_mjd(currentmjd());
428  response += ' ' + to_bool(antconnected);
429  response += ' ' + to_bool(antenabled);
430  response += ' ' + to_angle(current.azim);
431  response += ' ' + to_angle(current.elev);
432  response += ' ' + to_angle(current.azim-agent->cinfo->device[devindex].ant.azim, 'D');
433  response += ' ' + to_angle(current.elev-agent->cinfo->device[devindex].ant.elev, 'D');
434  response += ' ' + to_angle(agent->cinfo->device[devindex].ant.azim+antennaoffset.az, 'D');
435  response += ' ' + to_angle(agent->cinfo->device[devindex].ant.elev+antennaoffset.el, 'D');
436  response += ' ' + to_angle(antennaoffset.az + antennaoffset.el, 'D');
437  return (0);
438 }
static uint16_t devindex
Definition: agent_antenna.cpp:158
static bool antconnected
Definition: agent_antenna.cpp:162
vector< devicestruc > device
Vector of all general (common) information for devices (components) in node.
Definition: jsondef.h:4238
static azelstruc antennaoffset
Definition: agent_antenna.cpp:185
float elev
Elevation.
Definition: jsondef.h:2258
static Agent * agent
Definition: agent_antenna.cpp:178
static antstruc current
Definition: agent_antenna.cpp:161
float azim
Azimuth;.
Definition: jsondef.h:2256
string to_bool(bool value)
Definition: stringlib.cpp:326
double currentmjd(double offset)
Current UTC in Modified Julian Days.
Definition: timelib.cpp:65
cosmosstruc * cinfo
Definition: agentclass.h:346
static bool antenabled
Definition: agent_antenna.cpp:163
float az
Definition: agent_antenna.cpp:182
string to_mjd(double value)
Definition: stringlib.cpp:298
string to_angle(double value, char units, uint8_t precision)
Definition: stringlib.cpp:313
float el
Definition: agent_antenna.cpp:183
int32_t request_set_azel ( string &  req,
string &  response,
Agent  
)
488 {
489  float targetaz;
490  float targetel;
491 
492  sscanf(req.c_str() ,"%*s %f %f",&targetaz, &targetel);
493  target.azim = RADOF(targetaz);
494  target.elev = RADOF(targetel);
495  trackflag = false;
496  return (0);
497 }
static antstruc target
Definition: agent_antenna.cpp:160
float elev
Elevation.
Definition: jsondef.h:2258
float azim
Azimuth;.
Definition: jsondef.h:2256
static bool trackflag
Definition: agent_antenna.cpp:189
#define RADOF(deg)
Radians of a Degree value.
Definition: math/constants.h:29
int32_t request_track_azel ( string &  req,
string &  response,
Agent  
)
634 {
635  float az;
636  float el;
637  double utc;
638 
639  sscanf(req.c_str() ,"%*s %lf %f %f", &utc, &az, &el);
640  trackaz.update(utc, RADOF(az));
641  trackel.update(utc, RADOF(el));
642  trackflag = true;
643  return 0;
644 }
static LsFit trackaz(5, 2)
static bool trackflag
Definition: agent_antenna.cpp:189
static LsFit trackel(5, 2)
void update(double x, double y)
Update scalar Least Squares Fit.
Definition: mathlib.cpp:2478
#define RADOF(deg)
Radians of a Degree value.
Definition: math/constants.h:29
int32_t request_get_azel ( string &  req,
string &  response,
Agent  
)
500 {
501  // double az = agent->cinfo->device[devindex].ant.azim;
502  // double el = agent->cinfo->device[devindex].ant.elev;
503  response = to_angle(current.azim, 'D') + ' ' + to_angle(current.elev, 'D');
504  return (0);
505 }
float elev
Elevation.
Definition: jsondef.h:2258
static antstruc current
Definition: agent_antenna.cpp:161
float azim
Azimuth;.
Definition: jsondef.h:2256
string to_angle(double value, char units, uint8_t precision)
Definition: stringlib.cpp:313
int32_t request_jog ( string &  req,
string &  response,
Agent  
)
508 {
509  float az, el;
510  sscanf(req.c_str() ,"%*s %f %f", &az, &el);
511  target.azim += RADOF(az);
512  target.elev += RADOF(el);
513  return (0);
514 }
static antstruc target
Definition: agent_antenna.cpp:160
float elev
Elevation.
Definition: jsondef.h:2258
float azim
Azimuth;.
Definition: jsondef.h:2256
#define RADOF(deg)
Radians of a Degree value.
Definition: math/constants.h:29
int32_t request_get_horizon ( string &  req,
string &  response,
Agent  
)
517 {
518  sscanf(req.c_str() ,"%*s %f",&gsmin);
519  return (0);
520 }
static float gsmin
Definition: agent_antenna.cpp:152
int32_t request_enable ( string &  req,
string &  response,
Agent  
)
523 {
524  antenabled = true;
525  return 0;
526 }
static bool antenabled
Definition: agent_antenna.cpp:163
int32_t request_disable ( string &  req,
string &  response,
Agent  
)
529 {
530  antenabled = false;
531  stop_antenna();
532  return 0;
533 }
static bool antenabled
Definition: agent_antenna.cpp:163
int32_t stop_antenna()
Definition: agent_antenna.cpp:603
int32_t request_stop ( string &  req,
string &  response,
Agent  
)
441 {
442 
443  target = agent->cinfo->device[devindex].ant;
444  antenabled = false;
445  stop_antenna();
446  // switch (agent->cinfo->device[devindex].all.model)
447  // {
448  // case DEVICE_MODEL_GS232B:
449  // gs232b_stop();
450  // break;
451  // case DEVICE_MODEL_PRKX2SU:
452  // prkx2su_stop(PRKX2SU_AXIS_AZ);
453  // prkx2su_stop(PRKX2SU_AXIS_EL);
454  // break;
455  // }
456 
457  return 0;
458 }
static antstruc target
Definition: agent_antenna.cpp:160
static uint16_t devindex
Definition: agent_antenna.cpp:158
vector< devicestruc > device
Vector of all general (common) information for devices (components) in node.
Definition: jsondef.h:4238
static Agent * agent
Definition: agent_antenna.cpp:178
cosmosstruc * cinfo
Definition: agentclass.h:346
static bool antenabled
Definition: agent_antenna.cpp:163
int32_t stop_antenna()
Definition: agent_antenna.cpp:603
int32_t request_pause ( string &  req,
string &  response,
Agent  
)
461 {
462 
463  target = agent->cinfo->device[devindex].ant;
464 
465  return 0;
466 }
static antstruc target
Definition: agent_antenna.cpp:160
static uint16_t devindex
Definition: agent_antenna.cpp:158
vector< devicestruc > device
Vector of all general (common) information for devices (components) in node.
Definition: jsondef.h:4238
static Agent * agent
Definition: agent_antenna.cpp:178
cosmosstruc * cinfo
Definition: agentclass.h:346
int32_t request_set_offset ( string &  req,
string &  response,
Agent  
)
469 {
470  float targetaz;
471  float targetel;
472 
473  sscanf(req.c_str() ,"%*s %f %f",&targetaz, &targetel);
474  antennaoffset.az = RADOF(targetaz);
475  antennaoffset.el = RADOF(targetel);
476  return (0);
477 }
static azelstruc antennaoffset
Definition: agent_antenna.cpp:185
float az
Definition: agent_antenna.cpp:182
float el
Definition: agent_antenna.cpp:183
#define RADOF(deg)
Radians of a Degree value.
Definition: math/constants.h:29
int32_t request_set_rotctl ( string &  req,
string &  response,
Agent  
)
536 {
537  //uint16_t newport;
538 
539  vector<string> args = string_split(req, " ");
540  if (args.size() > 1)
541  {
542  targetrotctlport = stoi(args[1]);
543  }
544  else {
545  targetrotctlport = 4533;
546  }
547 
548  return (0);
549 }
static uint16_t targetrotctlport
Definition: agent_antenna.cpp:165
vector< string > string_split(string in, string delimeters)
Parse a string.
Definition: stringlib.cpp:47
int32_t request_get_rotctl ( string &  req,
string &  response,
Agent  
)
552 {
553  response = to_unsigned(rotctlport);
554  return (0);
555 }
string to_unsigned(uint64_t value, uint16_t digits, bool zerofill)
Definition: stringlib.cpp:265
static uint16_t rotctlport
Definition: agent_antenna.cpp:166
int32_t connect_antenna ( )
558 {
559  int32_t iretn;
560  antconnected = false;
561 
562  switch (agent->cinfo->device[devindex].model)
563  {
565  antconnected = true;
566  break;
567  case DEVICE_MODEL_GS232B:
568  iretn = gs232b_connect(antdevice);
569 
570  // Initialize values if we are connected
571  if (iretn == 0)
572  {
573  iretn = gs232b_get_az_el(agent->cinfo->device[devindex].ant.azim, agent->cinfo->device[devindex].ant.elev);
574  if (iretn >= 0)
575  {
578  antconnected = true;
579  }
580  }
581  break;
583  iretn = prkx2su_connect();
584 
585  // Initialize values if we are connected
586  if (iretn == 0)
587  {
588  iretn = prkx2su_get_az_el(agent->cinfo->device[devindex].ant.azim, agent->cinfo->device[devindex].ant.elev);
589  if (iretn >= 0)
590  {
593  antconnected = true;
594  }
595  }
596  break;
597  }
598 
599  return 0;
600 
601 }
static antstruc target
Definition: agent_antenna.cpp:160
int32_t prkx2su_get_az_el(float &az, float &el)
Definition: prkx2su_lib.cpp:385
static std::string antdevice
Definition: agent_antenna.cpp:157
int iretn
Definition: rw_test.cpp:37
int32_t gs232b_connect(string dev)
Definition: gs232b_lib.cpp:54
static uint16_t devindex
Definition: agent_antenna.cpp:158
static bool antconnected
Definition: agent_antenna.cpp:162
int32_t prkx2su_connect()
Definition: prkx2su_lib.cpp:65
vector< devicestruc > device
Vector of all general (common) information for devices (components) in node.
Definition: jsondef.h:4238
static azelstruc antennaoffset
Definition: agent_antenna.cpp:185
float elev
Elevation.
Definition: jsondef.h:2258
Definition: jsondef.h:564
static Agent * agent
Definition: agent_antenna.cpp:178
float azim
Azimuth;.
Definition: jsondef.h:2256
int32_t gs232b_get_az_el(float &az, float &el)
Definition: gs232b_lib.cpp:338
cosmosstruc * cinfo
Definition: agentclass.h:346
float az
Definition: agent_antenna.cpp:182
Definition: jsondef.h:565
Definition: jsondef.h:563
float el
Definition: agent_antenna.cpp:183
int32_t stop_antenna ( )
604 {
605  switch (agent->cinfo->device[devindex].all.model)
606  {
607  case DEVICE_MODEL_GS232B:
608  gs232b_stop();
609  break;
613  break;
614  }
615 
616  return 0;
617 }
#define PRKX2SU_AXIS_EL
Definition: prkx2su_class.h:16
static uint16_t devindex
Definition: agent_antenna.cpp:158
vector< devicestruc > device
Vector of all general (common) information for devices (components) in node.
Definition: jsondef.h:4238
int32_t prkx2su_stop(uint8_t axis)
Definition: prkx2su_lib.cpp:266
#define PRKX2SU_AXIS_AZ
Definition: prkx2su_class.h:15
Definition: jsondef.h:564
static Agent * agent
Definition: agent_antenna.cpp:178
cosmosstruc * cinfo
Definition: agentclass.h:346
int32_t gs232b_stop()
Definition: gs232b_lib.cpp:309
Definition: jsondef.h:563
void rotctl_loop ( )
647 {
648  int32_t iretn;
649  string command;
650  uint16_t direction;
651  uint16_t speed;
652  float az;
653  float el;
654  double utc = 0;
655  socket_channel clientchannel;
656  clientchannel.cudp = -1;
657  ElapsedTime et;
658 
659  while (agent->running())
660  {
662  {
663  if (rotctlport)
664  {
665  // Shut down active rotctl support if it is running
666  if (clientchannel.cudp)
667  {
668  // Disconnect
669  iretn = socket_close(&clientchannel);
670  }
672  rotctlport = 0;
673  }
674 
675  if (targetrotctlport)
676  {
677  // Establish new rotctl support if port is not zero
679  if (iretn < 0)
680  {
681  fprintf(agent->get_debug_fd(), "Error creating rotctl channel: %s\n", cosmos_error_string(iretn).c_str());
682  }
683  else {
685  }
686  }
687  }
688 
689  if (rotctlchannel.cudp >= 0)
690  {
691  if (clientchannel.cudp < 0)
692  {
693  do
694  {
695  iretn = socket_accept(rotctlchannel, clientchannel);
696  if (iretn < 0 && (-iretn != EWOULDBLOCK || -iretn != EAGAIN))
697  {
698  return;
699  }
700  } while (iretn < 0);
701  et.reset();
702  }
703 
704  iretn = socket_recvfrom(clientchannel, command, 100);
705  if (iretn > 0)
706  {
707  printf("[%f] In: %s", et.split(), command.c_str());
708  switch (command[0])
709  {
710  case '\\':
711  if (command.find("dump_state") != string::npos)
712  {
713  socket_sendto(clientchannel, "0\n603\n0.000000\n450.000000\n0.000000\n180.000000\n");
714  printf("Out: 0 603 0.000000 450.000000 0.000000 180.000000\n");
715  }
716  break;
717  case 'q':
718  case 'Q':
719  // Disconnect
720  socket_sendto(clientchannel, "RPRT 0\n");
721  printf("Out: RPRT 0\n");
722  iretn = socket_close(&clientchannel);
723  break;
724  case 'P':
725  // set_pos
726  sscanf(command.c_str() ,"%*s %f %f",&az, &el);
727  if (az > 360.)
728  {
729  target.azim = RADOF(az-360.);
730  }
731  else if (az < 0.)
732  {
733  target.azim = RADOF(az+360.);
734  }
735  else
736  {
737  target.azim = RADOF(az);
738  }
739  target.elev = RADOF(el);
740  trackflag = false;
741  socket_sendto(clientchannel, "RPRT 0\n");
742  printf("Out: RPRT 0\n");
743  break;
744  case 'p':
745  // get_pos
746  if (current.azim > D2PI)
747  {
748  socket_sendto(clientchannel, to_double(DEGOF(current.azim-D2PI), 6)+'\n');
749  printf("Out: %s ", to_double(DEGOF(current.azim-D2PI), 6).c_str());
750  }
751  else {
752  socket_sendto(clientchannel, to_double(DEGOF(current.azim), 6)+'\n');
753  printf("Out: %s ", to_double(DEGOF(current.azim), 6).c_str());
754  }
755  socket_sendto(clientchannel, to_double(DEGOF(current.elev), 6)+'\n');
756  printf("%s\n", to_double(DEGOF(current.elev), 6).c_str());
757  break;
758  case 'M':
759  // move
760  {
761  sscanf(command.c_str() ,"%*s %hu %hu",&direction, &speed);
762  double utcstart = currentmjd();
763  double utcstep = 200. / speed;
764  trackaz.initialize(5, 2);
765  trackel.initialize(5, 2);
766  switch (direction)
767  {
768  case 2:
769  case 4:
770  for (uint16_t i=0; i<5; ++i)
771  {
772  trackaz.update(utcstart+i*utcstep, i*(direction==2?RADOF(2.):RADOF(-2.)));
773  }
774  break;
775  case 8:
776  case 16:
777  for (uint16_t i=0; i<5; ++i)
778  {
779  trackel.update(utcstart+i*utcstep, i*(direction==16?RADOF(2.):RADOF(-2.)));
780  }
781  break;
782  }
783  trackaz.update(utc, RADOF(az));
784  trackel.update(utc, RADOF(el));
785  trackflag = true;
786  socket_sendto(clientchannel, "RPRT 0\n");
787  printf("Out: RPRT 0\n");
788  }
789  break;
790  case 'S':
791  // stop
792  target = agent->cinfo->device[devindex].ant;
793  antenabled = false;
794  stop_antenna();
795  socket_sendto(clientchannel, "RPRT 0\n");
796  printf("Out: RPRT 0\n");
797  break;
798  case 'K':
799  // park
800  target.azim = RADOF(180);
801  target.elev = RADOF(90);
802  trackflag = false;
803  socket_sendto(clientchannel, "RPRT 0\n");
804  printf("Out: RPRT 0\n");
805  break;
806  case 'R':
807  // reset
808  socket_sendto(clientchannel, "RPRT 0\n");
809  printf("Out: RPRT 0\n");
810  break;
811  case 'D':
812  {
813  vector<string> args = string_split(command, " ");
814  if (args.size() == 5)
815  {
816  double dec = stof(args[1]);
817  dec += stof(args[2]) / 60.;
818  dec += stof(args[3]) / 3600.;
819  if (args[4][0] == '1')
820  {
821  dec *= -1;
822  }
823  socket_sendto(clientchannel, to_double(dec, 9)+'\n');
824  printf("Out: %s\n", to_double(dec, 9).c_str());
825  }
826  else {
827  socket_sendto(clientchannel, "RPRT -1\n");
828  printf("Out: RPRT -1\n");
829  }
830  }
831  break;
832  case 'd':
833  {
834  vector<string> args = string_split(command, " ");
835  if (args.size() == 2)
836  {
837  uint8_t sign = 0;
838  double dec = stof(args[1]);
839  if (dec < 0)
840  {
841  sign = 1;
842  dec = -dec;
843  }
844  uint16_t deg = static_cast<uint16_t>(dec);
845  double fdec = (dec - deg) * 60.;
846  uint16_t min = static_cast<uint16_t>(fdec);
847  fdec = (fdec - min) * 60.;
848 
849  socket_sendto(clientchannel, to_unsigned(deg)+'\n');
850  printf("Out: %s ", to_unsigned(deg).c_str());
851  socket_sendto(clientchannel, to_unsigned(min)+'\n');
852  printf("%s ", to_unsigned(min).c_str());
853  socket_sendto(clientchannel, to_double(fdec)+'\n');
854  printf("%s ", to_double(fdec).c_str());
855  socket_sendto(clientchannel, to_unsigned(sign)+'\n');
856  printf("%s\n", to_unsigned(sign).c_str());
857  }
858  else {
859  socket_sendto(clientchannel, "RPRT -1\n");
860  printf("Out: RPRT -1\n");
861  }
862  }
863  case 'E':
864  {
865  vector<string> args = string_split(command, " ");
866  if (args.size() == 4)
867  {
868  double dec = stof(args[1]);
869  dec += stof(args[2]) / 60.;
870  if (args[3][0] == '1')
871  {
872  dec *= -1;
873  }
874  socket_sendto(clientchannel, to_double(dec, 9)+'\n');
875  printf("Out: %s\n", to_double(dec, 9).c_str());
876  }
877  else {
878  socket_sendto(clientchannel, "RPRT -1\n");
879  printf("Out: RPRT -1\n");
880  }
881  }
882  break;
883  case 'e':
884  {
885  vector<string> args = string_split(command, " ");
886  if (args.size() == 2)
887  {
888  uint8_t sign = 0;
889  double dec = stof(args[1]);
890  if (dec < 0)
891  {
892  sign = 1;
893  dec = -dec;
894  }
895  uint16_t deg = static_cast<uint16_t>(dec);
896  double fdec = (dec - deg) * 60.;
897 
898  socket_sendto(clientchannel, to_unsigned(deg)+'\n');
899  printf("Out: %s ", to_unsigned(deg).c_str());
900  socket_sendto(clientchannel, to_double(fdec)+'\n');
901  printf("%s ", to_double(fdec).c_str());
902  socket_sendto(clientchannel, to_unsigned(sign)+'\n');
903  printf("%s\n", to_unsigned(sign).c_str());
904  }
905  else {
906  socket_sendto(clientchannel, "RPRT -1\n");
907  printf("Out: RPRT -1\n");
908  }
909  }
910  }
911  }
912  }
913  else {
914  COSMOS_SLEEP(1.);
915  }
916  }
917 
918  if (rotctlport)
919  {
920  // Shut down active rotctl support if it is running
921  if (clientchannel.cudp)
922  {
923  // Disconnect
924  iretn = socket_close(&clientchannel);
925  }
927  rotctlport = 0;
928  }
929 }
double utcstart
Definition: fast_contacts.cpp:64
FILE * get_debug_fd(double mjd=0.)
Definition: agentclass.cpp:2645
Agent socket using Unicast TCP.
int i
Definition: rw_test.cpp:37
static antstruc target
Definition: agent_antenna.cpp:160
int32_t socket_close(socket_channel *channel)
Close socket.
Definition: socketlib.cpp:509
int32_t socket_recvfrom(socket_channel &channel, string &buffer, size_t maxlen, int flags)
Definition: socketlib.cpp:710
int iretn
Definition: rw_test.cpp:37
string to_unsigned(uint64_t value, uint16_t digits, bool zerofill)
Definition: stringlib.cpp:265
int32_t socket_sendto(socket_channel &channel, const string buffer, int flags)
Definition: socketlib.cpp:737
int32_t socket_accept(socket_channel server, socket_channel &client)
Definition: socketlib.cpp:311
static uint16_t devindex
Definition: agent_antenna.cpp:158
ElapsedTime et
Definition: agent_cpu_device_test.cpp:51
vector< devicestruc > device
Vector of all general (common) information for devices (components) in node.
Definition: jsondef.h:4238
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
float elev
Elevation.
Definition: jsondef.h:2258
static LsFit trackaz(5, 2)
int32_t cudp
Definition: socketlib.h:120
static Agent * agent
Definition: agent_antenna.cpp:178
static antstruc current
Definition: agent_antenna.cpp:161
Definition: socketlib.h:115
#define SOCKET_BLOCKING
Blocking Agent.
Definition: socketlib.h:78
#define DEGOF(rad)
Degrees of a Radian value.
Definition: math/constants.h:33
string command
Definition: add_radio.cpp:27
float azim
Azimuth;.
Definition: jsondef.h:2256
static socket_channel rotctlchannel
Definition: agent_antenna.cpp:167
Definition: elapsedtime.h:62
double currentmjd(double offset)
Current UTC in Modified Julian Days.
Definition: timelib.cpp:65
static uint16_t rotctlport
Definition: agent_antenna.cpp:166
static bool trackflag
Definition: agent_antenna.cpp:189
static LsFit trackel(5, 2)
const double D2PI
Double precision 2*PI.
Definition: math/constants.h:16
void update(double x, double y)
Update scalar Least Squares Fit.
Definition: mathlib.cpp:2478
#define SOCKET_LISTEN
Listen followed by optional talk (recvfrom INADDRANY)
Definition: socketlib.h:84
float speed
Definition: netperf_send.cpp:40
cosmosstruc * cinfo
Definition: agentclass.h:346
static bool antenabled
Definition: agent_antenna.cpp:163
int32_t socket_open(socket_channel *channel, NetworkType ntype, const char *address, uint16_t port, uint16_t role, bool blocking, uint32_t usectimeo, uint32_t rcvbuf, uint32_t sndbuf)
Open UDP socket.
Definition: socketlib.cpp:51
void initialize(uint16_t cnt=10, uint16_t ord=2)
Initialize Least Squares Fit.
Definition: mathlib.cpp:2450
double split()
ElapsedTime::split, gets the current elapsed time since the start()
Definition: elapsedtime.cpp:234
static uint16_t targetrotctlport
Definition: agent_antenna.cpp:165
void reset()
ElapsedTime::reset.
Definition: elapsedtime.cpp:278
int32_t stop_antenna()
Definition: agent_antenna.cpp:603
string to_double(double value, uint16_t precision)
Definition: stringlib.cpp:285
vector< string > string_split(string in, string delimeters)
Parse a string.
Definition: stringlib.cpp:47
#define RADOF(deg)
Radians of a Degree value.
Definition: math/constants.h:29
int load_tle_info ( char *  file)
int main ( int  argc,
char *  argv[] 
)
192 {
193  int iretn;
194 
195  // Initialization stuff
196 
197  switch (argc)
198  {
199  case 3:
200  nodename = argv[2];
201  case 2:
202  antbase = argv[1];
203  break;
204  default:
205  printf("Usage: agent_antenna antenna node");
206  exit (1);
207  }
208 
209  // if (nodename.empty())
210  // {
211  // agent = new Agent("", (antbase+"antenna").c_str(), 5.);
212  // }
213  // else
214  // {
215  agent = new Agent(nodename, (antbase).c_str(), 5.);
216  // }
217 
218  if ((iretn = agent->wait()) < 0)
219  {
220  fprintf(agent->get_debug_fd(), "%16.10f %s Failed to start Agent %s on Node %s Dated %s : %s\n",currentmjd(), mjd2iso8601(currentmjd()).c_str(), agent->getAgent().c_str(), agent->getNode().c_str(), utc2iso8601(data_ctime(argv[0])).c_str(), cosmos_error_string(iretn).c_str());
221  exit(iretn);
222  }
223  else
224  {
225  fprintf(agent->get_debug_fd(), "%16.10f %s Started Agent %s on Node %s Dated %s\n",currentmjd(), mjd2iso8601(currentmjd()).c_str(), agent->getAgent().c_str(), agent->getNode().c_str(), utc2iso8601(data_ctime(argv[0])).c_str());
226  }
228 
230  if (iretn < 0)
231  {
232  fprintf(agent->get_debug_fd(), "Failed to add %s ANT %s\n", antbase.c_str(), cosmos_error_string(iretn).c_str());
233  agent->shutdown();
234  exit(iretn);
235  }
236  devindex = agent->cinfo->pieces[static_cast <uint16_t>(iretn)].cidx;
237  antindex = agent->cinfo->device[devindex].didx;
238  agent->cinfo->device[devindex].ant.minelev = RADOF(5.);
239  if (antbase == "sband")
240  {
242  }
243  else if (antbase.find("loop") != string::npos)
244  {
246  }
247  else
248  {
250  }
251 
252  iretn = json_dump_node(agent->cinfo);
253  if (iretn < 0)
254  {
255  fprintf(agent->get_debug_fd(), "Failed to save node %s\n", cosmos_error_string(iretn).c_str());
256  agent->shutdown();
257  exit(iretn);
258  }
259 
260  // Add requests
261  if ((iretn=agent->add_request("track_azel",request_track_azel,"", "Supply next azimuth and elevation for tracking.")))
262  exit (iretn);
263  if ((iretn=agent->add_request("debug",request_debug,"", "Toggle debug messages.")))
264  exit (iretn);
265  if ((iretn=agent->add_request("get_offset",request_get_offset,"", "Return a report on the offset of the agent.")))
266  exit (iretn);
267  if ((iretn=agent->add_request("get_state",request_get_state,"", "Return a report on the state of the agent.")))
268  exit (iretn);
269  if ((iretn=agent->add_request("jog",request_jog,"{degrees aziumth} {degrees elevation}", "Jog the antenna azimuth and elevation in degrees.")))
270  exit (iretn);
271  if ((iretn=agent->add_request("get_horizon",request_get_horizon,"", "Return the antennas minimu elevation in degrees.")))
272  exit (iretn);
273  if ((iretn=agent->add_request("get_azel",request_get_azel,"", "Return the antenna azimuth and elevation in degrees.")))
274  exit (iretn);
275  if ((iretn=agent->add_request("set_azel",request_set_azel,"aaa.a eee.e", "Set the antenna azimuth and elevation in degrees.")))
276  exit (iretn);
277  if ((iretn=agent->add_request("enable",request_enable,"", "Enable antenna motion.")))
278  exit (iretn);
279  if ((iretn=agent->add_request("disable",request_disable,"", "Disable antenna motion.")))
280  exit (iretn);
281  if ((iretn=agent->add_request("pause",request_pause,"", "Stop where you are and make it your new target.")))
282  exit (iretn);
283  if ((iretn=agent->add_request("stop",request_stop,"", "Stop where you are, make it your new target AND disable antenna motion.")))
284  exit (iretn);
285  if ((iretn=agent->add_request("set_offset",request_set_offset,"aaa.a eee.e", "Set the antenna azimuth and elevation correction in degrees.")))
286  exit (iretn);
287  if ((iretn=agent->add_request("set_rotctl",request_set_rotctl,"[n]", "Set the rotctl port number. No argument sets it to the default of 4533. Zero turns it off.")))
288  exit (iretn);
289  if ((iretn=agent->add_request("get_rotctl",request_get_rotctl,"", "Return the current rotctl port number.")))
290  exit (iretn);
291 
292  // Set SOH string
293  char sohstring[200];
294  sprintf(sohstring, "{\"device_ant_temp_%03lu\",\"device_ant_align_%03lu\",\"device_ant_azim_%03lu\",\"device_ant_elev_%03lu\"}", antindex, antindex, antindex, antindex);
295  agent->set_sohstring(sohstring);
296 
297  // antdevice = agent->cinfo->port[agent->cinfo->device[devindex].all.portidx].name;
298  antdevice = "/dev/tty_" + antbase;
299 
300  // Connect to antenna and set sensitivity;
302  {
303  iretn = prkx2su_init(antdevice);
304  }
305 
306  iretn = connect_antenna();
307  switch (agent->cinfo->device[devindex].model)
308  {
309  case DEVICE_MODEL_GS232B:
311  break;
314  break;
315  }
316 
317  // Restore default offsets
318  string offsetname = get_nodedir(nodename) + '/' + antbase + ".offset";
319  FILE *op = fopen(offsetname.c_str(), "r");
320  if (op)
321  {
322  fscanf(op, "%f %f", &antennaoffset.az, &antennaoffset.el);
325  }
326 
327  rthread = thread([=] { rotctl_loop(); });
328 
329  ElapsedTime et;
330 
331  // Start performing the body of the agent
332  agent->cinfo->agent[0].aprd = .5;
334  while(agent->running())
335  {
336  if (antconnected)
337  {
338  // Find most recent position
339  switch (agent->cinfo->device[devindex].model)
340  {
342  iretn = 0;
343  break;
344  case DEVICE_MODEL_GS232B:
348  break;
353  break;
354  }
355  if (iretn < 0)
356  {
357  antconnected = false;
358  }
359  else
360  {
361  agent->cinfo->device[devindex].ant.azim = current.azim;
362  agent->cinfo->device[devindex].ant.elev = current.elev;
363  if (antenabled)
364  {
365  if (trackflag)
366  {
367  double utc = currentmjd();
368  if (utc - trackaz.lastx() < trackaz.lastx() - trackaz.firstx())
369  {
370  current.azim = trackaz.eval(utc);
371  current.elev = trackel.eval(utc);
372  }
373  else
374  {
375  trackflag = false;
376  }
377  }
378  else
379  {
382  }
383 
384  if (current.elev < agent->cinfo->device[devindex].ant.minelev)
385  {
386  current.elev = agent->cinfo->device[devindex].ant.minelev;
387  }
388  switch (agent->cinfo->device[devindex].model)
389  {
390  case DEVICE_MODEL_GS232B:
392  break;
395  break;
396  }
397  if (debug)
398  {
399  printf("%f: goto %f %f [%d]\n", et.lap(), DEGOF(current.azim + antennaoffset.az), DEGOF(current.elev + antennaoffset.el), iretn);
400  }
401  if (iretn < 0)
402  {
403  antconnected = false;
404  }
405  }
406  }
407  // COSMOS_SLEEP(.1);
408  }
409  else
410  {
411  if (debug)
412  {
413  printf("%f: Connect Antenna\n", et.lap());
414  }
415  connect_antenna();
416  // COSMOS_SLEEP(.1);
417  }
419  }
420 
421  agent->shutdown();
422 }
int32_t request_get_horizon(string &req, string &response, Agent *)
Definition: agent_antenna.cpp:516
int32_t finish_active_loop()
Definition: agentclass.cpp:355
FILE * get_debug_fd(double mjd=0.)
Definition: agentclass.cpp:2645
static string antbase
Definition: agent_antenna.cpp:154
int32_t prkx2su_goto(float az, float el)
Definition: prkx2su_lib.cpp:330
string nodeName
Definition: agentclass.h:367
string getNode()
Listen for heartbeat.
Definition: agentclass.cpp:2607
static antstruc target
Definition: agent_antenna.cpp:160
int32_t request_enable(string &req, string &response, Agent *)
Definition: agent_antenna.cpp:522
int32_t prkx2su_get_az_el(float &az, float &el)
Definition: prkx2su_lib.cpp:385
int32_t gs232b_goto(float az, float el)
Definition: gs232b_lib.cpp:224
static std::string antdevice
Definition: agent_antenna.cpp:157
int32_t request_track_azel(string &req, string &response, Agent *)
Definition: agent_antenna.cpp:633
int32_t request_disable(string &req, string &response, Agent *)
Definition: agent_antenna.cpp:528
int iretn
Definition: rw_test.cpp:37
int32_t request_debug(string &req, string &response, Agent *)
Definition: agent_antenna.cpp:619
int32_t wait(State state=State::RUN, double waitsec=10.)
Definition: agentclass.cpp:398
int32_t request_get_offset(string &req, string &response, Agent *)
Definition: agent_antenna.cpp:479
double lastx()
Least squares last independent value.
Definition: mathlib.cpp:2671
static uint16_t devindex
Definition: agent_antenna.cpp:158
static bool antconnected
Definition: agent_antenna.cpp:162
int32_t set_sohstring(string list)
Set Limited SOH string.
Definition: agentclass.cpp:641
static thread rthread
Definition: agent_antenna.cpp:168
static uint16_t antindex
Definition: agent_antenna.cpp:159
int32_t request_set_rotctl(string &req, string &response, Agent *)
Definition: agent_antenna.cpp:535
ElapsedTime et
Definition: agent_cpu_device_test.cpp:51
int32_t request_jog(string &req, string &response, Agent *)
Definition: agent_antenna.cpp:507
int32_t json_dump_node(cosmosstruc *cinfo)
Save Node entries to disk.
Definition: jsonlib.cpp:7233
vector< devicestruc > device
Vector of all general (common) information for devices (components) in node.
Definition: jsondef.h:4238
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
static string sohstring
Definition: agent_cpu-2-0.cpp:72
static azelstruc antennaoffset
Definition: agent_antenna.cpp:185
float elev
Elevation.
Definition: jsondef.h:2258
static LsFit trackaz(5, 2)
int32_t gs232b_set_sensitivity(float sensitivity)
Definition: gs232b_lib.cpp:417
Definition: jsondef.h:564
string getAgent()
Definition: agentclass.cpp:2609
static std::string nodename
Definition: agent_antenna.cpp:155
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
Definition: agent_antenna.cpp:178
int32_t request_set_azel(string &req, string &response, Agent *)
Definition: agent_antenna.cpp:487
static antstruc current
Definition: agent_antenna.cpp:161
Definition: agentclass.h:139
int32_t connect_antenna()
Definition: agent_antenna.cpp:557
double eval(double x)
Least squares dependent scalar value.
Definition: mathlib.cpp:2714
#define DEGOF(rad)
Degrees of a Radian value.
Definition: math/constants.h:33
int32_t shutdown()
Shutdown agent gracefully.
Definition: agentclass.cpp:366
int32_t request_pause(string &req, string &response, Agent *)
Definition: agent_antenna.cpp:460
int32_t request_get_rotctl(string &req, string &response, Agent *)
Definition: agent_antenna.cpp:551
double data_ctime(string path)
Definition: datalib.cpp:1910
float azim
Azimuth;.
Definition: jsondef.h:2256
int32_t prkx2su_set_sensitivity(float sensitivity)
Definition: prkx2su_lib.cpp:491
Definition: elapsedtime.h:62
int32_t request_set_offset(string &req, string &response, Agent *)
Definition: agent_antenna.cpp:468
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
static bool debug
Definition: agent_antenna.cpp:164
int32_t request_get_azel(string &req, string &response, Agent *)
Definition: agent_antenna.cpp:499
static bool trackflag
Definition: agent_antenna.cpp:189
string utc2iso8601(double utc)
ISO 8601 version of time.
Definition: timelib.cpp:1286
static LsFit trackel(5, 2)
vector< agentstruc > agent
Single entry vector for agent information.
Definition: jsondef.h:4247
int32_t gs232b_get_az_el(float &az, float &el)
Definition: gs232b_lib.cpp:338
Antenna.
Definition: jsondef.h:506
vector< piecestruc > pieces
Vector of all pieces in node.
Definition: jsondef.h:4232
int32_t request_get_state(string &req, string &response, Agent *)
Definition: agent_antenna.cpp:424
int32_t start_active_loop()
Definition: agentclass.cpp:347
int32_t json_createpiece(cosmosstruc *cinfo, string name, DeviceType ctype, double emi, double abs, double hcap, double hcon, double density)
Create new piece.
Definition: jsonlib.cpp:722
int32_t prkx2su_init(string dev)
Definition: prkx2su_lib.cpp:44
cosmosstruc * cinfo
Definition: agentclass.h:346
int32_t request_stop(string &req, string &response, Agent *)
Definition: agent_antenna.cpp:440
static bool antenabled
Definition: agent_antenna.cpp:163
void rotctl_loop()
Definition: agent_antenna.cpp:646
float az
Definition: agent_antenna.cpp:182
double firstx()
Least squares first independent value.
Definition: mathlib.cpp:2687
string mjd2iso8601(double mjd)
Definition: timelib.cpp:1316
Definition: jsondef.h:565
Definition: jsondef.h:563
float el
Definition: agent_antenna.cpp:183
#define RADOF(deg)
Radians of a Degree value.
Definition: math/constants.h:29

Variable Documentation

float gsmin = RADOF(10.)
static
string antbase = ""
static
std::string nodename = ""
static
std::string agentname
static
std::string antdevice
static
uint16_t devindex = -1
static
uint16_t antindex = -1
static
antstruc target
static
antstruc current
static
bool antconnected = false
static
bool antenabled = false
static
bool debug
static
uint16_t targetrotctlport = 0
static
uint16_t rotctlport = 0
static
socket_channel rotctlchannel
static
thread rthread
static
std::vector<tlestruc> tle
static
Agent* agent
static
azelstruc antennaoffset = {0., 0.}
static
LsFit trackaz(5, 2)
static
LsFit trackel(5, 2)
static
bool trackflag = false
static