OK, so we can spin a motor, but where do we actually point the thing to get the sunlight that we need to charge our batteries and what time do we need to open or close the pop hole door on the coop ?
As a human, its easy, you know where the big bright thing is in the sky that you're not allowed to look at or it will hurt your eyes and you can see when it comes up in the morning and dissappears with a fantastic sunset in the evening, but how do you explain this to a little Arduino that doesn't even have eyes in the first place ?
You can't just point the panel to a specific angle or open the door at a particular time, since the position and time changes each day as we go through the Summer and Winter equinoxes and and if this project ever ends up in more than one garden, or we move house, then it will have to be able to figure the times and angles out for its self. Surely there must be some calculations to sort all this out for us.
I could just do an Internet lookup to some web site each day from the Pi and send the answers to the Arduno, but with the principle of being off-net for power, we really shouldn't need to be on-line, it can't be that hard to calculate can it ?
The story starts with a Google for "Sunrise and Sunset calculations", you will find many of these and they seem to end up back at one of the following articles
- The Almanac for Computers, 1990 published by Nautical Almanac Office United States Naval Observatory Washington, DC, with some code by Ed Williams here
- Jarmo Lammi's 1999 implementation here
- Paul Schlyter's implementation here, based on earlier works in 1979 on an HP41C calculator
The journey to success in this area was a troubled one and many times it felt a bit like peeling an onion, you start in one place and end up several layers further in but no more informed on the problem you originally started with, until you discover the next bit. This is coupled with a dictionary of terms that is difficult to understand, for example there is the declination angle, the mean anomaly, the right of ascension, the equation of time (which sounds a bit like the answer to life, the universe and everything), the time correction factor, etc. The articles talk about taking into consideration the effects of time zones, of summer time offsets (which need to be calculated) and expecting you to know that solar noon is not the same as noon on our handy wristwatch, oh and you read comments such as "obviously, you must convert between degrees and radians at the appropriate times". So, you try another approach, you look at the code which is full of single character variable names or Greek alphabet symbols which further complicate the situation.
Initially before the rotator was considered and to get the unit up and running, we try the approach of "just try the code so we can get on", which was fine for the initial open and close time calculations - until we get to the start / end of summer time and the clocks change.
When the Rotator comes into the picture and we haven't got the angle we need, we try various approaches to break into this impenetrable problem ranging from "try other implementations as that code must be buggy" and when that doesn't work (and you can't debug it, since you don't understand it), you reach for your handy engineering calculator and repeat the calculations only to get different answers. So, whats wrong ? Whats going on ? After all, I'm quite good with maths, and it can't be that difficult can it ?
Where can we go to help break this elephant of a problem into manageable pieces ?
The answer as it turns out is to understand a bit more about the history of things; the underlying astronomy. The maths is about the calculations of planetary positions , so you drift into online forums and read up on terms that refer to other terms you also don't understand and gradually things start to make a bit more sense. You are in a world of polar and spherical coordinates and the centre of the planet you are on is the zero origin that other things are measured from, yes, we are the centre of the universe.
It then starts to become clear that the single character variables or their longer Greek letter names are the standard abbreviations of the term and some terms may have multiple names, for example alpha is the elevation angle which is also known as the altitude angle. Then details like the length of sunrise and sunset is 6 degrees, measured from the centre of the sun being on the horizon.
After a while, I stumble onto the PV Education web site, particularly the Solar Radiation at the Earths Surface section, which takes you through the specifics of solar tracking and importantly has a calculator that allows you to see the calculations and their values, this is here
All you need is a couple of inputs - your longitude and latitude, a date, which we can get from the RTC chip and the GMT offset, great, so where do I get my coordinates from ? It turns out that Google maps can do this for you, its under the location as the two numbers - here's yet another coordinate system, but at least we've all probably used that one a couple of times in our life before. :-)
So, armed with something to work against we can just drop the numbers in, hit the submit button on the web site and compare what it generates to what we can do on a calculator -- but the numbers STILL don't line up, even on simple calculations such as
EoT = 9.87 sin(2B) -7.53 cos(B) - 1.5 sin(B)
and the next line says
B= (360/365) * (d-81)
Where d is the number of days since the start of the year, which we just found by counting the months / days from the 1st of January.
Now this is getting silly, we can't even get a bit of basic trigonometry right. OK, lets go to Excel and repeat the same calculation - we get the same wrong values. OK, its a sine wave, this is a well known thing, lets go back to basics.
In the picture, you can see that sin(90) (as a quarter of 360 degrees in a clrcle) is 1, so I ask excel =sin(90) in one cell and it returns the value of 0.893997, rather than the 1.0 I was expecting. What on earth is happening ?
A quick read of the excel manual for the sin function states it takes an argument in Radians, not Degrees, so we change the cell to read =sin(radians(90)) and we get 1.0. Bingo, we're using the wrong angular system. We knew this already when we hit the statement we found in previous pieces of code of "obviously, you must convert between degrees and radians at the appropriate times" , but didn't know when to use that, now we do. After a couple of updates to the excel calculations, we start getting the right values out, success happens a short while later, all our maths lines up to the reference values on the web site.
Now we understand what's going on, we can do the same in our code. Finally we get a correct set of values and the all important azimuth angle that we can use to set our solar panel with.