00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #define AGCONNECT
00018 #include <AgConnect.h>
00019 #include "stkvisualization.h"
00020 #include <sys/time.h>
00021 #include <string>
00022 #include <sstream>
00023 #include <iostream>
00024 using std::string;
00025 using std::ostringstream;
00026 using namespace std;
00027
00028 extern "C" {
00029 char AgEAppName[ ]="stkvisualization";
00030 }
00031
00032
00033 double StkVisualization::GetOffsetTime()
00034 {
00035 struct timeval curtime;
00036 const double microSecPerSec = 1000000.0;
00037
00038 gettimeofday(&curtime, NULL);
00039 return curtime.tv_sec + (curtime.tv_usec / microSecPerSec) + Offset;
00040 }
00041
00042
00043 void StkVisualization::ClearError()
00044 {
00045 if (ErrorExists)
00046 {
00047 ErrorExists = false;
00048 AgConCleanupReturnInfo(ReturnInfo);
00049 }
00050 }
00051
00052
00053
00054 bool StkVisualization::SendCommand(ostringstream & commandLine)
00055 {
00056
00057 strcpy(CommandBuffer, commandLine.str().c_str());
00058
00059
00060 if (AgConProcessSTKCmd(ConID, CommandBuffer, ReturnInfo) == AgCError)
00061 {
00062 ErrorExists = true;
00063 return false;
00064 }
00065
00066 AgConCleanupReturnInfo(ReturnInfo);
00067 }
00068
00069
00070
00071 bool StkVisualization::SendCommand(string commandLine)
00072 {
00073 strcpy(CommandBuffer, commandLine.c_str());
00074
00075
00076
00077 if (AgConProcessSTKCmd(ConID, CommandBuffer, ReturnInfo) == AgCError)
00078 {
00079 ErrorExists = true;
00080 return false;
00081 }
00082
00083 AgConCleanupReturnInfo(ReturnInfo);
00084 }
00085
00086
00087 StkVisualization::StkVisualization(double offset) :
00088
00089 Offset(offset), ConID(NULL), ErrorExists(false)
00090 {
00091 strcpy(CommandBuffer, "");
00092
00093 ReturnInfo = new AgTConReturnInfo;
00094 }
00095
00096
00097 StkVisualization::~StkVisualization()
00098 {
00099 ClearError();
00100
00101 delete ReturnInfo;
00102 }
00103
00104
00105 bool StkVisualization::ConnectStk(const char* stkHostAddress, const char* stkHostPort)
00106 {
00107 ClearError();
00108
00109
00110 if (AgConInit(NULL) == AgCError)
00111 return false;
00112
00113
00114 ostringstream stk;
00115 stk << stkHostAddress;
00116 stk << ":";
00117 stk << stkHostPort;
00118 strcpy(CommandBuffer, stk.str().c_str());
00119
00120
00121
00122 if (AgConOpenSTK(&ConID, CommandBuffer) == AgCError)
00123 return false;
00124
00125 return true;
00126 }
00127
00128
00129 bool StkVisualization::SetUpScenario()
00130 {
00131 ClearError();
00132
00133
00134 string createScenario = "New / Scenario " + ScenarioName;
00135 SendCommand(createScenario);
00136
00137
00138 string setGUIDate = "SetGUIUnits * Date GREGUTC";
00139 SendCommand(setGUIDate);
00140
00141
00142
00143 string setConnectDate = "SetUnits / km GREGUTC";
00144 SendCommand(setConnectDate);
00145
00146
00147
00148 string setTimePeriod = "SetTimePeriod * \"Today\" \"+3 days\"";
00149 SendCommand(setTimePeriod);
00150
00151
00152 if (AgConProcessSTKCmd(ConID, "SetEpoch * \"1 Jan 1970 00:00:00.0\"", ReturnInfo) == AgCError)
00153 {
00154 ErrorExists = true;
00155 return false;
00156 }
00157
00158 AgConCleanupReturnInfo(ReturnInfo);
00159
00160
00161 string setAnimationMode = "Animate * SetAnimationMode RealTime";
00162 SendCommand(setAnimationMode);
00163
00164
00165 string realTimeOffset = "Animate * RealTimeOffset -26";
00166 SendCommand(realTimeOffset);
00167
00168
00169 string setRefreshMode = "Animate * SetRefreshMode HighSpeed";
00170 SendCommand(setRefreshMode);
00171
00172 return true;
00173 }
00174
00175
00176
00177
00178 bool StkVisualization::SetUpNumberOfSatellites()
00179 {
00180
00181 SatelliteName = new (nothrow) string[NumberOfSatellites];
00182
00183
00184 SatellitePositionUpdate = new (nothrow) bool[NumberOfSatellites];
00185
00186 AllSatellitesUpdated = false;
00187 WindowsSet = false;
00188
00189
00190 for (int iterator = 0; iterator < NumberOfSatellites; iterator++)
00191 {
00192 SatellitePositionUpdate[iterator] = false;
00193 }
00194
00195
00196 if (SatelliteName == 0)
00197 {
00198 cout << "Error in memory allocation for array of satellite names" << endl;
00199 return false;
00200 }
00201 else if (SatellitePositionUpdate == 0)
00202 {
00203 cout << "Error in memory allocation for array of satellite names" << endl;
00204 return false;
00205 }
00206 return true;
00207 }
00208
00209
00210
00211 bool StkVisualization::SetUpSatellite()
00212 {
00213 ClearError();
00214
00215
00216 for(int counter = 0; counter < NumberOfSatellites; counter++)
00217 {
00218
00219
00220
00221 string createSatellite = "New / Scenario/" + ScenarioName + "/Satellite " + SatelliteName[counter];
00222 SendCommand(createSatellite);
00223
00224
00225 string setRealTime;
00226 setRealTime = "Realtime */Satellite/" + SatelliteName[counter]+ " SetProp ";
00227 SendCommand(setRealTime);
00228
00229
00230
00231
00232
00233 string setLookAhead;
00234 setLookAhead = "Realtime */Satellite/" + SatelliteName[counter] + " SetLookAhead HoldCBIPosition 1500 3 30";
00235 SendCommand(setLookAhead);
00236
00237
00238 string setTimePeriod = "SetTimePeriod * \"Today\" \"+3 days\"";
00239 SendCommand(setTimePeriod);
00240
00241
00242 ostringstream setAttitudeRealTime;
00243 setAttitudeRealTime << "SetAttitude */Satellite/" + SatelliteName[counter] + " RealTime Extrapolate 1800 1800";
00244 SendCommand(setAttitudeRealTime);
00245
00246
00247
00248 string showAttitudeSphere = "VO */Satellite/" + SatelliteName[counter] + " AttitudeView Sphere Show On";
00249 SendCommand(showAttitudeSphere);
00250
00251
00252
00253 string showBodyAxes = "VO */Satellite/" + SatelliteName[counter] + " SetVectorGeometry Modify \"Satellite/" + SatelliteName[counter] + " Body Axes\" Show On";
00254 SendCommand(showBodyAxes);
00255
00256
00257
00258 string showVVLHAxes = "VO */Satellite/" + SatelliteName[counter] + " SetVectorGeometry Modify \"Satellite/" + SatelliteName[counter] + " VVLH Axes\" Show On Color red";
00259 SendCommand(showVVLHAxes);
00260
00261
00262 string allowAnimationUpdate = "AllowAnimationUpdate * On";
00263 SendCommand(allowAnimationUpdate);
00264
00265
00266 string resetAnimation = "Animate * Reset";
00267 SendCommand(resetAnimation);
00268
00269
00270 string satelliteLighting = "VO * Lighting ObjAmbient 100";
00271 SendCommand(satelliteLighting);
00272 }
00273
00274 return true;
00275 }
00276
00277
00278 bool StkVisualization::StartAnimation()
00279 {
00280 ClearError();
00281
00282
00283 string setDateToEpoch = "SetUnits / EpochSec";
00284 SendCommand(setDateToEpoch);
00285
00286
00287 ostringstream timeWithOffset;
00288 timeWithOffset.flags(std::ios_base::fixed);
00289 timeWithOffset << "Animate * SetValues \"";
00290 timeWithOffset << GetOffsetTime();
00291 timeWithOffset << "\" ";
00292 timeWithOffset << "0.1 ";
00293 timeWithOffset << "0.1";
00294 SendCommand(timeWithOffset);
00295
00296
00297 string startAnimation = "Animate * Start Realtime";
00298 SendCommand(startAnimation);
00299
00300 for (int counter = 0; counter < NumberOfSatellites; counter++)
00301 {
00302
00303 ostringstream clearAttitudeData;
00304 clearAttitudeData << "SetAttitude ";
00305 clearAttitudeData << "*/Satellite/";
00306 clearAttitudeData << SatelliteName[counter];
00307 clearAttitudeData << " ClearData";
00308 SendCommand(clearAttitudeData);
00309 }
00310
00311 return true;
00312 }
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322 bool StkVisualization::SetWindows()
00323 {
00324 int screenWidth = 1200;
00325 int screenHeight = 750;
00326 int windowWidth3D = (screenWidth/2)/(NumberOfSatellites/4+1);
00327
00328
00329
00330 int windowHeight3D;
00331
00332 if (NumberOfSatellites <= 3)
00333 windowHeight3D = screenHeight/(NumberOfSatellites);
00334 else
00335 windowHeight3D = screenHeight/(3);
00336
00337 int windowWidth2D = screenWidth/2;
00338 int windowHeight2D = screenHeight;
00339 int reducedCounter;
00340
00341
00342
00343 string removeFirst3DWindow = "Window3D * Remove 1";
00344 SendCommand(removeFirst3DWindow);
00345
00346
00347 for (int counter = 0; counter < NumberOfSatellites; counter++)
00348 {
00349
00350
00351
00352 string create3DWindow = "Window3D * CreateWindow Type Normal";
00353 SendCommand(create3DWindow);
00354
00355 string create3DWindowAttitude = "Window3D * CreateWindow Type Attitude Path Satellite/"
00356 + SatelliteName[counter];
00357
00358
00359
00360 ostringstream set3DWindowSize;
00361 set3DWindowSize << "Window3D * Size ";
00362 set3DWindowSize << windowWidth3D;
00363 set3DWindowSize << " ";
00364 set3DWindowSize << windowHeight3D;
00365 set3DWindowSize << " ";
00366 set3DWindowSize << counter+1;
00367 SendCommand(set3DWindowSize);
00368
00369 reducedCounter = counter;
00370
00371 while(reducedCounter>2)
00372 reducedCounter = reducedCounter - 3;
00373
00374
00375
00376
00377
00378 ostringstream set3DWindowPlace;
00379 set3DWindowPlace << "Window3D * Place ";
00380 set3DWindowPlace << (counter/3)*windowWidth3D;
00381 set3DWindowPlace << " ";
00382 set3DWindowPlace << reducedCounter*windowHeight3D;
00383 set3DWindowPlace << " ";
00384 set3DWindowPlace << counter+1;
00385 SendCommand(set3DWindowPlace);
00386
00387
00388 ostringstream setView;
00389 setView << "VO * View FromTo FromRegName \"STK Object\" FromName \"Satellite/";
00390 setView << SatelliteName[counter];
00391 setView << "\" ToRegName \"STK Object\" ToName \"Satellite/";
00392 setView << SatelliteName[counter];
00393 setView << "\" WindowID ";
00394 setView << counter+1;
00395 SendCommand(setView);
00396
00397
00398 ostringstream setZoom;
00399 setZoom << "VO * View Zoom WindowID ";
00400 setZoom << counter+1;
00401 setZoom << " FractionofCB 0.8";
00402 setZoom << "\" WindowID ";
00403 setZoom << counter+1;
00404 SendCommand(setZoom);
00405 }
00406
00407
00408 ostringstream set2DWindowSize;
00409 set2DWindowSize << "Window2D * Size";
00410 set2DWindowSize << screenWidth/2;
00411 set2DWindowSize << " ";
00412 set2DWindowSize << screenHeight;
00413 SendCommand(set2DWindowSize);
00414
00415 ostringstream set2DWindowPlace;
00416 set2DWindowPlace << "Window2D * Place ";
00417 set2DWindowPlace << screenWidth/2;
00418 set2DWindowPlace << " 0";
00419 SendCommand(set2DWindowPlace);
00420
00421 return true;
00422 }
00423
00424
00425 bool StkVisualization::Update( int satelliteNumber,
00426 int rotationSequence,
00427 double angle1,
00428 double angle2,
00429 double angle3)
00430 {
00431 ClearError();
00432
00433 double offsetTime = GetOffsetTime();
00434
00435 ostringstream newAttitude;
00436 newAttitude.flags(std::ios_base::fixed);
00437 newAttitude << "AddAttitude ";
00438 newAttitude << "*/Satellite/";
00439 newAttitude << SatelliteName[satelliteNumber-1];
00440 newAttitude << " Euler \"";
00441 newAttitude << offsetTime;
00442 newAttitude << "\" ";
00443 newAttitude << rotationSequence;
00444 newAttitude << " ";
00445 newAttitude << angle1;
00446 newAttitude << " ";
00447 newAttitude << angle2;
00448 newAttitude << " ";
00449 newAttitude << angle3;
00450 SendCommand(newAttitude);
00451
00452 return true;
00453 }
00454
00455
00456 bool StkVisualization::Update( int satelliteNumber,
00457 double Q1,
00458 double Q2,
00459 double Q3,
00460 double Q4)
00461 {
00462 ClearError();
00463
00464 double offsetTime = GetOffsetTime();
00465
00466 ostringstream newAttitude;
00467 newAttitude.flags(std::ios_base::fixed);
00468 newAttitude << "AddAttitude ";
00469 newAttitude << "*/Satellite/";
00470 newAttitude << SatelliteName[satelliteNumber-1];
00471 newAttitude << " Quat \"";
00472 newAttitude << offsetTime;
00473 newAttitude << "\" ";
00474 newAttitude << Q1;
00475 newAttitude << " ";
00476 newAttitude << Q2;
00477 newAttitude << " ";
00478 newAttitude << Q3;
00479 newAttitude << " ";
00480 newAttitude << Q4;
00481 SendCommand(newAttitude);
00482
00483 return true;
00484 }
00485
00486
00487 bool StkVisualization::Update( int satelliteNumber,
00488 double latitude,
00489 double longitude,
00490 double altitude,
00491 double latitudeRate,
00492 double longitudeRate,
00493 double altitudeRate)
00494 {
00495 ClearError();
00496
00497 double offsetTime = GetOffsetTime();
00498
00499 ostringstream newPosition;
00500 newPosition.flags(std::ios_base::fixed);
00501 newPosition << "SetPosition ";
00502 newPosition << "*/Satellite/";
00503 newPosition << SatelliteName[satelliteNumber-1];
00504 newPosition << " LLA \"";
00505 newPosition << offsetTime;
00506 newPosition << "\" ";
00507 newPosition << latitude;
00508 newPosition << " ";
00509 newPosition << longitude;
00510 newPosition << " ";
00511 newPosition << altitude;
00512 newPosition << " ";
00513 newPosition << latitudeRate;
00514 newPosition << " ";
00515 newPosition << longitudeRate;
00516 newPosition << " ";
00517 newPosition << altitudeRate;
00518 SendCommand(newPosition);
00519
00520
00521 SatellitePositionUpdate[satelliteNumber-1] = true;
00522
00523
00524 if (WindowsSet == false)
00525 {
00526 for(int iterator = 0; iterator < NumberOfSatellites; iterator++)
00527 {
00528 if(SatellitePositionUpdate[iterator] == false)
00529 {
00530 AllSatellitesUpdated = false;
00531 break;
00532 }
00533 else
00534 AllSatellitesUpdated = true;
00535 }
00536 }
00537
00538 if ((AllSatellitesUpdated == true) && (WindowsSet == false))
00539 {
00540 SetWindows();
00541 WindowsSet = true;
00542 }
00543
00544 return true;
00545 }
00546
00547
00548
00549
00550 const char* StkVisualization::GetLastError(char* buffer)
00551 {
00552 if (ErrorExists)
00553 {
00554 string errorMessage;
00555 for (int i=0; i < ReturnInfo->numEntries; i++)
00556 {
00557 errorMessage += ReturnInfo->returnList[i];
00558 errorMessage += "\n";
00559 }
00560 strcpy (buffer, errorMessage.c_str());
00561 return buffer;
00562 }
00563 else
00564 {
00565 strcpy (buffer, "No data about error exists.");
00566 return buffer;
00567 }
00568 }
00569
00570
00571 bool StkVisualization::ShutDownStk()
00572 {
00573 ClearError();
00574
00575 string stopAnimation = "Animate * Reset";
00576 SendCommand(stopAnimation);
00577
00578 bool conClosed = true;
00579 if (AgConCloseSTK(&ConID) == AgCError)
00580 conClosed == false;
00581
00582 AgConShutdownConnect();
00583
00584 return conClosed;
00585 }
00586
00587
00588
00589
00590
00591
00592
00593
00594