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

Start TwinCAT PLC Control.

Create a new PLC project with 'File > New'.

Select your target platform PC and CX (x86) or CX (ARM).

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

Open the Resources tab and the library manager.

Insert the library TcSPA.lib as shown in the picture below via 'Insert > Further library'.

Example 1:

All PLC blocks of the TwinCAT PLC SPA library are now available to you. All further implicitly required libraries have been automatically integrated with the TcSPA.lib.

 

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;
END_VAR
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;

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

 

You can add the visualisation included in the library to your project as described in section Visualisation. The previously declared variables are assigned to the placeholder as shown in the screenshot.

Example 2:

 

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 3:

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:
TcPlcSPA_Sample.zip.

 

Requirements

Development environment

Target platform

PLC libraries to be linked

TwinCAT v2.10 build >= 1320

PC or CX (x86, ARM)

TcSPA.Lib