示例#1
0
 def __init__ (self) :
     TRIG_ECHO = [[5, 0], [24, 25], [1, 7], [13, 6], [20, 21], [9, 10]]
     triggers = [26, 5, 9, 20, 12, 1, 8]
     echos = [0, 10, 21, 16, 7, 25]
     self.ultras = []
     for a in range(len(TRIG_ECHO)) :
         self.ultras.append(sensor.Measurement(TRIG_ECHO[a][0], TRIG_ECHO[a][1]))
示例#2
0
def test():
    value = sensor.Measurement(trig_pin, echo_pin)
    raw = value.raw_distance()

    imperial = value.distance_imperial(raw)

    print imperial
示例#3
0
def main():
    '''Calculate the distance of an object in inches using a HCSR04 sensor
       and a Raspberry Pi'''

    trig_pin = 17
    echo_pin = 27

    # Default values
    # unit = 'metric'
    # temperature = 20
    # round_to = 1

    # Create a distance reading with the hcsr04 sensor module
    # and overide the default values for temp, unit and rounding)
    value = sensor.Measurement(trig_pin,
                               echo_pin,
                               temperature=68,
                               unit='imperial',
                               round_to=2)

    raw_measurement = value.raw_distance()

    # Calculate the distance in inches
    imperial_distance = value.distance_imperial(raw_measurement)
    print("The Distance = {} inches".format(imperial_distance))
示例#4
0
def main():
    """Calculate the distance of an object in centimeters using a HCSR04 sensor
       and a Raspberry Pi. This script allows for a quicker reading by
       decreasing the number of samples and forcing the readings to be
       taken at quicker intervals."""

    trig_pin = "P9_23"
    echo_pin = "P9_25"

    #  Create a distance reading with the hcsr04 sensor module
    value = sensor.Measurement(trig_pin, echo_pin)

    # The default sample_size is 11 and sample_wait is 0.1
    # Increase speed by lowering the sample_size and sample_wait
    # The effect of reducing sample_size is larger variance in readings
    # The effect of lowering sample_wait is higher cpu usage and sensor
    # instability if you push it with too fast of a value.
    # These two options have been added to allow you to tweak a
    # more optimal setting for your application.

    # e.g.
    raw_measurement = value.raw_distance(sample_size=5, sample_wait=0.03)

    # Calculate the distance in centimeters
    print("The Distance = {} centimeters".format(round(raw_measurement, 1)))
示例#5
0
def main():
    '''Calculate the distance of an object in centimeters using a HCSR04 sensor
       and a Raspberry Pi'''

    trig_pin = 17
    echo_pin = 27
    # Default values
    # unit = 'metric'
    # temperature = 20
    # round_to = 1

    #  Create a distance reading with the hcsr04 sensor module
    value = sensor.Measurement(trig_pin, echo_pin)
    raw_measurement = value.raw_distance()

    # To overide default values you can pass the following to value
    # value = sensor.Measurement(trig_pin,
    #                            echo_pin,
    #                            temperature=10,
    #                            round_to=2
    #                            )


    # Calculate the distance in centimeters
    metric_distance = value.distance_metric(raw_measurement)
    print("The Distance = {} centimeters".format(metric_distance))
示例#6
0
def main():
    '''Example script using hcsr04sensor module for Raspberry Pi'''
    trig_pin = 17
    echo_pin = 27
    hole_depth = 31.5  # inches

    # Default values
    # unit = 'metric'
    # temperature = 20
    # round_to = 1

    # Create a distance reading with the hcsr04 sensor module
    # and overide the default values for temp, unit and rounding)
    value = sensor.Measurement(trig_pin,
                               echo_pin,
                               temperature=68,
                               unit='imperial',
                               round_to=2
                               )
    raw_measurement = value.raw_distance()

    # Calculate the liquid depth, in inches, of a hole filled
    # with liquid
    liquid_depth = value.depth_imperial(raw_measurement, hole_depth)
    print("Depth = {} inches".format(liquid_depth))
示例#7
0
def main():
    '''Calculate the depth of a liquid in centimeters using a HCSR04 sensor
       and a Raspberry Pi'''

    trig_pin = 17
    echo_pin = 27

    # Default values
    # unit = 'metric'
    # temperature = 20
    # round_to = 1

    hole_depth = 80  # centimeters

    # Create a distance reading with the hcsr04 sensor module

    value = sensor.Measurement(trig_pin, echo_pin)

    raw_measurement = value.raw_distance()

    # To overide default values you can pass the following to value
    # value = sensor.Measurement(trig_pin,
    #                            echo_pin,
    #                            temperature=10,
    #                            round_to=2
    #                            )

    # Calculate the liquid depth, in centimeters, of a hole filled
    # with liquid
    liquid_depth = value.depth_metric(raw_measurement, hole_depth)
    print("Depth = {} centimeters".format(liquid_depth))
示例#8
0
文件: echo_2.py 项目: EchoFUN/raspi
def distance():
    value = sensor.Measurement(17, 4)
    raw_measurement = value.raw_distance()

    # Calculate the distance in centimeters
    metric_distance = value.distance_metric(raw_measurement)

    print("The Distance = {} centimeters".format(metric_distance))
示例#9
0
def d1():
    sr04 = sensor.Measurement(GPIO_TRIGGER, GPIO_ECHO)
#    print(sr04)
    raw_measurement = sr04.raw_distance()
#    print(raw_measurement)
    distance = sr04.distance_metric(raw_measurement)
#    print(distance)
    print('distance is {:.1f} cm'.format(distance))
    #time.sleep(1)
    return distance
示例#10
0
def main():
    trig_pin = 12
    echo_pin = 18
    
    value = sensor.Measurement(trig_pin, echo_pin, gpio_mode=GPIO.BOARD)
    raw_measurement = value.raw_distance()
    
    metric_distance = value.distance_metric(raw_measurement)
    
    return metric_distance
示例#11
0
def get_distance(TRIGGER_PIN, ECHO_PIN):
    #try:
        #print('按下 Ctrl-C 可以中斷程式')
        #while True:
    sr04 = sensor.Measurement(TRIGGER_PIN, ECHO_PIN)
    raw_measurement = sr04.raw_distance()
    distance = sr04.distance_metric(raw_measurement)
    #print(type(distance))
    #print('距離為 {:.1f} 公分'.format(distance))
    time.sleep(0.01)
    return distance
示例#12
0
def water_lev(trig,echo):
	logger.debug('trig pin = gpio {}'.format(trig))
	logger.debug('echo pin = gpio {}'.format(echo))
	logger.debug('speed = {}'.format(speed))
	logger.debug('samples = {}'.format(samples))
	logger.debug('')
	value = sensor.Measurement(trig, echo)
	raw_distance = value.raw_distance(sample_size=samples, sample_wait=speed)
	imperial_distance = value.distance_imperial(raw_distance)
	metric_distance = value.distance_metric(raw_distance)
	logger.debug('The imperial distance is {} inches.'.format(imperial_distance))
	logger.debug('The metric distance is {} centimetres.'.format(metric_distance))
	return metric_distance
示例#13
0
    def distance(self):
        """
        Gets distance in Centimeter and returns it.
        :return: Float
        """
        #  Create a distance reading with the hcsr04 sensor module
        value = sensor.Measurement(self._trig_pin, self._echo_pin,
                                   self._temperature, self._unit,
                                   self._round_to)
        raw_measurement = value.raw_distance()

        # Calculate and return the distance in centimeters
        return value.distance_metric(raw_measurement)
示例#14
0
def main():
    '''Calculate the distance of an object in centimeters using a HCSR04 sensor
       and a Raspberry Pi
     '''

    trig_pin = 24
    echo_pin = 23
    value = sensor.Measurement(trig_pin, echo_pin)

    while (1):
        raw_measurement = value.raw_distance(sample_size=1, sample_wait=0.001)
        metric_distance = value.distance_metric(raw_measurement)
        if (metric_distance <= 200):
            print("The Distance = {} centimeters".format(metric_distance))
示例#15
0
def main():
    i = 0
    value = sensor.Measurement(trig_pin, echo_pin, unit='imperial', round_to=2)
    while (i < 500):
        raw = value.raw_distance(sample_size=1, sample_wait=0.01)

        imperial = value.distance_imperial(raw)
        print imperial is int
        print imperial < 4.00
        print imperial
        if (imperial < 4):
            print "ballz"
            break
        i += 1
示例#16
0
文件: main.py 项目: gorizond/hcsr04
async def send_to_server(ws):
    value = sensor.Measurement(int(os.getenv('TRIG_PIN', 23)),
                               int(os.getenv('ECHO_PIN', 24)))
    result = 0
    sample_size = 3
    skip_items = 50
    skipped_items = 0
    while True:
        await asyncio.sleep(0.03)
        try:
            send = False
            new_result = value.distance_metric(
                value.raw_distance(sample_size=sample_size, sample_wait=0.03))
            sample_size = 3
            send_result = new_result
            if new_result < 50:
                send_result = round(new_result)
            if new_result >= 50:
                send_result = 50
            if new_result >= 100:
                sample_size = 4
                send_result = 100
            if new_result >= 150:
                sample_size = 5
                send_result = 150
            if new_result >= 200:
                send_result = 200
            if new_result >= 250:
                send_result = 250
            if new_result >= 300:
                send_result = 300
            if new_result >= 365:
                send_result = 365
            if result != send_result:
                result = send_result
                send = True
            else:
                skipped_items += 1
                if skipped_items > skip_items:
                    print(f'{skip_items} skipped items!')
                    send = True
            if send:
                result = send_result
                skipped_items = 0
                await ws.send_str(str(result))
            else:
                sample_size = 5
        except Exception as e:
            print(e)
示例#17
0
def main():
    '''Example script using hcsr04sensor module for Raspberry Pi'''
    trig_pin = 27
    echo_pin = 22
    unit = 'metric'  # choices (metric or imperial)
    temperature = 20  # Celcius for metric, Fahrenheit for imperial
    round_to = 1  # report a cleaner rounded output.

    #  Create a distance reading with the hcsr04 sensor module
    value = sensor.Measurement(trig_pin, echo_pin, temperature, unit, round_to)
    raw_measurement = value.raw_distance()

    # Calculate the distance in centimeters
    metric_distance = value.distance_metric(raw_measurement)
    print("The Distance = {} centimeters".format(metric_distance))
示例#18
0
def main():
    '''Example script using hcsr04sensor module for Raspberry Pi'''
    trig_pin = 27  #17
    echo_pin = 22  #27
    unit = 'metric'  # choices (metric or imperial)
    temperature = 68  # Fahrenheit
    round_to = 1

    #  Create a distance reading with the hcsr04 sensor module
    value = sensor.Measurement(trig_pin, echo_pin, temperature, unit, round_to)
    raw_measurement = value.raw_distance()

    # Calculate the distance in inches
    imperial_distance = value.distance_imperial(raw_measurement)
    print("The Distance = {} cms".format(imperial_distance))
示例#19
0
def main():
    '''Calculate the distance of an object in centimeters using a HCSR04 sensor
       and a Raspberry Pi'''

    trig_pin = 14
    echo_pin = 15

    #  Create a distance reading with the hcsr04 sensor module
    value = sensor.Measurement(trig_pin, echo_pin)
    while (True):
        raw_measurement = value.raw_distance()

        # Calculate the distance in centimeters
        metric_distance = value.distance_metric(raw_measurement)
        print("The Distance = {} centimeters".format(metric_distance))
示例#20
0
    def get_distance(self):
        log.info("DS - Measuring distance...")

        distance = None

        if cfg.IF_IN_RPI:
            value = sensor.Measurement(self.trig_pin, self.echo_pin)
            raw_distance = value.raw_distance()
            distance = value.distance(raw_distance)
        else:
            distance = random.randint(0, 500)

        log.info(
            "DS - Distance: {distance} cm measured".format(distance=distance))
        return distance
示例#21
0
def water_reading():
    '''Initiate a water level reading.'''
    pit_depth = configs['pit_depth']
    trig_pin = configs['trig_pin']
    echo_pin = configs['echo_pin']
    round_to = 1
    temperature = configs['temperature']
    unit = configs['unit']

    value = sensor.Measurement(trig_pin, echo_pin, temperature, unit, round_to)
    raw_distance = value.raw_distance(sample_wait=0.3)

    if unit == 'imperial':
        return value.depth_imperial(raw_distance, pit_depth)
    if unit == 'metric':
        return value.depth_metric(raw_distance, pit_depth)
示例#22
0
def main():
    '''Main function to run the sensor with passed arguments'''

    trig, echo, speed, samples = get_args()
    print('trig pin = gpio {}'.format(trig))
    print('echo pin = gpio {}'.format(echo))
    print('speed = {}'.format(speed))
    print('samples = {}'.format(samples))
    print('')

    value = sensor.Measurement(trig, echo)
    raw_distance = value.raw_distance(sample_size=samples, sample_wait=speed)

    imperial_distance = value.distance_imperial(raw_distance)
    metric_distance = value.distance_metric(raw_distance)
    print('The imperial distance is {} inches.'.format(imperial_distance))
    print('The metric distance is {} centimetres.'.format(metric_distance))
示例#23
0
 def __init__(self, config):
     self._prev_presence = None
     self._prev_time = time.time()
     self._config = config
     self._working = False
     # Mqtt
     self._mqtt = Mqtt(config.get('mqtt_host'), config.get('topic_prefix'))
     self._mqtt.on_message = self.on_message
     self._mqtt.start(False)
     # Distance sensor
     self._sensor = sensor.Measurement(
         self._config.get('trigger_pin'),
         self._config.get('echo_pin'),
         temperature=self._config.get('sensor_temperature'),
         unit=self._config.get('unit_system'),
         round_to=2)
     _LOGGER.debug('Worker initialized')
示例#24
0
    def _get_distance(self):
        round_to = 1
        temperature = self._tempsensor.current_value
        self._error_count = 0
        value = sensor.Measurement(self.trig_pin, self.echo_pin, temperature,
                                   'metric', round_to)
        raw_distance = value.raw_distance()

        # If tank depth is defined then give a water depth
        if self.tank_depth is not None:
            water_depth = value.depth_metric(raw_distance, self.tank_depth)
            if water_depth < 0:
                water_depth = 0.0
            self._current_dist = water_depth
        else:
            # otherwise give a distance to water surface
            self._current_dist = raw_distance
def main():
    """Calculate the distance of an object in centimeters using a HCSR04 sensor
       and a Raspberry Pi"""
    # Use GPIO.BOARD values instead of BCM
    trig_pin = "P9_23"
    echo_pin = "P9_25"
    # Default values
    # unit = 'metric'
    # temperature = 20

    # Create a distance reading with the hcsr04 sensor module
    # using GPIO.BOARD pin values.
    value = sensor.Measurement(trig_pin, echo_pin)
    raw_measurement = value.raw_distance()

    # Calculate the distance in centimeters
    print("The Distance = {} centimeters".format(round(raw_measurement, 1)))
示例#26
0
def main():
    '''Example script using hcsr04sensor module for Raspberry Pi'''
    trig_pin = 17
    echo_pin = 27
    unit = 'metric'  # choices (metric or imperial)
    temperature = 20  # Celcius for metric, Fahrenheit for imperial
    round_to = 1  # report a cleaner rounded output.
    hole_depth = 80  # centimeters

    # Create a distance reading with the hcsr04 sensor module
    value = sensor.Measurement(trig_pin, echo_pin, temperature, unit, round_to)
    raw_measurement = value.raw_distance()

    # Calculate the liquid depth, in centimeters, of a hole filled
    # with liquid
    liquid_depth = value.depth_metric(raw_measurement, hole_depth)
    print("Depth = {} centimeters".format(liquid_depth))
示例#27
0
def main():
    '''Example script using hcsr04sensor module for Raspberry Pi'''
    trig_pin = 17
    echo_pin = 27
    unit = 'imperial'  # choices (metric or imperial)
    temperature = 68  # Fahrenheit
    round_to = 1
    hole_depth = 31.5  # inches

    #  Create a distance reading with the hcsr04 sensor module
    value = sensor.Measurement(trig_pin, echo_pin, temperature, unit, round_to)
    raw_measurement = value.raw_distance()

    # Calculate the liquid depth, in inches, of a hole filled
    # with liquid
    liquid_depth = value.depth_imperial(raw_measurement, hole_depth)
    print("Depth = {} inches".format(liquid_depth))
示例#28
0
def main():
    """Main function to run the sensor with passed arguments"""

    trig, echo, speed, samples = get_args()
    print("trig pin = gpio {}".format(trig))
    print("echo pin = gpio {}".format(echo))
    print("speed = {}".format(speed))
    print("samples = {}".format(samples))
    print("")

    value = sensor.Measurement(trig, echo)
    raw_distance = value.raw_distance(sample_size=samples, sample_wait=speed)

    imperial_distance = value.distance_imperial(raw_distance)
    metric_distance = value.distance_metric(raw_distance)
    print("The imperial distance is {} inches.".format(round(imperial_distance, 1)))
    print("The metric distance is {} centimetres.".format(round(metric_distance, 1)))
示例#29
0
def water_reading():
    """Initiate a water level reading."""
    pit_depth = configs["pit_depth"]
    trig_pin = configs["trig_pin"]
    echo_pin = configs["echo_pin"]
    temperature = configs["temperature"]
    unit = configs["unit"]

    value = sensor.Measurement(trig_pin, echo_pin, temperature, unit)

    try:
        raw_distance = value.raw_distance(sample_wait=0.3)
    except SystemError:
        log.log_errors(
            "**ERROR - Signal not received. Possible cable or sensor problem.")
        exit(0)

    return round(value.depth(raw_distance, pit_depth), 1)
示例#30
0
def main():
    '''Calculate the distance of an object in centimeters using a HCSR04 sensor
       and a Raspberry Pi'''
    # Use GPIO.BOARD values instead of BCM
    trig_pin = 11
    echo_pin = 13
    # Default values
    # unit = 'metric'
    # temperature = 20
    # round_to = 1

    # Create a distance reading with the hcsr04 sensor module
    # using GPIO.BOARD pin values.
    value = sensor.Measurement(trig_pin, echo_pin, gpio_mode=GPIO.BOARD)
    raw_measurement = value.raw_distance()

    # Calculate the distance in centimeters
    metric_distance = value.distance_metric(raw_measurement)
    print("The Distance = {} centimeters".format(metric_distance))