Go to Otherpower.com Home Page Go to Forcefield Shopping Cart Go to Wondermagnet.com Home Page
Front Page - [Homebrewed Electricity-- (wind) (solar) (hydro) (steam) (controls) (storage) (mechanical)] - Classifieds - Site News
Everything - Newbies - [Remote Living-- (housing) (heat) (light) (water)] - Rants & Opinion - Diaries - Our Products
software | 24 comments (24 topical, 0 editorial)
Re: software (3.00 / 0) (#5)
by david williams on Thu Jun 5th, 2008 at 12:00:15 PM MST
(User Info)

Here's a program that should be useful in the context of solar energy. It does the astronomical and trigonometrical calculations that have to be done in a computer-controlled sun-tracker or heliostat. With the addition of code to allow the computer to control motors that turn the tracker or mirror, the program could become the software for such a machine.

I'll post two versions. The first is in QBasic but will also run under Borland Turbo BASIC and some other implementations. The second is in a very generic BASIC, with line numbers. It will run (and has been tested) under many varieties of BASIC. The QBasic version is the "real" program, which should be easier to read and understand than the other.

If anyone wants to translate it to another language and post the result, please go ahead!

This is public-domain, free for any kind of use, including commercial use.

Enjoy!

                               dow

------------------------------------------------

' SunAlign.BAS (Version for QBasic and similar dialects)

' Calculates position of sun in sky, as azimuth (compass bearing
' measured clockwise from True North) and angle of elevation, as
' seen from any place on earth, on any date and any time.
' Also calculates alignment of a heliostat mirror.

' David Williams
' P.O. Box 48512
' 3605 Lakeshore Blvd. West
' Toronto, Ontario. M8W 4Y6
' Canada

' Initially dated 2007 Jul 07
' This version 2008 Jan 13

' All angles in radians except in i/o routines DegIn and DegOut

DECLARE SUB C2P (X, Y, Z, AZ, EL)
DECLARE SUB P2C (AZ, EL, X, Y, Z)
DECLARE FUNCTION Ang (X, Y)
DECLARE SUB DegIn (P$, X)
DECLARE SUB DegOut (P$, X)

CONST PY = 3.1415926536# ' "PI" not assignable in some BASICs
CONST DR = 180 / PY ' degree / radian factor
W = 2 * PY / 365 ' earth's mean orbital angular speed in radians/day
WR = PY / 12' earth's speed of rotation relative to sun (radians/hour)
C = -23.45 / DR ' reverse angle of earth's axial tilt in radians
ST = SIN(C) ' sine of reverse tilt
CT = COS(C) ' cosine of reverse tilt
E2 = 2 * .0167 ' twice earth's orbital eccentricity
SN = 10 * W ' 10 days from December solstice to New Year (Jan 1)
SP = 12 * W ' 12 days from December solstice to perihelion

CLS

Menu:

PRINT "1. Calculate sun's position"
PRINT "2. Calculate mirror orientation"
PRINT "3. Calculate both"
PRINT "4. Quit program"
PRINT
PRINT "Which? (1 - 4)";

DO
  S% = VAL(INKEY$)
LOOP UNTIL S% >= 1 AND S% <= 4
PRINT S%

IF S% = 4 THEN END

' Note: For brevity, no error checks on user inputs

PRINT
PRINT "Use negative numbers for directions opposite to those shown."
PRINT
DegIn "Observer's latitude (degrees North)", LT
DegIn "Observer's longitude (degrees East)", LG
INPUT "Time Zone (+- hours from GMT/UT)"; TZN
INPUT "Time (HH,MM) (24-hr format)"; HR, MIN
INPUT "Date (M#,D#)"; Mth%, Day%

PRINT

CL = PY / 2 - LT ' co-latitude

D = INT(30.6 * ((Mth% + 9) MOD 12) + 58.5 + Day%) MOD 365
' day of year (D = 0 on Jan 1)

A = W * D + SN ' orbit angle since solstice at mean speed
B = A + E2 * SIN(A - SP) ' angle with correction for eccentricity

C = (A - ATN(TAN(B) / CT)) / PY
SL = PY * (C - INT(C + .5))' solar longitude relative to mean position

C = ST * COS(B)
DC = ATN(C / SQR(1 - C * C)) ' solar declination (latitude)
' arcsine of C. ASN not directly available in QBasic

LD = (HR - TZN + MIN / 60) * WR + SL + LG ' longitude difference

CALL P2C(LD, DC, sX, sY, sZ) ' polar axis (perpend'r to azimuth plane)
CALL C2P(sY, sZ, sX, sAZ, sEL) ' horizontal axis
CALL P2C(sAZ - CL, sEL, sY, sZ, sX) ' rotate by co-latitude

IF sZ < 0 THEN
   BEEP
   PRINT "Sun Below Horizon"
   PRINT
   GOTO NewCalc
END IF

IF S% <> 2 THEN ' calculate and display sun's position

   CALL C2P(sX, sY, sZ, sAZ, sEL) ' vertical axis

   DegOut "Sun's azimuth: ", sAZ
   DegOut "Sun's elevation: ", sEL

   PRINT

END IF

IF S% > 1 THEN ' calculate and display mirror orientation

   PRINT "For target direction of light reflected from mirror:"
   DegIn "Azimuth of target direction (degrees)", tAZ
   DegIn "Elevation of target direction (degrees)", tEL

   PRINT

   CALL P2C(tAZ, tEL, tX, tY, tZ) ' target vector X,Y,Z
   CALL C2P(sX + tX, sY + tY, sZ + tZ, mAZ, mEL)
   ' angle bisection by vector addition

   PRINT "Mirror aim direction (perpendicular to surface):"
   DegOut "Azimuth: ", mAZ
   DegOut "Elevation: ", mEL

   PRINT

END IF

NewCalc:

PRINT
PRINT "New Calculation"
PRINT

GOTO Menu

FUNCTION Ang (X, Y)
 ' calculates angle from positive X axis to vector to (X,Y)

  SELECT CASE SGN(X)
    CASE 1: Ang = ATN(Y / X)
    CASE -1: Ang = ATN(Y / X) + PY
    CASE ELSE: Ang = SGN(Y) * PY / 2
  END SELECT

END FUNCTION

SUB C2P (X, Y, Z, AZ, EL)
  ' Cartesian to Polar. Convert from X,Y,Z to AZ,EL

  EL = Ang(SQR(X * X + Y * Y), Z)
  A = Ang(Y, X)
  IF A < PY THEN AZ = A + PY ELSE AZ = A - PY

END SUB

SUB DegIn (P$, X)
  ' Input angle in degrees and convert to radians

  PRINT P$;
  INPUT N
  X = N / DR

END SUB

SUB DegOut (P$, X)
 ' converts radians to degrees, rounds to nearest 0.1, and prints

  S$ = LTRIM$(STR$(INT(10 * ABS(X * DR) + .5)))
  IF S$ = "3600" THEN S$ = "0"
  IF LEN(S$) = 1 THEN S$ = "0" + S$
  IF X < 0 THEN IF VAL(S$) THEN S$ = "-" + S$
  PRINT P$; LEFT$(S$, LEN(S$) - 1); "."; RIGHT$(S$, 1); " degrees"

END SUB

SUB P2C (AZ, EL, X, Y, Z)
  ' Polar to Cartesian. Convert from AZ,EL to X,Y,Z

  Z = SIN(EL)
  C = -COS(EL)
  X = C * SIN(AZ)
  Y = C * COS(AZ)

END SUB

--------------------------------------------------------------

--------------------------------------------------------------

  1. REM SunAlign.BAS (Generic BASIC version)
  2. REM Calculates position of sun in sky, as azimuth (compass bearing
  3. REM measured clockwise from True North) and angle of elevation, as
  4. REM seen from any place on earth, on any date and any time.
  5. REM Also calculates alignment of a heliostat mirror.
  6. REM David Williams
  7. REM P.O. Box 48512
  8. REM 3605 Lakeshore Blvd. West
  9. REM Toronto, Ontario. M8W 4Y6
  10. REM Canada
  11. REM Original date 2007 Jul 07. This version 2007 Oct 07
  12. REM Note: For brevity, no error checks on user inputs
  13. CLS
  14. PRINT "Use negative numbers for opposite directions."
  15. INPUT "Observer's latitude (degrees North)"; LT
  16. INPUT "Observer's longitude (degrees East)"; LG
  17. INPUT "Date (M#,D#)"; Mth, Day
  18. INPUT "Time (HH,MM) (24-hr format)"; HR, MIN
  19. INPUT "Time Zone (+- hours from GMT/UT)"; TZN
  20. PY = 4 * ATN(1): REM "PI" not assignable in some BASICs
  21. DR = 180 / PY: REM degree/radian factor
  22. W = 2 * PY / 365: REM earth's mean orbital speed in radians/day
  23. C = -23.45 / DR: REM reverse angle of axial tilt in radians
  24. ST = SIN(C): REM sine of reverse tilt
  25. CT = COS(C): REM cosine of reverse tilt
  26. E2 = 2 * .0167: REM twice earth's orbital eccentricity
  27. SP = 12 * W: REM 12 days from December solstice to perihelion
  28. D = INT(30.6 * ((Mth + 9) MOD 12) + 58.5 + Day) MOD 365
  29. A = W * (D + 10): REM Solstice 10 days before Jan 1
  30. B = A + E2 * SIN(A - SP)
  31. C = (A - ATN(TAN(B) / CT)) / PY
  32. ET = 720 * (C - INT(C + .5)): REM equation of time
  33. REM in 720 minutes, earth rotates PI radians relative to sun
  34. C = ST * COS(B)
  35. EL = ATN(C / SQR(1 - C * C)) * DR: REM solar declination
  36. AZ = 15 * (HR - TZN) + (MIN + ET) / 4 + LG: REM longitude diff
  37. GOSUB 800
  38. R = SQR(Y * Y + Z * Z)
  39. AX = Y: AY = Z: GOSUB 710
  40. A = AA + (90 - LT) / DR
  41. Y = R * COS(A)
  42. Z = R * SIN(A)
  43. GOSUB 740
  44. PRINT : REM AZ & EL are now sun's azimuth & elevation in degrees
  45. IF EL < 0 THEN PRINT "Sun Below Horizon": END
  46. R = AZ: GOSUB 870: PRINT "Sun's azimuth: "; R; " degrees"
  47. R = EL: GOSUB 870: PRINT "Sun's elevation: "; R; " degrees"
  48. PRINT
  49. INPUT "Calculate heliostat mirror alignment (y/n)"; K$
  50. IF K$ = "N" OR K$ = "n" THEN END
  51. SX = X: SY = Y: SZ = Z
  52. PRINT
  53. INPUT "Azimuth of target direction (degrees)"; AZ
  54. INPUT "Elevation of target direction (degrees)"; EL
  55. GOSUB 800
  56. X = X + SX: Y = Y + SY: Z = Z + SZ: GOSUB 740
  57. PRINT : REM AZ & EL are now aim azimuth & elevation in degrees
  58. PRINT "Mirror aim direction (perpendicular to surface):"
  59. R = AZ: GOSUB 870: PRINT "Azimuth: "; R; " degrees"
  60. R = EL: GOSUB 870: PRINT "Elevation: "; R; " degrees"
  61. END
  62. IF AX = 0 THEN AA = SGN(AY) * PY / 2: RETURN
  63. AA = ATN(AY / AX): IF AX < 0 THEN AA = AA + PY
  64. RETURN
  65. AX = SQR(X * X + Y * Y): AY = Z: GOSUB 710
  66. EL = AA * DR
  67. AX = Y: AY = X: GOSUB 710
  68. AZ = AA * DR
  69. IF AZ < 180 THEN AZ = AZ + 180 ELSE AZ = AZ - 180
  70. RETURN
  71. E = EL / DR
  72. A = AZ / DR
  73. Z = SIN(E)
  74. C = 0 - COS(E): REM Won't work without "0" in Liberty Basic
  75. X = C * SIN(A)
  76. Y = C * COS(A)
  77. RETURN
  78. R = INT(10 * R + .5): IF R = 3600 THEN R = 0
  79. R = R / 10
  80. RETURN
--------------------------------------------------



Re: software (3.00 / 0) (#7)
by TomW on Thu Jun 5th, 2008 at 01:29:36 PM MST
(User Info)

Yeah, anyone who can translate this to a shell or perl script or other unix tool[s] that would allow me to locate the solar position based on my day/time and timezone would be cool beans. I can track horizon to horizon [treetops really] with a resolution of 515 steps. Roughly 3 degree steps.

Tom

PS

Speaking of solar, I just read and measured 56 inches multiple times then promptly laid it out as 59 while upgrading my trackers rack. Bolts are at least easy to change.

"Education consists mainly of what we have unlearned."--Mark Twain
[ Parent ]



Re: software (3.00 / 0) (#10)
by david williams on Sat Jun 7th, 2008 at 11:08:49 AM MST
(User Info)

If you, or anyone else, want to translate my Sunalign program into a perl script or anything else, I'd be happy to help however I can. I do undertand the calculation. I could do it with a pocket calculator if I had to. I coded it into BASIC because that's the only programming language I'm any good at. If anyone wants to re-code it into any other language (providing, of course, that it has provision for floating-point math and trig functions), and doesn't understand it from the BASIC, just contact me and I'll try to explain how it works. Contact me here or, probably better, send me an e-mail:

david.williams@bayman.org

Later...
                              dow


[ Parent ]



Re: software (3.00 / 0) (#11)
by DamonHD (d@hd.org) on Sat Jun 7th, 2008 at 11:20:33 AM MST
(User Info) http://www.earth.org.uk/

It's potentially the sort of thing that might be coded up as JavaScript on pages hosted here at FL/OP...

Easy free service which costs FL nothing (except in TomW et al handling newbie impotent rage when it doesn't give the desired answer...)

Rgds

Damon

[ Parent ]



Re: software (3.00 / 0) (#12)
by david williams on Tue Jun 10th, 2008 at 12:16:51 PM MST
(User Info)

Translating Sunalign to JavaScript sounds like a good idea. Making it so it doesn't work doesn't! There are already other programs out there that are claimed to do the same thing, but they aren't always reliable. Some, for example, don't work correctly for locations in the southern hemisphere. I am very confident that Sunalign always works correctly, with accuracy that is more than sufficient for solar-energy purposes. Let's keep it that way. Please don't post any version that doesn't work reliably.

                                dow


[ Parent ]



Re: software (3.00 / 0) (#13)
by DamonHD (d@hd.org) on Tue Jun 10th, 2008 at 02:28:12 PM MST
(User Info) http://www.earth.org.uk/

Your point is well made, though I was referring in part to the kind of rage incurred when the program gives the mathematically correct answer, but not the answer hoped for by the innumerate and physics-detesting user who wanted to stick it to the (power) man...

Rgds

Damon

[ Parent ]



Re: software (3.00 / 0) (#14)
by david williams on Wed Jun 11th, 2008 at 02:56:11 PM MST
(User Info)

In the middle of a Canadian winter, I'd love the sun to be high in the sky. I could easily write a program to tell me that this is so, but I don't think it would help much!

                            dow


[ Parent ]



Re: software (3.00 / 0) (#15)
by david williams on Fri Aug 15th, 2008 at 02:24:55 PM MST
(User Info)

I know of someone who has translated my BASIC Sunalign program into Perl. He says it seems to work fine, but he wants to give it a very thorough testing and polishing before releasing it into the big wide world.

When he feels it's ready, I'll ask him to post it here.


[ Parent ]



Re: software (3.00 / 0) (#16)
by bills on Wed Aug 20th, 2008 at 07:20:01 AM MST
(User Info)

Tom,

I have ported David Williams' SunAlign to a Perl program although I haven't completed the comprehensive testing on it yet. If you would like a copy drop me an email and I will send it on. Maybe you could try it and let me know what you think? I use gmail. [First initial][middle initial][last name]

William K Stewart (Bill)

[ Parent ]



software | 24 comments (24 topical, 0 editorial)

Menu
· create account
· How to use the board
· FAQs
· search the board
· Google search the board
· Old Otherpower Board

Login
Make a new account
Username:
Password:

Powered by Scoop
You must be a registered user to post here. It's easy and free, and the link is on the upper right side of your page.
All trademarks and copyrights on this page are owned by their respective companies. Postings are owned by the poster, but may be deleted or moved at the ADMIN's sole discretion. The Rest © 2003 Forcefield.
You can Email the board ADMIN here. PLEASE include the username you signed up with!