FB_CalcSunPosition
Calculation of sun position based on the date, time, longitude and latitude.
Description
The position of the sun for a given point in time can be calculated according to common methods with a defined accuracy. For applications with moderate requirements, the present function block is sufficient. As the basis for this, the SUNAE algorithm was used, which represents a favorable compromise between accuracy and computing effort.
The position of the sun at a fixed observation point is normally determined by specifying two angles. One angle indicates the height above the horizon, where 0° means that the sun is in the horizontal plane of the observation site and 90° means that the sun is directly over the observer's head. The other angle indicates the direction in which the sun is standing. The SUNAE algorithm is used to distinguish whether the observer is standing on the northern hemisphere (longitude > 0 degrees) or on the southern hemisphere (longitude < 0 degrees) of the earth. If the observation point is in the northern hemisphere, a value of 0° is assigned for the northern direction of the sun and then moves clockwise around the compass, i.e. 90° is east, 180° is south, 270° west, etc. If the observation point is in the southern hemisphere, 0° corresponds to the southern direction and moves counterclockwise, i.e. 90° is east, 180° is north, 270° is west, etc.
In specifying the time, the time according to Greenwich Mean Time (GMT) must be given.
The latitude is specified as the distance of a place on the surface of the earth from the equator to the north or to the south in degrees. The latitude can assume a value from 0° (at the equator) to ±90° (at the poles). A positive sign thereby indicates a northern direction and a negative sign a southern direction. The longitude is an angle that can assume values up to ±180° starting from the prime meridian 0° (an artificially determined North-South line). A positive sign indicates a longitude in an eastern direction and a negative sign in a western direction. Examples:
Location | Longitude | Latitude |
---|---|---|
Sydney, Australia | 151.2° | -33.9° |
New York, USA | 74.0° | 40.7° |
London, England | -0.1° | 51.5° |
Moscow, Russia | 37.6° | 55.7° |
Beijing, China | 116.3° | 39.9° |
Dubai, United Arab Emirates | 55.3° | 25.4° |
Rio de Janeiro, Brazil | -43.2° | -22.9° |
Hawaii, USA | -155.8° | 20.2° |
Verl, Germany | 8.5° | 51.9° |
If the function block FB_CalcSunPosition() returns a negative value for the height of the sun (fSunElevation), then the sun is not visible. This can be used to determine sunrise and sunset.
VAR_INPUT
fDegreeOfLongitude : LREAL := 8.5;
fDegreeOfLatitude : LREAL := 51.9;
dtGMT : TIMESTRUCT;
fDegreeOfLongitude: Longitude in degrees.
fDegreeofLatitude: Latitude in degrees.
dtGMT: Current time as Greenwich Mean Time (GMT).
VAR_OUTPUT
fSunAzimuth : LREAL;
fSunElevation : LREAL;
fSunAzimuth: Direction of the sun (northern hemisphere: 0° north ... 90° east ... 180° south ... 270° west ... / southern hemisphere: 0° south ... 90° east ... 180° north ... 270° west ...).
fSunElevation: Height of the sun (0° horizontal ... 90° vertical).
Example
PROGRAM MAIN
VAR
fbCalcSunPosition : FB_CalcSunPosition;
fSunAzimuth : LREAL;
fSunElevation : LREAL;
fbGetSystemTime : GETSYSTEMTIME;
fileTime : T_FILETIME;
END_VAR
fbGetSystemTime(timeLoDW=>fileTime.dwLowDateTime,
timeHiDW=>fileTime.dwHighDateTime);
fbCalcSunPosition( fDegreeOfLongitude := 8.5,
fDegreeOfLatitude := 51.9,
dtGMT := FILETIME_TO_SYSTEMTIME(fileTime));
fSunAzimuth := fbCalcSunPosition.fSunAzimuth;
fSunElevation := fbCalcSunPosition.fSunElevation;
Requirements
Development environment | Required PLC library |
---|---|
TwinCAT from v3.1.4020.32 | Tc2_BABasic from v3.1.0.0 |