def getSound(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 = Sound(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)
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 getSound(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 = Sound(self._bricks[self.active_nxt], port_aux) res = sensor.get_sample() except: pass return res else: pass else: pass
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)
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()
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()
# 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, Sound 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) sound = Sound(brick, PORT_1) ultrasonic = Ultrasonic(brick, PORT_2) compass = Ultrasonic(brick, PORT_3) #TODO # calibrate sleep times might need adjusting # find bin is probably non functional, and VERY necessary (very short range when picking up bin) # additional bin ID data to get more accurate values # line follow data # 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)
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()