Exemplo n.º 1
0
def ConnectToNxt():
    brick = nxt.bluesock.BlueSock('00:16:53:04:17:F1').connect()

    motor_right = Motor(brick, PORT_A)
    motor_left = Motor(brick, PORT_C)
    ultrasonic_front = Ultrasonic(brick, PORT_2)
    ultrasonic_left = Ultrasonic(brick, PORT_1)
    ultrasonic_right = Ultrasonic(brick, PORT_3)

    return motor_right, motor_left, ultrasonic_front, ultrasonic_left, ultrasonic_right
Exemplo n.º 2
0
def main():
    b = find_one_brick()
    eyes = Motor(b, PORT_A)
    wheels = [Motor(b, PORT_B), Motor(b, PORT_C)]
    ultrasonic = Ultrasonic(b, PORT_4)

    phizic(eyes, wheels, ultrasonic)
Exemplo n.º 3
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.º 4
0
 def initialize(self):
     super(DrivarNxt, self).initialize()
     self.m_block = nxt.locator.find_one_brick()
     self.m_leftMotor = Motor(self.m_block, PORT_A)
     self.m_rightMotor = Motor(self.m_block, PORT_C)
     self.m_ultrasonicSensor = Ultrasonic(self.m_block, PORT_4)
     self.m_initialized = True
Exemplo n.º 5
0
 def initialize(self):
     super(DrivarNxt, self).initialize()
     self.block = nxt.locator.find_one_brick()
     self.leftMotor = Motor(self.block, PORT_A)
     self.rightMotor = Motor(self.block, PORT_C)
     self.penMotor = Motor(self.block, PORT_B)
     self.ultrasonicSensor = Ultrasonic(self.block, PORT_4)
     self.lightSensor = Light(self.block, PORT_3)
     self.initialized = True
Exemplo n.º 6
0
 def getCompass(self, port):
     if self._bricks:
         try:
             port = int(port)
         except:
             pass
         if (port in NXT_SENSOR_PORTS):
             try:
                 port_aux = NXT_SENSOR_PORTS[port]
                 sensor = Ultrasonic(self._bricks[self.active_nxt], port_aux)
                 return sensor.get_sample()
             except:
                 return ERROR
         else:
             raise logoerror(ERROR_PORT_S % port)
     else:
         raise logoerror(ERROR_BRICK)
Exemplo n.º 7
0
    def __init__(self, brick='NXT'):
        r'''Creates a new Alpha Rex controller.

            brick
                Either an nxt.brick.Brick object, or an NXT brick's name as a
                string. If omitted, a Brick named 'NXT' is looked up.
        '''
        if isinstance(brick, basestring):
            brick = find_one_brick(name=brick)

        self.brick = brick
        self.arms = Motor(brick, PORT_C)
        self.left = Motor(brick, PORT_A)
        self.right = Motor(brick, PORT_B)

        self.direction = HTCompass(brick, PORT_2)
        self.ultrasonic = Ultrasonic(brick, PORT_4)
Exemplo n.º 8
0
    def __init__(self, brick='NXT'):
        r'''Creates a new Alpha Rex controller.
        
            brick
                Either an nxt.brick.Brick object, or an NXT brick's name as a
                string. If omitted, a Brick named 'NXT' is looked up.
        '''
        if isinstance(brick, str):
            brick = find_one_brick(name=brick)

        self.brick = brick
        self.arms = Motor(brick, PORT_A)
        self.legs = [Motor(brick, PORT_B), Motor(brick, PORT_C)]

        self.touch = Touch(brick, PORT_1)
        self.sound = Sound(brick, PORT_2)
        self.light = Light(brick, PORT_3)
        self.ultrasonic = Ultrasonic(brick, PORT_4)
Exemplo n.º 9
0
 def see(self, ultrasonicPort='PORT_4', ultrasonicDistance=25, operator='<', initial=False):
     """
     Use the ultrasonic sensor to see obstacles (0-255) with 25 being about a foot away
     
     ultrasonicPort (str): The port used for the ultrasonic sensor (default='PORT_4')
     ultrasonicDistance (int): The distance that the ultrasonic returns true [0-255, 25~1foot] (default=25)
     operator (str): The operator for comparing distance ['<','>','=','!='] (default='<')
     """
     
     ultrasonic=Ultrasonic(self.nxt.brick, eval(ultrasonicPort))
     if initial:
         return False                    #don't return true until actually checked sensor value
     else:
         data = ultrasonic.get_sample()  #integer between 0 and 255
         output = operation(data,operator,ultrasonicDistance)
         if output:
             print 'Ultrasonic Distsance is '+operator+' '+str(ultrasonicDistance)
         return output
Exemplo n.º 10
0
 def __init__(self, brick="NXT"):
     if isinstance(brick, basestring):
         brick = find_one_brick(name=brick)
     self.motor = True
     self.sensor = True
     try:
         self.brick = brick
     except:
         print("No brick found")
     try:
         self.wheels = [Motor(brick, PORT_A), Motor(brick, PORT_B)]
     except:
         self.motor = False
         print("No motors detected!")
     try:
         self.bat_eyes = Ultrasonic(brick, PORT_1)
     except Exception as e:
         self.sensor = False
         print(e)
         print("No sensor found")
Exemplo n.º 11
0
 def getDistance(self, port):
     if self._bricks:
         time.sleep(0.5)
         try:
             port = int(port)
         except:
             pass
         if (port in NXT_SENSOR_PORTS):
             res = ERROR
             #try:
             port_aux = NXT_SENSOR_PORTS[port]
             sensor = Ultrasonic(self._bricks[self.active_nxt], port_aux)
             res = sensor.get_sample()
             #except:
             #pass
             return res
         else:
             pass
     else:
         pass
Exemplo n.º 12
0
def sonar_reading():
    x = [Ultrasonic(brick, PORT_1).get_sample(), 0]
    return x
Exemplo n.º 13
0
def ultrasonic(port):
    u = Ultrasonic(b, SENSORS[port])
    return str(u.get_distance())
Exemplo n.º 14
0
    brick = nxtConnect.btConnect(brickName)
    
print(brick.get_device_info()) # check what brick you connected to
from time import sleep

from nxt.motor import Motor, PORT_A, PORT_B, PORT_C
from nxt.sensor import Touch, PORT_4, PORT_3, PORT_2, Light, PORT_1, Ultrasonic

#from basicFunctions import step, calibrate

light = Light(brick, PORT_1)
turningMotor = Motor(brick, PORT_B)
walkingMotor = Motor(brick, PORT_C)
armMotor = Motor(brick, PORT_A)
legPosition = Touch(brick, PORT_3)
ultrasonic = Ultrasonic(brick, PORT_4)

def step(forwardPower):
    #print('stepping')
    walkingMotor.run(power = forwardPower)
    sleep(.1)
    while True:
        if legPosition.is_pressed() == True:
            walkingMotor.run(power = 0)
            walkingMotor.brake()
            return
            
def lineFollow():
    step(120)
    pass
Exemplo n.º 15
0
 def ultrasonic(self):
     us = Ultrasonic(self.brick, PORT_3)
     return us.get_distance()
Exemplo n.º 16
0
print(brick.get_device_info())  # check what brick you connected to
from time import sleep
from nxt.sensor import Light, Touch, Ultrasonic
from nxt.sensor import PORT_1, PORT_2, PORT_3, PORT_4
from nxt.motor import Motor, PORT_A, PORT_B, PORT_C
"""##########################################################################################
################################     IMPORT MOTORS AND SENSORS HERE     ################################
###########################################################################################"""

motorLeft = Motor(brick, PORT_B)
motorRight = Motor(brick, PORT_C)
armMotor = Motor(brick, PORT_A)
light = Light(brick, PORT_3)
touch = Touch(brick, PORT_4)
sonar = Ultrasonic(brick, PORT_2)
led = Light(brick, PORT_1)  # experimental
"""########################################################################################"""


def binIdent():

    n = 50

    #wait .25 seconds after the kill-switch is released
    sleep(.25)

    while n > 0:
        print("Bin Identification Running, power = %d" % n)
        # print(sonar.get_distance())
        armMotor.run(power=n)
Exemplo n.º 17
0
    # the bluetooth function of the nxt library works too, but "wastes"
    # time searching for devices.
    brick = nxtConnect.btConnect(brickName)

print(brick.get_device_info())  # check what brick you connected to
from time import sleep

from nxt.motor import Motor, PORT_A, PORT_B, PORT_C
from nxt.sensor import Touch, PORT_4, PORT_3, PORT_2, Light, PORT_1, Ultrasonic

light = Light(brick, PORT_4)
turningMotor = Motor(brick, PORT_B)
walkingMotor = Motor(brick, PORT_C)
armMotor = Motor(brick, PORT_A)
touch = Touch(brick, PORT_1)
ultrasonic = Ultrasonic(brick, PORT_2)
compass = Ultrasonic(brick, PORT_3)

# LINE FOLLOW VARIABLES
turningPower = 65  # 70, normalized, motor power used when turning in line follow
negInertiaPower = 70  # 65, normalized, motor power for negative inertia
findLineTimeOut = 0.5  # 0.5, time between switching motor to the opposite direction
negInertiaLengthOnWhite = 0.07  # 0.2, time before braking on negative inertia when originally on white
negInertiaLengthOnBlack = 0.07  # 0.05, time before braking on ngative inertia when originally on black (should be smaller than white to prevent overshooting the line)

# CALIBRATION VARIABLES
calTurningPower = 70  # 70, normalized, motor power used to turn when calibrate
calFirstTurnTime = 0.2  # 0.2, time to turn on first turn
calSecondTurnTme = 0.1  # 0.15, time to turn on second turn
calDelta = 10  # no default since it's new, range of light values for which line follow continues going straight (range is 2 * delta)
Exemplo n.º 18
0
brick = nxtConnect.btConnect(brickName)

from time import sleep
from nxt.motor import SynchronizedMotors

# see files in library ( /usr/local/lib/python2.7/dist-packages/nxt )
# for a more comprehensive list of ports / commands available
from nxt.motor import Motor, PORT_A, PORT_B, PORT_C
from nxt.sensor import Light, Sound, Touch, Ultrasonic
from nxt.sensor import PORT_1, PORT_2, PORT_3, PORT_4

try:
    motorLeft = Motor(brick, PORT_A)  # plug motor into Port A
    motorRight = Motor(brick, PORT_B)  # plug motor into Port B
    motorBoth = nxt.SynchronizedMotors(motorRight, motorLeft, 0)
    ultraSensor = Ultrasonic(brick,
                             PORT_1)  # plug ultrasonic sensor into Port 1

    #motorSync = SynchronizedMotors(motorA, motorB, 10000)
    #motorSync.idle()
    while ultraSensor.get_sample() > 10:
        print("Current ultrasonic sensor state: {}".format(
            ultraSensor.get_sample()))
        motorBoth.run(power=-120)
        '''motorA.run(power = -128) #go forward
        motorB.run(power = -128) #go forward'''

finally:
    Motor(brick, PORT_A).idle()  # otherwise motor keeps running
    Motor(brick, PORT_B).idle()  # otherwise motor keeps running
    print("Terminating Program")
    brick.sock.close()