Exemplo n.º 1
1
def main(robot):
    # Définition des moteurs / capteurs
    m_left = Motor(robot, PORT_B)
    m_right = Motor(robot, PORT_A)

    touch_right = Touch(robot, PORT_2)
    touch_left = Touch(robot, PORT_1)
    ultrason = Ultrasonic(robot, PORT_3)

    DEFAULT_POWER = 80
    DISTANCE_LIMIT = 20
    TURN_TIME = 1.5

    # Début
    while True:
        m_left.run(power=DEFAULT_POWER)
        m_right.run(power=DEFAULT_POWER)

        while not touch_right.is_pressed() and not touch_left.is_pressed() and ultrason.get_distance() > DISTANCE_LIMIT:
            sleep(0.01)

        print("Aieee")
        if touch_right.is_pressed():
            m_left.run(power=DEFAULT_POWER)
            m_left.run(power=-DEFAULT_POWER)
        else:
            m_left.run(power=-DEFAULT_POWER)
            m_left.run(power=DEFAULT_POWER)

        sleep(TURN_TIME)
Exemplo n.º 2
0
def main(robot):
    # Définition des moteurs / capteurs
    m_left = Motor(robot, PORT_B)
    m_right = Motor(robot, PORT_A)

    touch_right = Touch(robot, PORT_2)
    touch_left = Touch(robot, PORT_1)
    ultrason = Ultrasonic(robot, PORT_3)

    DEFAULT_POWER = 80
    DISTANCE_LIMIT = 20
    TURN_TIME = 1.5

    # Début
    while True:
        m_left.run(power=DEFAULT_POWER)
        m_right.run(power=DEFAULT_POWER)

        while not touch_right.is_pressed() and not touch_left.is_pressed(
        ) and ultrason.get_distance() > DISTANCE_LIMIT:
            sleep(0.01)

        print("Aieee")
        if touch_right.is_pressed():
            m_left.run(power=DEFAULT_POWER)
            m_left.run(power=-DEFAULT_POWER)
        else:
            m_left.run(power=-DEFAULT_POWER)
            m_left.run(power=DEFAULT_POWER)

        sleep(TURN_TIME)
Exemplo n.º 3
0
 def ultrasonic(self):
     us = Ultrasonic(self.brick, PORT_3)
     return us.get_distance()
Exemplo n.º 4
0

def legPosition():
    touchState = touch.get_input_values().calibrated_value
    if touchState == 829 or touchState == 810:
        return True
    else:
        return False
        
def step(forwardPower = 120):
    walkingMotor.run(forwardPower)
    sleep(.2) # give it time to move off touch sensor
    while not legPosition():
        pass
    walkingMotor.run(0)
    walkingMotor.brake()
    return

outFile = open('compassValueswithMagnet.txt', 'w')
try:
    while True:
        step()
        compassVal = compass.get_distance()
        outFile.write('%f\n' % compassVal)
        print(compassVal)
            
except:
    outFile.close()
    walkingMotor.idle()
    
Exemplo n.º 5
0
def ultrasonic(port):
    u = Ultrasonic(b, SENSORS[port])
    return str(u.get_distance())
