Ejemplo n.º 1
0
class Robot:
    
    def __init__(self):
        print 'Searching for NXT bricks...'
        self.robot = nxt.locator.find_one_brick()
        print 'NXT brick found'
        self.right_motor = Motor(self.robot, PORT_B)
        self.left_motor = Motor(self.robot, PORT_C)
        self.locator = Ultrasonic(self.robot, PORT_1)
        self.haptic = Touch(self.robot, PORT_4)
        
    def forward(self):
        
        if(random.random() > .5):
            self.right_motor.run(-STRAIGHT_POWER)
            self.left_motor.run(-STRAIGHT_POWER)
        else:
            self.left_motor.run(-STRAIGHT_POWER)
            self.right_motor.run(-STRAIGHT_POWER)
        
        sleep(SECONDS)
        
        if(random.random() > .5):
            self.right_motor.idle()
            self.left_motor.idle()
        else:
            self.left_motor.idle()
            self.right_motor.idle()
        
    def back(self):
        
        self.right_motor.run(STRAIGHT_POWER)
        self.left_motor.run(STRAIGHT_POWER)
        
        sleep(SECONDS)
        
        self.right_motor.idle()
        self.left_motor.idle()
        
    def right(self):
        self.left_motor.turn(-TURN_POWER, ANGLE)
     
    def left(self):
        self.right_motor.turn(-TURN_POWER, ANGLE)
        
    def distance(self):
        return self.locator.get_sample()
    
    def is_touching(self):
        return self.haptic.get_sample()
    
    def beep_ok(self):
        self.robot.play_tone_and_wait(FREQ_C, DURATION)
        self.robot.play_tone_and_wait(FREQ_D, DURATION)
        self.robot.play_tone_and_wait(FREQ_E, DURATION)
        
    def beep_not_ok(self):
        self.robot.play_tone_and_wait(FREQ_E, DURATION)
        self.robot.play_tone_and_wait(FREQ_D, DURATION)
        self.robot.play_tone_and_wait(FREQ_C, DURATION)
Ejemplo n.º 2
0
 def feel(self, touchPort='PORT_1', initial=False):
     """
     Use the touch sensors (pressed = True)
     
     touchPort (str): The port number of the touch sensor(default='PORT_1')
     """
     
     touch = Touch(self.nxt.brick, eval(touchPort))
     if initial:
         return False                #don't return true until actually checked sensor value
     else:
         data = touch.get_sample()   #already in boolean format
         if data: print 'Touch Sensor '+str(touchPort)+' pressed' 
         return data
Ejemplo n.º 3
0
    def feel(self, touchPort='PORT_1', initial=False):
        """
        Use the touch sensors (pressed = True)
        
        touchPort (str): The port number of the touch sensor(default='PORT_1')
        """

        touch = Touch(self.nxt.brick, eval(touchPort))
        if initial:
            return False  #don't return true until actually checked sensor value
        else:
            data = touch.get_sample()  #already in boolean format
            if data: print 'Touch Sensor ' + str(touchPort) + ' pressed'
            return data
Ejemplo n.º 4
0
 def getButton(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 = Touch(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)
Ejemplo n.º 5
0
 def getButton(self, port):
     if self._bricks:
         try:
             port = int(port)
         except:
             pass
         if (port in NXT_SENSOR_PORTS):
             res = ERROR
             try:
                 port_aux = NXT_SENSOR_PORTS[port]
                 sensor = Touch(self._bricks[self.active_nxt], port_aux)
                 res = sensor.get_sample()
             except:
                 pass
             return res
         else:
             raise logoerror(ERROR_PORT_S % port)
     else:
         raise logoerror(ERROR_BRICK)
Ejemplo n.º 6
0
 def getButton(self, port):
     if self._bricks:
         try:
             port = int(port)
         except:
             pass
         if (port in NXT_SENSOR_PORTS):
             res = ERROR
             try:
                 port_aux = NXT_SENSOR_PORTS[port]
                 sensor = Touch(self._bricks[self.active_nxt], port_aux)
                 res = sensor.get_sample()
             except:
                 pass
             return res
         else:
             pass
     else:
         pass
Ejemplo n.º 7
0
class AlphaRex(object):
    r'''A high-level controller for the Alpha Rex model.
    
        This class implements methods for the most obvious actions performable
        by Alpha Rex, such as walk, wave its arms, and retrieve sensor samples.
        Additionally, it also allows direct access to the robot's components
        through public attributes.
    '''
    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)

    def echolocate(self):
        r'''Reads the Ultrasonic sensor's output.
        '''
        return self.ultrasonic.get_sample()

    def feel(self):
        r'''Reads the Touch sensor's output.
        '''
        return self.touch.get_sample()

    def hear(self):
        r'''Reads the Sound sensor's output.
        '''
        return self.sound.get_sample()

    def say(self, line, times=1):
        r'''Plays a sound file named (line + '.rso'), which is expected to be
            stored in the brick. The file is played (times) times.

            line
                The name of a sound file stored in the brick.

            times
                How many times the sound file will be played before this method
                returns.
        '''
        for i in range(0, times):
            self.brick.play_sound_file(False, line + '.rso')
            sleep(1)

    def see(self):
        r'''Reads the Light sensor's output.
        '''
        return self.light.get_sample()

    def walk(self, secs, power=FORTH):
        r'''Simultaneously activates the leg motors, causing Alpha Rex to walk.
        
            secs
                How long the motors will rotate.
            
            power
                The strength effected by the motors. Positive values will cause
                Alpha Rex to walk forward, while negative values will cause it
                to walk backwards. If you are unsure about how much force to
                apply, the special values FORTH and BACK provide reasonable
                defaults. If omitted, FORTH is used.
        '''
        for motor in self.legs:
            motor.run(power=power)

        sleep(secs)

        for motor in self.legs:
            motor.idle()

    def wave(self, secs, power=100):
        r'''Make Alpha Rex move its arms.
        
            secs
                How long the arms' motor will rotate.
            
            power
                The strength effected by the motor. If omitted, (100) is used.
        '''
        self.arms.run(power=power)
        sleep(secs)
        self.arms.idle()
