-1
\$\begingroup\$

i try to programme a microkontroller with Mikro c to found the position of sun

i puted a function CalculateSunPosition to calcule the sun position but the problem wenn i change the time the result is fix i dont know why maybe the problem in calling of the function or the declaration

please i need help this is my programme

sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
double posa;
double posh;


double   Deg2Rad = (3.141592653589793/ 90);
double  Rad2Deg = (90 / 3.141592653589793);
char value1 [15] ;
char value2 [15] ;
char value3 [15] ;
char value4 [15] ;
double abc;

double CorrectAngle(double angleInRadians)
{
  double a;
  if (angleInRadians < 0) {
    // 2 * Math.PI - (Math.Abs(angleInRadians) % (2 * Math.PI));
    a= (2* 3.141592653589793) -( Abs(angleInRadians) - ((2* 3.141592653589793) *((Abs(angleInRadians) / (2* 3.141592653589793)))));
    return a;
  } else if (angleInRadians > (2 * 3.141592653589793)) {
    //angleInRadians % (2 * Math.PI);
    a= angleInRadians-((2 * 3.141592653589793)*(angleInRadians/(2 * 3.141592653589793)));
    return a;
  } else {
    a= (angleInRadians);
    return a;
  }
}

void CalculateSunPosition( int year, int month, int day, int hour, int minute, int second,double latitude, double longitude)
{
  //OSCCON= 0X66;
  double altitude  ;
  double aziDenom ;
  double azimuth;
  double aziNom;
  double meanLongitude ;
  double meanAnomaly ;
  double equationOfCenter ;
  double elipticalLongitude ;
  double obliquity ;
  double rightAscension ;
  double declination  ;
  double hourAngle;
  double siderealTimeHours ;
  double siderealTimeUT  ;
  double siderealTime;
  double julianDate;

  double julianCenturies ;
  double  result;
  double ticks;


  // Number of days from J2000.0.
  julianDate = 367 * year - (int)((7.0 / 4.0) * (year +(int)((month + 9.0) / 12.0))) + (int)((275.0 * month) / 9.0) +  day - 730531.5;

  julianCenturies = julianDate / 36525.0;


  ticks = (second + (minute * 60) + (hour * 3600)) * 10000000;
  result = ticks * 2.7777777777777777E-11;

  // julianCenturies = julianDate / 36525.0;

  // Sidereal Time
  siderealTimeHours = 6.6974 + 2400.0513 * julianCenturies;
  siderealTimeUT = siderealTimeHours +  (366.2422 / 365.2422) * result;
  siderealTime = siderealTimeUT * 15 + longitude;

  julianDate =julianDate + (result/ 24.0);
  julianCenturies = julianDate / 36525.0;

  // Solar Coordinates
  meanLongitude = CorrectAngle(Deg2Rad *(280.466 + 36000.77 * julianCenturies));
  meanAnomaly = CorrectAngle(Deg2Rad *
    (357.529 + 35999.05 * julianCenturies));

  equationOfCenter = Deg2Rad * ((1.915 - 0.005 * julianCenturies) *
    sin(meanAnomaly) + 0.02 * sin(2 * meanAnomaly));

  elipticalLongitude =
    CorrectAngle(meanLongitude + equationOfCenter);

  obliquity = (23.439 - 0.013 * julianCenturies) * Deg2Rad;

  // Right Ascension
  rightAscension = atan2(
    cos(obliquity) * sin(elipticalLongitude),
    cos(elipticalLongitude));

  declination = asin( sin(rightAscension) * sin(obliquity));

  hourAngle = CorrectAngle(siderealTime * Deg2Rad) - rightAscension;

  if (hourAngle > 3.141592653589793) {
    hourAngle = hourAngle -( 2 * 3.141592653589793);
  }

  altitude = asin(sin(latitude * Deg2Rad) *
    sin(declination) + cos(latitude * Deg2Rad) *
    cos(declination) * cos(hourAngle));

  // Nominator and denominator for calculating Azimuth
  // angle. Needed to test which quadrant the angle is in.
  aziNom = -sin(hourAngle);
  aziDenom =
    tan(declination) * cos(latitude * Deg2Rad) -
    sin(latitude * Deg2Rad) * cos(hourAngle);

  azimuth = atan(aziNom / aziDenom);

  if (aziDenom < 0) // In 2nd or 3rd quadrant {
    azimuth =azimuth + (2 * 3.141592653589793);
  }

  posa=altitude * Rad2Deg;
  posh=   azimuth * Rad2Deg;
  //julianDate
  floattostr(posa,value1);
  floattostr(posh,value2);

  // Cursor off
  Lcd_out(1,1,"alt:");
  Lcd_out(1,6,value1);
  Lcd_out(2,1,"azim:");
  Lcd_out(2,6,value2);
}

void main() {
  //OSCCON= 0X66;
  I2C1_Init(100000);
  Lcd_Init ();
  Lcd_Cmd(_LCD_CURSOR_OFF);

  CalculateSunPosition (2016,9,3,4,30,50,1222223,322323) ;//time and position
}
\$\endgroup\$
  • 4
    \$\begingroup\$ I'm voting to close this question as off-topic because it is a pure programming question and should have been posted on Stack Overflow. \$\endgroup\$ – Bence Kaulics Sep 3 '16 at 18:36
1
\$\begingroup\$

Do you get any warnings from the compiler?

The call

CalculateSunPosition (2016,9,3,4,30,50,1222223,322323);

has the last two values as int values, but the definition is

void CalculateSunPosition(...,double latitude, double longitude)

So I would start by changing the call to

CalculateSunPosition (2016,9,3,4,30,50,1222223.0,322323.0);

A very useful skill in solving programming programs, is to reduce the size of the program to the smallest program which has the same bug. The act of doing that often helps identify the bug, and even if it is still a mystery, it helps others because they have less code to read.

I would also suggest, if you are having problems, trying a compiler which is capable of giving thorough error messages, on your host PC. You might get some extra help from using an alternative compiler, and it might be quicker to run the program (with some printf's) on a PC than upload and program an embedded system. All the embedded system calls, like Lcd_out(2,6,value2); could be 'mocked' with simple printf statements, or even less, for the host PC. That way you can easily add extra debugging by printing values from the program to check everything is working. You might try a 'proper' logging package, but I find fprintf(stderr, ...) quick and easy to use when I am baffled

\$\endgroup\$
  • \$\begingroup\$ thank you for answer. i dont have a warnings it compileted i tried now to change like what you said but it is the same problem \$\endgroup\$ – Bilel Kaddachi Sep 3 '16 at 16:52
  • \$\begingroup\$ @BilelKaddachi - please tell us if it fixes it. \$\endgroup\$ – gbulmer Sep 3 '16 at 16:53
  • \$\begingroup\$ not yet. do you think is the calling correct ? \$\endgroup\$ – Bilel Kaddachi Sep 3 '16 at 16:56
  • 1
    \$\begingroup\$ I think you should make you program small and simple, and build it up in small pieces, checking it is corect as you go. \$\endgroup\$ – gbulmer Sep 3 '16 at 16:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.