#!/usr/bin/env python import time,RPi.GPIO as gpio import RPi.GPIO as GPIO def setup(): gpio.setwarnings(False) gpio.setmode(gpio.BOARD) gpio.setup(19, gpio.OUT) gpio.setup(21, gpio.OUT) gpio.setup(24, gpio.OUT) gpio.setup(26, gpio.OUT) def offmotors(): gpio.output(26,0) gpio.output(24,0) gpio.output(21,0) gpio.output(19,0) print("motors off") def forward(): print("moving forward") gpio.output(26,0) gpio.output(24,1) gpio.output(21,0) gpio.output(19,1) def backward(): print("moving backward") gpio.output(26,1) gpio.output(24,0) gpio.output(21,1) gpio.output(19,0) def Sensor_Input_to_Distance(Sensor): # Use BCM GPIO references # instead of physical pin numbers GPIO.setmode(GPIO.BCM) # Define GPIO to use on Pi if Sensor == FRONT: GPIO_TRIGGER = 23 GPIO_ECHO = 24 else GPIO_TRIGGER = 23 GPIO_ECHO = 24 print "Ultrasonic Measurement" # Set pins as output and input GPIO.setup(GPIO_TRIGGER,GPIO.OUT) # Trigger GPIO.setup(GPIO_ECHO,GPIO.IN) # Echo # Set trigger to False (Low) GPIO.output(GPIO_TRIGGER, False) # Allow module to settle time.sleep(0.5) # Send 10us pulse to trigger GPIO.output(GPIO_TRIGGER, True) time.sleep(0.00001) GPIO.output(GPIO_TRIGGER, False) start = time.time() while GPIO.input(GPIO_ECHO)==0: start = time.time() while GPIO.input(GPIO_ECHO)==1: stop = time.time() # Calculate pulse length elapsed = stop-start # Distance pulse travelled in that time is time # multiplied by the speed of sound (cm/s) distance = elapsed * 34000 # That was the distance there and back so halve the value distance = distance / 2 print ( "Distance : %.1f" % distance ) # Reset GPIO settings GPIO return distance def Wifi_Owner_Near (WIFI_SSID): """ use library funciton from raspberry PI to get signal strength handle dynamic signal level for Dummy signal strength """ #dummynear = prompt("Dummy Near or not? (Yes or No)") #if near = True near = True #else: # near = False return def Create_Motor_drive_instructions (Movement): #Make robot move return False def Drive_Robotrolley(distance): if distance > (MINDIST + BUFFER) <MAXDIST: Forward () # turn motor on while distance = getdistance() distance > MINDIST else: #Variable definition Movement = 0 #integer dictating if move forwards - 1, stop -0, or move backwards -1 FRONT = 0 #integer for front sensor selection REAR = 1 #integer for rear sensor WIFI_SSID = "" #String to hold SSID distance = 0 #integer for number of centimeters from nearest object in front of ultrasonic sensor near = False #bool for whether wifi is near or not MINDIST = 50 MAXDIST = 200 FORWARD = 1 BACKWARD = -1 STOP = 0 BUFFER =5 # Main function to decide how the trolley moves def TrolleyControl(): #if Owner near trolley then we can consider moving if Wifi_Owner_Near (WIFI_SSID): # Use ultrasonic sensor to check if the trolley is close to owner Movement = Stop_Forward_or_Backwards(Sensor_Input_to_Distance(FRONT)) # -1 returned int movmement, move backwards, 0 returned, stop, 1 returned move forward Create_Motor_drive_instructions (Movement) else: #Owner is not close to trolley #display a message or sound a beep Movement = 0 return
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question