Étape 9 - Coder le tracking en "dur"
Le code mis à jour est le suivant
import time
import RPi.GPIO as GPIO
import gpiozero
import ephem
def forwardzero(wait):
led16=gpiozero.LED(16) #motor1
led23=gpiozero.LED(23) #motor1
led20=gpiozero.LED(20) #enable
led21=gpiozero.LED(21) #enable
led1=gpiozero.LED(1)
led7=gpiozero.LED(7)
try:
print("forwardzero")
#cas où le hbridge necessite un signal enable
led21.on()
led20.off()
#signal à zero sur interupteur 2
led1.off()
led7.off()
#mettre un signal sur l'interrupteur 1
led23.on()
led16.off()
#laisser le signal actif le temps de wait
for k in range(wait):
print(k)
time.sleep(1)
#eteindre le signal sur l'interrupteur
led23.off()
led21.off()
except KeyboardnInterrupt:
print("keyboard interrupt")
except Exception as err:
print(err)
finally:
print("zero")
#signal à zero sur interupteur 2
led1.off()
led7.off()
#signal à zero sur l'interrupteur 1
led16.off()
led23.off()
#signal à zero sur la fiche enable
led20.off()
led21.off()
def backwardzero(wait):
led1=gpiozero.LED(1)
led7=gpiozero.LED(7)
led16=gpiozero.LED(16) #motor1
led23=gpiozero.LED(23) #motor1
led20=gpiozero.LED(20) #enable
led21=gpiozero.LED(21) #enable
try:
print("backwardzero")
#signal zero sur interupteur 1
led16.on()
led23.off()
#cas où le hbrige necessite un signal sur enable
led20.off()
led21.on()
#signal sur interrupteur 2
led1.off()
led7.off()
#wait
for k in range(wait):
print(k)
time.sleep(1)
#signal off sur interrupteur
led16.off()
led21.off()
except KeyboardInterrupt:
print("keyboardinterrupt")
except Exception as err:
print(err)
finally:
print("zero")
#signal à zero sur interupteur 1
led16.off()
led23.off()
#signal à zero sur l'interupteur 2
led1.off()
led7.off()
#signal à zero sur la fiche enable
led20.off()
led21.off()
#pin16 gpio23
#pin36 gpio25
def forward(wait):
GPIO.setmode(GPIO.BCM)
GPIO.setup(16,GPIO.OUT) #motor1
GPIO.setup(23,GPIO.OUT) #motor1
GPIO.setup(20,GPIO.OUT) #enable
GPIO.setup(21,GPIO.OUT) #enable
GPIO.setup(1,GPIO.OUT) #motor2
GPIO.setup(7,GPIO.OUT) #motor2
try:
print("forward")
GPIO.output(1,GPIO.HIGH)
GPIO.output(7,GPIO.LOW)
GPIO.output(21,GPIO.HIGH)
GPIO.output(20,GPIO.LOW)
GPIO.output(23,GPIO.LOW)
GPIO.output(16,GPIO.LOW)
for k in range(wait):
print(k)
time.sleep(1)
GPIO.output(1,GPIO.LOW)
GPIO.output(21,GPIO.LOW)
except KeyboardInterrupt:
print("keyboard interrupt")
except Exception as err:
print(err)
finally:
print("zero")
GPIO.output(1,GPIO.LOW)
GPIO.output(7,GPIO.LOW)
GPIO.output(20,GPIO.LOW)
GPIO.output(21,GPIO.LOW)
GPIO.output(23,GPIO.LOW)
GPIO.output(16,GPIO.LOW)
#GPIO.cleanup() chez moi, ca n'enleve pas les +3V
def backward(wait):
GPIO.setmode(GPIO.BCM)
GPIO.setup(20,GPIO.OUT) #enable
GPIO.setup(21,GPIO.OUT) #enable
GPIO.setup(1,GPIO.OUT) #motor2
GPIO.setup(7,GPIO.OUT) #motor2
GPIO.setup(16,GPIO.OUT) #motor1
GPIO.setup(23,GPIO.OUT) #motor1
try:
print("backward")
GPIO.output(21,GPIO.HIGH)
GPIO.output(20,GPIO.LOW)
GPIO.output(16,GPIO.LOW)
GPIO.output(23,GPIO.LOW)
GPIO.output(7,GPIO.HIGH)
GPIO.output(1,GPIO.LOW)
for k in range(wait):
print(k)
time.sleep(1)
GPIO.output(7,GPIO.LOW)
GPIO.output(21,GPIO.HIGH)
except KeyboardInterrupt:
print("keyboardinterrupt")
except Exception as err:
print(err)
finally:
print("zero")
GPIO.output(20,GPIO.LOW)
GPIO.output(21,GPIO.LOW)
GPIO.output(16,GPIO.LOW)
GPIO.output(23,GPIO.LOW)
GPIO.output(1,GPIO.LOW)
GPIO.output(7,GPIO.LOW)
#GPIO.cleanup() chez moi ca n'enleve pas les +3V
forward(2) #rotation horaire motor2
backward(2) #rotation antihoraire motor2
forwardzero(10) #verin extension motor1 111 max
backwardzero(10) #verin retractation motor1 107 max
#Etalonnage
#dict_angle_verin= sun_degre_horizontal:motoractivationtime
dict_angle_verin={1:1,
2:2,
3:3,
4:4,
5:5,
6:6,
7:7,
8:8,
9:9,
10:10,
11:11,
12:12,
13:13,
14:14,
15:15,
16:16,
17:17,
18:18,
19:19,
20:20,
21:21,
22:22,
23:23,
24:24,
25:25,
26:26,
27:27,
28:28,
29:29,
30:30,
31:31,
32:32,
33:33,
34:34,
35:35,
36:36,
37:37,
38:38,
39:39,
40:40,
41:41,
42:42,
43:43,
44:44,
45:45,
46:46,
47:47,
48:48,
49:49,
50:50,
51:51,
52:52,
53:53,
54:54,
55:55,
56:56,
57:57,
58:58,
59:59,
60:60,
61:61,
62:62,
63:63,
64:64,
65:65,
66:66,
67:67,
68:68,
69:69,
70:70,
71:71,
72:72,
73:73,
74:74,
75:75,
76:76,
77:77,
78:78,
79:79,
80:80,
81:81,
82:82,
83:83,
84:84,
85:85,
86:86,
87:87,
88:88,
89:89,
90:90}
#dict_angle_rotation= sun_degre_azimut:motoractivationtime
dict_angle_rotation={1:0,
2:2,
3:3,
4:4,
5:5,
6:6,
7:7,
8:8,
9:9,
10:10,
11:11,
12:12,
13:13,
14:14,
15:15,
16:16,
17:17,
18:18,
19:19,
20:20,
21:21,
22:22,
23:23,
24:24,
25:25,
26:26,
27:27,
28:28,
29:29,
30:30,
31:31,
32:32,
33:33,
34:34,
35:35,
36:36,
37:37,
38:38,
39:39,
40:40,
41:41,
42:42,
43:43,
44:44,
45:45,
46:46,
47:47,
48:48,
49:49,
50:50,
51:51,
52:52,
53:53,
54:54,
55:55,
56:56,
57:57,
58:58,
59:59,
60:60,
61:61,
62:62,
63:63,
64:64,
65:65,
66:66,
67:67,
68:68,
69:69,
70:70,
71:71,
72:72,
73:73,
74:74,
75:75,
76:76,
77:77,
78:78,
79:79,
80:80,
81:81,
82:82,
83:83,
84:84,
85:85,
86:86,
87:87,
88:88,
89:89,
90:90
-1:0,
-2:2,
-3:3,
-4:4,
-5:5,
-6:6,
-7:7,
-8:8,
-9:9,
-10:10,
-11:11,
-12:12,
-13:13,
-14:14,
-15:15,
-16:16,
-17:17,
-18:18,
-19:19,
-20:20,
-21:21,
-22:22,
-23:23,
-24:24,
-25:25,
-26:26,
-27:27,
-28:28,
-29:29,
-30:30,
-31:31,
-32:32,
-33:33,
-34:34,
-35:35,
-36:36,
-37:37,
-38:38,
-39:39,
-40:40,
-41:41,
-42:42,
-43:43,
-44:44,
-45:45,
-46:46,
-47:47,
-48:48,
-49:49,
-50:50,
-51:51,
-52:52,
-53:53,
-54:54,
-55:55,
-56:56,
-57:57,
-58:58,
-59:59,
-60:60,
-61:61,
-62:62,
-63:63,
-64:64,
-65:65,
-66:66,
-67:67,
-68:68,
-69:69,
-70:70,
-71:71,
-72:72,
-73:73,
-74:74,
-75:75,
-76:76,
-77:77,
-78:78,
-79:79,
-80:80,
-81:81,
-82:82,
-83:83,
-84:84,
-85:85,
-86:86,
-87:87,
-88:88,
-89:89,
-90:90}
def sun_position(time_now,lat,lon):
now_here = ephem.Observer()
now_here.lat = lat
now_here.lon = lon
#PyEphem only processes and returns dates that are in Universal Time (UT), which is simliar to Standard Time in Greenwich, England, on the Earth's Prime Meridian
# Europe/Paris is GMT+2
#tester angles pyephem sur mesures réelles
utc_now=datetime.datetime.utcnow()
#is_dst=datetime.datetime(year=utc_now.year,month=utc_now.month,day=utc_now.day).dst()
#time_diff=datetime.timedelta(hours=(1 if not is_dst else 2))
now_here.date = time_now #+datetime.timedelta(hours=time_diff) #'2007/10/02 00:50:22'
sun.compute(now_here)
sun_degre_azimut=int(sun.az*180/3.141592653589793)
sun_degre_horizontal=int(sun.alt*180/3.141592653589793)
return(sun_degre_horizontal,sun_degre_azimut)
tracker_degré_horizontal=0
tracker_degré_azimut=0
def init():
global tracker_degré_horizontal
forwardzero(111)
tracker_degré_horizontal=0
def track(time_now,lat,lon):
global tracker_degré_horizontal
global tracker_degré_azimut
init()
(sun_degre_horizontal,sun_degre_azimut)=sun_position(time_now,lat,lon)
backwardzero(dict_angle_verin[sun_degre_horizontal])
tracker_degré_horizontal=sun_degre_horizontal
if sun_degre_azimut-tracker_degré_azimut<0:
backward(dict_angle_rotation[sun_degre_azimut-tracker_degré_azimut])
tracker_degré_azimut=sun_degre_azimut
else:
forward(dict_angle_rotation[sun_degre_azimut-tracker_degré_azimut])
tracker_degré_azimut=sun_degre_azimut
#test Agen
lat=44.2
lon=0.6
while True:
time.sleep(20*60)#activer le tracking toutes les 20 minutes
track(datetime.datetime.now(),lat,lon)
#Etalonnage
#dict_angle_verin= sun_degre_horizontal:motoractivationtime
dict_angle_verin={1:0,
2:0,
3:0,
4:0,
5:0,
6:0,
7:0,
8:0,
9:0,
10:0,
11:1,
12:1,
13:2,
14:3,
15:4,
16:5,
17:6,
18:7,
19:8,
20:9,
21:10,
22:11,
23:12,
24:13,
25:14,
26:15,
27:16,
28:16,
29:17,
30:18,
31:19,
32:20,
33:21,
34:22,
35:23,
36:24,
37:25,
38:27,
39:28,
40:29,
41:30,
42:32,
43:33,
44:34,
45:35,
46:37,
47:38,
48:39,
49:41,
50:42,
51:44,
52:45,
53:47,
54:48,
55:50,
56:51,
57:52,
58:54,
59:56,
60:58,
61:59,
62:59,
63:59,
64:60,
65:63,
66:64,
67:65,
68:66,
69:68,
70:68,
71:71,
72:73,
73:75,
74:77,
75:79,
76:81,
77:82,
78:83,
79:85,
80:86,
81:87,
82:89,
83:91,
84:94,
85:96,
86:98,
87:100}
#dict_angle_rotation= sun_degre_azimut:motoractivationtime
dict_angle_rotation={1:0,
2:2,
3:3,
4:4,
5:5,
6:6,
7:7,
8:8,
9:9,
10:10,
11:11,
12:12,
13:13,
14:14,
15:15,
16:16,
17:17,
18:18,
19:19,
20:20,
21:21,
22:22,
23:23,
24:24,
25:25,
26:26,
27:27,
28:28,
29:29,
30:30,
31:31,
32:32,
33:33,
34:34,
35:35,
36:36,
37:37,
38:38,
39:39,
40:40,
41:41,
42:42,
43:43,
44:44,
45:45,
46:46,
47:47,
48:48,
49:49,
50:50,
51:51,
52:52,
53:53,
54:54,
55:55,
56:56,
57:57,
58:58,
59:59,
60:60,
61:61,
62:62,
63:63,
64:64,
65:65,
66:66,
67:67,
68:68,
69:69,
70:70,
71:71,
72:72,
73:73,
74:74,
75:75,
76:76,
77:77,
78:78,
79:79,
80:80,
81:81,
82:82,
83:83,
84:84,
85:85,
86:86,
87:87,
88:88,
89:89,
90:90
-1:0,
-2:2,
-3:3,
-4:4,
-5:5,
-6:6,
-7:7,
-8:8,
-9:9,
-10:10,
-11:11,
-12:12,
-13:13,
-14:14,
-15:15,
-16:16,
-17:17,
-18:18,
-19:19,
-20:20,
-21:21,
-22:22,
-23:23,
-24:24,
-25:25,
-26:26,
-27:27,
-28:28,
-29:29,
-30:30,
-31:31,
-32:32,
-33:33,
-34:34,
-35:35,
-36:36,
-37:37,
-38:38,
-39:39,
-40:40,
-41:41,
-42:42,
-43:43,
-44:44,
-45:45,
-46:46,
-47:47,
-48:48,
-49:49,
-50:50,
-51:51,
-52:52,
-53:53,
-54:54,
-55:55,
-56:56,
-57:57,
-58:58,
-59:59,
-60:60,
-61:61,
-62:62,
-63:63,
-64:64,
-65:65,
-66:66,
-67:67,
-68:68,
-69:69,
-70:70,
-71:71,
-72:72,
-73:73,
-74:74,
-75:75,
-76:76,
-77:77,
-78:78,
-79:79,
-80:80,
-81:81,
-82:82,
-83:83,
-84:84,
-85:85,
-86:86,
-87:87,
-88:88,
-89:89,
-90:90}
def sun_position(time_now,lat,lon):
now_here = ephem.Observer()
now_here.lat = lat
now_here.lon = lon
# PyEphem only processes and returns dates that are in Universal Time (UT), which is simliar to Standard Time in Greenwich, England, on the Earth's Prime Meridian
# Europe/Paris is GMT+2
# hypothesis system clock is already automatically synced with daytime saving time
utc_now=datetime.datetime.utcnow()
now_here.date = utc_now
sun.compute(now_here)
sun_degre_azimut=int(sun.az*180/3.141592653589793)
sun_degre_horizontal=int(sun.alt*180/3.141592653589793)
return(sun_degre_horizontal,sun_degre_azimut)
tracker_degré_horizontal=0
tracker_degré_azimut=0
def init():
global tracker_degré_horizontal
forwardzero(111)
tracker_degré_horizontal=0
def track(time_now,lat,lon):
global tracker_degré_horizontal
global tracker_degré_azimut
init()
(sun_degre_horizontal,sun_degre_azimut)=sun_position(time_now,lat,lon)
sun_degre_azimut_motor_referential=-(180-sun_degre_azimut) # pyephem azimut 0 is north. we assume in motor referential 0° is south and -1° is directed to the east.
backwardzero(dict_angle_verin[sun_degre_horizontal])
tracker_degré_horizontal=sun_degre_horizontal
if sun_degre_azimut_motor_referential-tracker_degré_azimut<0:
backward(dict_angle_rotation[sun_degre_azimut_motor_referential-tracker_degré_azimut])
tracker_degré_azimut=sun_degre_azimut
else:
forward(dict_angle_rotation[sun_degre_azimut_motor_referential-tracker_degré_azimut])
tracker_degré_azimut=sun_degre_azimut
#test Agen
lat=44.2
lon=0.6
while True:
time.sleep(20*60)#activer le tracking toutes les 20 minutes
track(datetime.datetime.now(),lat,lon)