Nxt-python-doc

From Earlham CS Department
Revision as of 06:24, 23 February 2010 by Charliep (talk | contribs)
Jump to navigation Jump to search

Back to Robotics Main Page


NXT-Python Documentation

Largely a work in-progress, please add/edit as you learn the details. Note that this page is documenting the NXT-Python v1.1 release (http://code.google.com/p/nxt-python/), not the NXT_Python v0.7 release which appears to have gone dormant (note hyphen vs underscore).

nxt.locator

The function of this module is to locate NXT brick(s). Returns an nxt.bluesock.BlueSock object for robots connected by Bluetooth and a nxt.usbsock.USBSock object for robot connected by USB, and throws nxt.locator.BrickNotFoundError if none can be found.

  • Status: incomplete as of 2010-02-14
  • Objects
    • nxt.bluesock.BlueSock - The object returned by find_one_brick() when a brick is found on a Bluetooth connection [defined in nxt.bluesock]
    • nxt.usbsock.USBSock - The object returned by find_one_brick()when a brick is found on a USB connection [defined in nxt.usbsock]
    • nxt.brick.Brick - The object returned by the connect() method [defined in nxt.brick]
  • Methods
    • connect() - Try to connect to the brick.
    • close() - Close connection to brick.
    • host - Returns the address of the brick (if connected via Bluetooth connection)
    • generator object find_bricks()
      • next() - Find the next brick in the sequence, returns the same thing as find_one_brick().
    • find_one_brick()
    • find_bricks(host=None, name=None) - Find all the bricks in the area, returns a generator object find_bricks.
  • Example
import nxt.locator 
sock = nxt.locator.find_one_brick() 
brick = sock.connect()
if hasattr(brick, 'host'):
 print brick.host
else:
 print "brick.host is not defined for USB connections"
sock.close()

nxt.motor

  • Port Constants
    • PORT_A
    • PORT_B
    • PORT_C
    • PORT_ALL
  • Mode Constants
    • MODE_IDLE
    • MODE_MOTOR_ON
    • MODE_BRAKE
    • MODE_REGULATED
  • Regulation Constants
    • REGULATION_IDLE
    • REGULATION_MOTOR_SPEED
    • REGULATION_MOTOR_SYNC
  • Run State Constants
    • RUN_STATE_IDLE
    • RUN_STATE_RAMP_UP
    • RUN_STATE_RUNNING
    • RUN_STATE_RAMP_DOWN
  • Objects
    • nxt.motor.Motor - The object returned by Motor(brick, port)
    • Class Methods
      • _debug_out(self, message)
      • set_output_state(self)
      • get_output_state(self)
      • reset_position(self, relative)
      • run(self, power=100, regulated=1)
      • stop(self, braking=1)
      • update(self, power, tacho_limit, braking=False, max_retries=-1)
  • Methods
  • Example
import nxt.locator
import nxt.motor
from time import sleep
sock = nxt.locator.find_one_brick()
brick = sock.connect()
motor_a = nxt.motor.Motor(brick, nxt.motor.PORT_A)
print "Initial Position:", motor_a.get_output_state()[7]
motor_a.update( 127, 360, True ) # Turn 360 degrees at full power and brake
motor_a.update( -127, 360 ) # Turn -360 degrees at full power and coast
sleep( 3 ) # update is non-blocking when braking is false, must wait for finish
print "Final Position:", motor_a.get_output_state()[7]
sock.close()

nxt.sensor

  • Status: incomplete as of 2010-02-14
  • Objects
  • Methods
  • Example

nxt.compass

  • Status: incomplete as of 2010-02-14
  • Objects
    • CompassSensor
  • Methods
    • CompassSensor(bot,port)
    • get_sample() returns a value between zero and 180 depending on the heading of the sensor. There is a one-to-two ratio between degrees from due North and get_sample() values. At the moment this generally returns an error.
    • Methods to add
      • begin_calibration() tells the sensor to go into calibration mode.
      • end_calibration() tells the sensor to leave calibration mode
  • Example
compass = UltrasonicSensor(bot, PORT_1) #declare the sensor plugged into port 1 "compass"
heading = compass.get_sample()*2 # get the current heading in degrees from due north
  • Notes
    • The sensor gives errors and fails to register almost all of the time. The reasoning for this is under investigation.
    • Compass Sensor hardware info
    • URL to compass.py in source control (hint)

Scripts

Located in $DISTRIBUTION/scripts.

  • nxt_filer
  • nxt_push
  • nxt_test

Server Mode and nxt.server

Tips

Wrap your code in a try/finally block so that you can stop the motors on control-c.

try:
  [your code here]
finally:
  [stop all your motors]