more code - kids, don't try this at home...
//Routines to interface with the Plan 13 library
void updtime(time_t t){ //set or update plan13's time from the parameter
p13.setTime(year(t),month(t),day(t),hour(t),minute(t),second(t));
}
void setelements(){ //set the plan13 orbital elements
p13.setElements(fparams[0],fparams[1],fparams[2],fparams[3],fparams[4],fparams[5],
fparams[6],fparams[7],fparams[8],fparams[9],fparams[10]);
updtime(now());
p13.initSat();
fpcnt=0;
}
void cycle13(time_t when){ // run a plan 13 cycle
updtime(when);
p13.satvec();
p13.rangevec();
digitalClockDisplay(when);
cout<<" AZ="<<p13.AZ<<" ALT="<<p13.EL<<endl;
setaltaz(int(p13.EL+.5),int(p13.AZ+.5)); // rounded alt az
}
//two line elements are stored PROGMEM prog_uchar array tles
//23456789+123456789+123456789+123456789+123456789+123456789+123456789
prog_char PROGMEM tles[]={
"1 31793U 07029B 11260.12782331 -.00000001 00000-0 24514-4 0 2274"
"2 31793 070.9752 119.3719 0001888 267.5186 092.5714 14.14086103217800"
"1 25407U 98045B 11259.94296736 -.00000291 +00000-0 -12451-3 0 07277"
"2 25407 071.0084 134.2580 0007876 181.9965 178.1127 14.15544012678959"
"1 36089U 09061B 11259.73403693 .00000148 00000-0 49611-4 0 5618"
"2 36089 098.2453 308.2997 0084104 018.8975 341.5307 14.48573105 97504"
"1 25391U 98042C 11260.57814793 .00007762 00000-0 14131-3 0 2838"
"2 25391 078.9276 127.9292 0128209 222.2239 136.9070 15.40664767732124"
"1 27597U 02056A 11260.21763093 .00000157 00000-0 82866-4 0 5368"
"2 27597 098.3679 303.0927 0001784 055.3868 304.7481 14.26301238455969"
"1 22566U 93016B 11260.12337304 -.00000267 00000-0 -11384-3 0 3062"
"2 22566 071.0072 154.2698 0008387 091.0393 269.1712 14.14692990954243"
"1 28353U 04021B 11260.28898009 -.00000295 00000-0 -13017-3 0 211"
"2 28353 070.9977 161.5059 0004816 170.0950 190.0266 14.13954218375311"
"1 22220U 92076B 11259.94284285 -.00000214 +00000-0 -83949-4 0 05837"
"2 22220 070.9964 159.3696 0014849 236.9349 123.0349 14.16188382973403"
};
int numtles=8;
#define tlelen 69 //length of a tle line
#define yebegin 18 //offset to 1st byte of YE element epoch year
#define yelen 2 //length of YE in tle
#define tebegin 20 //offset to TE
#define telen 12
#define inbegin tlelen+8 //offset to INclination (in 2nd line)
#define inlen 8
#define rabegin tlelen+17 //offset to Right Ascension (in 2nd line)
#define ralen 8
#define ecbegin tlelen+26 //offset to eccentricity (in 2nd line)
#define eclen 7
#define wpbegin tlelen+34 //offset to WP - argument of perigee (in 2nd line)
#define wplen 8
#define mabegin tlelen+43 //offset to mean anomaly (in 2nd line)
#define malen 8
#define mmbegin tlelen+52 //offset to mean motion (in 2nd line)
#define mmlen 11
#define m2begin 33 //offset to 1st derivative of mm/2 (used in drag calc)
#define m2len 10
#define snbegin 2 //satellite designator
#define snlen 5
int findtle(){ //find a tle in the list with satellite # the same as what's in the buffer
int i, foundtle=0;
cout <<"looking for tle "<<buff<<endl;
if (bufflen>= 4&& bufflen<6){ //if length looks ok
strcpy(satnum,buff); //copy the buffer to the satellite #
for (i=1;i<=numtles;i++){ //cycle thru the tles
tle2buff(i,snbegin,snlen); //get the satellite number
// cout<<" considering "<<i<<": "<<buff<<endl;
if (strcmp(satnum,buff)==0){ //if it matches
foundtle=i; //note the #
}
}
}
else{
cout <<"inv. sat. @ "<<buff<<endl;
}
if (foundtle==0){
cout <<"tle "<<satnum<<" not found\n";
}
bufflen=0;
cout <<"ret. "<<foundtle<<endl;
return foundtle;
}
void tle2params(int n){ //set the parameters from tle n
tle2buff(n,snbegin,snlen);
cout<<"satno "<<buff<<endl;
fparams[0]=tle2ye(n);fparams[1]=tle2te(n);fparams[2]=tle2in(n);fparams[3]=tle2ra(n);fparams[4]=tle2ec(n);fparams[5]=tle2wp(n);
fparams[6]=tle2ma(n);fparams[7]=tle2mm(n);fparams[8]=0;fparams[9]=0;fparams[10]=180; //phonies out drag factor, orbit # and stellite orientation
fpcnt=11; //11 parameters loaded
//from setElements(double YE_in, double TE_in, double IN_in,
// double RA_in, double EC_in, double WP_in,
// double MA_in, double MM_in, double M2_in,
// double RV_in, double ALON_in );
}
void tle2buff(int n, int byteoffset,int len){ //copy n bytes out of tle n at offset given
for (int i=0;i<len;i++){
int tleoffset=(n-1)*tlelen*2; //1st byte of the 1st line of the tle pointed to
char x=pgm_read_byte_near(tles+tleoffset+byteoffset+i);
buff[i]=x;
}
buff[len]='\0';
}
double tle2ye(int n){ //return epoch year of tle n
int len=yelen, offst=yebegin;
tle2buff(n,offst,len);
return 2000.0+atof(buff);
}
double tle2te(int n){ //return epoch time of tle n
int len=telen, offst=tebegin;
tle2buff(n,offst,len);
return atof(buff);
}
double tle2in(int n){ //return inclination from tle n
int len=inlen, offst=inbegin;
tle2buff(n,offst,len);
return atof(buff);
}
double tle2ra(int n){ //return right ascension from tle n
int len=ralen, offst=rabegin;
tle2buff(n,offst,len);
return atof(buff);
}
double tle2wp(int n){ //return argument of perigee from tle n
int len=wplen, offst=wpbegin;
tle2buff(n,offst,len);
return atof(buff);
}
double tle2ma(int n){ //return mean anomaly from tle n
int len=malen, offst=mabegin;
tle2buff(n,offst,len);
return atof(buff);
}
double tle2mm(int n){ //return mean motion from tle n
int len=mmlen, offst=mmbegin;
tle2buff(n,offst,len);
return atof(buff);
}
double tle2ec(int n){ //return eccentricity from tle n
int i=0,len=eclen, offst=(n-1)*2*tlelen+ecbegin;
buff[0]='0';buff[1]='.'; //make it look floaty
for (i=0;i<len;i++){
char x=pgm_read_byte_near(tles+offst+i);
buff[i+2]=x;
}
buff[len+2]='\0';
return atof(buff);
}
void setltime(){ //set time to hour min sec day month year
if (fpcnt<4){ //if some params are missing in fill from the plan
fparams[3]=planday; fparams[4]=planmonth; fparams[5]=planyear-2000;
if (fpcnt<3){ //zero the seconds if not supplied
fparams[2]=0;
}
}
setTime(fparams[0],fparams[1],fparams[2],fparams[3],fparams[4],fparams[5]); // another way to set the time setTime(hr,min,sec,day,month,yr)
digitalClockDisplay(now());
fpcnt=0; //eat the parameters
setTime(now()+lcloffset);
cout<<"\n UCT: ";
digitalClockDisplay(now());
cout<<endl;
}
void syncrtc(){ //set the RTC to the millis() clock
RTC.set(now()); // set the RTC to the system time
cout <<"RTC set\n";
}
//debug routines
void prtstate(){
cout<<endl<<" fpcnt="<<_DEC(fpcnt)<<endl;
for (int j=1;j<=fpcnt;j++){
cout<<fparams[j]<<" ";
}
cout<<"\n UCT:";
digitalClockDisplay(now());
}
void digitalClockDisplay(time_t when){
// digital clock display of the time
Serial.print(hour(when));
printDigits(minute(when));
printDigits(second(when));
Serial.print(" ");
Serial.print(day(when));
Serial.print(" ");
Serial.print(month(when));
Serial.print(" ");
Serial.print(year(when));
// Serial.println();
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}