sgp4/passpredict/passpredict.cpp

368 lines
8.4 KiB
C++
Raw Normal View History

2011-12-31 16:42:18 +00:00
#include <Observer.h>
#include <SGP4.h>
#include <Util.h>
2012-10-11 18:52:51 +00:00
#include <CoordTopographic.h>
#include <CoordGeodetic.h>
2011-05-28 01:02:13 +00:00
2011-05-28 11:47:14 +00:00
#include <cmath>
2011-05-28 01:02:13 +00:00
#include <iostream>
2012-10-11 18:52:51 +00:00
#include <list>
struct PassDetails
{
DateTime aos;
DateTime los;
double max_elevation;
};
double FindMaxElevation(
const CoordGeodetic& user_geo,
SGP4& sgp4,
const DateTime& aos,
const DateTime& los)
{
2011-06-08 15:21:33 +00:00
Observer obs(user_geo);
2012-10-11 18:52:51 +00:00
double max_elevation = 0.0;
2011-06-08 15:21:33 +00:00
/*
* time step in seconds
*/
double time_step = 180.0;
/*
* still searching for max elevation
*/
bool searching = true;
/*
* fine tune the max elevation value
*/
bool fine_tune = false;
2012-10-11 18:52:51 +00:00
DateTime current_time(aos);
while (current_time < los && searching)
{
2011-06-08 15:21:33 +00:00
/*
* find position
*/
2011-12-13 22:37:13 +00:00
Eci eci = sgp4.FindPosition(current_time);
2011-06-08 15:21:33 +00:00
CoordTopographic topo = obs.GetLookAngle(eci);
/*
* keep updating max elevation
*/
2012-10-11 18:52:51 +00:00
if (topo.elevation > max_elevation)
{
2011-06-08 15:21:33 +00:00
max_elevation = topo.elevation;
2012-10-11 18:52:51 +00:00
}
else if (!fine_tune)
{
2011-06-08 15:21:33 +00:00
/*
* passed max elevation
* max elevation happened in the last 6 minutes
* go back and fine tune max elevation value
*/
2012-10-11 18:52:51 +00:00
current_time = current_time.AddSeconds(-2.0 * time_step);
2011-06-08 15:21:33 +00:00
/*
* dont go back before aos
*/
if (current_time < aos)
current_time = aos;
/*
* 1 second increment
*/
time_step = 1.0;
fine_tune = true;
/*
* reset elevation
*/
max_elevation = -99.9;
2011-06-10 19:34:39 +00:00
2012-10-11 18:52:51 +00:00
}
else
{
2011-06-08 15:21:33 +00:00
/*
* found max elevation
*/
searching = false;
}
2012-10-11 18:52:51 +00:00
if (searching)
{
current_time = current_time.AddSeconds(time_step);
2011-06-08 15:21:33 +00:00
if (current_time > los)
2012-10-11 18:52:51 +00:00
{
2011-06-08 15:21:33 +00:00
current_time = los;
2012-10-11 18:52:51 +00:00
}
2011-06-08 15:21:33 +00:00
}
2012-10-11 18:52:51 +00:00
}
2011-06-08 15:21:33 +00:00
return max_elevation;
}
2012-10-11 18:52:51 +00:00
DateTime FindCrossingPoint(
const CoordGeodetic& user_geo,
SGP4& sgp4,
const DateTime& initial_time1,
const DateTime& initial_time2,
bool finding_aos)
{
2011-05-28 01:02:13 +00:00
Observer obs(user_geo);
bool searching = true;
2012-10-11 18:52:51 +00:00
int cnt = 0;
2011-05-28 01:02:13 +00:00
2012-10-11 18:52:51 +00:00
DateTime time1(initial_time1);
DateTime time2(initial_time2);
2011-05-28 01:02:13 +00:00
2012-10-11 18:52:51 +00:00
double diff = (time2 - time1).TotalSeconds();
if (finding_aos)
{
diff = std::floor(diff);
}
else
{
diff = std::ceil(diff);
}
DateTime middle_time(time1.AddSeconds(diff));
2011-05-28 01:02:13 +00:00
2012-10-11 18:52:51 +00:00
while (searching && cnt < 25)
{
2011-05-28 11:47:14 +00:00
/*
2012-10-11 18:52:51 +00:00
* calculate satellite position
2011-05-28 11:47:14 +00:00
*/
2011-12-13 22:37:13 +00:00
Eci eci = sgp4.FindPosition(middle_time);
2011-05-28 01:02:13 +00:00
CoordTopographic topo = obs.GetLookAngle(eci);
2012-10-11 18:52:51 +00:00
if (topo.elevation > 0.0)
{
if (finding_aos)
{
2011-05-28 01:02:13 +00:00
time2 = middle_time;
2012-10-11 18:52:51 +00:00
}
else
{
2011-05-28 01:02:13 +00:00
time1 = middle_time;
}
2012-10-11 18:52:51 +00:00
}
else
{
if (finding_aos)
{
2011-05-28 01:02:13 +00:00
time1 = middle_time;
2012-10-11 18:52:51 +00:00
}
else
{
2011-05-28 01:02:13 +00:00
time2 = middle_time;
}
}
/*
* when two times are within a second, stop
*/
2012-10-11 18:52:51 +00:00
if ((time2 - time1).TotalSeconds() < 1.5)
{
2011-05-28 01:02:13 +00:00
searching = false;
}
2012-10-11 18:52:51 +00:00
else
{
diff = (time2 - time1).TotalSeconds();
if (finding_aos)
{
diff = std::floor(diff);
}
else
{
diff = std::ceil(diff);
}
middle_time = time1.AddSeconds(diff);
}
cnt++;
}
2011-05-28 01:02:13 +00:00
return middle_time;
}
2012-10-11 18:52:51 +00:00
std::list<struct PassDetails> GeneratePassList(
const CoordGeodetic& user_geo,
SGP4& sgp4,
const DateTime& start_time,
const DateTime& end_time,
const int time_step)
{
std::list<struct PassDetails> pass_list;
2011-05-28 01:02:13 +00:00
Observer obs(user_geo);
2012-10-11 18:52:51 +00:00
DateTime aos_time;
DateTime los_time;
2011-05-28 01:02:13 +00:00
bool found_aos = false;
bool found_los = false;
2012-10-11 18:52:51 +00:00
DateTime previous_time(start_time);
DateTime current_time(start_time);
2011-05-28 01:02:13 +00:00
2012-10-11 18:52:51 +00:00
while (current_time < end_time)
{
/*
* calculate satellite position
*/
2011-12-13 22:37:13 +00:00
Eci eci = sgp4.FindPosition(current_time);
2011-05-28 01:02:13 +00:00
CoordTopographic topo = obs.GetLookAngle(eci);
2012-10-11 18:52:51 +00:00
std::cout << std::fixed << current_time << " " << topo.elevation << std::endl;
if (topo.elevation > 0.0)
{
2011-05-28 01:02:13 +00:00
/*
* satellite is above horizon
*/
2012-10-11 18:52:51 +00:00
if (start_time == current_time)
{
/*
* satellite was already above the horizon at the start time,
* so use the start time
*/
2011-05-28 01:02:13 +00:00
aos_time = start_time;
2012-10-11 18:52:51 +00:00
}
else
{
2011-05-28 01:02:13 +00:00
/*
2012-10-11 18:52:51 +00:00
* find the point at which the satellite crossed the horizon
2011-05-28 01:02:13 +00:00
*/
2012-10-11 18:52:51 +00:00
aos_time = FindCrossingPoint(
user_geo,
sgp4,
previous_time,
current_time,
true);
2011-05-28 01:02:13 +00:00
}
2012-10-11 18:52:51 +00:00
found_aos = true;
found_los = false;
}
else if (found_aos)
{
2011-05-28 01:02:13 +00:00
/*
2012-10-11 18:52:51 +00:00
* satellite now below horizon and we have an AOS, so record the
* pass
2011-05-28 01:02:13 +00:00
*/
2012-10-11 18:52:51 +00:00
los_time = FindCrossingPoint(
user_geo,
sgp4,
previous_time,
current_time,
false);
found_aos = false;
found_los = true;
struct PassDetails pd;
pd.aos = aos_time;
pd.los = los_time;
pd.max_elevation = FindMaxElevation(
user_geo,
sgp4,
aos_time,
los_time);
pass_list.push_back(pd);
2011-05-28 01:02:13 +00:00
}
previous_time = current_time;
2011-06-08 15:21:33 +00:00
2012-10-11 18:52:51 +00:00
if (found_los)
{
/*
* at the end of the pass move the time along by 20mins
*/
current_time = current_time + TimeSpan(0, 20, 0);
}
else
2012-10-11 18:52:51 +00:00
{
/*
* move the time along by the time step value
*/
current_time = current_time + TimeSpan(0, 0, time_step);
}
2011-06-08 15:21:33 +00:00
if (current_time > end_time)
2012-10-11 18:52:51 +00:00
{
/*
* dont go past end time
*/
2011-06-08 15:21:33 +00:00
current_time = end_time;
2012-10-11 18:52:51 +00:00
}
2012-10-11 18:52:51 +00:00
found_los = false;
2011-06-08 15:21:33 +00:00
};
2011-05-28 01:02:13 +00:00
2012-10-11 18:52:51 +00:00
if (found_aos)
{
/*
* satellite still above horizon at end of search period, so use end
* time as los
*/
struct PassDetails pd;
pd.aos = aos_time;
pd.los = end_time;
pd.max_elevation = FindMaxElevation(user_geo, sgp4, aos_time, los_time);
pass_list.push_back(pd);
2011-05-28 01:02:13 +00:00
}
2012-10-11 18:52:51 +00:00
return pass_list;
2011-05-28 01:02:13 +00:00
}
int main() {
2011-12-15 22:46:42 +00:00
CoordGeodetic geo(51.507406923983446, -0.12773752212524414, 0.05);
2012-10-11 18:52:51 +00:00
Tle tle("UK-DMC 2 ",
"1 25544U 98067A 12285.65009259 .00017228 00000-0 30018-3 0 4501",
"2 25544 051.6477 262.7396 0017757 155.0745 185.1532 15.50683239796101");
2011-05-28 01:02:13 +00:00
SGP4 sgp4(tle);
2012-10-11 18:52:51 +00:00
std::cout << tle << std::endl;
2011-05-28 01:02:13 +00:00
/*
2012-10-11 18:52:51 +00:00
* generate 1 day schedule
2011-05-28 01:02:13 +00:00
*/
2012-10-11 18:52:51 +00:00
DateTime start_date = DateTime::Now();
DateTime end_date(start_date.AddDays(1.0));
std::list<struct PassDetails> pass_list;
std::list<struct PassDetails>::const_iterator itr;
std::cout << "Start time: " << start_date << std::endl;
std::cout << "End time : " << end_date << std::endl << std::endl;
/*
* generate passes
*/
pass_list = GeneratePassList(geo, sgp4, start_date, end_date, 180);
if (pass_list.begin() == pass_list.end())
{
std::cout << "No passes found" << std::endl;
}
else
{
itr = pass_list.begin();
do
{
std::cout
<< "AOS: " << itr->aos
<< ", LOS: " << itr->los
<< ", Max El: " << Util::RadiansToDegrees(itr->max_elevation)
<< std::endl;
}
while (++itr != pass_list.end());
}
2011-05-28 01:02:13 +00:00
return 0;
}