def getDistance(self): # pins setup GPIO.setup(self.trig, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(self.echo, GPIO.IN) # set Trigger to HIGH for 10 us GPIO.output(self.trig, GPIO.HIGH) time.sleep(0.00001) # 10 us GPIO.output(self.trig, GPIO.LOW) # start counting time at Echo rising edge GPIO.wait_for_edge(self.echo, GPIO.RISING, timeout=100) # 100 ms startTime = time.time() # stop counting time at Echo falling edge GPIO.wait_for_edge(self.echo, GPIO.FALLING, timeout=100) # 100 ms elapsedTime = time.time() - startTime # in seconds distance = -1 # check if the measurement succeeded if elapsedTime < 0.1: # get the distance in cm using sonic speed (aprox. 34300 cm/s) distance = (elapsedTime * 34300) / 2 GPIO.cleanup([self.trig, self.echo]) return distance
def main(): # Pin Setup: GPIO.setmode(GPIO.BOARD) # BOARD pin-numbering scheme #GPIO.setup(led_pin, GPIO.OUT) # LED pin set as output GPIO.setup(but_pin, GPIO.IN) # button pin set as input # Initial state for LEDs: #GPIO.output(led_pin, GPIO.LOW) print("Starting demo now! Press CTRL+C to exit") try: while True: print("Waiting for button event") GPIO.wait_for_edge(but_pin, GPIO.FALLING) # event received when button pressed print("Button Pressed!") #GPIO.output(led_pin, GPIO.HIGH) time.sleep(1) #GPIO.output(led_pin, GPIO.LOW) finally: GPIO.cleanup() # cleanup all GPIOs
def mov_detector(gpio_sensor=0, cm_hole=0, distance_interval=0, start_photo_inference=0, start_photo_check=0, spray_line1=0, spray_line2=0): ''' In this function we will use a disk with 20 or 40 holes and a radius of 5.1 cm, the the end of the disk (perpendicular), we will use an infrared sensor to detect interruptions, every interruption is a movement. It will also be used to check the speed, trigger a photo for inference and checage. Parameters: gpio_sensor: this is the device port to connect as an input cm_hole: it means how many centimeters each hole in the disk has (it's one step in the disk) distance_interval: This is the distance in CM to be defined to trigger a photo start_photo_inference: the minimal distance to trigger the photo for inference start_photo_check: the minimal distance to trigger the check photo (check the spray) spray_line1: if KeyBoard interrupt is triggered it turns off the solenoids in the first line spray_line2: if KeyBoard interrupt is triggered it turns off the solenoids in the second line # TODO: Use two sensors to detect the direction of the disk ''' # Setting initial time to measure the speed global curr_position global counter_speed global counter_infer_photo global counter_check_photo global start global speed try: if counter_speed == 0: start = datetime.now() if GPIO.wait_for_edge(gpio_sensor, GPIO.RISING): pass if GPIO.wait_for_edge( gpio_sensor, GPIO.FALLING): # Change in signal, counting steps curr_position += 1 counter_speed += 1 counter_infer_photo += 1 counter_check_photo += 1 # Measuring distance travelled distance = curr_position * cm_hole # LOOK AT THIS print('[WEEDS] Distance Traveled: ', round(distance / 100, 4), ' meters') # Measuring variation on time and distance if counter_speed == 10: end = datetime.now() diff = end - start delta_time = diff.seconds + diff.microseconds / 1e6 delta_distance = counter_speed * cm_hole / 100 #in meters speed = delta_distance / delta_time # m/sec counter_speed = 0 if curr_position > 10: print('[WEEDS] Current speed: ', round(speed, 2), ' m/sec or: ', round(speed * 3.6, 2), ' km/h.') # Triggering Inference Photo if curr_position >= start_photo_inference: if counter_infer_photo * cm_hole >= distance_interval: infer_trigger = True counter_infer_photo = 0 print('[WEEDS] Inference Photo Triggered') else: infer_trigger = False # Triggering Check Photo if curr_position >= start_photo_check: if counter_check_photo * cm_hole >= distance_interval: check_trigger = True counter_check_photo = 0 print('[WEEDS] Check Photo Triggered') else: check_trigger = False print('[WEEDS] Position:', curr_position, ', Infer Trigger:', infer_trigger, ', Check Trigger:', check_trigger) except KeyboardInterrupt: print("[WEEDS] KeyboardInterrupt has been caught.") GPIO.output(spray_line1, GPIO.LOW) GPIO.output(spray_line2, GPIO.LOW) GPIO.cleanup() sys.exit() return curr_position, infer_trigger, check_trigger
#!/usr/bin/env python2 import os import Jetson.GPIO as GPIO SWITCH_PIN = 22 if __name__ == "__main__": try: GPIO.setmode(GPIO.BOARD) GPIO.setup(SWITCH_PIN, GPIO.IN) print("Waiting for switch edge...") GPIO.wait_for_edge(SWITCH_PIN, GPIO.FALLING) print("Switch triggered; shutting down...") os.system("shutdown -h now") except KeyboardInterrupt: GPIO.cleanup()