00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #ifndef lint
00038 static const char rcsid[] =
00039 "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/satellite/satposition.cc,v 1.9 2005/08/22 05:08:34 tomh Exp $";
00040 #endif
00041
00042 #include "satposition.h"
00043 #include "satgeometry.h"
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046 #include <math.h>
00047
00048 static class TermSatPositionClass : public TclClass {
00049 public:
00050 TermSatPositionClass() : TclClass("Position/Sat/Term") {}
00051 TclObject* create(int argc, const char*const* argv) {
00052 if (argc == 5) {
00053 float a, b;
00054 sscanf(argv[4], "%f %f", &a, &b);
00055 return (new TermSatPosition(a, b));
00056 } else
00057 return (new TermSatPosition);
00058 }
00059 } class_term_sat_position;
00060
00061 static class PolarSatPositionClass : public TclClass {
00062 public:
00063 PolarSatPositionClass() : TclClass("Position/Sat/Polar") {}
00064 TclObject* create(int argc, const char*const* argv) {
00065 if (argc == 5) {
00066 float a = 0, b = 0, c = 0, d = 0, e = 0;
00067 sscanf(argv[4], "%f %f %f %f %f", &a, &b, &c, &d, &e);
00068 return (new PolarSatPosition(a, b, c, d, e));
00069 }
00070 else {
00071 return (new PolarSatPosition);
00072 }
00073 }
00074 } class_polarsatposition;
00075
00076 static class GeoSatPositionClass : public TclClass {
00077 public:
00078 GeoSatPositionClass() : TclClass("Position/Sat/Geo") {}
00079 TclObject* create(int argc, const char*const* argv) {
00080 if (argc == 5)
00081 return (new GeoSatPosition(double(atof(argv[4]))));
00082 else
00083 return (new GeoSatPosition);
00084 }
00085 } class_geosatposition;
00086
00087 static class SatPositionClass : public TclClass {
00088 public:
00089 SatPositionClass() : TclClass("Position/Sat") {}
00090 TclObject* create(int, const char*const*) {
00091 printf("Error: do not instantiate Position/Sat\n");
00092 return (0);
00093 }
00094 } class_satposition;
00095
00096 double SatPosition::time_advance_ = 0;
00097
00098 SatPosition::SatPosition() : node_(0)
00099 {
00100 bind("time_advance_", &time_advance_);
00101 }
00102
00103 int SatPosition::command(int argc, const char*const* argv) {
00104
00105 if (argc == 2) {
00106 }
00107 if (argc == 3) {
00108 if(strcmp(argv[1], "setnode") == 0) {
00109 node_ = (Node*) TclObject::lookup(argv[2]);
00110 if (node_ == 0)
00111 return TCL_ERROR;
00112 return TCL_OK;
00113 }
00114 }
00115 return (TclObject::command(argc, argv));
00116 }
00117
00119
00121
00122
00123
00124 TermSatPosition::TermSatPosition(double Theta, double Phi) {
00125 initial_.r = EARTH_RADIUS;
00126 period_ = EARTH_PERIOD;
00127 set(Theta, Phi);
00128 type_ = POSITION_SAT_TERM;
00129 }
00130
00131
00132
00133
00134
00135
00136
00137
00138 void TermSatPosition::set(double latitude, double longitude)
00139 {
00140 if (latitude < -90 || latitude > 90)
00141 fprintf(stderr, "TermSatPosition: latitude out of bounds %f\n",
00142 latitude);
00143 if (longitude < -180 || longitude > 180)
00144 fprintf(stderr, "TermSatPosition: longitude out of bounds %f\n",
00145 longitude);
00146 initial_.theta = DEG_TO_RAD(90 - latitude);
00147 if (longitude < 0)
00148 initial_.phi = DEG_TO_RAD(360 + longitude);
00149 else
00150 initial_.phi = DEG_TO_RAD(longitude);
00151 }
00152
00153 coordinate TermSatPosition::coord()
00154 {
00155 coordinate current;
00156
00157 current.r = initial_.r;
00158 current.theta = initial_.theta;
00159 current.phi = fmod((initial_.phi +
00160 (fmod(NOW + time_advance_,period_)/period_) * 2*PI), 2*PI);
00161
00162 #ifdef POINT_TEST
00163 current = initial_;
00164 #endif
00165 return current;
00166 }
00167
00169
00171
00172 PolarSatPosition::PolarSatPosition(double altitude, double Inc, double Lon,
00173 double Alpha, double Plane) : next_(0), plane_(0) {
00174 set(altitude, Lon, Alpha, Inc);
00175 bind("plane_", &plane_);
00176 if (Plane)
00177 plane_ = int(Plane);
00178 type_ = POSITION_SAT_POLAR;
00179 }
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 void PolarSatPosition::set(double Altitude, double Lon, double Alpha, double Incl)
00194 {
00195 if (Altitude < 0) {
00196 fprintf(stderr, "PolarSatPosition: altitude out of \
00197 bounds: %f\n", Altitude);
00198 exit(1);
00199 }
00200 initial_.r = Altitude + EARTH_RADIUS;
00201 if (Alpha < 0 || Alpha >= 360) {
00202 fprintf(stderr, "PolarSatPosition: alpha out of bounds: %f\n",
00203 Alpha);
00204 exit(1);
00205 }
00206 initial_.theta = DEG_TO_RAD(Alpha);
00207 if (Lon < -180 || Lon > 180) {
00208 fprintf(stderr, "PolarSatPosition: lon out of bounds: %f\n",
00209 Lon);
00210 exit(1);
00211 }
00212 if (Lon < 0)
00213 initial_.phi = DEG_TO_RAD(360 + Lon);
00214 else
00215 initial_.phi = DEG_TO_RAD(Lon);
00216 if (Incl < 0 || Incl > 180) {
00217
00218 fprintf(stderr, "PolarSatPosition: inclination out of \
00219 bounds: %f\n", Incl);
00220 exit(1);
00221 }
00222 inclination_ = DEG_TO_RAD(Incl);
00223
00224 double num = initial_.r * initial_.r * initial_.r;
00225 period_ = 2 * PI * sqrt(num/MU);
00226 }
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 coordinate PolarSatPosition::coord()
00239 {
00240 coordinate current;
00241 double partial;
00242 partial =
00243 (fmod(NOW + time_advance_, period_)/period_) * 2*PI;
00244 double theta_cur, phi_cur, theta_new, phi_new;
00245
00246
00247
00248 theta_cur = fmod(initial_.theta + partial, 2*PI);
00249 phi_cur = initial_.phi;
00250
00251
00252
00253
00254
00255
00256 assert (inclination_ < PI);
00257
00258
00259
00260 theta_new = PI/2 - asin(sin(inclination_) * sin(theta_cur));
00261
00262
00263 if (theta_cur > PI/2 && theta_cur < 3*PI/2)
00264 phi_new = atan(cos(inclination_) * tan(theta_cur)) +
00265 phi_cur + PI;
00266 else
00267 phi_new = atan(cos(inclination_) * tan(theta_cur)) +
00268 phi_cur;
00269 phi_new = fmod(phi_new + 2*PI, 2*PI);
00270
00271 current.r = initial_.r;
00272 current.theta = theta_new;
00273 current.phi = phi_new;
00274 return current;
00275 }
00276
00277
00278
00279
00280
00281
00282
00283 bool PolarSatPosition::isascending()
00284 {
00285 double partial = (fmod(NOW + time_advance_, period_)/period_) * 2*PI;
00286 double theta_cur = fmod(initial_.theta + partial, 2*PI);
00287 if ((theta_cur > PI/2)&&(theta_cur < 3*PI/2)) {
00288 return 0;
00289 } else {
00290 return 1;
00291 }
00292 }
00293
00294 int PolarSatPosition::command(int argc, const char*const* argv) {
00295 Tcl& tcl = Tcl::instance();
00296 if (argc == 2) {
00297 }
00298 if (argc == 3) {
00299 if (strcmp(argv[1], "set_next") == 0) {
00300 next_ = (PolarSatPosition *) TclObject::lookup(argv[2]);
00301 if (next_ == 0) {
00302 tcl.resultf("no such object %s", argv[2]);
00303 return (TCL_ERROR);
00304 }
00305 return (TCL_OK);
00306 }
00307 }
00308 return (SatPosition::command(argc, argv));
00309 }
00310
00312
00314
00315 GeoSatPosition::GeoSatPosition(double longitude)
00316 {
00317 initial_.r = EARTH_RADIUS + GEO_ALTITUDE;
00318 initial_.theta = PI/2;
00319 set(longitude);
00320 type_ = POSITION_SAT_GEO;
00321 period_ = EARTH_PERIOD;
00322 }
00323
00324 coordinate GeoSatPosition::coord()
00325 {
00326 coordinate current;
00327 current.r = initial_.r;
00328 current.theta = initial_.theta;
00329 double fractional =
00330 (fmod(NOW + time_advance_, period_)/period_) *2*PI;
00331 current.phi = fmod(initial_.phi + fractional, 2*PI);
00332 return current;
00333 }
00334
00335
00336
00337
00338 void GeoSatPosition::set(double longitude)
00339 {
00340 if (longitude < -180 || longitude > 180)
00341 fprintf(stderr, "GeoSatPosition: longitude out of bounds %f\n",
00342 longitude);
00343 if (longitude < 0)
00344 initial_.phi = DEG_TO_RAD(360 + longitude);
00345 else
00346 initial_.phi = DEG_TO_RAD(longitude);
00347 }