Calculating sun position in ladder logic


Thread Starter

Jonatan Magnusson


I need to calculate the sun position using ladder logic programming. It will be used in a greenhouse environment. I'm using a Koyo DL250 PLC.

The limited floating point operations on the PLC makes it hard. I'm considering calculating angles by interpolating values from a data table. But an equation would be much much nicer. Low precision is ok (within 20 degrees or so).

My main problems are:

* Comparison of float values is not available
* arctan, arccos and arcsin is not available (i've implemented sin and cos using taylor approximations)
* Square root is not available

I would appreciate any help!

// Jonatan Magnusson
Just use some look up tables. It's most likely that you are not pressed for memory in your PLC. The chips are already there, you may as well use them as not. Look up tables generally execute faster than taylor approximations and you can customize them with fudged values as needed.

Break the tables down to the number of steps/precision you need. You can multiply the trig table values by 100 and use them as integers if you have limited floating point capabilities, eg .56 becomes 56.

Since precision within 20 degrees is OK try several small lookup tables for the various times of the year, each table has values for sun position at a time of day.

If your trying to position an object such as louvers, just skip the trig and put your device position feedback value in the tables instead.

Troy Stearns

You can use the CMPR instruction to compare the floats. The results will be reflected in SP60, SP61, and SP62.

I also needed to calculate a square root in the DL205. I used "": as a model and used an iterative loop until my resolution was good enough for my application. Instead of making a "guess", as this link describes for step 1, I just start with a value of one every time.

Troy Stearns
Interesting problem.

The time of day is related to the position of the Sun. Of course you need to adjust for the variations in your time zone and disregard daylight (not) savings time. I’m not familiar with the Koyo DL250 PLC, but if it has a Real Time of day clock or if you can build one then this will work.

Where Sun at Midnight is 0 DEG. Meaning that noon would be 180 DEG. 1440 is the number of minutes in a day (24 * 60).

SunDeg = (HourOfDay *60 + MinuteOfHour) *360 / 1440

To adjust for local variation you could use:

SunDeg = (HourOfDay *60 + MinuteOfHour + MinCorrection) *360 / 1440

Where MinCorrection is the minutes variance within your time zone (plus or minus).

The results of the above formulas yield’s Degrees. This can easily be put into an integer for comparison.

The Sun rises and sets at different points on the horizon each day, therefor making each day longer or shorter. I would suggest using a light sensor to determine approximate sunrise and sunset times to adjust you angles. You would need some
filtering to compensate for cloudy days and I would suggest only looking for changes in a window of time to eliminate any false triggers do to extraneous light sources like lighting and flash lights. But this may be getting too elaborate.

If you are trying to position the panel relative to the position of the Sun, I would suggest using a pair of light sensors mounted on either side of a divider mounted to the panel. The divider would be positioned so that when the panel is pointed
correctly at the Sun, both light sensors would be shaded. When one light sensor detects light then the panel moves so as to shade that sensor. You would have to make adjustment so the panel doesn’t hunt if it over shots.

Friedrich Haase

Moin Jonatan,
moin all,

>Calculating sun position in ladder logic

I will NEVER use that for navigation at high sea.

>Low precision is ok (within 20 > degrees or so).

For such a low precision you could use a precomputed table an do some interpolation. Lets say columns of 8 to 12 hourly values, one for each month. In high latitudes you might need some more hourly values. Use time and date to select row and column as starting point for the interpolation. Don't forget summer time if it applies.

Friedrich Haase

Jonatan Magnusson

Thanks! I had missed the CMPR instruction. The square root helped as well!

I have now finished the PLC program. I succeeded in doing so without using tables. Thanks for the help!