Python machine 模块,time_pulse_us() 实例源码

我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用machine.time_pulse_us()

项目:MicroPython    作者:Euter2    | 项目源码 | 文件源码
def distance(self):
        """Measure pulse length and return calculated distance [m]."""
        self._pulse()
        pulse_width_s = time_pulse_us(self._echo, Pin.high) / 1000000
        dist_m = (pulse_width_s / 2) * self._sound_speed
        return dist_m
项目:ulnoiot    作者:ulno    | 项目源码 | 文件源码
def _send_pulse_and_wait(self):
        # Send the pulse to trigger and listen on echo pin.
        # We use the method `machine.time_pulse_us()` to
        # get the microseconds until the echo is received.
        self.trigger_pin.value(0)  # Stabilize the sensor
        sleep_us(5)
        self.trigger_pin.on()
        # Send a 10us pulse.
        sleep_us(10)
        self.trigger_pin.off()
        # try:
        #     pulse_time = machine.time_pulse_us(self.echo_pin, 1, self.echo_timeout_us)
        #     return pulse_time
        # except OSError as ex:
        #     if ex.args[0] == 110: # 110 = ETIMEDOUT
        #         return -1 # out of range
        #     raise ex

        start = ticks_us()
        while not self.echo_pin():
            t = ticks_us()
            if ticks_diff(t, start) > self.echo_timeout_us:
                print("HCR04: timeout")
                return -1
        start = ticks_us()
        while self.echo_pin():
            t = ticks_us()
            if ticks_diff(t, start) > self.echo_timeout_us:
                print("HCR04: timeout")
                return -1
        delta = ticks_diff(ticks_us(), start)
        return delta
项目:micropython-dev-kit    作者:db4linq    | 项目源码 | 文件源码
def distance(self):
        """Measure pulse length and return calculated distance [m]."""
        self._pulse()
        pulse_width_s = time_pulse_us(self._echo, 1) / 1000000
        dist_m = (pulse_width_s / 2) * self._sound_speed
        return dist_m
项目:esp    作者:mhoffma    | 项目源码 | 文件源码
def ping0(self):
        self.trig.high()
        time.sleep_us(10)
        self.trig.low()
        d= machine.time_pulse_us(self.echo,1)
        return d
项目:esp-garage    作者:craftyguy    | 项目源码 | 文件源码
def check_door_state(lock):
    global umqtt_client
    global mqtt_connected
    global door_opened

    echo = machine.Pin(ECHO_PIN, machine.Pin.IN)
    trig = machine.Pin(TRIG_PIN, machine.Pin.OUT)
    distance = 0.0
    state = 'closed'
    await asyncio.sleep(5)

    while True:
        # HC-SR04 is triggered by a 10us pulse on 'trig' pin
        # Set trigger pin to low to make sure there's a good rise when set high
        trig.value(0)
        utime.sleep_us(100)
        # Toggle trigger pin for 10us
        trig.value(1)
        utime.sleep_us(10)
        trig.value(0)
        # echo pin will be high for a duration (is us) indicating distance.
        try:
            pulse_width = machine.time_pulse_us(echo, 1)
        except:
            # Something weird happened
            print("Unable to time echo pulse")
            pass
        # If no pulse measured before timeout or no drop at end of pulse,
        # then width is < 0
        if pulse_width > 0:
            # Hint: 29 = cm / us for sound, divided by 2 since we want half
            # the distance measured
            distance_cm = ((pulse_width / 2) / SPEED_OF_SOUND )
            print("Measured distance of %f cm" % distance_cm)
            # DOOR_OPEN_DISTANCE is in meters
            if (distance_cm / 100.0) <= DOOR_OPEN_DISTANCE:
                door_state = b'open'
                door_opened = True
            else:
                door_state = b'closed'
                door_opened = False
            # publish state
            async with lock:
                if mqtt_connected:
                    try:
                        umqtt_client.publish(STATUS_TOPIC, door_state)
                    except:
                        loop.create_task(reconnect(lock))
        else:
            print("Unable to get distance.")
        await asyncio.sleep(CHECK_DOOR_INTERVAL * 60)


#  sub_cb: callback for mqtt client subscriptions