def SeekLine(SeekSpeed, SeekSize):
    print("Seeking the line")
    # The direction the robot will turn - True = Left
    Direction = True

    #SeekSize = 0.25 # Turn for 0.25s
    SeekCount = 1  # A count of times the robot has looked for the line
    MaxSeekCount = 3  # The maximum time to seek the line in one direction

    # Turn the robot left and right until it finds the line
    # Or it has been searched for long enough
    while SeekCount <= MaxSeekCount:
        # Set the seek time
        SeekTime = SeekSize * SeekCount

        # Start the motors turning in a direction
        if Direction:
            print("Seeking - Looking left")
            pz.spinRight(SeekSpeed)
            sl.neoPixelLight("left")
            sleep(SeekSize / 50)
        else:
            print("Seeking - Looking Right")
            pz.spinLeft(SeekSpeed)
            sl.neoPixelLight("right")
            sleep(SeekSize / 50)

        # Save the time it is now
        StartTime = time()

        # While the robot is turning for SeekTime seconds,
        # check to see whether the line detector is over black
        while time() - StartTime <= SeekTime:
            if GPIO.input(lineCentre) == 1:
                pz.stop()
                # Exit the SeekLine() function returning
                # True - the line was found
                return True

        # The robot has not found the black line yet, so stop
        pz.stop()
        sl.neoPixelLight("off")

        # Increase the seek count
        SeekCount += 1

        # Change direction
        Direction = not Direction
    # The line wasn't found, so return False
    return False
Exemple #2
0
cam.hflip = True
cam.start_recording("/home/pi/qmpiwars/videos/golf-" + ts + ".h264")

while True:

    buttons = wii.state['buttons']
    #pixelFlash()

    # If Plus and Minus buttons pressed
    # together then rumble and quit.
    if (buttons - cwiid.BTN_PLUS - cwiid.BTN_MINUS == 0):
        print '\nClosing connection ...'
        wii.rumble = 1
        time.sleep(1)
        wii.rumble = 0
        sl.neoPixelLight("off")
        pz.cleanup()
        cam.stop_recording()
        exit(wii)

    # Check if other buttons are pressed by
    # doing a bitwise AND of the buttons number
    # and the predefined constant for that button.
    if (buttons & cwiid.BTN_LEFT):
        print 'Left pressed'
        pz.spinRight(90)
        time.sleep(button_delay)
        sl.neoPixelLight("left")

    if (buttons & cwiid.BTN_RIGHT):
        print 'Right pressed'
cam.hflip = True
cam.start_recording("/home/pi/qmpiwars/videos/straight-" + ts + ".h264")

try:
    while True:
        distance = int(dist.getDistance())
        LeftDist = sideDist.DistSensor(27, 22) #Left
        RightDist = sideDist.DistSensor(17, 18) #Right
        print "----------------------------------"
        print "Front Distance:", distance
        print "Left Distance:", LeftDist
        print "Right Distance:", RightDist
        print "----------------------------------"
        pz.forward(forwardSpeed)
        sleep(forwardTime)
        sideDist.neoPixelLight("forward")
        if(distance <= frontStop): # too close - STOP!
            print "Stop!"
            #pz.stop()
            sideDist.neoPixelLight("off")
        elif(LeftDist <= sideStop): # heading left - turn right
           print "turning right"
           pz.spinLeft(spinSpeed)
           sleep(spinTime)
           sideDist.neoPixelLight("right")
           pz.forward(forwardSpeed)
           sleep(forwardTime)
           sideDist.neoPixelLight("forward")
           if(LeftDist < sideStop): # still too close move forward a bit more
               pz.forward(forwardSpeed)
               sleep(spinTime)