Advertisement
CahyoF

advanced solar tracking system

Dec 10th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 36.55 KB | None | 0 0
  1. # import library yang dibutuhkan
  2. from picamera.array import PiRGBArray
  3. from picamera import PiCamera
  4. from signal import pause
  5. from subprocess import check_call
  6. from datetime import date, timedelta
  7. import time
  8. import cv2
  9. import sys
  10. import RPi.GPIO as GPIO
  11. import numpy as np
  12. import astral
  13. import datetime
  14. import os
  15. import math
  16.  
  17. # membuat variabel variabel penting
  18. global cerah, luas, minimum, cerahi, luasi
  19. lock_x                  = False
  20. lock_y                  = True
  21. lockknn                 = False
  22. lockkri                 = False
  23. invkri                  = True
  24. invknn                  = True
  25. jamlog                  = float(time.strftime("%H.%M"))
  26. rad2                    = 7
  27. rad3                    = 7
  28. hitungcari              = 0
  29. ctxskg                  = int(160)
  30. start_time              = time.time()
  31. wait                    = time.time()
  32. waitx                   = time.time()
  33. wait3                   = time.time()
  34. waitclean               = time.time()
  35. scanning                = False
  36. step1                   = True
  37. step2                   = False
  38. step2b                  = False
  39. step2c                  = False
  40. step3                   = False
  41. font                    = cv2.FONT_HERSHEY_SIMPLEX
  42. bottomLeftCornerOfText  = (5,235)
  43. atasnya                 = (5,220)
  44. atasnyalagi             = (5,205)
  45. polatas                 = (5,20)
  46. fontScale               = 0.4
  47. fontColor               = (255,175,220)
  48. lineType                = 1
  49. teksstring2             = str("bootup")
  50. centre_x                = 160
  51. centre_y                = 120
  52. ignore                  = False
  53. cerahinv                = False
  54.  
  55. #memulai kamera
  56. camera                  = PiCamera()
  57. camera.resolution       = (320, 240)
  58. camera.iso              = 0
  59. camera.exposure_mode    = 'auto'
  60. camera.framerate        = 60
  61. rawCapture              = PiRGBArray(camera, size=(320, 240))
  62. time.sleep(0.1) #'pemanasan' kamera
  63.  
  64. #set lokasi ke semarang & perhitungan matahari
  65. semarang                    = astral.Location(
  66.                                 info=("Semarang",
  67.                                       "Indonesia",
  68.                                       -6.966667, 110.416664,
  69.                                       "Asia/Jakarta",
  70.                                       40)
  71.                                 )
  72. semarang.solar_depression   = "civil"
  73. matahari                    = semarang.sun()
  74.  
  75. #menggunakan BCM sebagai referensi GPIO
  76. GPIO.setmode(GPIO.BCM)
  77.  
  78. #mengatur GPIO untuk keluaran
  79. Mot1F   =24
  80. Mot1R   =25
  81. Mot2F   =23
  82. Mot2R   =18
  83. kipas   =27
  84.  
  85. GPIO.setup(Mot1F, GPIO.OUT)
  86. GPIO.setup(Mot1R, GPIO.OUT)
  87. GPIO.setup(Mot2F, GPIO.OUT)
  88. GPIO.setup(Mot2R, GPIO.OUT)
  89. GPIO.setup(kipas, GPIO.OUT)
  90.  
  91. #mengatur GPIO masukan
  92. GPIO.setup(16,  GPIO.IN, pull_up_down=GPIO.PUD_UP)
  93. GPIO.setup(12,  GPIO.IN, pull_up_down=GPIO.PUD_UP)
  94. GPIO.setup(7,   GPIO.IN, pull_up_down=GPIO.PUD_UP)
  95. GPIO.setup(8,   GPIO.IN, pull_up_down=GPIO.PUD_UP)
  96. GPIO.setup(17,  GPIO.IN, pull_up_down=GPIO.PUD_UP)
  97.  
  98.  
  99. #membuat fungsi-fungsi yang diperlukan
  100.  
  101. #fungsi motor dengan pwm software menggunakan frekuensi 60Hz
  102. def motor1forward(dur, cycle):
  103.     print("forwarding motor 1")
  104.     pm1f = GPIO.PWM(Mot1F, 60)
  105.     pm1f.start(cycle)
  106.     time.sleep(dur)
  107.     pm1f.stop
  108.     del dur, cycle, pm1f
  109.  
  110. def motor1reverse(dur, cycle):
  111.     print("backwarding motor 1")
  112.     pm1r = GPIO.PWM(Mot1R, 60)
  113.     pm1r.start(cycle)
  114.     time.sleep(dur)
  115.     pm1r.stop
  116.     del dur, cycle, pm1r
  117.  
  118. def motorsafe(): # fungsi untuk mengamankan motor
  119.     GPIO.output(Mot1F, GPIO.LOW)
  120.     time.sleep(0.001)
  121.     GPIO.output(Mot1R, GPIO.LOW)
  122.     time.sleep(0.001)
  123.     GPIO.output(Mot2F, GPIO.LOW)
  124.     time.sleep(0.001)
  125.     GPIO.output(Mot2R, GPIO.LOW)
  126.     time.sleep(0.001)
  127.  
  128. def CPUtemp(): #membaca suhu CPU
  129.     res = os.popen('vcgencmd measure_temp').readline()
  130.     return(res.replace("temp=","").replace("'C\n",""))
  131.  
  132. #mengambil gambar dari kamera ke vairabel 'frame' dalam bentuk array
  133. for frame in camera.capture_continuous(rawCapture,
  134.                                        format="bgr",
  135.                                        use_video_port=False):
  136.     #inisialisasi limit switch
  137.     Lim1        =GPIO.input(16) #bawah
  138.     Lim2        =GPIO.input(12) #atas
  139.     Lim3        =GPIO.input(8) #kiri
  140.     Lim4        =GPIO.input(7) #kanan
  141.     tomboloff   =GPIO.input(17)
  142.  
  143.     if tomboloff == False:
  144.         mulaipencet     = time.time()
  145.         time.sleep(0.05)
  146.         lps             = None
  147.         lps             = GPIO.wait_for_edge(17, GPIO.RISING)
  148.         if lps is not None:
  149.             time.sleep(0.01)
  150.             global waktupencet
  151.             waktupencet = time.time() - mulaipencet
  152.         if waktupencet > 9 :
  153.             GPIO.output(kipas, GPIO.LOW)                   
  154.             cv2.destroyAllWindows()
  155.             for i in range (1,5):
  156.                     cv2.waitKey(1)
  157.             del camera
  158.             check_call(['/sbin/poweroff'])
  159.         if 10 > waktupencet > 4 :
  160.             GPIO.output(kipas, GPIO.LOW)                   
  161.             cv2.destroyAllWindows()
  162.             for i in range (1,5):
  163.                     cv2.waitKey(1)
  164.             del camera
  165.             check_call(['/sbin/reboot'])
  166.         if 5 > waktupencet > 1 :
  167.             print("memulai pemicu runtutan pemindaian")
  168.             teksstring2 = str("memulai runtutan pemindaian")
  169.             scanning    = True
  170.             mulaiscan   = True
  171.  
  172.     rise    = str( matahari['sunrise'].time())
  173.     noon    = str( matahari['noon'].time())
  174.     sunset  = str( matahari['sunset'].time())
  175.  
  176. #memuat waktu terbit, siang & tenggelam matahari dalam variabel
  177.     jmtbt,      mntbt,      dttbt   = rise.split(':')
  178.     jmsiang,    mnsiang,    dtsiang = noon.split(':')
  179.     jmtgm,      mntgm,      dttgm   = sunset.split(':')
  180.  
  181.     terbit      = float(jmtbt + "." + mntbt)
  182.     siang       = float(jmsiang+"."+mnsiang)
  183.     tenggelam   = float( jmtgm + "." + mntgm)
  184.  
  185.     jamskrg     = float(time.strftime("%H.%M"))
  186.  
  187.     suhu        = float(CPUtemp())
  188.  
  189.     puncakonmin = ((int(jmsiang) * 60) + int(mnsiang))
  190.     mulailock   = puncakonmin - 15
  191.     selesailock = puncakonmin + 15
  192.     jamonminute = ((int(time.strftime("%H")) * 60) + int(time.strftime("%M")))
  193.     print(mulailock, puncakonmin, selesailock, jamonminute)
  194.    
  195.     #menyalakan dan mematikan kipas
  196.     if suhu > 60:
  197.         GPIO.output(kipas, GPIO.HIGH)
  198.     if suhu < 45:
  199.         GPIO.output(kipas, GPIO.LOW)
  200.  
  201.     #mengubah tangkapan gambar jadi grayscale dan mencari titik paling terang
  202.     gray = cv2.cvtColor(frame.array, cv2.COLOR_BGR2GRAY)
  203.     gray = cv2.GaussianBlur(gray, (rad3,rad3), 0)
  204.     (minVal, maxVal, minLoc, maxLoc)=cv2.minMaxLoc(gray)
  205.    
  206.     image = frame.array
  207.  
  208.     teksstring1 = str("min:"+str(minVal)+
  209.                       " |max:"+str(maxVal)+
  210.                       " |%.2f" %jamskrg+
  211.                       " |"+"%.1f" %suhu+"'C"+
  212.                       " |R:"+str(rad2)+"/"+str(rad3))
  213.     teksstring3 = str(str(maxLoc))
  214.     teksstring4 = str("terbit:"+"%.2f" %terbit+
  215.                       " | puncak:"+"%.2f" %siang+
  216.                       " | tenggelam:"+"%.2f" %tenggelam)
  217.  
  218.     #menandai titik paling terang
  219.     cv2.circle(image, maxLoc, rad2, (0, 250, 150), 1)
  220.     cv2.circle(image, maxLoc, 3, (50, 200, 50), 1)
  221.  
  222.     #menambahkan teks informasi
  223.     cv2.putText(image,teksstring1, bottomLeftCornerOfText,
  224.                 font, fontScale, fontColor, lineType)
  225.     cv2.putText(image,teksstring2, atasnya,
  226.                 font, fontScale, fontColor, lineType)
  227.     cv2.putText(image,teksstring3, atasnyalagi,
  228.                 font, fontScale, fontColor, lineType)
  229.     cv2.putText(image,teksstring4, polatas,
  230.                 font, fontScale, fontColor, lineType)
  231.    
  232.     #menampilkan tangkapan kamera
  233.     cv2.imshow("Bismillah", image)
  234.  
  235.     #auto radius, menyesuaikan deviasi gaussian blur & radius deteksi
  236.     retval, thresholded         = cv2.threshold(gray, ((80/100)*maxVal),
  237.                                                 maxVal, cv2.THRESH_BINARY)
  238.            
  239.     im2, contours, hierarchy    = cv2.findContours(thresholded,
  240.                                                    cv2.RETR_LIST,
  241.                                                    cv2.CHAIN_APPROX_SIMPLE)
  242.    
  243.     for idx, contour in enumerate(contours):
  244.         global area
  245.         area = cv2.contourArea(contour)
  246.     if maxVal < 10 :
  247.         rad2 = 5
  248.     if ((time.time()-start_time)>1) and (maxVal != 0):
  249.         start_time  =time.time()
  250.         rad1        =math.sqrt(area/(math.pi))
  251.         rad2        =int(round((125/100)*rad1))
  252.         if rad2 < 7 :
  253.             rad2 = 7
  254.            
  255.     if rad2 %2 == 0:
  256.         rad3 = rad2 -1
  257.     if rad2 %2 != 0:
  258.         rad3 = rad2
  259.        
  260.     # mencegah false detection karena nois
  261.     if rad3 < 71:
  262.         minimum = 33
  263.     if rad3 > 70:
  264.         minimum = 5
  265.  
  266.     pa = int(round(120-(5)))
  267.     pb = int(round(120+(5)))
  268.  
  269.     #menampilkan informasi
  270.     print ("lok:", maxLoc, "| min:", int(minVal), "| max:", int(maxVal), "| waktu sekarang:", "%.2f" %jamskrg, "| suhu:", "%.2f" %suhu, "°C")
  271.     print ("terbit:", "%.2f" %terbit, "| puncak:", "%.2f" %siang, "| tenggelam:", "%.2f" %tenggelam, "| radius :", rad2, "/", rad3)
  272.        
  273.     centre_x1, centre_y1 = (int(x) for x in maxLoc)
  274.     centre_x            = int(round((centre_x + centre_x1)/2))
  275.     centre_y            = int(round((centre_y + centre_y1)/2))
  276.  
  277.     if (pa<centre_y<pb) & (155<centre_x<165):
  278.         teksstring2 = str("sudah tengah")
  279.     if maxVal<minimum :
  280.         teksstring2 = str("intensitas tidak cukup")
  281.  
  282.     if ((jamskrg)<(6.0)):
  283.         tglskrg = str(time.strftime("%Y-%m-%d"))
  284.         namalog = str("log"+tglskrg+"log1")
  285.     if ((5.59)<(jamskrg)<(12)):
  286.         tglskrg = str(time.strftime("%Y-%m-%d"))
  287.         namalog = str("log"+tglskrg+"log2")
  288.     if ((11.59)<(jamskrg)<(18)):
  289.         tglskrg = str(time.strftime("%Y-%m-%d"))
  290.         namalog = str("log"+tglskrg+"log3")
  291.     if ((17.59)<(jamskrg)<(24)):
  292.         tglskrg = str(time.strftime("%Y-%m-%d"))
  293.         namalog = str("log"+tglskrg+"log4")
  294.  
  295.     if jamlog != jamskrg:
  296.         jamlog = jamskrg
  297.         teks1 = str("lok:"+str(maxLoc)+" | min:"+str(minVal)+" | max:"+str(maxVal)+" | waktu sekarang:"+"%.2f" %jamskrg+"| suhu:"+"%.2f" %suhu+ "°C")
  298.         teks2 = str("terbit:"+"%.2f" %terbit+" | puncak:"+"%.2f" %siang+" | tenggelam:"+"%.2f" %tenggelam)
  299.         if (pa<centre_y<pb) & (155<centre_x<165):
  300.             teks1 = str("lok:"+str(maxLoc)+" sudah tengah | min:"+str(minVal)+" | max:"+str(maxVal)+" | waktu sekarang:"+"%.2f" %jamskrg+"| suhu:"+"%.2f" %suhu+"°C")
  301.         if maxVal<minimum :
  302.             teks1 = str("intensitas tidak cukup | min:"+str(minVal)+" | max:"+str(maxVal)+" | waktu sekarang:"+"%.2f" %jamskrg+"| suhu:"+"%.2f" %suhu+"°C")
  303.         if (jamskrg<(terbit + 1.3)) or (jamskrg>(tenggelam-0.30)) :
  304.             teks1 = str("sudah malam, kembali ke timur. sekarang jam"+"%.2f" %jamskrg+"| suhu:"+"%.2f" %suhu+"°C")
  305.         if (invknn == True):
  306.             if (invkri == True):
  307.                 teks3 = str("kanan invers, kiri invers")
  308.             if (invkri == False):
  309.                 teks3 = str("kanan invers, kiri biasa")
  310.         if (invknn == False):
  311.             if (invkri == True):
  312.                 teks3 = str("kanan biasa, kiri invers")
  313.             if (invkri == False):
  314.                 teks3 = str("kanan biasa, kiri biasa")
  315.         logging = open(namalog+".txt", 'a')
  316.         logging.write(teks1+"||"+teks2+"||"+teks3+"\n")
  317.         logging.write("---------------------------------\n")
  318.         logging.close()
  319.         del namalog
  320.         del tglskrg
  321.         print("log diperbarui")
  322.  
  323.     if scanning == True:
  324.         teksstring2 = str("scanning")
  325.         if mulaiscan == True:
  326.             motorsafe()
  327.         #kembali ke nol
  328.         if step1 == True:
  329.             mulaiscan = False
  330.             print("memulai runtutan scanning")
  331.             teksstring2 = str("scanning - 1.kembali ke 0")
  332.             if (Lim4 == True):
  333.                 print("step 1.a - kembali ke nol azimut")
  334.                 motor1forward(.250, 40)
  335.             if (Lim4 == False):
  336.                 if (Lim2 == True):
  337.                     print("step 1.b - kembali ke nol zenit")
  338.                     GPIO.output(Mot2F, GPIO.HIGH)
  339.                     time.sleep(.025)
  340.                    
  341.                 if (Lim2 == False):
  342.                     print("step 1 finish")
  343.                     GPIO.output(Mot2F, GPIO.LOW)
  344.                     teksstring2 = str("scanning - 1. selesai")
  345.                     step1   = False
  346.                     step2   = True
  347.                     cerah   = 0
  348.                     luas    = 0
  349.                     cerahi  = 0
  350.                     luasi   = 0
  351.                    
  352.         if step2 == True:
  353.             print("memulai mencari")
  354.             teksstring2 = str("scanning - 2. mencari")
  355.             if maxVal > cerah:
  356.                 cerah   = maxVal
  357.                 luas    = area
  358.                 print("ditemukan titik lebih cerah,", cerah)
  359.                 teksstring2 = str("scanning - 2. lebih cerah, "+str(cerah))
  360.             if cerah == maxVal:
  361.                 if luas < area:
  362.                     luas = area
  363.                     print("ditemukan titik area luas,", luas)
  364.                     teksstring2 = str("scanning - 2. lebih luas, "+str(luas))
  365.             if (Lim3 == True):
  366.                 print("step 2 memulai pemindaian")
  367.                 motor1reverse(.250, 40)
  368.             if (Lim3 == False):
  369.                 print("step 2 selesai")
  370.                 step2   = False
  371.                 step2b  = True
  372.  
  373.         if step2b == True:
  374.             if (Lim1 == True):
  375.                 print("step 2.b - scan reverse")
  376.                 teksstring2 = str("scanning - 2. mencari")
  377.                 GPIO.output(Mot2R, GPIO.HIGH)
  378.                 time.sleep(.025)
  379.                
  380.             if (Lim1 == False):
  381.                 print("step 2.b finish")
  382.                 GPIO.output(Mot2R, GPIO.LOW)
  383.                 teksstring2 = str("scanning - 1. selesai")
  384.                 step2b   = False
  385.                 step2c   = True
  386.                
  387.         if step2c == True:
  388.             if maxVal > cerahi:
  389.                 cerahi   = maxVal
  390.                 luasi    = area
  391.                 print("ditemukan titik lebih cerah,", cerah)
  392.                 teksstring2 = str("scanning - 2. lebih cerah, "+str(cerah))
  393.             if cerahi == maxVal:
  394.                 if luasi < area:
  395.                     luasi = area
  396.                     print("ditemukan titik area luas,", luas)
  397.                     teksstring2 = str("scanning - 2. lebih luas, "+str(luas))
  398.             if (Lim4 == True):
  399.                 print("step 2 memulai pemindaian")
  400.                 motor1forward(.250, 40)
  401.             if (Lim4 == False):
  402.                 print("step 2 selesai")
  403.                 hasilscan       = cerah * luas
  404.                 hasilscaninv    = cerahi * luasi
  405.                 if hasilscan > hasilscaninv or hasilscan == hasilscaninv:
  406.                     scaninv = False
  407.                     if cerah > (minimum -1):
  408.                         step2c = False
  409.                         step3  = True
  410.                     if cerah < minimum:
  411.                         print("tidak ada cahaya ditemukan atau cahaya kurang dari minimum")
  412.                         teksstring2 = str("scanning - gagal, cahaya kurang dari minimum")
  413.                         step2c      = False
  414.                         scanning    = False
  415.                         step3       = False
  416.                         step1       = True
  417.                         lock_x      = False
  418.                         lock_y      = True
  419.                         if cerahi > (minimun -1):
  420.                             step2c  = False
  421.                             step3   = True
  422.                             scaninv = True
  423.  
  424.                 if hasilscaninv > hasilscan :
  425.                     scaninv = True
  426.                     if cerahi > (minimum -1):
  427.                         step2c = False
  428.                         step3  = True
  429.                     if cerahi < minimum:
  430.                         print("tidak ada cahaya ditemukan atau cahaya kurang dari minimum")
  431.                         teksstring2 = str("scanning - gagal, cahaya kurang dari minimum")
  432.                         step2c      = False
  433.                         scanning    = False
  434.                         step3       = False
  435.                         step1       = True
  436.                         lock_x      = False
  437.                         lock_y      = True
  438.                         if cerah > (minimum -1):
  439.                             step2c  = False
  440.                             step3   = True
  441.                             scaninv = False
  442.                    
  443.         if step3 == True:
  444.             if cerahinv == True :
  445.                 print("menuju ke titik paling terang,")
  446.                 teksstring2 = str("scanning - 3. menuju titik terang")
  447.                 areabawah = luasi*(77/100)
  448.                 cerahbawah = cerahi*(77/100)
  449.                 if (Lim3 == True):
  450.                     motor1reverse(.125, 40)
  451.                 if (Lim3 == False):
  452.                     print("pemindaian gagal, memindai ulang")
  453.                     teksstring2 = str("scanning - 3. gagal, mengulangi dari awal")
  454.                     scanning    = True
  455.                     step3       = False
  456.                     step1       = True
  457.                     lock_x      = False
  458.                     lock_y      = True
  459.                
  460.                 if maxVal > cerahbawah:
  461.                     if area > areabawah:
  462.                         print("pemindaian selesai")
  463.                         teksstring2 = str("scanning - selesai")
  464.                         scanning    = False
  465.                         step3       = False
  466.                         step1       = True
  467.                         lock_x      = False
  468.                         lock_y      = True
  469.             if cerahinv == False :
  470.                 print("menuju ke titik paling terang,")
  471.                 if (Lim2 == True):
  472.                     print("step 1.b - kembali ke nol zenit")
  473.                     GPIO.output(Mot2F, GPIO.HIGH)
  474.                     time.sleep(.025)                    
  475.                 if (Lim2 == False):
  476.                     GPIO.output(Mot2F, GPIO.LOW)
  477.                     teksstring2 = str("scanning - 3. menuju titik terang")
  478.                     areabawah = luas*(77/100)
  479.                     cerahbawah = cerah*(77/100)
  480.                     if (Lim3 == True):
  481.                         motor1reverse(.125, 40)
  482.                     if (Lim3 == False):
  483.                         print("pemindaian gagal, memindai ulang")
  484.                         teksstring2 = str("scanning - 3. gagal, mengulangi dari awal")
  485.                         scanning    = True
  486.                         step3       = False
  487.                         step1       = True
  488.                         lock_x      = False
  489.                         lock_y      = True
  490.                    
  491.                     if maxVal > cerahbawah:
  492.                         if area > areabawah:
  493.                             print("pemindaian selesai")
  494.                             teksstring2 = str("scanning - selesai")
  495.                             scanning    = False
  496.                             step3       = False
  497.                             step1       = True
  498.                             lock_x      = False
  499.                             lock_y      = True
  500.        
  501.     if (ignore == False) and (scanning == False):
  502.         teksstring2 = str("mode normal - siap")
  503.         if (jamskrg<(terbit + 2)) or (jamskrg>(tenggelam -0.15 )) :
  504.             cv2.destroyAllWindows()
  505.             for i in range (1,16):
  506.                 cv2.waitKey(1)
  507.             #kembali ke timur bila belum di timur
  508.             if (Lim4 == True):
  509.                 print("sudah malam, kembali ke timur", "| suhu:", "%.2f" %suhu, "°C")
  510.                 motor1forward(.125, 39)
  511.  
  512.             #bila sudah di timur, mode siaga sampai pukul 5.00
  513.             if (Lim4 == False):
  514.                 if (Lim2 == True):
  515.                     print("sudah malam, kembali ke timur", "| suhu:", "%.2f" %suhu, "°C")
  516.                     GPIO.output(Mot2F, GPIO.HIGH)
  517.                     time.sleep(0.1)
  518.                 if (Lim2 == False):                    
  519.                     GPIO.output(Mot2F, GPIO.LOW)
  520.                     print("sudah malam, istirahat", "| suhu:", "%.2f" %suhu, "°C")
  521.                     lock_x = True
  522.                     lock_y = False
  523.                     GPIO.output(kipas, GPIO.LOW)
  524.                     tidur = float(time.strftime("%H.%M"))    
  525.                     while (jamskrg<(terbit + 2)) or (jamskrg>(tenggelam -0.3)) :
  526.                         tomboloff=GPIO.input(17)
  527.                         if tomboloff == False:
  528.                             mulaipencet = time.time()
  529.                             time.sleep(0.1)
  530.                             lps = None
  531.                             lps = GPIO.wait_for_edge(17, GPIO.RISING)
  532.                             if lps is not None:
  533.                                 time.sleep(0.01)
  534.                                 waktupencet = time.time() - mulaipencet
  535.                             if waktupencet > 9 :
  536.                                 GPIO.output(kipas, GPIO.LOW)                   
  537.                                 cv2.destroyAllWindows()
  538.                                 for i in range (1,5):
  539.                                         cv2.waitKey(1)
  540.                                 del camera
  541.                                 check_call(['/sbin/poweroff'])
  542.                             if 10 > waktupencet > 4 :
  543.                                 GPIO.output(kipas, GPIO.LOW)                   
  544.                                 cv2.destroyAllWindows()
  545.                                 for i in range (1,5):
  546.                                         cv2.waitKey(1)
  547.                                 del camera
  548.                                 check_call(['/sbin/reboot'])
  549.                             if 5 > waktupencet > 1 :
  550.                                 print("tidak dapat memulai pemindaian, belum terbit")
  551.                         rise                = str( matahari['sunrise'].time())
  552.                         sunset              = str( matahari['sunset'].time())
  553.                         jmtgm, mntgm, dttgm = sunset.split(':')
  554.                         jmtbt, mntbt, dttbt = rise.split(':')
  555.                         terbit              = float(jmtbt + "." + mntbt)
  556.                         kerja               = terbit + 2
  557.                         tenggelam           = float( jmtgm + "." + mntgm )
  558.                         skrg                = float(time.strftime("%H.%M"))
  559.                         suhu                = float(CPUtemp())
  560.                         if skrg < 12 :
  561.                             sisa = terbit - skrg
  562.                             sker = kerja - skrg
  563.                         if skrg > 12 :
  564.                             sker = kerja + (24 - skrg)
  565.                             sisa = terbit + (24 - skrg)
  566.                         if ((skrg) < (6.0)):
  567.                             tglskrg = str(time.strftime("%Y-%m-%d"))
  568.                             namalog = str("log"+tglskrg+"log1")
  569.                         if ((5.59) < (skrg) < (12)):
  570.                             tglskrg = str(time.strftime("%Y-%m-%d"))
  571.                             namalog = str("log"+tglskrg+"log2")
  572.                         if ((11.59) < (skrg) < (18)):
  573.                             tglskrg = str(time.strftime("%Y-%m-%d"))
  574.                             namalog = str("log"+tglskrg+"log3")
  575.                         if ((17.59) < (skrg) < (24)):
  576.                             tglskrg = str(time.strftime("%Y-%m-%d"))
  577.                             namalog = str("log"+tglskrg+"log4")
  578.                         if tidur != skrg :
  579.                             tidur = skrg
  580.                             print("sudah malam, istirahat", "%.2f" %skrg, "| suhu:", "%.2f" %suhu, "°C")
  581.                             if sisa > 0:
  582.                                 print("tinggal",
  583.                                       "%.2f" %sisa, "lagi terbit | terbit: ",
  584.                                       "%.2f" %terbit,
  585.                                       "| %.2f" %sker, "lagi kerja" )
  586.                                 teks2   = str("tinggal"+"%.2f" %sisa+"lagi terbit"
  587.                                               +"| %.2f" %sker+
  588.                                               " lagi mulai | terbit: %.2f" %terbit)
  589.                             if sisa < 0:
  590.                                 print("terbit: ",
  591.                                       "%.2f" %terbit,
  592.                                       "| %.2f" %sker, "lagi kerja" )
  593.                                 teks2   = str("%.2f" %sker+" lagi mulai | terbit: %.2f" %terbit)
  594.                             teks1   = str("sudah malam, istirahat. ini jam "+"%.2f" %skrg+
  595.                                           "| suhu:"+"%.2f" %suhu+"°C")
  596.                            
  597.                             logging = open(namalog+".txt", 'a')
  598.                             logging.write(teks1+" || "+teks2+"\n")
  599.                             logging.write("--------------------\n")
  600.                             logging.close()
  601.                             del namalog
  602.                             del tglskrg
  603.                             print("log diperbarui")
  604.                         time.sleep(5)
  605.                         if skrg>(terbit + 2) and skrg<(tenggelam-0.3) :
  606.                             break
  607.  
  608.         if (jamskrg > (terbit + 2)) or (jamskrg<(tenggelam-0.15)):
  609.            
  610.             if(mulailock < jamonminute < selesailock):
  611.                 lock_x = False
  612.                 lock_y = True
  613.                 teksstring3 = teksstring3 + "siang, zenith locked"
  614.                
  615.             #ketika intensitas tidak cukup
  616.             if maxVal<minimum:
  617.                 motorsafe()
  618.                 print("intensitas cahaya tidak cukup")
  619.                 teksstring2 = str("intensitas tidak cukup")
  620.  
  621.             #ketika intensitas cukup, mulai mencari
  622.             if maxVal>(minimum -1) :
  623.                 if ((terbit +2)<jamskrg<(tenggelam -0.15)):
  624.                     if (pa<centre_y<pb) & (155<centre_x<165):
  625.                         motorsafe()
  626.                         print("posisi sudah tengah")
  627.                            
  628.                     if lock_x == True:
  629.                         if pa<centre_y<pb:
  630.                             motorsafe()
  631.                             print("motor2 stop")
  632.                             lock_y  = True
  633.                             lock_x  = False
  634.                             lockknn = False
  635.                             lockkri = False
  636.                                    
  637.                         if (centre_y<(pa+1)):
  638.                             if (Lim1 == True):
  639.                                 print("kebawah (Zenit -)")
  640.                                 teksstring2 = str("kebawah (Zenit -)")
  641.                                 GPIO.output(Mot2R, GPIO.HIGH)
  642.                                 time.sleep(0.125)
  643.                             if (Lim1 == False):
  644.                                 print("kebawah sudah maksimal")
  645.                                 GPIO.output(Mot2R, GPIO.LOW)
  646.                                 lock_x = False
  647.                                 lock_y = True
  648.                                
  649.                         if ((pb-1)<centre_y):
  650.                             if (Lim2 == True):
  651.                                 print("keatas (Zenit +)")
  652.                                 teksstring2 = str("keatas (Zenit +)")
  653.                                 GPIO.output(Mot2F, GPIO.HIGH)
  654.                                 time.sleep(.125)
  655.                             if (Lim2 == False):
  656.                                 print("keatas sudah maksimal")
  657.                                 GPIO.output(Mot2F, GPIO.LOW)
  658.                                 lock_x = False
  659.                                 lock_y = True
  660.  
  661.                     if(jamonminute < (selesailock +90)):
  662.                         invknn = True
  663.                         invkri = True
  664.  
  665.                     if lock_y == True:
  666.                         if 155<centre_x<165:
  667.                             print("motor1 stop")
  668.                             lock_x = True
  669.                             lock_y = False
  670.                            
  671.                         if (centre_x<154):
  672.                             print("motor 1 kekanan")
  673.                             if lockknn == False :
  674.                                 if (invknn == False):
  675.                                     if (Lim4 == True):
  676.                                         print("motor 1 kekanan (Azimut +)")
  677.                                         teksstring2 = str("kekanan (Azimut +)")
  678.                                         lockknn     = False
  679.                                         lockkri     = False
  680.                                         motor1forward(.075, 33)
  681.                                     if (Lim4 == False):
  682.                                         print("motor 1 maksimal")
  683.                                         lock_x  = True
  684.                                         lock_y  = False
  685.                                         lockknn = True
  686.                                         lockkri = False
  687.                                         if(time.time()-wait3) > 5:
  688.                                             invknn  = not invknn
  689.                                             wait3   = time.time()
  690.                                 if (invknn == True):
  691.                                     if (Lim3 == True):
  692.                                         print("motor 1 kekanan (invers) (Azimut -)")
  693.                                         teksstring2 = str("kekanan (inv) (Azimut -)")
  694.                                         lockknn     = False
  695.                                         lockkri     = False
  696.                                         motor1reverse(.075, 33)
  697.                                     if (Lim3 == False):
  698.                                         print("motor 1 maksimal (invers)")
  699.                                         lock_x  = True
  700.                                         lock_y  = False
  701.                                         lockknn = True
  702.                                         lockkri = False
  703.                                         if(time.time()-wait3) > 5:
  704.                                             invknn  = not invknn
  705.                                             wait    = time.time()
  706.                                 if (time.time()-wait) > 1:
  707.                                     wait=time.time()
  708.                                     if ((ctxskg)>(centre_x)):
  709.                                         invknn = not invknn
  710.                                        
  711.                             if lockknn == True :
  712.                                 print("kanan maksimal")
  713.                                 lock_x  = True
  714.                                 lock_y  = False
  715.                                 lockknn = True
  716.                                 lockkri = False
  717.                                 if(time.time()-wait3) > 5:
  718.                                     invknn  = not invknn
  719.                                     wait3   = time.time()
  720.  
  721.                         if (166<centre_x):
  722.                             print("motor 1 kekiri")
  723.                             if lockkri == False:
  724.                                 if (invkri == False):
  725.                                     if (Lim3 == True):
  726.                                         print("motor 1 kiri (Azimut -)")
  727.                                         teksstring2 = str("kekiri (Azimut -)")
  728.                                         motor1reverse(.075, 33)
  729.                                         lockknn     = False
  730.                                         lockkri     = False
  731.                                     if (Lim3 == False):
  732.                                         print("motor 1 maksimal")
  733.                                         lock_x  = True
  734.                                         lock_y  = False
  735.                                         lockknn = False
  736.                                         lockkri = True
  737.                                         if(time.time()-wait3) > 5:
  738.                                             invkri  = not invkri
  739.                                             wait3   = time.time()
  740.                                 if (invkri == True):
  741.                                     if (Lim4 == True):
  742.                                         print("motor 1 kiri (invers)(Azimut +)")
  743.                                         teksstring2 = str("kekiri(inv)(Azimut +)")
  744.                                         motor1forward(.075, 33)
  745.                                         lockknn     = False
  746.                                         lockkri     = False
  747.                                     if (Lim4 == False):
  748.                                         print("motor 1 maksimal (invers)")
  749.                                         lock_x  = True
  750.                                         lock_y  = False
  751.                                         lockknn = False
  752.                                         lockkri = True
  753.                                         if(time.time()-wait3) > 5:
  754.                                             invkri  = not invkri
  755.                                             wait3   = time.time()
  756.                                 if (time.time()-wait) > 1:
  757.                                     wait=time.time()
  758.                                     if((ctxskg)<(centre_x)):
  759.                                         invkri = not invkri
  760.                                
  761.                             if lockkri == True :
  762.                                 print("kanan maksimal")
  763.                                 lock_x  = True
  764.                                 lock_y  = False
  765.                                 lockknn = False
  766.                                 lockkri = True
  767.                                 if(time.time()-wait3) > 5:
  768.                                     invknn  = not invknn
  769.                                     wait3   = time.time()
  770.  
  771.         if (time.time()-waitx) > 3:
  772.             waitx   = time.time()
  773.             del ctxskg
  774.             ctxskg  = centre_x
  775.  
  776.     # mengosongi memory stream
  777.     rawCapture.truncate(0)
  778.    
  779.     #motorsafe()
  780.  
  781.     if (time.time()-waitclean) > 600:
  782.         cv2.destroyAllWindows()
  783.         for i in range (1,5):
  784.                 cv2.waitKey(1)
  785.         waitclean = time.time()
  786.  
  787.     key = cv2.waitKey(1) & 0xFF
  788.     if (key == ord("q")) or (key == 27):
  789.             break
  790.     if (key == ord("i")):
  791.         ignore = not ignore
  792.        
  793.     if ignore == True:
  794.         teksstring2 = str("mode manual, p:scan, a,s,d,w:gerak")
  795.         if (key == ord("a")):
  796.             if (Lim4 == True):
  797.                 print("motor 1 kiri (invers)(Azimut +)")
  798.                 teksstring2 = str("kekiri(inv)(Azimut +)")
  799.                 motor1forward(.075, 33)
  800.                 lockknn     = False
  801.                 lockkri     = False
  802.             if (Lim4 == False):
  803.                 print("motor 1 maksimal (invers)")
  804.                 lock_x  = True
  805.                 lock_y  = False
  806.                 lockknn = False
  807.                 lockkri = True
  808.         if (key == ord("s")):
  809.             if (Lim1 == True):
  810.                 print("kebawah (Zenit -)")
  811.                 teksstring2 = str("kebawah (Zenit -)")
  812.                 GPIO.output(Mot2R, GPIO.HIGH)
  813.                 time.sleep(1)
  814.                 GPIO.output(Mot2R, GPIO.LOW)
  815.             if (Lim1 == False):
  816.                 print("kebawah sudah maksimal")
  817.                 GPIO.output(Mot2R, GPIO.LOW)
  818.                 lock_x = False
  819.                 lock_y = True
  820.         if (key == ord("d")):
  821.             if (Lim3 == True):
  822.                 print("motor 1 kekanan (invers) (Azimut -)")
  823.                 teksstring2 = str("kekanan (inv) (Azimut -)")
  824.                 lockknn     = False
  825.                 lockkri     = False
  826.                 motor1reverse(.075, 33)
  827.             if (Lim3 == False):
  828.                 print("motor 1 maksimal (invers)")
  829.                 lock_x  = True
  830.                 lock_y  = False
  831.                 lockknn = True
  832.                 lockkri = False
  833.         if (key == ord("w")):
  834.             if (Lim2 == True):
  835.                 print("keatas (Zenit +)")
  836.                 teksstring2 = str("keatas (Zenit +)")
  837.                 GPIO.output(Mot2F, GPIO.HIGH)
  838.                 time.sleep(1)
  839.                 GPIO.output(Mot2F, GPIO.LOW)
  840.             if (Lim2 == False):
  841.                 print("keatas sudah maksimal")
  842.                 GPIO.output(Mot2F, GPIO.LOW)
  843.                 lock_x = False
  844.                 lock_y = True
  845.         if (key == ord("p")):
  846.             print("memulai pemicu runtutan pemindaian")
  847.             scanning = True
  848.             mulaiscan = True
  849.  
  850. motorsafe()        
  851. GPIO.output(kipas, GPIO.LOW)                   
  852. cv2.destroyAllWindows()
  853. for i in range (1,5):
  854.     cv2.waitKey(1)
  855. del camera
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement