Advertisement
CahyoF

maxtracker

Jun 29th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.73 KB | None | 0 0
  1. #memanggil library yang diperlukan
  2. import sys
  3. import time
  4. import RPi.GPIO as GPIO
  5. import cv2
  6. import numpy as np
  7.  
  8. #memulai kamera
  9. cap = cv2.VideoCapture(0)
  10.  
  11. cap.set(3, 640) # width        
  12. cap.set(4, 480) # height
  13.  
  14. #mereset GPIO agar siap digunakan
  15. GPIO.cleanup()
  16.  
  17. #menggunakan BCM sebagai referensi GPIO
  18. GPIO.setmode(GPIO.BCM)
  19.  
  20. #mengatur GPIO untuk keluaran
  21. Mot1F=24
  22. Mot1R=25
  23. Mot2F=18
  24. Mot2R=23
  25.  
  26. GPIO.setup(Mot1F, GPIO.OUT)
  27. GPIO.setup(Mot1R, GPIO.OUT)
  28. GPIO.setup(Mot2F, GPIO.OUT)
  29. GPIO.setup(Mot2R, GPIO.OUT)
  30.  
  31. #mengatur GPIO masukan
  32. GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  33. GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  34. GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  35. GPIO.setup(8, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  36.  
  37. Lim1=GPIO.input(16)
  38. Lim2=GPIO.input(12)
  39. Lim3=GPIO.input(8)
  40. Lim4=GPIO.input(7) #kanan
  41.  
  42. #membuat fungsi-fungsi yang diperlukan
  43.  
  44. #fungsi motor dengan pwm software menggunakan frekuensi 50Hz
  45. def motor1forward(dur, cycle):
  46.     t1=0.02
  47.     a1=cycle/100
  48.     ton=a1*t1
  49.     toff=t1-ton
  50.     t4 = int(round(50*dur))
  51.     print("forwarding motor 1")
  52.     for i in range (1, t4) :
  53.         GPIO.output(Mot1F, GPIO.HIGH)
  54.         time.sleep(ton)
  55.         GPIO.output(Mot1F, GPIO.LOW)
  56.         time.sleep(toff)
  57.     del dur, cycle, t1, a1, ton, toff, t4
  58.  
  59. def motor1reverse(dur, cycle):
  60.     t1=0.02
  61.     a1=cycle/100
  62.     ton=a1*t1
  63.     toff=t1-ton
  64.     t4 = int(round(50*dur))
  65.     print("backwarding motor 1")
  66.     for i in range (1, t4) :
  67.         GPIO.output(Mot1R, GPIO.HIGH)
  68.         time.sleep(ton)
  69.         GPIO.output(Mot1R, GPIO.LOW)
  70.         time.sleep(toff)
  71.     del dur, cycle, t1, a1, ton, toff, t4
  72.        
  73. def motor2forward(dur, cycle):
  74.     t1=0.02
  75.     a1=cycle/100
  76.     ton=a1*t1
  77.     toff=t1-ton
  78.     t4 = int(round(50*dur))
  79.     print("forwarding motor 2")
  80.     for i in range (1, t4) :
  81.         GPIO.output(Mot2F, GPIO.HIGH)
  82.         time.sleep(ton)
  83.         GPIO.output(Mot2F, GPIO.LOW)
  84.         time.sleep(toff)
  85.     del dur, cycle, t1, a1, ton, toff, t4
  86.  
  87. def motor2reverse(dur, cycle):
  88.     t1=0.02
  89.     a1=cycle/100
  90.     ton=a1*t1
  91.     toff=t1-ton
  92.     t4 = int(round(50*dur))
  93.     print("backwarding motor 2")
  94.     for i in range (1, t4) :
  95.         GPIO.output(Mot2R, GPIO.HIGH)
  96.         time.sleep(ton)
  97.         GPIO.output(Mot2R, GPIO.LOW)
  98.         time.sleep(toff)
  99.     del dur, cycle, t1, a1, ton, toff, t4
  100.  
  101.  
  102. while True: #infinite looping agar program terus berjalan
  103.    
  104.     _, frame = cap.read()
  105.  
  106.     #orig = frame.copy()
  107.    
  108.     gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  109.     gray = cv2.GaussianBlur(gray,(161,161),0)
  110.     (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)
  111.  
  112.     image = frame.copy()
  113.     cv2.circle(image, maxLoc, 161, (255, 0, 0), 2)
  114.     cv2.imshow("Alhamdulillah", image)
  115.  
  116.     centre_x, centre_y = (int(x) for x in maxLoc)
  117.     print (centre_x, centre_y)
  118.    
  119.     if 280<centre_x<360:
  120.         print("motor1 stop")
  121.     if 200<centre_y<280:
  122.         print("motor2 stop")
  123.  
  124.     if (80<centre_x<280):
  125.         if (Lim4 == True):
  126.             print("motor 1 kekanan pelan")
  127.             motor1reverse(.250, 20)
  128.         if (Lim4 == False):
  129.             print("motor 1 maksimal")
  130.            
  131.     if (80<centre_y<200):
  132.         if (Lim1 == True):
  133.             print("motor 2 bawah pelan")
  134.             motor2forward(.250, 80)
  135.         if (Lim1 == False):
  136.             print("motor 2 maksimal")
  137.  
  138.     if (360<centre_x<560):
  139.         if (Lim3 == True):
  140.             print("motor 1 kiri pelan")
  141.             motor1forward(.250, 20)
  142.         if (Lim1 == False):
  143.             print("motor 1 maksimal")
  144.            
  145.     if (280<centre_y<400):
  146.         if (Lim2 == True):
  147.             print("motor 2 keatas pelan")
  148.             motor2forward(.250, 80)
  149.         if (Lim2 == False):
  150.             print("motor 2 maksimal")
  151.          
  152.     if (centre_x<80):
  153.         if (Lim4 == True):
  154.             print("motor 1 kekanan fullspeed")
  155.             motor1forward(.250, 21)
  156.         if (Lim4 == False):
  157.             print("motor 1 maksimal")
  158.            
  159.     if (centre_y<80):
  160.         if (Lim1 == True):
  161.             print("motor 2 bawah fullspeed")
  162.             motor2forward(.250, 100)
  163.         if (Lim1 == False):
  164.             print("motor 2 maksimal")
  165.  
  166.     if (560<centre_x):
  167.         if (Lim3 == True):
  168.             print("motor 1 kiri fullspeed")
  169.             motor1reverse(.250, 21)
  170.         if (Lim1 == False):
  171.             print("motor 1 maksimal")
  172.            
  173.     if (400<centre_y):
  174.         if (Lim2 == True):
  175.             print("motor 2 keatas fullspeed")
  176.             motor1forward(.250, 100)
  177.         if (Lim2 == False):
  178.             print("motor 2 maksimal")
  179.            
  180.     k = cv2.waitKey(5) & 0xFF
  181.     if k == 27:
  182.         break
  183.    
  184. cv2.destroyAllWindows()
  185. del cap
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement