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 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
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()
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 getLightColor(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 = Light(self._bricks[self.active_nxt], port_aux) return sensor.get_lightness() 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 getLightColor(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 = Light(self._bricks[self.active_nxt], port_aux) res = sensor.get_lightness() except: pass return res else: pass else: pass
def getGray(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 = Light(self._bricks[self.active_nxt], port_aux) sensor.set_illuminated(True) res = sensor.get_lightness() except: pass return res else: raise logoerror(ERROR_PORT_S % port) else: raise logoerror(ERROR_BRICK)
def getLight(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 = Light(self._bricks[self.active_nxt], port_aux) sensor.set_illuminated(False) res = sensor.get_lightness() #except: #pass print port print res 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 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()
# 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.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)
brick = nxt.find_one_brick(name=brickName, strict=True, method=nxt.locator.Method(usb=True, bluetooth=True)) else: # 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 light = Light(brick, PORT_1) turningMotor = Motor(brick, PORT_B) walkingMotor = Motor(brick, PORT_C) legPosition = Touch(brick, PORT_3) #ultrasonic = Sonar(brick, PORT_2) def calibrate(): # turn on light sensor light.set_illuminated(True) sleep(0.25) # calibrates black value black = light.get_lightness() print("Black = %d" % black)
method=nxt.locator.Method(usb=True, bluetooth=True)) else: # 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.sensor import Light from nxt.sensor.hitechnic import Gyro from nxt.sensor import PORT_1, PORT_2 from nxt.motor import Motor, PORT_A, PORT_B, PORT_C motor = Motor(brick, PORT_C) light = Light(brick, PORT_2) motor.reset_position(False) print(motor.get_tacho()) black = light.get_lightness() print("black = ", black) motor.turn(30, 50, brake=True, timeout=1.5, emulate=True) sleep(0.05) print(motor.get_tacho()) motor.brake() white = light.get_lightness() print("white = ", white) motor.turn(-30, 50, brake=True, timeout=1.5, emulate=True) threshold = (black + white) / 2 print(motor.get_tacho()) print("Threshold = ", threshold)
if useUSB: brick = nxt.find_one_brick( name = brickName, strict = True, method = nxt.locator.Method(usb = True, bluetooth = True)) else: # 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 nxt.sensor import Light, Gyro from nxt.sensor import PORT_1, PORT_2 lightSensor = Light(brick, PORT_1) gyro = Gyro(brick, PORT_2) gyro.calibrate() while True: print(gyro.get_rotation_speed()) """#turn sensor left white = lightSensor.get_lightness() #turn to initial position black = lightSensor.get_lightness threshold = (white + black) / 2 while True: if lightSensor.get_lightness() > threshold: while motor has turned less than 91 degrees
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()