class Car: brick = None left = None right = None def __init__ (self): brick = nxt.locator.find_one_brick() self.brick = brick self.left = Motor(brick, PORT_A) self.right = Motor(brick, PORT_C) self.light = Light(brick, PORT_4) # self.ultrasonic = Ultrasonic(brick, PORT_4) print "Connection established." def turn (self, angle=100, speed = 30): if angle > 0: self.left.turn(angle, speed) elif angle < 0: self.right.turn(-angle, speed) def forward (self, distance=50, speed=30): self.left.run(power=speed) self.right.run(power=speed) sleep(distance/speed) self.left.idle() self.right.idle() def range(self): return self.ultrasonic.get_sample() def surface(self): return self.light.get_sample()
class Car: brick = None left = None right = None def __init__(self): brick = nxt.locator.find_one_brick() self.brick = brick self.left = Motor(brick, PORT_A) self.right = Motor(brick, PORT_C) self.light = Light(brick, PORT_4) # self.ultrasonic = Ultrasonic(brick, PORT_4) print "Connection established." def turn(self, angle=100, speed=30): if angle > 0: self.left.turn(angle, speed) elif angle < 0: self.right.turn(-angle, speed) def forward(self, distance=50, speed=30): self.left.run(power=speed) self.right.run(power=speed) sleep(distance / speed) self.left.idle() self.right.idle() def range(self): return self.ultrasonic.get_sample() def surface(self): return self.light.get_sample()
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 DrivarNxt(Drivar): def __init__(self): self.m_initialized = False self.m_block = None self.m_leftMotor = None self.m_rightMotor = None self.m_penMotor = None self.m_ultrasonicSensor = None self.m_lightSensor = None self.m_moving = False 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_penMotor = Motor(self.m_block, PORT_B) self.m_ultrasonicSensor = Ultrasonic(self.m_block, PORT_4) self.m_lightSensor = Light(self.m_block, PORT_3) self.m_initialized = True def move(self, direction=Drivar.DIR_FORWARD, durationInMs=1000, callback=None): durationInMs = max(durationInMs, 100) _direct = direction self.rotateWheels(direction=_direct) time.sleep(durationInMs / 1000) self.stop() if callback is not None: callback() def rotateWheels(self, wheelSet=Drivar.WHEELS_BOTH, direction=Drivar.DIR_FORWARD, speedLevel=Drivar.SPEED_FAST, callback=None): power = self._getNxtSpeed(speedLevel) # Correct the power (positive vs negative) depending on the direction if (direction == Drivar.DIR_FORWARD): if (power < 0): power = power * -1 if (direction == Drivar.DIR_BACKWARD): if (power > 0): power = power * -1 # Get the wheels turning if (wheelSet == Drivar.WHEELS_LEFT or wheelSet == Drivar.WHEELS_BOTH): self.m_leftMotor.run(power) if (wheelSet == Drivar.WHEELS_RIGHT or wheelSet == Drivar.WHEELS_BOTH): self.m_rightMotor.run(power) self.m_moving = True if callback is not None: callback() def turn(self, direction=Drivar.DIR_LEFT, angle=90, callback=None): left_power = -100 right_power = 100 if (direction == Drivar.DIR_RIGHT): left_power *= -1 right_power *= -1 self.m_leftMotor.turn(left_power, angle) self.m_rightMotor.turn(right_power, angle) def stop(self, callback=None): self.m_leftMotor.idle() self.m_rightMotor.idle() self.m_moving = False if callback is not None: callback() ''' Return the distance to the nearest obstacle, in centimeters ''' def getDistanceToObstacle(self): return self.m_ultrasonicSensor.get_sample() ''' Indicate with a boolean whether there is an obstacle within the given distance ''' def isObstacleWithin(self, distance): dist = self.m_ultrasonicSensor.get_sample() if (dist <= distance): return True else: return False def rotatePen(self, angle): power = 70 if angle < 0: angle = -1 * angle power = -70 self.m_penMotor.turn(power, angle) def getReflectivityMeasurement(self): self.m_lightSensor.set_illuminated(True) return self.m_lightSensor.get_sample() def wait(self, milliseconds): time.sleep(milliseconds / 1000) ''' Return the NXT speed equivalent for the given DRIVAR speed flag ''' @staticmethod def _getNxtSpeed(speed): if (speed == Drivar.SPEED_SLOW): return 70 elif (speed == Drivar.SPEED_MEDIUM): return 100 elif (speed == Drivar.SPEED_FAST): return 127 else: return 100
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()
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()