Example

This example offers an introduction into the handling of function block FB_SPA, which is available with the TwinCAT Solar Position Algorithm library.

The objective in this example is to determine the sun position on 4 March 2010 at 14:27:00 at the Cheops pyramid in Egypt.
Time zone: UTC + 2 hours
Latitude: 29.979, [°]
Longitude: 31.134 [°]
Height: 70 [m]
Annual average temperature: 21.7 [°C]
Other locations and times are determined similarly.

Overview

The following steps are now performed:

1. Installation of the PLC library

2. Program structure

3. Test

1. Installation of the PLC library

Create a new TwinCAT PLC project and select your target platform.

Your first POU is a program called MAIN and in the programming language ST (Structured Text).

Mark node References and insert the library Tc2_SPA.

Example 1:

2. Program structure

For sun position calculations you should declare an instance of function block FB_SPAand local variables for allocating the required result values.

The input parameter for the calculation can be directly assigned to the inputs of the function block.
In addition to the sun angles the sunrise and sunset is required as output, which means the advanced functionality is required, which is specified via the enumeration value eSPA_ZA_RTS of type E_SPA_FunctionCode.
The output values of the function block are assigned to your local variables.

The program section should now look as follows:


PROGRAM MAIN
VAR
    fbSPA     : FB_SPA;
    fSunZenith : LREAL;
    fSunAzimuth : LREAL;
    tSunrise    : TIME;
    tSunset     : TIME;
    eErrorCode : E_SPA_ErrorCode;
    bExecute    : BOOL;
    bInit     : BOOL := TRUE;
END_VAR

IF bInit THEN
bInit         := FALSE;
fbSPA.stTime.iYear     := 2010;
fbSPA.stTime.iMonth    := 3;
fbSPA.stTime.iDay     := 4;
fbSPA.stTime.iHour     := 14;
fbSPA.stTime.iMinute := 27;
fbSPA.fTimezone     := 2;
fbSPA.fLongitude     := 31.134;
fbSPA.fLatitude     := 29.979;
fbSPA.fElevation     := 70;
fbSPA.fTemperature     := 21.7;
fbSPA.eFunction     := eSPA_ZA_RTS;
END_IF
IF bExecute THEN
    fbSPA();
    eErrorCode := fbSPA.iErrorCode;

    fSunZenith := fbSPA.fZenith;
    fSunAzimuth := fbSPA.fAzimuth;
    tSunrise    := LREAL_TO_TIME(fbSPA.fSunrise*60*60*1000);
    tSunset     := LREAL_TO_TIME(fbSPA.fSunset*60*60*1000);
END_IF

This sample of the TwinCAT Solar Position Algorithm library contains a visualisation facility that provides a quick overview of current inputs and outputs of function block FB_SPA. It is therefore ideal for test purposes.

3. Test

Compile the created PLC program.
Make sure that TwinCAT is in the Run mode on the desired system.
Login to the desired run-time system from TwinCAT PLC Control. Start the PLC program.

The calculation is executed by setting the local variable bExecute to TRUE. This can be done via 'online write' or the corresponding button in the visualisation, for example.
The visualisation should now present the following results:

Example 2:

The sun angles at other locations and at other times within the given value ranges can be calculated accordingly. If an input parameter is invalid, an eErrorCode with the corresponding enumeration value for the error is displayed.

Click here to save this example program:
SPA_InfoSys_Sample1.zip.