def __init__(self, detectDist=12, timeout=15): self.u = ultrasonic.Ultrasonic() self.p = pir.PIR() self.birdHere = 0 self.detectDist = detectDist self.timeout = timeout self.statusWrite("on")
def get_sensor_reading(): ultrasonic_sensor = US.Ultrasonic() readings = [ 0 ] * 3 #make list of 3 zeroes to store the 3 readings (front, left, and right) readings[0] = ultrasonic_sensor.measure( ) #get X3 parameter for learner, front obstacle detection time.sleep(0.1) MC.MotorController.turnLeft( ) #get X2 parameter for learner, left obstacle detection GPIO.cleanup() readings[1] = ultrasonic_sensor.measure() time.sleep(0.1) MC.MotorController.turnRight( ) #turn right twice because we originally turned left from facing forward GPIO.cleanup() MC.MotorController.turnRight() GPIO.cleanup() readings[2] = ultrasonic_sensor.measure( ) #get X1 parameter for learner, right obstacle detection MC.MotorController.turnLeft() #Face front again GPIO.cleanup() return readings
# -*- coding: utf-8 -*- Created on Fri Feb 16 02:54:09 2018 @author: Etcyl """ import motor_controller as MC import ultrasonic as US import RPi.GPIO as GPIO import support_funcs as sf #DO setup here #Set GPIO mode to BCM GPIO.setmode(GPIO.BCM) #Create objects for Vikingbot motor controller and HC-SR04 sensor vb_motor = MC.MotorController() HC_sensor = US.Ultrasonic() HC_sensor.setup_GPIO() vb_motor.setup_GPIO(1, 0) # setup and start PWM set the dulty cycles to 90 vb_motor.setup_PWM() vb_motor.start_PWM() vb_motor.set_motorSpeed(90, 90) # set the delay between motions to 2 seconds vb_motor.set_SleepTime(2) sensor_vals = [0] * 3 #...Finished setup #Check front, left, and right of the vb for objects using the sk model
import pyb import ultrasonic # setting pins to accomodate Ultrasonic Sensor (HC-SR04) sensor1_trigPin = pyb.Pin.board.X3 sensor1_echoPin = pyb.Pin.board.X4 sensor2_trigPin = pyb.Pin.board.Y3 sensor2_echoPin = pyb.Pin.board.Y4 # sensor needs 5V and ground to be connected to pyboard's ground # creating two Ultrasonic Objects using the above pin config sensor1 = ultrasonic.Ultrasonic(sensor1_trigPin, sensor1_echoPin) sensor2 = ultrasonic.Ultrasonic(sensor2_trigPin, sensor2_echoPin) # using USR switch to print the sensor's values when pressed switch = pyb.Switch() # function that prints each sensor's distance def print_sensor_values(): # get sensor1's distance in cm distance1 = sensor1.distance_in_cm() # get sensor2's distance in inches distance2 = sensor2.distance_in_inches() print("Sensor1 (Metric System)", distance1, "cm") print("Sensor2 (Imperial System)", distance2, "inches") # prints values every second while True:
import motor_controller as MC import ultrasonic as US import RPi.GPIO as GPIO import subprocess #set GPIO mode to BCM GPIO.setmode(GPIO.BCM) # create objects for motor controller and distance sensor vikingbotMotors = MC.MotorController() ultrasonicSensorBack = US.Ultrasonic() #GPIO.cleanup() # setup the motors vikingbotMotors.setup_GPIO(1, 0) # setup and start PWM set the dulty cycles to 90 vikingbotMotors.setup_PWM() vikingbotMotors.start_PWM() vikingbotMotors.set_motorSpeed(55, 100) # set the delay between motions to 2 seconds vikingbotMotors.set_SleepTime(2) #Test the movements #Test singing frog GPIO.setup(19, GPIO.OUT) GPIO.output(19, GPIO.HIGH) print "Frog is singing"
""" demo of Ultrasonic device with ultrasonic module of Euter2. 2017-1014 PePo adopted for MicropPython 1.9+, configuration ESP32 / Wemos Lolin32 (Revision 0) """ import ultrasonic from time import sleep # GPIO pins for specific development board Wemos D1 mini # PePo: not relevant for WeMOS Lolin32 #gpio = {"TX":01, "RX":03, "D0": 16, "D1": 05, "D2": 04, "D3": 00, "D4": 02, "D5": 14, "D6": 12, "D7": 13, "D8": 15} trigger = 12 #gpio[ "D6" ] echo = 14 #gpio[ "D5" ] #create sensor hc on pins trigger and echo hc = ultrasonic.Ultrasonic(trigger, echo) dist = 0 # distance value while True: # Get reading from sensor ''' distance in meters dist = hc.distance() print('afstand is {0:4.1f} m'.format(dist)) #''' #''' distance in cm dist = hc.distance_in_cm() print('afstand is {0:3.1f} m'.format(dist)) #''' sleep(1)
def fetchData(): src = None data = "" try: src = urlopen(URL) data = src.read() except (ValueError, RuntimeError, TypeError, NameError, urllib2.HTTPError): return False finally: if src: src.close() return json.loads(data) us = ultrasonic.Ultrasonic() servo = servo.Servo() qrlcd = qrlcd.QRLCD() qrlcd.blank() print("System initialized.") while True: payed = False distance = us.calculateDistance() if (distance < 4): print("Object detected.\nSending image to LCD.") st = time.time() qrlcd.send(address, str(float(PRICE) / 100000) + " BTC") while not payed: print("Fetching data from API.")
def __init__(self): self.sensor = ultrasonic.Ultrasonic()
]) # Tensor("ResizeBilinear:0", shape=(1, 224, 224, 3), dtype=float32) normalized = tf.divide( tf.subtract(resized, [0]), [255]) # Tensor("truediv:0", shape=(1, 224, 224, 3), dtype=float32) return normalized # for logging logging.basicConfig( level=logging.INFO, format='[%(levelname)s] (%(threadName)-10s) %(message)s', ) stream = frame.Frame(1, "Frame") # setup stream from Frame class car = drive.Drive(2, 'Drive') # setup car motors from Drive class ultrasonic = ultrasonic.Ultrasonic( 3, "Ultrasonic", 23, 24) # setup distance sensor from Ultrasonic class car.car_stopped() # stop motors # setup model model_file = "mobilenet_v2_graph.pb" # model file to use input_height = 224 # image height expected in the model input_width = 224 # image width expected in the model input_mean = 0 # mean per channel input_std = 255 # standard deviation associated input_layer = "Placeholder" output_layer = "final_result" graph = load_graph(model_file) # load the graph from the model file input_name = "import/" + input_layer output_name = "import/" + output_layer input_operation = graph.get_operation_by_name(input_name)
import pyb import time import ultrasonic switch = pyb.Switch() sensor = ultrasonic.Ultrasonic(pyb.Pin.board.X3, pyb.Pin.board.X4) while not switch.value(): try: dist = sensor.distance_in_cm() print("Dist = {}".format(dist)) except ultrasonic.MeasurementTimeout as e: print("{}".format(e)) time.sleep(1)
import machine import time from time import sleep import ultrasonic import neopixel np = neopixel.NeoPixel(machine.Pin(0), 8) trig = machine.Pin(16, machine.Pin.OUT) echo = machine.Pin(12, machine.Pin.IN) sensor = ultrasonic.Ultrasonic(trig, echo) while True: distanceFound = sensor.distance() print(distanceFound) time.sleep(0.2) if distanceFound < 0.3: np[0] = (50, 0, 0) np.write() else: np[0] = (0, 0, 0) np.write()
def ultraSonicDistanceTask(): '''Is a bot too close? Run away. Is a bot almost too close? Run away. Can you see the bot? Run away. Running away is our bots survival tactic. If an enemy bot is detected within a 2 foot square area, our bot will turn an appropriate direction and continue running. The sensors are checked in order, so the front and rear sensors must be checked first. The sensors are sensitive, if anything is detected further than 30 inches away it is disregarded and the bot can run on.''' US_1 = ultrasonic.Ultrasonic(P.US_DIST_TRIG_1, P.US_DIST_ECHO_1) US_2 = ultrasonic.Ultrasonic(P.US_DIST_TRIG_2, P.US_DIST_ECHO_2) US_3 = ultrasonic.Ultrasonic(P.US_DIST_TRIG_3, P.US_DIST_ECHO_3) US_4 = ultrasonic.Ultrasonic(P.US_DIST_TRIG_4, P.US_DIST_ECHO_4) OFF1 = const(0) # pylint: disable=undefined-variable ANALYZE_US = const(1) ANALYZE_FRONT = const(2) ANALYZE_REAR = const(3) ANALYZE_RIGHT = const(4) ANALYZE_LEFT = const(5) ANALYZE_BOT = const(6) DONT_ANALYZE_US = const(7) state = OFF1 def eStop(state): if IR.getCommand() != C.START: state = OFF1 return state def checkForTurning(state): if turning.get() != 0: state = DONT_ANALYZE_US return state def checkSensor(state, direction, last_dir, last_prox): if direction == 'front': uSensor = US_2 share = us_front elif direction == 'left': uSensor = US_4 share = us_left elif direction == 'right': uSensor = US_3 share = us_right elif direction == 'rear': uSensor = US_1 share = us_rear distance = uSensor.distance_in_inches() if distance < 6: level = 1 share.put(level) elif distance > 6 and distance < 18: level = 2 share.put(level) elif distance > 18 and distance < 24: level = 3 share.put(level) elif distance > 24 and distance < 30: level = 4 share.put(level) elif distance > 30: level = 5 share.put(level) print(str('us ') + direction + ': ' + str(share.get())) print(str('us ') + direction + ': ' + str(distance)) if share.get() != 5: curr_dir = state - 1 curr_prox = share.get() if last_prox < curr_prox: curr_prox = last_prox curr_dir = last_dir else: pass else: curr_prox = last_prox curr_dir = last_dir last_prox = curr_prox last_dir = curr_dir state = state + 1 state = checkForTurning(state) state = eStop(state) return curr_dir, curr_prox, last_dir, last_prox, state while True: if state == ANALYZE_US: state = ANALYZE_FRONT last_prox = 5 last_dir = 1 state = checkForTurning(state) state = eStop(state) yield (state) if state == ANALYZE_FRONT: curr_dir, curr_prox, last_dir, last_prox, state = \ checkSensor(state, 'front', last_dir, last_prox) yield (state) if state == ANALYZE_REAR: # REAR US SENSOR # for a bot in closing position from the rear, turn 90 degrees and drive away curr_dir, curr_prox, last_dir, last_prox, state = \ checkSensor(state, 'rear', last_dir, last_prox) yield (state) if state == ANALYZE_RIGHT: # RIGHT US SENSOR # for a bot in closing position from the right curr_dir, curr_prox, last_dir, last_prox, state = \ checkSensor(state, 'right', last_dir, last_prox) yield (state) if state == ANALYZE_LEFT: # ## LEFT US SENSOR # for a bot in closing position from the left curr_dir, curr_prox, last_dir, last_prox, state = \ checkSensor(state, 'left', last_dir, last_prox) yield (state) if state == ANALYZE_BOT: print('analyze bot') print(curr_dir) print(curr_prox) us_current_dir.put(curr_dir) us_current_prox.put(curr_prox) if curr_prox == 1: if curr_dir == 1: # front turn.put(2) elif curr_dir == 2: # back turn.put(4) elif curr_dir == 3: # right turn.put(1) elif curr_dir == 4: # left turn.put(1) state = ANALYZE_US eStop(state) yield (state) if state == DONT_ANALYZE_US: if turning.get() == 0: state = ANALYZE_US yield (state) if state == OFF1: if IR.getCommand() == C.START: state = ANALYZE_US yield (state)
def __init__(self): self.sensors = ultrasonic.Ultrasonic() self.value = 20
import helpers import math import numpy as np import ultrasonic #config values WIDTH = 32 * .0254 #M HEIGHT = 28 * .0254 #M #turret position from center of robot TURRET_X = 0 TURRET_Y = 0 ultrasonics = [ultrasonic.Ultrasonic(0, 0, 0)] #Instance values class Robot: ppm = 0 xPos = 12.4 #real world position in m , center of frame yPos = 5.7 #real worl position in m rotation = 180 #These values are information for the AI to work on balls = 3 game_time = 0 #Command values, These are set by the program and are sent to the robot. #The top command is followed first, and some commands will run at the same time
print("Current PWM %d" % (self.current)) def setDutyCycle(self, pwm): self.__change_duty_cycle(pwm) def stop(self): self.pwm.stop() # stop the PWM output OG.cleanup() if __name__ == '__main__': echo = 31 trigger = 29 servo = Servo(12, 39) filter_window = 10 ul = U.Ultrasonic(echo, trigger, None, filter_window=filter_window) #warm up ultra distance = ul.getDistance() wait = 1 try: while True: print("------------------") servo.setDutyCycle(servo.start) t.sleep(wait) servo.setDutyCycle(100) t.sleep(wait) servo.setDutyCycle(0) t.sleep(wait) for i in range(100): val = ran.randint(0, 100) servo.setDutyCycle(i)