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)
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
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
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)
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)
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 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)
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 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)
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
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 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()
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) # print(sonar.get_distance())
brick = nxtConnect.btConnect(brickName) print(brick.get_device_info()) # check what brick you connected to ####################################################################### ## Then, you can specify what you want the NXT to do ####################################################################### from time import sleep from nxt.motor import Motor, PORT_A, PORT_B, PORT_C from nxt.sensor import Touch, PORT_4, PORT_3 turningMotor = Motor(brick, PORT_B) walkingMotor = Motor(brick, PORT_C) turnerSwitch = Touch(brick, PORT_4) legPosition = Touch(brick, PORT_3) while True: if turnerSwitch.is_pressed() == False: turningMotor.run(power=0) a = 0 walkingMotor.run(power=120) else: if legPosition.is_pressed() == False: walkingMotor.run(power=100) else: a = 1 if a == 1: walkingMotor.run(power=0)
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) # turns right ~30 degrees turningMotor.turn(70, 80, brake=True, timeout=3, emulate=True)
def touch(port): t = Touch(b, SENSORS[port]) return '1' if t.is_pressed() else '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()
####################################################################### ## Then, you can specify what you want the NXT to do ####################################################################### 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:
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, 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)
import nxt import nxtConnect # has to be in search path import time brickName = "Team60" useUSB = False 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 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 turningMotor = Motor(brick, PORT_B) walkingMotor = Motor(brick, PORT_C) armMotor = Motor(brick, PORT_A) legPosition = Touch(brick, PORT_3) touch = Touch(brick, PORT_1) while True: print(touch.get_input_values().calibrated_value)
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()