Skip to content

Conversation

@Nick806
Copy link

@Nick806 Nick806 commented Jan 15, 2025

Fixed issue causing NaN azimuth when azimuth was close to 0-360 or 180

Description:

The issue causing the azimuth to return NaN when it was close to 0-360 or 180 has been fixed.

Example of the problem:

CODE:

#include <SunPosition.h>
#include "Arduino.h"

#define PRECISION 5

unsigned long int t = 1700000000;
float lat = 45;
float lon = 11;

float getSunElevation() {
  SunPosition sun(lat, lon, t);
  return sun.altitude();
}

float getSunAzimuth() {
  SunPosition sun(lat, lon, t);
  return sun.azimuth();
}

void setup() {
  Serial.begin(115200);
  while (!Serial) {}
}

void loop() {
  t++;

  if (isnan(getSunElevation()) || isnan(getSunAzimuth())){
    t--; 
    do{
      Serial.println("Time: " + String(t) + " - Ele: " + String(getSunElevation(), PRECISION) + " - Azi:" + String(getSunAzimuth(), PRECISION));
      t++;
    } while (isnan(getSunElevation()) || isnan(getSunAzimuth()));

    Serial.println("Time: " + String(t) + " - Ele: " + String(getSunElevation(), PRECISION) + " - Azi:" + String(getSunAzimuth(), PRECISION));
    Serial.println();
  }
}

SERIAL OUTPUT:

Time: 1700175624 - Ele: -63.18660 - Azi:359.93439
Time: 1700175625 - Ele: -63.18661 - Azi:    nan
Time: 1700175626 - Ele: -63.18661 - Azi:    nan
Time: 1700175627 - Ele: -63.18661 - Azi:    nan
Time: 1700175628 - Ele: -63.18661 - Azi:    nan
Time: 1700175629 - Ele: -63.18661 - Azi:    nan
Time: 1700175630 - Ele: -63.18661 - Azi:    nan
Time: 1700175631 - Ele: -63.18661 - Azi:    nan
Time: 1700175632 - Ele: -63.18661 - Azi:    nan
Time: 1700175633 - Ele: -63.18660 - Azi:0.06561

Time: 1700218835 - Ele: 26.55935 - Azi:180.00000
Time: 1700218836 - Ele: 26.55936 - Azi:    nan
Time: 1700218837 - Ele: 26.55936 - Azi:    nan
Time: 1700218838 - Ele: 26.55936 - Azi:    nan
Time: 1700218839 - Ele: 26.55936 - Azi:    nan
Time: 1700218840 - Ele: 26.55936 - Azi:    nan
Time: 1700218841 - Ele: 26.55936 - Azi:    nan
Time: 1700218842 - Ele: 26.55935 - Azi:180.00000

Time: 1700262034 - Ele: -63.44598 - Azi:359.94064
Time: 1700262035 - Ele: -63.44599 - Azi:    nan
Time: 1700262036 - Ele: -63.44599 - Azi:    nan
Time: 1700262037 - Ele: -63.44599 - Azi:    nan
Time: 1700262038 - Ele: -63.44599 - Azi:    nan
Time: 1700262039 - Ele: -63.44599 - Azi:    nan
Time: 1700262040 - Ele: -63.44599 - Azi:    nan
Time: 1700262041 - Ele: -63.44599 - Azi:    nan
Time: 1700262042 - Ele: -63.44599 - Azi:    nan
Time: 1700262043 - Ele: -63.44598 - Azi:0.05936

......

How the issue was fixed:

The issue was in the following line of code:

azm = degrees(acos(((sin(lat) * cos(zen)) - sin(decl)) / (cos(lat) * sin(zen)))); // Azimuth

The acos() function accepts values only in the range of -1 to 1. Due to floating-point rounding errors, the expression ((sin(lat) * cos(zen)) - sin(decl)) / (cos(lat) * sin(zen)) could result in values slightly outside this range, which causes the acos() function to return NaN.

For example, in some cases, the calculation produced values slightly greater than 1 or slightly less than -1:

Unix:1700175628   Lat:45   Lon:11   ((sin(lat)*cos(zen))-sin(decl))/(cos(lat)*sin(zen)):-1.0000001192
Unix:1700218838   Lat:45   Lon:11   ((sin(lat)*cos(zen))-sin(decl))/(cos(lat)*sin(zen)):1.0000001192

Solution:
To address this issue, the calculation has been modified to clamp the result between -1 and 1 before passing it to acos(). This ensures that the value remains within the valid range for acos() and prevents it from returning NaN:

azm = degrees(acos(max(min(((sin(lat) * cos(zen)) - sin(decl)) / (cos(lat) * sin(zen)),-1),1))); // Azimuth

This adjustment prevents the expression from exceeding the allowable range for acos(), thereby eliminating the errors and returning valid azimuth values.


Fixed issue causing NaN azimuth when azimuth was close to 0-360 or 180

Risolto problema che causava azimut NaN quando l'azimut era vicino a 0-360 oppure 180

Исправлена ​​проблема, вызывающая азимут NaN, когда азимут был близок к 0–360 или 180.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant