Example #1
0
class DistanceSensor(object):

    def __init__(self, pID, pSensor):
        '''
        Constructor for new DistanceSensor
        @param pID: string the id of the IR-Sensor-Module (check with brickv)
        @param pSensor: the Sensors-Object to attach to.
        '''
        self.__id = pID
        self.__dist = DistanceIR(self.__id)
        pSensor.add(self.__dist)
        
    def getDistance(self):
        '''
        Return the actual distance, in mm
        @return int: Distance in mm
        '''
        return self.__dist.get_distance()
    
    def setDistanceCallback(self, pDebouncePeriod, pCallbackFunction, pOperator, pMin, pMax):
        '''
        
        @param pDebouncePeriod: int
        @param pCallbackFunction: function
        @param pOperator: string
        @param pMin: int
        @param pMax: int
        '''
        self.__dist.set_debounce_period(int(pDebouncePeriod))
        
        self.__dist.register_callback(self.__dist.CALLBACK_DISTANCE_REACHED, pCallbackFunction)
        self.__dist.set_distance_callback_threshold(pOperator, int(pMin), int(pMax))
Example #2
0
 def __init__(self, pID, pSensor):
     '''
     Constructor for new DistanceSensor
     @param pID: string the id of the IR-Sensor-Module (check with brickv)
     @param pSensor: the Sensors-Object to attach to.
     '''
     self.__id = pID
     self.__dist = DistanceIR(self.__id)
     pSensor.add(self.__dist)
#!/usr/bin/env python
# -*- coding: utf-8 -*-

HOST = "192.168.178.91"
PORT = 4223
UID = "gy2" # Change to your UID

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_distance_ir import DistanceIR

if __name__ == "__main__":
    ipcon = IPConnection() # Create IP connection
    dir = DistanceIR(UID, ipcon) # Create device object

    ipcon.connect(HOST, PORT) # Connect to brickd
    # Don't use device before ipcon is connected

    fails = 0

    while True:
        try:
            c = 0
            while True:
                c += 1
                if c == 256:
                    c = 0
                value = dir.get_sampling_point(c)
                if value != c:
                    fails += 1
                print('value: ' + str(c) + ' -> ' + str(value)) + ' -> ' + str(fails)
        except: