Ejemplo n.º 1
0
def main():
    '''
    Makes a LED to blink according to the distance meassured
    by an ultrasonic sensor.
    Finish when user press a toggle key.
    '''

    blinker = Blinker(27, MAX_DELAY)  #P8.17
    ultrasonic = Ultrasonic(66, 69)  #P8.7, P8.9
    key = Gpio(65, Gpio.IN)  #P8.18

    try:

        print("Ready. Press toggle key to start.")

        #Wait for key down event
        while key.getValue() == Gpio.LOW:

            sleep(0.2)

        #Wait for key up event
        while key.getValue() == Gpio.HIGH:

            sleep(0.2)

        print("Started. Press toggle key again to finish.")
        blinker.start()

        while key.getValue() == Gpio.LOW:
            dist = ultrasonic.read()
            delay = calculateDelay(dist)
            blinker.setDelay(delay)
            sleep(0.2)

        print("Bye!")

    finally:
        key.cleanup()
        blinker.stop()
        blinker.cleanup()
        ultrasonic.cleanup()
Ejemplo n.º 2
0
from time import sleep

led = Gpio(27, Gpio.OUT) #P8.17
key = Gpio(65, Gpio.IN) #P8.18

try:

    light = Gpio.LOW
    led.setValue(light)
    
    lastKeyState = Gpio.LOW
    print("Ready.\nPress toggle key to change led state or Ctrl+C to exit.")

    while True:
    
        keyState = key.getValue()
        if lastKeyState != keyState and keyState == Gpio.HIGH:
            light = not light
            led.setValue(light)
            print("Led state changed to {0}.".format("ON" if light else "OFF"))
            
        lastKeyState = keyState
            
        sleep(0.2)

except KeyboardInterrupt:

    print("\nBye!")

finally:
    led.setValue(Gpio.LOW)
Ejemplo n.º 3
0
class Ultrasonic(object):

    PULSE2CM = 17241.3793  # cm/s
    MAX_RANGE = 3500  # cm
    OUT_OF_RANGE = 0xffffffff

    def __init__(self, triggerPort, echoPort, nSamples=5):
        '''
        Constructor
        @param triggerPort: Port number of the trigger signal
        @param echoPort: Port number of the echo port
        @param nSamples: Number of samples to measure the distance
        '''

        self._nSamples = nSamples

        #Configure ports
        self._trigger = Gpio(triggerPort, Gpio.OUT)
        self._trigger.setValue(Gpio.LOW)

        self._echo = Gpio(echoPort, Gpio.IN)

        time.sleep(2)

    def read(self):
        '''
        Measures distance
        @return: Distance as centimeters
        '''

        MAX_POLL = 1000
        i = 0
        dist = 0

        while i < self._nSamples and dist != Ultrasonic.OUT_OF_RANGE:
            self._trigger.setValue(Gpio.HIGH)
            time.sleep(0.001)
            self._trigger.setValue(Gpio.LOW)

            #TODO: use system's poll mechanism or event to wait for GPIO-level change
            nPoll = 0
            while nPoll < MAX_POLL and self._echo.getValue() == Gpio.LOW:
                nPoll += 1

            if nPoll == MAX_POLL:
                raise ReadGpioException(
                    "Max poll reached: waiting for echo HIGH")

            pulseStart = time.time()

            nPoll = 0
            while nPoll < MAX_POLL and self._echo.getValue() == Gpio.HIGH:
                nPoll += 1

            if nPoll == MAX_POLL:
                raise ReadGpioException(
                    "Max poll reached: waiting for echo LOW")

            pulseEnd = time.time()

            pulseDuration = pulseEnd - pulseStart
            distSample = round(pulseDuration * Ultrasonic.PULSE2CM, 0)  #cm

            if distSample < Ultrasonic.MAX_RANGE:
                dist = (dist + distSample) / 2.0 if i != 0 else distSample
            else:
                dist = Ultrasonic.OUT_OF_RANGE

            i += 1

        return dist

    def cleanup(self):
        '''
        Frees ressources
        '''

        self._trigger.cleanup()
        self._echo.cleanup()