Ejemplo n.º 8
0
class Robot(object):
    def __init__(self, brick="NXT"):
        r"""Creates a new Robot 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.tool = Motor(brick, PORT_B)
        self.tracks = [Motor(brick, PORT_A), Motor(brick, PORT_C)]

        self.ultrasonic = Ultrasonic(brick, PORT_1)
        self.sound = Sound(brick, PORT_2)
        self.light = Light(brick, PORT_3)
        self.touch = Touch(brick, PORT_4)

    def turn(self, power, angle):
        for motor in self.tracks:
            motor.turn(power, angle)

    def move(self, power=FORTH):
        r"""Simultaneously activates the tracks motors, causing Robot to move.
        
            power
                The strength effected by the motors. Positive values will cause
                Robot to move forward, while negative values will cause it
                to move backwards. If you are unsure about how much force to
                apply, the special values FORTH and BACK provide reasonable
                defaults. If omitted, FORTH is used.
        """
        for motor in self.tracks:
            motor.run(power=power)

    def wait(self, seconds):
        """ secsonds
                How long the motors will rotate.
                Will this take values < 0? Most motor commands work in ms. 
                Try passing sleep 1/seconds (miliseconds)
        """
        sleep(seconds)

    def stop(self):
        for motor in self.tracks:
            motor.idle()

    def tacho(self):
        """
           returns an array of two elements which are the motor tacho readings
        """
        tachos = []
        for motor in self.tracks:
            # tachos.append(motor.get_tacho())
            tachos.append(motor.tacho_count)  # , rotation_count

        return tachos

    def act(self, power=FORTH):
        r"""Make Robot move its tool.
        
            power
                The strength effected by the motor. If omitted, (100) is used.
        """
        self.tool.run(power=power)

    def echolocate(self):
        r"""Reads the Ultrasonic sensor's output.
        """
        return self.ultrasonic.get_sample()

    def feel(self):
        r"""Reads the Touch sensor's output.
        """
        return self.touch.get_sample()

    def hear(self):
        r"""Reads the Sound sensor's output.
        """
        return self.sound.get_sample()

    def say(self, line, times=1):
        r"""Plays a sound file named (line + '.rso'), which is expected to be
            stored in the brick. The file is played (times) times.

            line
                The name of a sound file stored in the brick.

            times
                How many times the sound file will be played before this method
                returns.
        """
        for i in range(0, times):
            self.brick.play_sound_file(False, line + ".rso")
            sleep(1)

    def see(self):
        r"""Reads the Light sensor's output.
        """
        return self.light.get_sample()
Ejemplo n.º 9
0
class AlphaRex(object):
    r'''A high-level controller for the Alpha Rex model.
    
        This class implements methods for the most obvious actions performable
        by Alpha Rex, such as walk, wave its arms, and retrieve sensor samples.
        Additionally, it also allows direct access to the robot's components
        through public attributes.
    '''
    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)

    def echolocate(self):
        r'''Reads the Ultrasonic sensor's output.
        '''
        return self.ultrasonic.get_sample()
    
    def feel(self):
        r'''Reads the Touch sensor's output.
        '''
        return self.touch.get_sample()

    def hear(self):
        r'''Reads the Sound sensor's output.
        '''
        return self.sound.get_sample()

    def say(self, line, times=1):
        r'''Plays a sound file named (line + '.rso'), which is expected to be
            stored in the brick. The file is played (times) times.

            line
                The name of a sound file stored in the brick.

            times
                How many times the sound file will be played before this method
                returns.
        '''
        for i in range(0, times):
            self.brick.play_sound_file(False, line + '.rso')
            sleep(1)

    def see(self):
        r'''Reads the Light sensor's output.
        '''
        return self.light.get_sample()

    def walk(self, secs, power=FORTH):
        r'''Simultaneously activates the leg motors, causing Alpha Rex to walk.
        
            secs
                How long the motors will rotate.
            
            power
                The strength effected by the motors. Positive values will cause
                Alpha Rex to walk forward, while negative values will cause it
                to walk backwards. If you are unsure about how much force to
                apply, the special values FORTH and BACK provide reasonable
                defaults. If omitted, FORTH is used.
        '''
        for motor in self.legs:
            motor.run(power=power)
        
        sleep(secs)

        for motor in self.legs:
            motor.idle()

    def wave(self, secs, power=100):
        r'''Make Alpha Rex move its arms.
        
            secs
                How long the arms' motor will rotate.
            
            power
                The strength effected by the motor. If omitted, (100) is used.
        '''
        self.arms.run(power=power)
        sleep(secs)
        self.arms.idle()
Ejemplo n.º 10
0
from time import sleep

# 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

# use try with finally to stop motors at end, even if program
# encountered an (programming) error.
try:
    touchSensor = Touch(brick, PORT_1) # plug touch sensor into Port 1
    motor = Motor(brick, PORT_A) # plug motor into Port A
    
    # Note: |Power| <50 might not be strong enough to turn motor / 
    # overcome the internal friction
    motor.run(power = 70) # go forward
    sleep(2.5) # let NXT do its thing for 2.5 seconds
    motor.run(power = -70) # go backward
    sleep(2)
    
    # will read when this line of code is reached, so KEEP sensor
    # pressed till then
    print("Current touch sensor state: {}".format(
            touchSensor.get_sample()))
    
finally:
    Motor(brick, PORT_A).idle() # otherwise motor keeps running
    print("Terminating Program")
    brick.sock.close()
Ejemplo n.º 11
0
class Robot(object):
    
    def __init__(self, brick='NXT'):
        r'''Creates a new Robot 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.tool = Motor(brick, PORT_B)
        self.tracks = [Motor(brick, PORT_A), Motor(brick, PORT_C)]

        
        self.ultrasonic = Ultrasonic(brick, PORT_1)
        self.sound = Sound(brick, PORT_2)
        self.light = Light(brick, PORT_3)
        self.touch = Touch(brick, PORT_4)
        
    def turn(self, power, angle):
        for motor in self.tracks:
            motor.turn(power, angle)

    def move(self, power=FORTH):
        r'''Simultaneously activates the tracks motors, causing Robot to move.
        
            power
                The strength effected by the motors. Positive values will cause
                Robot to move forward, while negative values will cause it
                to move backwards. If you are unsure about how much force to
                apply, the special values FORTH and BACK provide reasonable
                defaults. If omitted, FORTH is used.
        '''
        for motor in self.tracks:
            motor.run(power=power)
            
    def wait(self, seconds):
        ''' secsonds
                How long the motors will rotate.
                Will this take values < 0? Most motor commands work in ms. 
                Try passing sleep 1/seconds (miliseconds)
        '''    
        sleep(seconds)
        
    def stop(self):
        for motor in self.tracks:
            motor.idle()
            
    def tacho(self):
        '''
           returns an array of two elements which are the motor tacho readings
        '''
        tachos = []
        for motor in self.tracks:
            #tachos.append(motor.get_tacho())
            tachos.append(motor.tacho_count) #, rotation_count
            
        return tachos        

    def act(self, power=FORTH):
        r'''Make Robot move its tool.
        
            power
                The strength effected by the motor. If omitted, (100) is used.
        '''
        self.tool.run(power=power)


    def echolocate(self):
        r'''Reads the Ultrasonic sensor's output.
        '''
        return self.ultrasonic.get_sample()
    
    def feel(self):
        r'''Reads the Touch sensor's output.
        '''
        return self.touch.get_sample()

    def hear(self):
        r'''Reads the Sound sensor's output.
        '''
        return self.sound.get_sample()

    def say(self, line, times=1):
        r'''Plays a sound file named (line + '.rso'), which is expected to be
            stored in the brick. The file is played (times) times.

            line
                The name of a sound file stored in the brick.

            times
                How many times the sound file will be played before this method
                returns.
        '''
        for i in range(0, times):
            self.brick.play_sound_file(False, line + '.rso')
            sleep(1)

    def see(self):
        r'''Reads the Light sensor's output.
        '''
        return self.light.get_sample()