Sunrise & Sunset Calculator - General Discussion and Assistance - CHDK Forum

Sunrise & Sunset Calculator

  • 15 Replies
  • 7893 Views
*

Offline @rt

  • *
  • 34
    • Art's Fantastic Website
Sunrise & Sunset Calculator
« on: 26 / June / 2009, 18:54:15 »
Advertisements
Hi Guys,
I have a Sunrise & Sunset routine written in C that I have working on another C platform.

I thought it would have been nice to add it to the calendar screen on my camera, as it would
be useful to know the time of day the Sun will set to be ready for photos.
but I found the compiler doesn't like sin, cos & tan math functions (among others).

I see that they are not defined in math.h.

Is there a way to support these functions?
Cheers, Art.
If not actually, then potentially.

*

Offline reyalp

  • ******
  • 12736
Re: Sunrise & Sunset Calculator
« Reply #1 on: 26 / June / 2009, 19:14:38 »
You would either have to implement them yourself, or find them in the firmware dumps, preferable using http://chdk.wikia.com/wiki/Signature_finder

I'd look near the math functions that have already been found, e.g. _pow, _log etc.

Keep in mind the cameras do not have an FPU.

Don't forget what the H stands for.

*

Offline PhyrePhoX

  • *****
  • 2254
  • make RAW not WAR
    • PhyreWorX
Re: Sunrise & Sunset Calculator
« Reply #2 on: 26 / June / 2009, 19:34:08 »
actually a function like this has been long on my personal todo list (dunno if i added it to the bugtracker).
as a temporary solution you could do it in lua. i really would like to have a function that returns the sunrise & sunset (plus moon) times for a given location, so you could set up the cam that it automatically starts taking pictures @ sunrise.
other than that, if it is to be done in native c, it should be added to the calendar function, i concur.

*

Offline ewavr

  • ****
  • 1057
  • A710IS
Re: Sunrise & Sunset Calculator
« Reply #3 on: 26 / June / 2009, 20:08:51 »
Although trigonometric functions are present in VxWorks cameras, they are not used. It is therefore not known how to find them in DryOS (if they are really present there) :(.


*

Offline @rt

  • *
  • 34
    • Art's Fantastic Website
Re: Sunrise & Sunset Calculator
« Reply #4 on: 26 / June / 2009, 20:47:53 »
That's u bummer :(

Quote
as a temporary solution you could do it in lua
When I finish my LUA camera timer, I'd rather not use it,
but how would LUA have those function if the underlying C program doesn't?


Well it's only my second day with this platform, but I've got the moon phase algorithm from this page:
http://www.voidware.com/moon_phase.htm
working fine displaying the moon phase in the calendar screen.

I Have a  moorise and moonset times algo I've previously used and tested that I ported it from BASIC to C.
The Sunrise/Sunset routine I've also tweaked, tried and tested on another platform is here:
http://personal.inet.fi/cool/jjlammi/rscalc.c

And unfortunately they both use sin,cos,tan and more math functions :(
Art.




« Last Edit: 26 / June / 2009, 21:07:35 by @rt »
If not actually, then potentially.

*

Offline @rt

  • *
  • 34
    • Art's Fantastic Website
Re: Sunrise & Sunset Calculator
« Reply #5 on: 08 / July / 2009, 04:23:49 »
I found a Sunrise/Sunset algorithm here:
http://williams.best.vwh.net/sunrise_sunset_algorithm.htm
which gives step by step English explanation of what it is doing.
It uses sin, cos, tan, asin, acos & atan functions.

I have generated all the tables with a program and saved them as c arrays
in an effort to implement the needed trig function using lookup tables.

Just to clarify...

sin, cos & atan should range from 0-360,

asin, acos & tan from 0 to 1.

Is that right?
If not actually, then potentially.

*

Offline nonno

  • *
  • 15
Re: Sunrise & Sunset Calculator
« Reply #6 on: 08 / July / 2009, 06:17:17 »
Maybe you can use the Taylor/mcLaurin series approximation to calculate sin(x), cos(x) and nearly whatever function you need using just sum,multiplication and division. Of course it's just an approximation, but if you use the right amount of terms and you choose the right point to approximate you function you can have a really good result.

Here is the wikipedia link: http://en.wikipedia.org/wiki/Taylor_series

you can also use walfram alpha (http://www01.wolframalpha.com/) to calculate and look at the graphs of function and approximation:
ex: "taylor cos(x) of order 10"

If you want to tell me the functions you need i can calculate some.

Maybe there are also better ways to approx specific functions.

(also remember that cos(x)=sin(x+pi/2) so they are the same function)

*

Offline @rt

  • *
  • 34
    • Art's Fantastic Website
Re: Sunrise & Sunset Calculator
« Reply #7 on: 08 / July / 2009, 07:44:42 »
If not actually, then potentially.


*

Offline nonno

  • *
  • 15
Re: Sunrise & Sunset Calculator
« Reply #8 on: 08 / July / 2009, 10:09:58 »
i found this site with a lot of taylor series, i the only one missing is the atan2 that's a C function and not a classical mathematical object (i'll try to work it out, but i think it can be decomposed in basic functions):

http://web.mit.edu/kenta/www/three/taylor.html

Just select the number of terms that are necessary to obtain an acceptable error and delete the highest.
to use all this functions you should work with radiants and not degrees, so you can choose to work between -pi and pi or from 0 to 2*pi. Working between 0 and 2*pi means an easier conversion but you have to traslate all those functions (i have traslated some of them) but i don't know if it will be a good solution to indroduce a so big number of differences in terms both of time and accuracy. Translating all degree values [0:180] to [0:pi] and [180:360] to [-pi:o] shouldn't be a problem.

9th order taylor series translated to pi:
sin(x)=-(x-pi)^9/362880+(x-pi)^7/5040-(x-pi)^5/120+(x-pi)^3/6-x+pi
cos(x)=-1+(x-pi)^2/2-(x-pi)^4/24+(x-pi)^6/720-(x-pi)^8/40320+(x-pi)^10/3628800
tan(x)=x+(x-pi)^3/3+(2*(x-pi)^5)/15+(17*(x-pi)^7)/315+(62*(x-pi)^9)/2835-pi

if you need all the translated functions (tell me the order you need) just ask me.

*

Offline @rt

  • *
  • 34
    • Art's Fantastic Website
Re: Sunrise & Sunset Calculator
« Reply #9 on: 08 / July / 2009, 18:53:18 »
So they can be used directly as C functions?
That would have to be of use to a lot of ppl who want a maths library.

So where I'd normally call sin,

DEFINE PI 3.1415.....

sin (180);

sin(double x) {
x =-(x-pi)^9/362880+(x-pi)^7/5040-(x-pi)^5/120+(x-pi)^3/6-x+pi;
}

The page with the Sunset algo on it gives formula to convert deg to rad:
Quote
       NOTE: the algorithm assumes the use of a calculator with the
        trig functions in "degree" (rather than "radian") mode. Most
        programming languages assume radian arguments, requiring back
        and forth convertions. The factor is 180/pi. So, for instance,
        the equation RA = atan(0.91764 * tan(L)) would be coded as RA
        = (180/pi)*atan(0.91764 * tan((pi/180)*L)) to give a degree
        answer with a degree input for L.


EDIT... Is this "^" supposed to be XOR?
My C compiler complains about it being in that function.



« Last Edit: 08 / July / 2009, 19:04:18 by @rt »
If not actually, then potentially.

 

Related Topics