def __init__(self, connected_brick): self._brick = connected_brick self.left_motor = ev3.Motor(connected_brick, ev3.MOTOR_PORTS.PORT_D) self.right_motor = ev3.Motor(connected_brick, ev3.MOTOR_PORTS.PORT_A) self.ultrasonic = ev3.EV3UltrasonicSensor(connected_brick, ev3.SENSOR_PORTS.PORT_1).get_distance_mode()
# -*- coding: utf-8 -*- import time import ev3 import threading print "starting" time.sleep(1000) # brick = ev3.connect_to_brick(address='10.0.1.1') brick = ev3.connect_to_brick('00:16:53:3D:E4:77') ultrasonic = ev3.EV3UltrasonicSensor(brick, 1) distance = ultrasonic.get_selected_mode() print "opened ultrasonic" def time_that(iterations): time_taken = 0.0 for _ in range(iterations + 1): start = time.time() sample = distance.fetch_sample() end = time.time() time_taken += end - start print(end - start, sample) print "time taken", time_taken print "avg time", time_taken / iterations print "Starting timer" time_that(100000)
# -*- coding: utf-8 -*- import time from behaviors.fuzzy import * import ev3 MIN_BLOCKING_DISTANCE = 0.20 MAX_BLOCKING_DISTANCE = 0.50 BRICK = ev3.connect_to_brick('10.0.1.1') distance = ev3.EV3UltrasonicSensor(BRICK, 1).get_distance_mode() class Cache(object): def __init__(self): self.value = 0 self.counter = 0 def get_distance(self): if self.counter <= 0: self.value = distance.fetch_sample()[0] self.counter = 2 self.counter -= 1 if self.value <= -1: # means infinity, setting it to a high value self.value = 1 return self.value cache = Cache()