Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.04 KB | None | 0 0
  1. //#include <iostream>
  2. #include <inttypes.h>
  3. //typedef char byte;
  4. //#define float double
  5. using namespace std;
  6. #define PROGMEM
  7. #define PI M_PI
  8. #define DEG_TO_RAD (PI/180)
  9. #define pgm_read_float(o) (*(o))
  10.  
  11. #include <math.h>
  12. //#include "orbitalElements.h"
  13. #include <avr/pgmspace.h>
  14. //http://www.stjarnhimlen.se/comp/ppcomp.html
  15. //sun orbital elements
  16. const PROGMEM float soe[] = { 0,0,282.9404,4.70935e-5,1,
  17. 0.016709,-1.151e-9,356.047,0.9856002585
  18. };
  19. #define SOE_N (pgm_read_float(soe))
  20. #define SOE_I (pgm_read_float(soe+1))
  21. #define SOE_W(day) (pgm_read_float(soe+2)+(day)*(pgm_read_float(soe+3)))
  22. #define SOE_A (pgm_read_float(soe+4))
  23. #define SOE_E(day) (pgm_read_float(soe+5)+(day)*(pgm_read_float(soe+6)))
  24. #define SOE_M(day) (pgm_read_float(soe+7)+(day)*(pgm_read_float(soe+8)))
  25.  
  26. //moon orbital elements
  27. const PROGMEM float moe[] = { 125.1228, -0.0529538083, 5.1454, 318.0634, 0.1643573223,
  28. 60.266,0.0549,115.3654, 13.0649929509
  29. };
  30. #define MOE_N(day) (pgm_read_float(moe)+(day)*(pgm_read_float(moe+1)))
  31. #define MOE_I (pgm_read_float(moe+2))
  32. #define MOE_W(day) (pgm_read_float(moe+3)+(day)*(pgm_read_float(moe+4)))
  33. #define MOE_A (pgm_read_float(moe+5))
  34. #define MOE_E (pgm_read_float(moe+6))
  35. #define MOE_M(day) (pgm_read_float(moe+7)+(day)*(pgm_read_float(moe+8)))
  36.  
  37. float sunRise, sunSet,
  38. moonRise, moonSet;
  39. double dayNumber(int year, byte m, byte D, double UT)
  40. {
  41. return (double)(367 * (long)year - 7 * (year + (m + 9) / 12) / 4 + 275 * m / 9 + D - 730530) + UT / 24;
  42. }
  43.  
  44. void sun(int year, byte m, byte D, double UT)
  45. {
  46. float h = -0.8 * DEG_TO_RAD;
  47. float d = dayNumber(year, m, D, UT);
  48. float M = SOE_M(d);
  49. while (M >= 360)M -= 360;
  50. M *= DEG_TO_RAD;
  51. float e = SOE_E(d);
  52. float E = M + e * sin(M) * (1.0 + e * cos(M));
  53. float xv = cos(E) - e;
  54. float yv = sqrt(1.0 - e * e) * sin(E);
  55. float v = atan2(yv, xv);
  56. float r = sqrt(xv * xv + yv * yv);
  57. float lonsun = ( v + SOE_W(d) * DEG_TO_RAD );
  58. float xs = r * cos(lonsun);
  59. float ys = r * sin(lonsun);
  60. float xe = xs;
  61. float ecl = (23.4393 - 3.563e-7 * d) * DEG_TO_RAD;
  62. float ye = ys * cos(ecl);
  63. float ze = ys * sin(ecl);
  64. float sunRA = atan2(ye, xe);
  65. float sunDec = atan2(ze, sqrt(xe * xe + ye * ye));
  66. float longit = 20 * DEG_TO_RAD; //wspolrzedne geograficzne +20E,+50N
  67. float lat = 50 * DEG_TO_RAD;
  68. float UT_Sun_in_south = (sunRA - (PI + M + SOE_W(d) * DEG_TO_RAD) - longit) / (15 * DEG_TO_RAD);
  69. while (UT_Sun_in_south < 0) UT_Sun_in_south += 24;
  70. float clha = (sin(h) - sin(lat) * sin(sunDec)) / (cos(lat) * cos(sunDec));
  71. if (abs(clha) <= 1) clha = acos(clha);
  72. else
  73. {
  74. /*polarny dzien lub noc, lub Slonce nie schodzi ponizej h*/
  75. }
  76. sunRise = UT_Sun_in_south - clha * 12 / PI;
  77. sunSet = UT_Sun_in_south + clha * 12 / PI;
  78. }
  79. void moon(int year, byte m, byte D, double UT)
  80. {
  81. float h = 0 * DEG_TO_RAD;
  82. float d = dayNumber(year, m, D, UT);
  83. float M = MOE_M(d);
  84. while (M >= 360)M -= 360;
  85. while (M < 0) M += 360;
  86. M *= DEG_TO_RAD;
  87. float e = MOE_E;
  88. float E = M + e * sin(M) * (1.0 + e * cos(M));
  89. float a=MOE_A;
  90. float xv = a*(cos(E) - e);
  91. float yv = a*sqrt(1.0 - e * e) * sin(E);
  92. float v = atan2(yv, xv);
  93. float r = sqrt(xv * xv + yv * yv);
  94. float N=MOE_N(d);
  95. while (N>=360) N-=360;
  96. while (N<0) N+=360;
  97. N *= DEG_TO_RAD;
  98. float w=MOE_W(d);
  99. while (w>=360) w-=360;
  100. while (w<0) w+=360;
  101. w *= DEG_TO_RAD;
  102. float i=MOE_I*DEG_TO_RAD;
  103. float xh=r*(cos(N)*cos(v+w)-sin(N)*sin(v+w)*cos(i));
  104. float yh=r*(sin(N)*cos(v+w)+cos(N)*sin(v+w)*cos(i));
  105. float zh=r*(sin(v+w)*sin(i));
  106. float lonecl=atan2(yh,xh);
  107. float latecl=atan2(zh,sqrt(xh*xh+yh*yh));
  108. float Ls=(SOE_M(d)-SOE_W(d))*DEG_TO_RAD;
  109. float Lm=M+N+w;
  110. lonecl+=(-1.274*sin(M-2*(Lm-Ls))+.658*sin(2*(Lm-Ls))-0.186*sin(SOE_M(d)*DEG_TO_RAD))*DEG_TO_RAD;
  111. latecl+=(-0.173*sin(M+w-2*(Lm-Ls))+0.055*sin(w+2*(Lm-Ls)))*DEG_TO_RAD;//tutaj przy 0.055 troche poskracalem, bo dalo sie :)
  112. r-=(0.58*cos(M-2*(Lm-Ls))+0.46*cos(2*(Lm-Ls)));
  113. xh=r*cos(lonecl)*cos(latecl);
  114. yh=r*sin(lonecl)*cos(latecl);
  115. zh=r*sin(latecl);
  116. float ecl= (23.4393 - 3.563E-7 * d)*DEG_TO_RAD;
  117. float xe=xh;
  118. float ye=yh*cos(ecl)-zh*sin(ecl);
  119. float ze=yh*sin(ecl)+zh*cos(ecl);
  120. float moonRA=atan2(ye,xe);
  121. float moonDec = atan2(ze, sqrt(xe*xe+ye*ye));
  122. float longit = 20 * DEG_TO_RAD; //wspolrzedne geograficzne +20E,+50N
  123. float lat = 50 * DEG_TO_RAD;
  124. // cout << "RA "<< moonRA*12/PI << ":Dec " << (moonDec/(DEG_TO_RAD) )<<" r:" << r<<endl;
  125. float UT_Moon_in_south = (moonRA - ( PI+(SOE_M(d)+SOE_W(d))*DEG_TO_RAD) - longit) / (15 * DEG_TO_RAD);
  126. while (UT_Moon_in_south < 0) UT_Moon_in_south += 24;
  127. float clha = (sin(h) - sin(lat) * sin(moonDec)) / (cos(lat) * cos(moonDec));
  128. if (abs(clha) <= 1) clha = acos(clha);
  129. else
  130. {
  131. clha=0;
  132. /*Yet another thing to consider: the Sun is always in the south near 12:00 local time, but the Moon may be in the
  133. * south at any time of the day (or night). This means you must pay more attention that you're really iterating
  134. * towards the rise or set time you want, and not some rise/set time one day earlier, or later.
  135.  
  136. Since the Moon rises and sets on the average 50 minutes later each day, there usually will be one day each month when
  137. the Moon never rises, and another day when it never sets. You must have this in mind when iterating your rise/set times,
  138. otherwise your program may easily get caught into an infinite loop when it tries to force e.g. a rise time between
  139. 00:00 and 24:00 local time on a day when the Moon never rises.
  140.  
  141. At high latitudes the Moon occasionally rises, or sets, twice on a single calendar day. This may happen above the
  142. "lunar arctic circle", which moves between 61.5 and 71.9 deg latitude during the 18-year period of the motion of
  143. the lunar nodes. You may want to pay attention to this.*/
  144. }
  145. moonRise = UT_Moon_in_south - clha * 12 / PI;
  146. moonSet = UT_Moon_in_south + clha * 12 / PI;
  147. }
  148.  
  149. float Minutes(float bad_time){
  150. float good_time;
  151. int time_int;
  152. float help;
  153. float help_dec;
  154. time_int=(int)bad_time;
  155. help_dec=bad_time-time_int;
  156. help=help_dec*60/100;
  157. good_time=time_int+help;
  158. return good_time;
  159. }
  160. void setup()
  161. {
  162. Serial.begin(9600);
  163. int y=2018,m=4;
  164. for(int i=1; i<=30; i++)
  165. {
  166. Serial.println(String(i)+"."+String(m)+"."+String(y));
  167.  
  168. sun(y, m, i, 12);
  169. moon(y, m, i, 12);
  170. if(sunRise>24){
  171. sunRise=sunRise-24;
  172. sunRise=Minutes(sunRise);
  173. Serial.println("Sun rise: "+String(sunRise)+"("+String(i+1)+"."+String(m)+"."+String(y)+")");
  174. }
  175. else if(sunRise<0){
  176. sunRise=24+sunRise;
  177. sunRise=Minutes(sunRise);
  178. Serial.println("Sun rise: "+String(sunRise)+"("+String(i-1)+"."+String(m)+"."+String(y)+")");
  179. }
  180. else {
  181. sunRise=Minutes(sunRise);
  182. Serial.println("Sun rise: "+String(sunRise));
  183. }
  184. if(sunSet>24){
  185. sunSet=sunSet-24;
  186. sunSet=Minutes(sunSet);
  187. Serial.println("Sun rise: "+String(sunSet)+"("+String(i+1)+"."+String(m)+"."+String(y)+")");
  188. }
  189. else if(sunSet<0){
  190. sunSet=24+sunSet;
  191. sunSet=Minutes(sunSet);
  192. Serial.println("Sun rise: "+String(sunSet)+"("+String(i-1)+"."+String(m)+"."+String(y)+")");
  193. }
  194. else {
  195. sunSet=Minutes(sunSet);
  196. Serial.println("Sun rise: "+String(sunSet));
  197. }
  198.  
  199.  
  200.  
  201. float moonRise1=moonRise;
  202. moon(y,m,i,moonSet);
  203. float moonSet1=moonSet;
  204. moon(y,m,i,moonRise1);
  205.  
  206.  
  207. if(moonRise>24){
  208. moonRise=moonRise-24;
  209. moonRise=Minutes(moonRise);
  210. Serial.println("Moon rise: "+String(moonRise)+"("+String(i+1)+"."+String(m)+"."+String(y)+")");
  211. }
  212. else if(moonRise<0){
  213. moonRise=24+moonRise;
  214. moonRise=Minutes(moonRise);
  215. Serial.println("Moon rise: "+String(moonRise)+"("+String(i-1)+"."+String(m)+"."+String(y)+")");
  216. }
  217. else {
  218. moonRise=Minutes(moonRise);
  219. Serial.println("Moon rise: "+String(moonRise));
  220. }
  221. if(moonSet1>24){
  222.  
  223. moonSet1=moonSet1-24;
  224. moonSet1=Minutes(moonSet1);
  225. Serial.println("Moon set: "+String(moonSet1)+"("+String(i+1)+"."+String(m)+"."+String(y)+")");
  226. }
  227. else if(moonSet1<0){
  228. moonSet1=24+moonSet1;
  229. moonSet1=Minutes(moonSet1);
  230. Serial.println("Moon set: "+String(moonSet1)+"("+String(i-1)+"."+String(m)+"."+String(y)+")");
  231. }
  232. else {
  233. moonSet1=Minutes(moonSet1);
  234. Serial.println("Moon set: "+String(moonSet1));
  235. }
  236.  
  237. }
  238.  
  239. }
  240.  
  241.  
  242. void loop()
  243. {
  244. // put your main code here, to run repeatedly:
  245.  
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement