-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnocal.py
177 lines (136 loc) · 6.67 KB
/
nocal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import string
import RPi.GPIO as GPIO
import ephem
from datetime import datetime,date,timedelta
import time
import io
wormTime = float(5.73)
actTime = float(5.73)
#pin numbers for each gear
wormpin1 = 23
wormpin2 = 24
leftact1 = 8
leftact2 = 7
rightact1 = 9
rightact2 = 11
#making the panels the observer and the sun the object
panels= ephem.Observer()
sun = ephem._sun()
panels.lat='42.5'
panels.long='90'
#setting up the GPIO pins
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(wormpin1,GPIO.OUT)
GPIO.setup(wormpin2,GPIO.OUT)
GPIO.setup(leftact1,GPIO.OUT)
GPIO.setup(leftact2,GPIO.OUT)
GPIO.setup(rightact1,GPIO.OUT)
GPIO.setup(rightact2,GPIO.OUT)
GPIO.output(leftact1,False)
GPIO.output(leftact2,False)
GPIO.output(rightact1,False)
GPIO.output(rightact2,False)
GPIO.output(wormpin1,False)
GPIO.output(wormpin2,False)
#url = "test.ics"
data = str(datetime.now())
with open('solarlog.txt','a') as logfile:
logfile.write("\n the program was stated at:")
logfile.write(data)
logfile.write("\n")
#imports calendar file and creates an array for it to placed in
print "welcome to the best solar tracker ever made lol XD"
#loop that actually moves the panels
while True:
print "you are now in the main loop"
#parses the next event in the calendar and creates a unix time stamp
#setup for the localtime, risetime and settime that is performed eachday
panels.date = datetime.now()
localtime = datetime.now()
sun.compute()
riseTime = ephem.localtime(panels.previous_rising(sun))
setTime = ephem.localtime(panels.next_setting(sun))
print "today the sun will rise at {}".format(riseTime)
print "today the sun will set at {}".format(setTime)
print "tomorrow the sun will set at {}".format(tomorrowRise)
dayLength = int((setTime - datetime.now()).total_seconds())
delay = dayLength/77
today = date.today()
print "today is {} seconds long and the flywheel will spin every {} seconds".format(dayLength, delay)
print "riseTime: {}".format(riseTime)
print "setTime: {}".format(setTime)
print "current time {}:".format(datetime.now())
print "is the sun up:{}".format(datetime.now() >= riseTime and datetime.now() <=setTime)
#adds the turn time, date, and day length to the log file
with open('solarlog.txt','a') as logfile:
logfile.write("today is: ")
logfile.write(str(e[index].name))
logfile.write("\n")
logfile.write("the day the length is:")
logfile.write(str(dayLength))
logfile.write("\n")
logfile.write("the turn Length for today is:")
logfile.write(str(delay))
logfile.write("\n")
logfile.write("current sunsise: ")
logfile.write(str(riseTime))
logfile.write("\n")
logfile.write("current sunset: ")
logfile.write(str(setTime))
logfile.write("\n")
logfile.write("next sunrise: ")
logfile.write(str(tomorrowRise))
logfile.write("\n")
TurnNumber = 0
while riseTime <= datetime.now() and datetime.now() <= setTime:
print "you are in position {} of 77".format(turnNumber + 1)
if turnNumber <= 38:
print "it is the first half of the day"
GPIO.output(wormpin1,False)
GPIO.output(wormpin2,True)
GPIO.output(leftact1,True)
GPIO.output(leftact2,False)
GPIO.output(rightact1,True)
GPIO.output(rightact2,False)
else:
"it is the second half of the day"
GPIO.output(wormpin1,True)
GPIO.output(wormpin2,False)
GPIO.output(leftact1,True)
GPIO.output(leftact2,False)
GPIO.output(rightact1,True)
GPIO.output(rightact2,False)
print "now waiting {} seconds".format(wormTime)
time.sleep(wormTime)
GPIO.output(wormpin1,False)
GPIO.output(wormpin2,False)
GPIO.output(leftact1,False)
GPIO.output(leftact2,False)
GPIO.output(rightact1,False)
GPIO.output(rightact2,False)
print "now waiting {} seconds".format(delay)
turnNumber = turnNumber + 1
time.sleep(delay)
#move to sunrise fix
print "the sun has just set and the solar array is returning to the sunrise position"
for index in range(77):
GPIO.output(wormpin1,True)
GPIO.output(wormpin2,False)
GPIO.output(leftact1,True)
GPIO.output(leftact2,False)
GPIO.output(rightact1,True)
GPIO.output(rightact2,False)
time.sleep(wormTime)
print "the array is in postition {} of 77".format(index + 1)
#find the time between sunrises and go to sleep
sleepTime = int((tomorrowRise - setTime).total_seconds())
print "the array has been moved to the sunrise position and will go to sleep {} seconds".format(sleepTime)
print "the next sunrise will occur at {}".format(tomorrowRise)
with open('solarlog.txt','a') as logfile:
logfile.write("the array has been set back to the sunrise position \n")
logfile.write("the program will wait ")
logfile.write(str(sleepTime))
logfile.write(" seconds until the next sunrise \n")
time.sleep(sleepTime)
exit()