[질문] 초음파센서 와 PIR센터를 이용한 파이카메라 촬영 관련



  • 초음파 센서 2개와 파이카메라를 이용해서 물체가 감지되면 촬영되게 할려하는데 코드 오류가 뜨는데 머가 틀렸는지 찝어주시면 감사하겠습니다!!!

    1. import RPi.GPIO as GPIO
    2. import time
    3. import time as t
    4. from time import localtime, strftime
    5. import subprocess
    6. 
    7. filepath = "/home/pi/home/pi/u_cam/practice"
    8. fileNamePrefix = "Capture"
    9. 
    10. trigger_pin = 18
    11. echo_pin = 17
    12. 
    13. trig = 20
    14. echo = 16
    15. 
    16. GPIO.setmode(GPIO.BCM)
    17. GPIO.setup(trigger_pin, GPIO.OUT)
    18. GPIO.setup(echo_pin, GPIO.IN)
    19. GPIO.setup(trig, GPIO.OUT)
    20. GPIO.setup(echo, GPIO.IN)
    21. GPIO.setup(18, GPIO.IN)
    22. 
    23. def send_trigger_pulse():
    24.     GPIO.output(trigger_pin, True)
    25.     time.sleep(0.0001)
    26.     GPIO.output(trigger_pin, False)
    27. 
    28. def send_trig_pulse() :
    29.     GPIO.output(trig, True)
    30.     time.sleep(0.0001)
    31.     GPIO.output(trig, False)
    32. 
    33. def wait_for_echo(value, timeout):
    34.     count = timeout
    35.     while GPIO.input(echo_pin) != value and count > 0 :
    36. 	count = count - 1
    37. def wait_for_echo_t(val, tiou):
    38.     cou = tiou
    39.     while GPIO.input(echo)!= val and cou > 0 :
    40. 	cou = cou - 1
    41.  
    42. def get_distance():
    43. 	send_trigger_pulse()
    44. 	wait_for_echo(True, 10000)
    45. 	start = time.time()
    46. 	wait_for_echo(False, 10000)
    47. 	finish = time.time()
    48. 	pulse_len = finish - start
    49. 	distance_cm = pulse_len / 0.000058
    50. 	distance_in = distance_cm / 2.5
    51. 	return distance_cm
    52. 
    53. def get_dist():
    54. 	send_trig_pulse()
    55. 	wait_for_echo_t(True, 10000)
    56. 	sta = time.time()
    57. 	wait_for_echo_t(False, 10000)
    58. 	fin = time.time()
    59. 	pul_len = fin - sta
    60. 	dist_cm =pul_len / 0.000058
    61. 	dist_in = dist_cm / 2.5
    62. 	return dist_cm
    63. 
    64. while True:
    65. 	x=get_distance()
    66. 	y=get_dist()
    67. 	if x<100 and y<100:
    68. 		filename = filepath + "/" + fileNamePrefix + strftime("%y%m%d-%H:%M:%S", localtime())
    69. 		command = "raspistill -t 1 -o %s" % (filename)
    70. 		subprocess.call(command, shell = True)
    71. 		print "Captured %s" % filename 
    72. 		t.sleep(3)
    73. 	else:
    74. 		print("Ultra_1 distance : %fcm" % get_distance(), "Ultra_2 distance : %fcm" % get_dist())
    75. 		time.sleep(1)
    

  • Global Moderator

    중요한건 에러코드라서요…
    에러코드를 올려주시면 더 잘 맞출 수 있을 것 같아요.


  • Global Moderator

    한가지 눈에 띄는건 36, 40 라인에 들여쓰기가 안되었네요.


답변을 위해 로그인하기