Exemplo n.º 6
0
class Dog(object):

    STATE_WALKING = 0
    STATE_SEARCHING = 2
    STATE_RESTING = 1
    
    def __init__(self):
        self.brick = nxt.bluesock.BlueSock('00:16:53:08:51:40').connect()
        self.right = Motor(self.brick, PORT_A)
        self.left = Motor(self.brick, PORT_C)
        self.legs = Legs(self.right, self.left)
        
        #self.ear = Sound(self.brick, PORT_2)
        self.sonic = Ultrasonic(self.brick, PORT_4)

        self.owner_distance = 0

        self.stamina = 200

    @property
    def distance_ahead(self):
        return self.sonic.get_distance()

    def tire(self):
        self.stamina -= 1
        
    def rest(self):
        self.state = self.STATE_RESTING
        print "Resting"

        self.stop()
        
        time.sleep(random.randint(30, 90))

        self.stamina = 200

        return self.walk()

    def walk(self):
        self.state = self.STATE_WALKING
        print "Walking"

        self.legs.walk()

        while True:
            if self.distance_ahead < 40:
                return self.search()

            self.tire()

            if self.stamina == 0:
                return self.rest()

    def search(self):
        self.state = self.STATE_SEARCHING
        print "Searching"
        
        angle = 0.5
        max_angle = 15
        direction = random.choice((-1, 1))

        while angle < max_angle:
            start = time.time()
            while self.distance_ahead > 100 and time.time() < start + angle:
                self.legs.turn(direction)
            self.legs.stop()
            if self.distance_ahead <= 100:
                return self.walk()
            direction *= -1
            angle *= 1.5

            self.tire()

            if self.stamina == 0:
                return self.rest()

        return self.walk()

    def stop(self):
        self.legs.stop()
Exemplo n.º 7
0
            step(100)
        binIdentity = binID() # picks up bin and returns what it is (1 for organic, 2 for ceramic, 3 for metallic)
        hasBin = True
        n = 0 # no bin drop off locations have been found yet
        initialCompass = compass.get_distance()
        compassUpper = initialCompass + compassDelta
        compassLower = initialCompass - compassDelta
        while hasBin:
            if compassLower < compass.get_distance() < compassUpper:
                lineFollow()
            else:
                n += 1 # bin drop off location found
                if n == binIdentity:
                    binDropOff()
                    hasBin = False
                else:
                    for i in range(binDropOffStepBuffer): # don't want to register the same bin drop off location twice, may not be necessary
                        lineFollow()

#main()

while ultrasonic.get_distance() >= 7:
    step(100)
    print(ultrasonic.get_distance())
    sleep(.1)
    
binPickup()

sleep(.5)
binDropOff()
Exemplo n.º 8
0
class Dog(object):

    MIN_OWNER_DISTANCE = 30
    MAX_OWNER_DISTANCE = 40
    LOST_DISTANCE = 100

    def __init__(self):
        self.brick = nxt.bluesock.BlueSock("00:16:53:08:51:40").connect()
        # self.brick = nxt.locator.find_one_brick()
        self.right = Motor(self.brick, PORT_A)
        self.left = Motor(self.brick, PORT_C)
        self.legs = Legs(self.right, self.left)

        # self.ear = Sound(self.brick, PORT_2)
        self.sonic = Ultrasonic(self.brick, PORT_4)

        self.owner_distance = 0

        self.last_distance = 255

    @property
    def distance_ahead(self):
        sample = self.sonic.get_distance()
        if sample == 255:
            self.last_distance += 20
            return self.last_distance
        self.last_distance = sample
        return sample

    def wait(self):
        print "Waiting"

        self.stop()

        while True:
            distance = self.distance_ahead
            if distance > 30 and distance < 40:
                self.owner_distance = distance
                print "owner distance: %d" % distance
                return self.follow()

    def follow(self):
        print "Following"

        while True:
            distance = self.distance_ahead

            if distance > self.owner_distance + 50:
                return self.search()

            if distance > self.owner_distance:
                self.legs.walk(50 + distance - self.owner_distance)
            elif distance < self.owner_distance:
                self.legs.walk_back(50 + self.owner_distance - distance)
            else:
                self.stop()
                time.sleep(0.5)

    def search(self):
        print "Searching"

        angle = 0.5
        max_angle = 64
        direction = random.choice((-1, 1))

        while angle < max_angle:
            start = time.time()
            while self.distance_ahead > 100 and time.time() < start + angle:
                self.legs.turn(direction)
            self.legs.stop()
            if self.distance_ahead <= 100:
                return self.follow()
            direction *= -1
            angle *= 2

        return self.wait()

    def stop(self):
        self.legs.stop()