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)
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)
def touch(port): t = Touch(b, SENSORS[port]) return '1' if t.is_pressed() else '0'
####################################################################### ## 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:
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)