Пример #1
0
def track_object(distance_stay, distance_range, speed_adj=0.4):
    # distance_stay,distance_range in inches
    #Tracking with Ultrasonic
    motor.setup()
    led.setup()
    turn.ahead()
    turn.middle()
    dis = checkdist_inches()
    if dis < distance_range:  #Check if the target is in distance range
        if dis > (
                distance_stay + 4
        ):  #If the target is in distance range and out of distance stay, then move forward to track
            turn.ahead()
            moving_time = (dis - distance_stay) / 0.38
            if moving_time > 1:
                moving_time = 1
            print('mf')
            led.both_off()
            led.cyan()
            motor.motor_left(status, backward,
                             int(left_spd * spd_ad_u * speed_adj))
            motor.motor_right(status, forward,
                              int(right_spd * spd_ad_u * speed_adj))
            time.sleep(moving_time)
            motor.motorStop()
        elif dis < (
                distance_stay - 4
        ):  #Check if the target is too close, if so, the car move back to keep distance at distance_stay
            moving_time = (distance_stay - dis) / 0.38
            print('mb')
            led.both_off()
            led.pink()
            motor.motor_left(status, forward,
                             int(left_spd * spd_ad_u * spd_adj))
            motor.motor_right(status, backward,
                              int(right_spd * spd_ad_u * spd_adj))
            time.sleep(moving_time)
            motor.motorStop()
        else:  #If the target is at distance, then the car stay still
            motor.motorStop()
            led.both_off()
            led.yellow()
    else:
        motor.motorStop()
Пример #2
0
def test_led():    
    led.setup()
    print("both_on()")
    led.both_on()
    time.sleep(3)
    
    print("red()")
    led.red()
    time.sleep(3)
    
    print("both_off()")
    led.both_off()
    time.sleep(1)
    
    print("yellow()")
    led.yellow()
    time.sleep(3)
    
    print("pink()")
    led.pink()
    time.sleep(3)
    
    print("both_off()")
    led.both_off()
    time.sleep(1)
    
    print("cyan()")
    led.cyan()
    time.sleep(3)
    
    # Flashing in 2 cycles
    print("police_on(2)")
    led.police_on(2)
    
    print("both_off()")
    led.both_off()
    time.sleep(3)

    # Flashing in 6 seconds
    print("police_on()")    
    led.police_on()
    time.sleep(6) #6 seconds
    led.police_off()
Пример #3
0
def loop(distance_stay,distance_range):
    '''Tracking with Ultrasonic'''
    motor.setup()
    led.setup()
    turn.ahead()
    turn.middle()
    dis = checkdist()
    if dis < distance_range:
        #Check if the target is in diatance range
        if dis > (distance_stay+0.1) :
            #If the target is in distance range and out of distance stay,
            #then move forward to track
            turn.ahead()
            moving_time = (dis-distance_stay)/0.38 #??? magic number
            if moving_time > 1:
                moving_time = 1
            print('mf')
            led.both_off()
            led.cyan()
            motor.motor_left(status, backward,left_spd*spd_ad_u)
            motor.motor_right(status,forward,right_spd*spd_ad_u)
            time.sleep(moving_time)
            motor.motorStop()
        elif dis < (distance_stay-0.1) :
            #Check if the target is too close,
            #if so, the car move back to keep distance at distance_stay
            moving_time = (distance_stay-dis)/0.38
            print('mb')
            led.both_off()
            led.pink()
            motor.motor_left(status, forward,left_spd*spd_ad_u)
            motor.motor_right(status,backward,right_spd*spd_ad_u)
            time.sleep(moving_time)
            motor.motorStop()
        else:
            #If the target is at distance, then the car stay still
            motor.motorStop()
            led.both_off()
            led.yellow()
    else:
        motor.motorStop()