Nxt notes

From IPRE Wiki
Jump to: navigation, search
  • Turn on bluetooth on NXT
  • Add device on PC

from NxtNet import Nxt
robot = Nxt()
robot.Connect("com5")
robot.PlayTone(4400, 100)


from NxtNet import Sensor
touch = Sensor()

from NxtNet import SensorPort
touch.Port = SensorPort.Port1

from NxtNet import SensorType
from NxtNet import SensorMode


robot.SetInputMode(SensorPort.Port1, SensorType.Switch, SensorMode.Boolean)

from NxtNet import SensorState

print(robot.GetInputValues(SensorPort.Port1) )

# returns true if sensor is pressed and false if not pressed
# assumes sensor plugged into port 1
# assumes you've already done setInput mode on the robot for that port (maybe you
#     don't have to???

def touchIsPressed(robot):
    if (robot.GetInputValues(SensorPort.Port1).ScaledValue) == 0:
        return False
    else:
        return True


# move wheel 360 degrees maybe? except alternates between 1 deg and 360 ...
robot.SetOutputState(MotorPort.All, 100, MotorModes.On, MotorRegulationMode.Speed, 0,MotorRunState.Running,360)




  • Motor and touch sensor demo (need to better understand all the args but it works!!!!
from NxtNet import *
from Myro import wait


# Demo 1: robot bounce
# For some reason I need to init the robot in the shell rather than here globally
# Perhaps just sun spots the one time I tried - definitely worth looking at again
# 
#
# 
# In Shell:
#     robot = Nxt()
#     robot.Connect("com5")
#     bounce(robot)
#
# whoo hooo!!!


def initRobot(robot):
    robot.Connect("com5")


# returns true if sensor is pressed and false if not pressed
# assumes sensor plugged into port 1
# assumes you've already done setInput mode on the robot for that port

def touchIsPressed(robot):
    if (robot.GetInputValues(SensorPort.Port1).ScaledValue) == 0:
        return False
    else:
        return True

def forward(robot):
    robot.SetOutputState(MotorPort.All, 100, MotorModes.On, MotorRegulationMode.Speed, 0,MotorRunState.Running,0)

def backward(robot):
    robot.SetOutputState(MotorPort.All, -100, MotorModes.On, MotorRegulationMode.Speed, 0,MotorRunState.Running,0)


def stop(robot):
    robot.SetOutputState(MotorPort.All, 0, MotorModes.Brake, MotorRegulationMode.Speed, 0,MotorRunState.Idle,2)


def bounce(robot):
    initTouch(robot)
    backward(robot)
    while(not (touchIsPressed(robot))):
        wait(.1)
    forward(robot)
    wait(.3)
    stop(robot)

def initTouch(robot):
    robot.SetInputMode(SensorPort.Port1, SensorType.Switch, SensorMode.Boolean)



  • some random code snippets so I remember later
  • OK, here's my last line following code ... it's got lots of junk in it from the line follower that I don't have time to edit out but at least

now it's uploaded so I can look at it sometime!

from NxtNet import *
from Myro import wait

# defaults for my robot


# Demo 1: robot bounce
# For some reason I need to init the robot in the shell
#
# Load this code
# In Shell:
#     robot = Nxt()
#     robot.Connect("com5")
#     bounce(robot)
#
# whoo hooo!!!


def initRobot(robot):
    robot.Connect("com5")


# returns true if sensor is pressed and false if not pressed
# assumes sensor plugged into port 1
# assumes you've already done setInput mode on the robot for that port

def touchIsPressed(robot):
    if (robot.GetInputValues(SensorPort.Port1).ScaledValue) == 0:
        return False
    else:
        return True

def forward(robot):
    robot.SetOutputState(MotorPort.All, 100, MotorModes.On, MotorRegulationMode.Speed, 0,MotorRunState.Running,0)

def backward(robot):
    robot.SetOutputState(MotorPort.All, -100, MotorModes.On, MotorRegulationMode.Speed, 0,MotorRunState.Running,0)


def stop(robot):
    robot.SetOutputState(MotorPort.All, 0, MotorModes.Brake, MotorRegulationMode.Idle, 0,MotorRunState.Idle,2)


def bounce(robot):
    initTouch(robot)
    backward(robot)
    while(not (touchIsPressed(robot))):
        wait(.1)
    forward(robot)
    wait(.3)
    stop(robot)

def initTouch(robot):
    robot.SetInputMode(SensorPort.Port1, SensorType.Switch, SensorMode.Boolean)


# ---------------------------------------------------------
def beep(robot):
    robot.PlayTone(4400, 100)

def initActiveLightSensor(robot):
    robot.SetInputMode(SensorPort.Port3, SensorType.LightActive, SensorMode.Raw)

def closeActiveLightSensor(robot):
    robot.SetInputMode(SensorPort.Port3, SensorType.LightInactive, SensorMode.Raw)


def isLine(robot):
    #x =int(robot.GetInputValues(SensorPort.Port3).RawValue)
    #print (x)
    if int((robot.GetInputValues(SensorPort.Port3).RawValue)) > 500:
        return True
    else:
        return False

RIGHTMOTORPORT = MotorPort.PortB
LEFTMOTORPORT = MotorPort.PortC

FASTER = 68
SLOWER = 0
WAITTIME = 0.1

def leftIsh(robot):
    robot.SetOutputState(RIGHTMOTORPORT, FASTER, MotorModes.On, MotorRegulationMode.Speed, 0,MotorRunState.Running,0)
    robot.SetOutputState(LEFTMOTORPORT, SLOWER, MotorModes.On, MotorRegulationMode.Speed, 0,MotorRunState.Running,0)
    wait(WAITTIME)
    stop(robot)

def rightIsh(robot):
    robot.SetOutputState(RIGHTMOTORPORT, SLOWER, MotorModes.On, MotorRegulationMode.Speed, 0,MotorRunState.Running,0)
    robot.SetOutputState(LEFTMOTORPORT, FASTER, MotorModes.On, MotorRegulationMode.Speed, 0,MotorRunState.Running,0)
    wait(WAITTIME)
    stop(robot)

def leftIshNoStop(robot):
    robot.SetOutputState(RIGHTMOTORPORT, FASTER, MotorModes.On, MotorRegulationMode.Speed, 0,MotorRunState.Running,0)
    robot.SetOutputState(LEFTMOTORPORT, SLOWER, MotorModes.On, MotorRegulationMode.Speed, 0,MotorRunState.Running,0)


def rightIshNoStop(robot):
    robot.SetOutputState(RIGHTMOTORPORT, SLOWER, MotorModes.On, MotorRegulationMode.Speed, 0,MotorRunState.Running,0)
    robot.SetOutputState(LEFTMOTORPORT, FASTER, MotorModes.On, MotorRegulationMode.Speed, 0,MotorRunState.Running,0)


def wiggle(robot):
    for i in range (4):
        leftIshNoStop(robot)
        wait(0.25)
        rightIshNoStop(robot)
        wait(0.25)
    stop(robot)

def lineFollow(robot):
    initActiveLightSensor(robot)
    for i in range(20):
        if isLine(robot):
            rightIsh(robot)
        else:
            leftIsh(robot)
    stop(robot)

def lineFollow(robot, repeats):
    initActiveLightSensor(robot)
    for i in range(repeats):
        if isLine(robot):
            rightIsh(robot)
        else:
            leftIsh(robot)
    stop(robot)


from time import clock
def testLineSensor(robot, repeats):
    initActiveLightSensor(robot)
    wait(1)

   

    start = clock()
    for i in range(repeats):
        x = robot.GetInputValues(SensorPort.Port1).ScaledValue
    end = clock()
    print ("duration", end - start , "seconds")
    print ("average = ", (end-start)/repeats)


Random Jen notes ... ignore for now ...

  • Perhaps change timeout in private void TransmitAndWait( byte[] data, int responseLength )
  • Cygwin bash: export PATH='/cygdrive/c/ProgramFiles (x86)/Mono-2.10.8/bin':$PATH